nslllava
nslllava

Reputation: 599

SCNBox make the front side rotated to the camera in ARKit

I have a simple ARKit app. When the user touches the screen

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

I add a cube

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

When I add it for the first time, I see the front side and a bit of the right side. if I go around the cube on the other side and add another cube, I see the back side of the cube. So, can I do it so that the user sees only the front side whatever how the user goes around.

Thank you.

Upvotes: 3

Views: 667

Answers (1)

jpetrichsr
jpetrichsr

Reputation: 247

You can use SCNBillboardConstraint to make a given node face the camera. Just add boxNode.constraints = [SCNBillboardConstraint()] before adding it to the rootNode.

Upvotes: 1

Related Questions