SceneKit 구 그리기(+ 조명)

Zion·2023년 4월 23일
1

import UIKit
import SceneKit

class ViewController: UIViewController {
    
    let lightNode = SCNNode()
    let scene = SCNScene()

    override func viewDidLoad() {
        super.viewDidLoad()

        let sceneView = SCNView(frame: self.view.frame)
        self.view.addSubview(sceneView)

        
        sceneView.scene = scene

        let camera = SCNCamera()
        let cameraNode = SCNNode()
        cameraNode.camera = camera
        cameraNode.position = SCNVector3(x: -3.0, y: 3.0, z: 3.0)

        let light = SCNLight()
        light.type = SCNLight.LightType.directional
//        let lightNode = SCNNode()
        lightNode.light = light
        lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)

        let sphereGeometry = SCNSphere(radius: 0.8)

        let sphereNode = SCNNode(geometry: sphereGeometry)

        let constraint = SCNLookAtConstraint(target: sphereNode)
        constraint.isGimbalLockEnabled = true
        cameraNode.constraints = [constraint]


        scene.rootNode.addChildNode(lightNode)
        scene.rootNode.addChildNode(cameraNode)
        scene.rootNode.addChildNode(sphereNode)

    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            let light = SCNLight()
            light.type = SCNLight.LightType.directional
            
            let lightNode = SCNNode()
            lightNode.light = light
            lightNode.position = SCNVector3(x: 200, y: 1001, z: 100)
            self.scene.rootNode.addChildNode(lightNode)
        }
    }

}
profile
어제보다만 나아지는

0개의 댓글