我试图将 SCNCylinder 节点放在触摸点的场景中。我总是希望显示面向相机的圆柱形状直径。它适用于水平场景,但在垂直场景中有问题。在垂直场景中,我可以看到圆柱面,但我想显示朝向相机的全直径,无论相机方向是什么。我知道根据相机变换需要应用一些转换,但不知道如何。我没有使用平面检测它直接添加到场景中的简单节点。
垂直图像:
水平图像:
插入节点的代码如下,
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitResult = result.last else {
print("returning because couldn't find the touch point")
return
}
let hitTransform = SCNMatrix4(hitResult.worldTransform)
let position = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
let ballShape = SCNCylinder(radius: 0.02, height: 0.01)
let ballNode = SCNNode(geometry: ballShape)
ballNode.position = position
sceneView.scene.rootNode.addChildNode(ballNode)
}
任何帮助,将不胜感激。
1 回答
我不确定这是处理你需要的正确方法,但这里有一些可以帮到你的东西。
我认为
CoreMotion
可以帮助您确定设备是处于水平还是垂直角度。首先导入
CoreMotion
:然后创建以下变量:
然后我们将创建一个函数来检查我们设备的角度,如下所示:
这只需要调用一次,例如在
viewDidLoad
中:在 touchesBegan 方法中,您可以将其添加到最后:
我相信有更好的方法来实现你所需要的(我也非常有兴趣听到它们),但我希望它会让你开始。
结果如下: