[英]How to tap to add SCNNode to sceneView using ARKit?
我正在尝试实现一个功能,该功能允许用户点击并将节点添加到用户单击位置的场景中。 我希望这是在飞机上。 我做了一些研究,发现了以下函数,但我Value of type 'simd_float4x4' has no member 'translation'
收到警告Value of type 'simd_float4x4' has no member 'translation'
let translation = hitTestResult.worldTransform.translation
有谁知道我如何改变它,所以我没有收到警告?
@objc func addRoomToSceneView(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
guard let hitTestResult = hitTestResults.first else { return }
//THE FOLLOWING LINE HAS THE warning: Value of type 'simd_float4x4' has no member 'translation'
let translation = hitTestResult.worldTransform.translation
let x = translation.x
let y = translation.y
let z = translation.z
let room = createMaskedRectangleRoom(width: 4, height: 4, depth: 4, color: .white)
room.scale = SCNVector3(2, 2, 2)
room.position = SCNVector3(x,y,z)
sceneView.scene.rootNode.addChildNode(room)
}
hitResult.worldTransform
的结果是一个SCNMatrix4
,一个 4x4 矩阵没有translation
属性。 当使用矩阵乘法应用于一个点时,完成的转换可能包括平移,但底层数据类型本质上是一个 16 元素的矩形数组。
您可以使用以下方法提取矩阵的平移方面:
let translation = SCNVector4(float4x4(hitTestResult.worldTransform) * SIMD4<Float>(0,0,0,1))
我假设hitTestResult.worldTransform
矩阵是仿射的。 如果不是,那会很奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.