SCNBox使正面在ARKit中旋转到相机

时间:2017-10-14 13:29:40

标签: 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.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)
}

当我第一次添加它时,我会看到正面和右边的一点。如果我绕过另一边的立方体并添加另一个立方体,我会看到立方体的背面。因此,我是否可以这样做,以便用户只看到正面,无论用户如何绕过。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用SCNBillboardConstraint使给定节点面向相机。只需添加boxNode.constraints = [SCNBillboardConstraint()],然后再将其添加到rootNode