Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a box with SceneKit and ARKit

I am trying to create a primitive with SceneKit and ARKit. For whatever reason, it is not working.

let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

    let node = SCNNode(geometry: box)

    node.position = SCNVector3(0,0,0)

    sceneView.scene.rootNode.addChildNode(node)

Do I need to take in the camera coordinates as well?

like image 380
paralaxbison Avatar asked Mar 08 '23 16:03

paralaxbison


1 Answers

Your code looks good and it should work. I have tried it as the below code: after creating a new app with ARKit template, I have replaced the function viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the view's delegate
    sceneView.delegate = self

    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    let node = SCNNode(geometry: box)
    node.position = SCNVector3(0,0,0)
    sceneView.scene.rootNode.addChildNode(node)
}

It creates a box at the original point (0, 0, 0). Unfortunately your device is inside the box thus you cannot see that box straightly. To see the box, move your device far aways a bit.

The attached image is the box after moving my device:

enter image description here

If you want to see it immediately, move the box to front a bit, add colour and make the first material be double side (to see it even in or out side):

    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    box.firstMaterial?.diffuse.contents = UIColor.red
    box.firstMaterial?.isDoubleSided = true
    let boxNode = SCNNode(geometry: box)
    boxNode.position = SCNVector3(0, 0, -1)
    sceneView.scene.rootNode.addChildNode(boxNode)
like image 110
Tony Avatar answered Mar 20 '23 18:03

Tony