gpt4 book ai didi

ios - 旋转 SCNNode.pivot 后 SceneKit projectPoint 不正确

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:10:04 30 4
gpt4 key购买 nike

我想从 SCNNode 检索屏幕坐标,这样我就可以在 SpriteKit 叠加层中使用这些坐标。该节点是一个子节点。当我旋转父节点的枢轴 projectPoint 时返回乱码结果。

场景:

class ScenekitScene:SCNScene {
override init() {
super.init()

let nodeCamera = SCNNode()
nodeCamera.camera = SCNCamera()
rootNode.addChildNode(nodeCamera)

let geoPyramid = SCNPyramid(width: 0.2, height: 0.5, length: 0.2)
let nodeCenterOfUniverse = SCNNode(geometry: geoPyramid)
nodeCenterOfUniverse.position = SCNVector3(0, 0, -5)
nodeCenterOfUniverse.pivot = SCNMatrix4MakeRotation(CGFloat.pi/4, 0.0, 0.0, 1.0)
rootNode.addChildNode(nodeCenterOfUniverse)

let geoSphere = SCNSphere(radius: 0.3)
let nodePlanet = SCNNode(geometry: geoSphere)
nodePlanet.name = "planet"
nodePlanet.position = SCNVector3(2, 0, 0)
nodeCenterOfUniverse.addChildNode(nodePlanet)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

SCNSceneRenderDelegate:

extension ViewController:SCNSceneRendererDelegate {
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
if let planet = scene.rootNode.childNode(withName: "planet", recursively: true) {
let position = planet.convertPosition(planet.position, to: scene.rootNode)
let screencoords = renderer.projectPoint(position)
print (screencoords)

# result:
# SCNVector3(x: 461.913848876953, y: 2.58612823486328, z: 0.808080852031708)
# y should clearly be more to the center of the screen somewhere between 100-200
}
}
}

有谁知道如何获得正确的屏幕坐标?

最佳答案

看起来世界坐标中的行星位置不正确。当我手动计算行星位置时,我得到了不同的结果,并且 projectPoints 像宣传的那样工作。 [我可能会发布一个关于 worldTransform 的不同问题]。

// using SIMD
let pivCou = float4x4(nodeCenterOfUniverse.pivot)
let posCou = float3(nodeCenterOfUniverse.position)
let posCou4 = float4(posCou.x, posCou.y, posCou.z, 1.0) // convert float3 to float4
let posPlanet = float3(nodePlanet.position)
let posPlanet4 = float4(posPlanet.x, posPlanet.y, posPlanet.z, 1.0)

let worldcoordinatesPlanet = posCou4 + posPlanet4 * pivCou

关于ios - 旋转 SCNNode.pivot 后 SceneKit projectPoint 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430385/

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