获取 3D 模型的高度及其转换后的屏幕坐标
Get 3D model's height and its transformed screen coordinates
我正在使用 ARKit
渲染 Collada (*.dae) 文件。作为我的 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
}
}
但是我的气泡总是在 3D 模型的 英尺 处,因为我只能获得模型的 SCNNode 位置,它被锚定在那里。我希望它在我的模型中处于领先地位。
有没有办法获取我的 3D 模型的高度,然后是它转换后的屏幕坐标,所以无论我和我的 phone 在哪里,气泡消息看起来总是紧挨着头?
你可以得到 borisNode.boundingBox : (float3, float3) 来计算节点的大小,你得到一个2点的元组,然后通过一个点减去另一个点的y来计算高度。最后将叠加层的 Y 位置移动您获得的数字。
每个 SCNNode 都有一个 boundingBox
属性 即:
The minimum and maximum corner points of the object’s bounding box.
所以这意味着:
Scene Kit defines a bounding box in the local coordinate space using two points identifying its corners, which implicitly determine six axis-aligned planes marking its limits. For example, if a geometry’s bounding box has the minimum corner {-1, 0, 2} and the maximum corner {3, 4, 5}, all points in the geometry’s vertex data have an x-coordinate value between -1.0 and 3.0, inclusive.
如果您在 SceneKit 编辑器中查看,您还可以看到以米为单位的模型大小(我只是说这是一个点,您可以参考以检查计算):
在我的示例中,我使用的是上述尺寸的口袋妖怪模型。
我缩放了模型(你可能也这样做了)例如:
pokemonModel.scale = SCNVector3(0.01, 0.01, 0.01)
因此,为了获得 SCNNode
的 boundingBox
,我们可以这样做:
/// 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)
""")
}
这样称呼它:
getSizeOfModel(pokemonModel)
当然,既然我们的 SCNNode 已经被缩放,这并没有多大帮助,所以显然我们需要考虑到这一点,通过 re-writing 函数:
/// 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)
""")
}
会这样称呼:
getOriginalAndScaledSizeOfNode(pokemonModel, scalar: 0.01)
完成后你说你想在你的模型上方放置一个 'bubble',然后可以这样做:
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)
}
这会产生以下结果(我也在其他一些不幸的口袋妖怪上进行了测试):
您可能还想在计算中添加一点 'padding',以便节点比模型顶部高一点,例如:
pointNodeHolder.position = SCNVector3(node.position.x, node.position.y + heightOfNodeScaled + 0.1, node.position.z)
我的数学不是很好,这使用 SCNNode 作为气泡而不是 SKScene
,但希望它能为您指明正确的方向...
我正在使用 ARKit
渲染 Collada (*.dae) 文件。作为我的 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
}
}
但是我的气泡总是在 3D 模型的 英尺 处,因为我只能获得模型的 SCNNode 位置,它被锚定在那里。我希望它在我的模型中处于领先地位。
有没有办法获取我的 3D 模型的高度,然后是它转换后的屏幕坐标,所以无论我和我的 phone 在哪里,气泡消息看起来总是紧挨着头?
你可以得到 borisNode.boundingBox : (float3, float3) 来计算节点的大小,你得到一个2点的元组,然后通过一个点减去另一个点的y来计算高度。最后将叠加层的 Y 位置移动您获得的数字。
每个 SCNNode 都有一个 boundingBox
属性 即:
The minimum and maximum corner points of the object’s bounding box.
所以这意味着:
Scene Kit defines a bounding box in the local coordinate space using two points identifying its corners, which implicitly determine six axis-aligned planes marking its limits. For example, if a geometry’s bounding box has the minimum corner {-1, 0, 2} and the maximum corner {3, 4, 5}, all points in the geometry’s vertex data have an x-coordinate value between -1.0 and 3.0, inclusive.
如果您在 SceneKit 编辑器中查看,您还可以看到以米为单位的模型大小(我只是说这是一个点,您可以参考以检查计算):
在我的示例中,我使用的是上述尺寸的口袋妖怪模型。
我缩放了模型(你可能也这样做了)例如:
pokemonModel.scale = SCNVector3(0.01, 0.01, 0.01)
因此,为了获得 SCNNode
的 boundingBox
,我们可以这样做:
/// 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)
""")
}
这样称呼它:
getSizeOfModel(pokemonModel)
当然,既然我们的 SCNNode 已经被缩放,这并没有多大帮助,所以显然我们需要考虑到这一点,通过 re-writing 函数:
/// 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)
""")
}
会这样称呼:
getOriginalAndScaledSizeOfNode(pokemonModel, scalar: 0.01)
完成后你说你想在你的模型上方放置一个 'bubble',然后可以这样做:
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)
}
这会产生以下结果(我也在其他一些不幸的口袋妖怪上进行了测试):
您可能还想在计算中添加一点 'padding',以便节点比模型顶部高一点,例如:
pointNodeHolder.position = SCNVector3(node.position.x, node.position.y + heightOfNodeScaled + 0.1, node.position.z)
我的数学不是很好,这使用 SCNNode 作为气泡而不是 SKScene
,但希望它能为您指明正确的方向...