获取 3D 模型的高度及其转换后的屏幕坐标

mag*_*oth 1 ios swift arkit

我正在渲染一个 Collada (*.dae) 文件ARKit。作为我的叠加层,我ARSCNView添加了一个SKScene只显示消息气泡(还没有文本)的。

目前,我知道如何修改气泡的位置,使其看起来总是在我的 3D 模型的脚下。我这样做:

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 boxWorldCoordinates = sceneView.scene.rootNode.convertPosition(borisNode.position, from:sceneView.scene.rootNode.parent)
        let screenCoordinates = self.sceneView.projectPoint(boxWorldCoordinates)
        let boxY = overlay.size.height - CGFloat(screenCoordinates.y)

        overlay.bubbleNode?.position.x = CGFloat(screenCoordinates.x) - (overlay.bubbleNode?.size.width)!/2
        overlay.bubbleNode?.position.y = boxY
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,我的气泡总是在3D 模型的脚下,因为我只能获得我的模型的 SCNNode 位置,它被锚定在那里。我希望它位于我的模型的头部。

有什么方法可以获取我的 3D 模型的高度,然后获取其转换后的屏幕坐标,所以无论我在哪里使用手机,看起来气泡消息总是在头部旁边?

在此处输入图片说明

Bla*_*orz 6

每个 SCNNode 都有一个boundingBox属性,它是:

对象边界框的最小和最大角点。

所以这意味着:

Scene Kit 使用标识其角的两个点在局部坐标空间中定义了一个边界框,这些点隐式地确定了六个轴对齐的平面来标记其边界。例如,如果几何体的边界框具有最小角 {-1, 0, 2} 和​​最大角 {3, 4, 5},则几何体顶点数据中的所有点的 x 坐标值都介于 -1.0 和 3.0 之间, 包括的。

如果您查看 SceneKit 编辑器,您还可以看到以米为单位的模型大小(我说这只是作为一个点,您可以参考以检查计算):

在此处输入图片说明

在我的示例中,我使用的是上述尺寸的 Pokemon 模型。

我对模型进行了缩放(您可能也这样做了),例如:

 pokemonModel.scale = SCNVector3(0.01, 0.01, 0.01)
Run Code Online (Sandbox Code Playgroud)

因此,为了得到boundingBoxSCNNode,我们可以这样做:

/// Returns The Original Width & Height Of An SCNNode
///
/// - Parameter node: SCNNode
func getSizeOfModel(_ node: SCNNode){

     //1. Get The Size Of The Node Without Scale
     let (minVec, maxVec) = node.boundingBox
     let unScaledHeight = maxVec.y - minVec.y
     let unScaledWidth = maxVec.x - minVec.x

     print("""
         UnScaled Height = \(unScaledHeight)
         UnScaled Width = \(unScaledWidth)
           """)
    }
Run Code Online (Sandbox Code Playgroud)

像这样调用它:

 getSizeOfModel(pokemonModel)
Run Code Online (Sandbox Code Playgroud)

现在当然,由于我们的 SCNNode 已经被缩放,这并没有多大帮助,所以显然我们需要通过重写函数来考虑这一点:

/// Returns The Original & Scaled With & Height On An SCNNode
///
/// - Parameters:
///   - node: SCNode
///   - scalar: Float
func getOriginalAndScaledSizeOfNode(_ node: SCNNode, scalar: Float){

     //1. Get The Size Of The Node Without Scale
     let (minVec, maxVec) = node.boundingBox
     let unScaledHeight = maxVec.y - minVec.y
     let unScaledWidth = maxVec.x - minVec.x

     print("""
         UnScaled Height = \(unScaledHeight)
         UnScaled Width = \(unScaledWidth)
         """)


     //2. Get The Size Of The Node With Scale
     let max = node.boundingBox.max
     let maxScale = SCNVector3(max.x * scalar, max.y * scalar, max.z * scalar)

     let min = node.boundingBox.min
     let minScale = SCNVector3(min.x * scalar, min.y * scalar, min.z * scalar)

     let heightOfNodeScaled = maxScale.y - minScale.y
     let widthOfNodeScaled = maxScale.x - minScale.x

     print("""
         Scaled Height = \(heightOfNodeScaled)
         Scaled Width = \(widthOfNodeScaled)
         """)

}
Run Code Online (Sandbox Code Playgroud)

这将被称为这样:

 getOriginalAndScaledSizeOfNode(pokemonModel, scalar: 0.01)
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您说要在模型上方放置一个“气泡”,然后可以这样做:

func getSizeOfNodeAndPositionBubble(_ node: SCNNode, scalar: Float){

     //1. Get The Size Of The Node Without Scale
     let (minVec, maxVec) = node.boundingBox
     let unScaledHeight = maxVec.y - minVec.y
     let unScaledWidth = maxVec.x - minVec.x

     print("""
         UnScaled Height = \(unScaledHeight)
         UnScaled Width = \(unScaledWidth)
         """)

     //2. Get The Size Of The Node With Scale
     let max = node.boundingBox.max
     let maxScale = SCNVector3(max.x * scalar, max.y * scalar, max.z * scalar)

     let min = node.boundingBox.min
     let minScale = SCNVector3(min.x * scalar, min.y * scalar, min.z * scalar)

     let heightOfNodeScaled = maxScale.y - minScale.y
     let widthOfNodeScaled = maxScale.x - minScale.x

     print("""
         Scaled Height = \(heightOfNodeScaled)
         Scaled Width = \(widthOfNodeScaled)
         """)

     //3. Create A Buubble
     let pointNodeHolder = SCNNode()
     let pointGeometry = SCNSphere(radius: 0.04)
     pointGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
     pointNodeHolder.geometry = pointGeometry

     //4. Place The Bubble At The Origin Of The Model, At The Models Origin + It's Height & At The Z Position

     pointNodeHolder.position = SCNVector3(node.position.x, node.position.y + heightOfNodeScaled, node.position.z)
          self.augmentedRealityView.scene.rootNode.addChildNode(pointNodeHolder)

}
Run Code Online (Sandbox Code Playgroud)

这产生了以下结果(我也在其他一些不幸的 Pokemon 上进行了测试):

在此处输入图片说明

您可能还想在计算中添加一些“填充”,以便节点比模型顶部高一点,例如:

 pointNodeHolder.position = SCNVector3(node.position.x, node.position.y + heightOfNodeScaled + 0.1, node.position.z) 
Run Code Online (Sandbox Code Playgroud)

我不太擅长数学,这使用 SCNNode 来表示气泡而不是SKScene,但希望它会为您指明正确的方向......