Press ESC to close

Creating and displaying objects with ARKit

Hello friends, in this article, I will talk about how we can show objects on the camera in the real world with ARKit. In my previous article, I talked about how to create a simple project with ARKit. You can find that article here.

First, we need to create a Node to create an object on the screen. We need to adjust where this object will stand in the real world, its color, etc. In the example below, the object is created in many different ways. From here, you can create objects as necessary for your own project.

//
//  ViewController.swift
//  scene-view
//
//  Created by Omer Sezer on 3.07.2021.
//

import UIKit
import ARKit

class ViewController: UIViewController {

    @IBOutlet weak var sceneView: ARSCNView!
    let configuration = ARWorldTrackingConfiguration()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    func setUI() {
        // MARK: sceneView
        sceneView.debugOptions = [.showFeaturePoints, .showWorldOrigin]
        sceneView.session.run(configuration)
        sceneView.autoenablesDefaultLighting = true
        
        let createdNode = createCustom()
        createdNode.position = SCNVector3(0, 0, -0.2)
        sceneView.scene.rootNode.addChildNode(createdNode)
    }

    func createBox() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0.03)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createCapsule() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNCapsule(capRadius: 0.1, height: 0.3)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createCone() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNCone(topRadius: 0, bottomRadius: 0.3, height: 0.3)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createCylinder() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNCylinder(radius: 0.2, height: 0.1)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createSphere() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNSphere(radius: 0.1)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createTube() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNTube(innerRadius: 0.1, outerRadius: 0.3, height: 0.3)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createTorus() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNTorus(ringRadius: 0.3, pipeRadius: 0.1)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createPlane() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNPlane(width: 0.2, height: 0.2)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createPyramid() -> SCNNode {
        let node = SCNNode()
        node.geometry = SCNPyramid(width: 0.1, height: 0.1, length: 0.1)
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }
    
    func createCustom() -> SCNNode {
        let node = SCNNode()
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 0, y: 0.02))
        path.addLine(to: CGPoint(x: 0.02, y: 0.03))
        path.addLine(to: CGPoint(x: 0.04, y: 0.02))
        path.addLine(to: CGPoint(x: 0.04, y: 0))
        let shape = SCNShape(path: path, extrusionDepth: 0.02)
        node.geometry = shape
        node.geometry?.firstMaterial?.specular.contents = UIColor.orange
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        return node
    }

}

Box

More detailed information can be found here.

Capsule

Detailed information can be found here.

Cone

Detailed information can be found here.

Cylinder

More detailed information can be found here.

Sphere

More detailed information can be found here.

Tube

More detailed information can be found here.

Torus

More detailed information can be found here.

Plane

More detailed information can be found here.

Pyramid

More detailed information can be found here.

Special

Sometimes you may want to create different custom objects in addition to these objects. Here it depends a little on your imagination and work. I created a small house object. I shared the codes above, you can check it from there. The screenshot is as follows.

 

If you have questions, you can reach us by sending an e-mail or comment. Good work.

Leave a Reply

Your email address will not be published. Required fields are marked *