简体   繁体   English

如何使用 ARKit 将 SCNNode 添加到sceneView?

[英]How to tap to add SCNNode to sceneView using ARKit?

I am trying to implement a function that allows the user to tap and add a node to the to a scene at the location where the user clicked.我正在尝试实现一个功能,该功能允许用户点击并将节点添加到用户单击位置的场景中。 I would like this to be on a plane.我希望这是在飞机上。 I did some research and found the following function, but I get the warning Value of type 'simd_float4x4' has no member 'translation' on the line let translation = hitTestResult.worldTransform.translation我做了一些研究,发现了以下函数,但我Value of type 'simd_float4x4' has no member 'translation'收到警告Value of type 'simd_float4x4' has no member 'translation' let translation = hitTestResult.worldTransform.translation

Does anyone know how I can change this so I am not getting the warning?有谁知道我如何改变它,所以我没有收到警告?

@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)
    }

The result of hitResult.worldTransform is an SCNMatrix4 and a 4x4 matrix has no translation property. hitResult.worldTransform的结果是一个SCNMatrix4 ,一个 4x4 矩阵没有translation属性。 When applied to a point using matrix multiplication the transformation done might include a translation, but the underlying data type is essentially a 16 element rectangular array.当使用矩阵乘法应用于一个点时,完成的转换可能包括平移,但底层数据类型本质上是一个 16 元素的矩形数组。

You can probably extract the translation aspect of the matrix with:您可以使用以下方法提取矩阵的平移方面:

let translation = SCNVector4(float4x4(hitTestResult.worldTransform) * SIMD4<Float>(0,0,0,1))

I'm assuming the hitTestResult.worldTransform matrix is affine.我假设hitTestResult.worldTransform矩阵是仿射的。 It would be weird if it were not.如果不是,那会很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM