[Swift] SceneKitに用意されている基本形状を表示させる

2019年6月14日ITARKit, Swift

SceneKitにはジオメトリタイプに様々なノードが用意されています。

https://developer.apple.com/documentation/scenekit/built-in_geometry_types

ひと通り表示させてみました。

左上から順番に

SCNBox、SCNCapsule、SCNCone、

SCNCylinder、SCNPlane、SCNPyramid、

SCNSphere、SCNTorus、SCNTube

です。

重ね合わせるとちょっとした3Dモデルを表現できます。

ShapesViewController.swift

import UIKit
import SceneKit
import ARKit

class ShapesViewController: UIViewController {

    let BODY_H: CGFloat = 0.2
    let FACE_H: CGFloat = 0.3
    
    @IBOutlet var sceneView: ARSCNView!
    let configuration = ARWorldTrackingConfiguration()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // ARに内の座標軸と点群表示
        self.sceneView.debugOptions = [.showWorldOrigin, .showFeaturePoints]
        // fpsなどの表示
        sceneView.showsStatistics = true
        // 全方向光源ON
        sceneView.autoenablesDefaultLighting = true
        
        sceneView.session.run(configuration)
        
        self.addShapes()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // Pause the view's session
        sceneView.session.pause()
    }
    
    func addShapes() {

        // SCNBox
        let boxNode = SCNNode(geometry: SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0))
        boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
        boxNode.position = SCNVector3(-0.5, 0.5, -1)
        self.sceneView.scene.rootNode.addChildNode(boxNode)
        
        // SCNCapsule
        let capsuleNode = SCNNode(geometry: SCNCapsule(capRadius: 0.2, height: 0.3))
        capsuleNode.geometry?.firstMaterial?.diffuse.contents = UIColor.yellow
        capsuleNode.position = SCNVector3(0, 0.5, -1)
        self.sceneView.scene.rootNode.addChildNode(capsuleNode)
        
        // SCNCone
        let coneNode = SCNNode(geometry: SCNCone(topRadius: 0.1, bottomRadius: 0.2, height: 0.2))
        coneNode.geometry?.firstMaterial?.diffuse.contents = UIColor.orange
        coneNode.position = SCNVector3(0.5, 0.5, -1)
        self.sceneView.scene.rootNode.addChildNode(coneNode)
        
        // SCNCylinder
        let cylinderNode = SCNNode(geometry: SCNCylinder(radius: 0.2, height: 0.2))
        cylinderNode.geometry?.firstMaterial?.diffuse.contents = UIColor.green
        cylinderNode.position = SCNVector3(-0.5, 0, -1)
        self.sceneView.scene.rootNode.addChildNode(cylinderNode)
        
        // SCNPlane
        let planeNode = SCNNode(geometry: SCNPlane(width: 0.3, height: 0.3))
        planeNode.geometry?.firstMaterial?.diffuse.contents = UIColor.purple
        planeNode.position = SCNVector3(0, 0, -1)
        self.sceneView.scene.rootNode.addChildNode(planeNode)
        
        // SCNPyramid
        let pyramidNode = SCNNode(geometry: SCNPyramid(width: 0.2, height: 0.2, length: 0.2))
        pyramidNode.geometry?.firstMaterial?.diffuse.contents = UIColor.cyan
        pyramidNode.position = SCNVector3(0.5, 0, -1)
        self.sceneView.scene.rootNode.addChildNode(pyramidNode)
        
        // SCNSphere
        let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.2))
        sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        sphereNode.position = SCNVector3(-0.5, -0.5, -1)
        self.sceneView.scene.rootNode.addChildNode(sphereNode)
        
        // SCNTorus
        let torusNode = SCNNode(geometry: SCNTorus(ringRadius: 0.1, pipeRadius: 0.05))
        torusNode.geometry?.firstMaterial?.diffuse.contents = UIColor.brown
        torusNode.position = SCNVector3(0, -0.5, -1)
        self.sceneView.scene.rootNode.addChildNode(torusNode)
        
        // SCNTube
        let tubeNode = SCNNode(geometry: SCNTube(innerRadius: 0.2, outerRadius: 0.2, height: 0.2))
        tubeNode.geometry?.firstMaterial?.diffuse.contents = UIColor.magenta
        tubeNode.position = SCNVector3(0.5, -0.5, -1)
        self.sceneView.scene.rootNode.addChildNode(tubeNode)
        
        
        let bodyNode = SCNNode(geometry: SCNBox(width: 0.2, height: BODY_H, length: 0.2, chamferRadius: 0))
        bodyNode.geometry?.firstMaterial?.diffuse.contents = UIColor.gray
        bodyNode.position = SCNVector3(-0.5, -0.5, 0)
        
        let faceNode = SCNNode(geometry: SCNBox(width: 0.3, height: FACE_H, length: 0.3, chamferRadius: 0))
        faceNode.position = SCNVector3(0, (BODY_H+FACE_H)/2, 0)
        faceNode.geometry?.firstMaterial?.diffuse.contents = UIColor.gray
        
        let eyeLeftNode = SCNNode(geometry: SCNPlane(width: 0.03, height: 0.06))
        eyeLeftNode.geometry?.firstMaterial?.diffuse.contents = UIColor.black
        eyeLeftNode.position = SCNVector3(0.05, 0, 0.151)
        
        let eyeRightNode = SCNNode(geometry: SCNPlane(width: 0.03, height: 0.06))
        eyeRightNode.geometry?.firstMaterial?.diffuse.contents = UIColor.black
        eyeRightNode.position = SCNVector3(-0.05, 0, 0.151)
        
        self.sceneView.scene.rootNode.addChildNode(bodyNode)
        bodyNode.addChildNode(faceNode)
        faceNode.addChildNode(eyeLeftNode)
        faceNode.addChildNode(eyeRightNode)
    }

}

スポンサーリンク

Posted by nobuhiro harada