gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:29:24 24 4
gpt4 key购买 nike

我正在使用 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 模型的高度,然后是它转换后的屏幕坐标,这样无论我拿着手机在哪里,气泡信息看起来总是在头部旁边?

enter image description here

最佳答案

每个 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 Editor 中查看,您还可以看到以米为单位的模型大小(我说这只是作为您可以引用以检查计算的一个点):

enter image description here

在我的示例中,我使用的是上述尺寸的口袋妖怪模型。

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

 pokemonModel.scale = SCNVector3(0.01, 0.01, 0.01)

所以为了得到 SCNNodeboundingBox 我们可以这样做:

/// 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 已经缩放,这并没有多大帮助,所以显然我们需要通过重写函数来考虑到这一点:

/// 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)

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

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)

}

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

enter image description here

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

 pointNodeHolder.position = SCNVector3(node.position.x, node.position.y + heightOfNodeScaled + 0.1, node.position.z) 

我的数学不是很好,这使用 SCNNode 作为气泡而不是 SKScene,但希望它能为您指明正确的方向...

关于ios - 获取 3D 模型的高度及其转换后的屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49412174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com