Swift SCNBox使正面旋转到ARKit中的相机
我有一个简单的ARKit应用程序。当用户触摸屏幕时Swift SCNBox使正面旋转到ARKit中的相机,swift,scenekit,arkit,scnnode,Swift,Scenekit,Arkit,Scnnode,我有一个简单的ARKit应用程序。当用户触摸屏幕时 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.
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 { return }
let hitTrasform = SCNMatrix4(hitResult.worldTransform)
let hitVector = SCNVector3Make(hitTrasform.m41, hitTrasform.m42, hitTrasform.m43)
createBox(position: hitVector)
}
当我第一次添加它时,我看到了正面和一些右侧。如果我在另一侧的立方体周围添加另一个立方体,我会看到立方体的背面。所以,我可以这样做,让用户只看到前面,无论用户如何走
谢谢。您可以使用使给定节点面向摄影机。只需添加
boxNode.constraints=[SCNBillboardConstraint()]
然后再将其添加到rootNode
这是正确的答案,但也可以将节点的.orientation属性设置为渲染代理中SCNView(活动摄影机)视点的.orientation。
func createBox(position: SCNVector3) {
let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 1)
box.firstMaterial?.diffuse.contents = UIColor.black
let sides = [
UIColor.red, // Front
UIColor.black, // Right
UIColor.black, // Back
UIColor.black, // Left
UIColor.black, // Top
UIColor.black // Bottom
]
let materials = sides.map { (side) -> SCNMaterial in
let material = SCNMaterial()
material.diffuse.contents = side
material.locksAmbientWithDiffuse = true
return material
}
box.materials = materials
let boxNode = SCNNode(geometry: box)
boxNode.position = position
sceneView.scene.rootNode.addChildNode(boxNode)
}