You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取3D模型高度及转换后屏幕坐标 | ARKit渲染Collada模型问题

嘿,我来帮你搞定这两个需求——获取3D模型的高度,以及把模型相关坐标转换成屏幕坐标,结合你现有的ARKit场景来一步步说明:

获取3D模型的高度

要拿到Collada模型的高度,核心是获取模型节点的**包围盒(bounding box)**在Y轴上的差值,这个包围盒会自动包含整个模型的空间范围。结合你代码里的borisNode,可以这么写:

func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
    if let overlay = sceneView.overlaySKScene as? BubbleMessageScene {
        guard let borisNode = sceneView.scene?.rootNode.childNode(withName: "Boris", recursively: true) else { return }
        
        // 获取模型包围盒并计算高度
        let boundingBox = borisNode.boundingBox
        let modelHeight = boundingBox.max.y - boundingBox.min.y
        print("当前模型高度:\(modelHeight)")
        
        // 你原本调整气泡位置的代码...
    }
}

注意:如果你的模型是由多个子节点组成的,一定要确保borisNode是整个模型的根节点,这样它的boundingBox才会覆盖所有子节点的范围,计算出的高度才准确。

转换3D模型坐标到屏幕坐标

结合你要定位模型脚部的需求,我们需要先把脚部的本地坐标转换成世界坐标,再通过ARSCNView的方法转成屏幕坐标。如果你的模型原点不在脚部,记得替换成实际的脚部本地坐标:

func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
    if let overlay = sceneView.overlaySKScene as? BubbleMessageScene {
        guard let borisNode = sceneView.scene?.rootNode.childNode(withName: "Boris", recursively: true) else { return }
        
        // 1. 定义模型脚部的本地坐标(根据你的模型实际情况调整,比如(0, 0, 0)是脚部的话)
        let footLocalPos = SCNVector3(0, 0, 0)
        // 2. 转换成世界坐标
        let footWorldPos = borisNode.convertPosition(footLocalPos, to: nil)
        // 3. 转换成屏幕坐标(注意:这个坐标的Y轴是UIKit的坐标系,从左上角开始)
        let footScreenPos = sceneView.projectPoint(footWorldPos)
        
        // 如果要在SKScene叠加层使用,需要转换为SKScene的坐标系(Y轴从左下角开始)
        let skFootPos = CGPoint(x: footScreenPos.x, y: overlay.frame.height - footScreenPos.y)
        print("脚部屏幕坐标(SKScene适配后):\(skFootPos)")
        
        // 用这个坐标调整气泡位置...
    }
}

小提示:如果要获取模型顶部的屏幕坐标,只需要把footLocalPos换成模型顶部的本地坐标(比如SCNVector3(0, modelHeight, 0))即可。

内容的提问来源于stack exchange,提问作者magohamote

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:45:55