我有一个简单的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.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)
}

我添加了一个多维数据集
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)
}

当我第一次添加它时,我看到了前面和右边。如果我在另一边的立方体周围添加另一个立方体,我会看到立方体的背面。所以,我能不能这样做,让用户只看到前面,不管用户怎么转。
谢谢您。

最佳答案

您可以使用SCNBillboardConstraint使给定节点朝向相机。只需在添加到boxNode.constraints = [SCNBillboardConstraint()]之前添加rootNode

关于swift - SCNBox使正面在ARKit中旋转到相机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46745115/

10-12 14:39