高級 3D 圖形渲染:
簡易結構:
動畫和物理模擬:
材質和光照:
內置幾何和相機控制:
支援 ARKit:
SCNView:
SCNScene:
SCNNode:
SCNGeometry:
SCNCamera:
SCNLight:
以下是一個簡單的 SceneKit 使用示例,在 SCNView
中顯示一個旋轉的立方體:
import UIKit
import SceneKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 創建SCNView
let sceneView = SCNView(frame: self.view.bounds)
self.view.addSubview(sceneView)
// 創建SCNScene
let scene = SCNScene()
sceneView.scene = scene
// 創建SCNBox
let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
// 創建SCNNode
let boxNode = SCNNode(geometry: box)
// 將節點加入場景
scene.rootNode.addChildNode(boxNode)
// 添加運動
let spin = CABasicAnimation(keyPath: "rotation")
spin.toValue = NSValue(scnVector4: SCNVector4(x: 0, y: 1, z: 0, w: Float.pi * 2))
spin.duration = 10
spin.repeatCount = Float.infinity
boxNode.addAnimation(spin, forKey: "spin")
// 添加光源
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light?.type = .omni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
// 添加相機
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 1)
scene.rootNode.addChildNode(cameraNode)
// 驗證設置
sceneView.allowsCameraControl = true
sceneView.backgroundColor = UIColor.black
}
}