番外篇 - 座標系統
Last updated
Last updated
extension Int {
func degreesToRadians() -> CGFloat {
return CGFloat(self) * CGFloat.pi / 180.0
}
} override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
sceneView.session.run(configuration)
drawRightWall()
drawLeftWall()
drawFrontWall()
drawTopWall()
}
func drawRightWall() {
let wall = SCNNode(geometry: SCNBox(width: 0.01, height: 0.2, length: 0.2, chamferRadius: 0))
wall.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
wall.position = SCNVector3(0.2, 0, 0)
// 預設是直放
wall.eulerAngles = SCNVector3(0, 0, 0)
sceneView.scene.rootNode.addChildNode(wall)
}
func drawLeftWall() {
let wall = SCNNode(geometry: SCNBox(width: 0.01, height: 0.2, length: 0.2, chamferRadius: 0))
wall.geometry?.firstMaterial?.diffuse.contents = UIColor.red
wall.position = SCNVector3(-0.2, 0, 0)
// 根據Y軸旋轉180度(在這個case,因為box是單一色,所以其實是跟沒旋轉的效果是一樣,但如果box的兩面顏色不同,那就會有差異了)
wall.eulerAngles = SCNVector3(0, 180.degreesToRadians(), 0)
sceneView.scene.rootNode.addChildNode(wall)
}
func drawFrontWall() {
let wall = SCNNode(geometry: SCNBox(width: 0.01, height: 0.2, length: 0.4, chamferRadius: 0))
wall.geometry?.firstMaterial?.diffuse.contents = UIColor.green
wall.position = SCNVector3(0, 0, -0.1)
// 根據Y軸旋轉90度,這樣是box就會面朝使用者
wall.eulerAngles = SCNVector3(0, 90.degreesToRadians(), 0)
sceneView.scene.rootNode.addChildNode(wall)
}
func drawTopWall() {
let wall = SCNNode(geometry: SCNBox(width: 0.01, height: 0.2, length: 0.4, chamferRadius: 0))
wall.geometry?.firstMaterial?.diffuse.contents = UIColor.yellow
wall.position = SCNVector3(0, 0.1, 0)
// 先根據x軸旋轉90度,再根據z軸旋轉90度
wall.eulerAngles = SCNVector3(90.degreesToRadians(), 0, 90.degreesToRadians())
sceneView.scene.rootNode.addChildNode(wall)
} guard let nodes = sceneView?.scene.rootNode.childNodes else { return }
// 每個node都要進行三次的運算
for node in nodes {
node.position.x += 10
node.position.y += 10
node.position.z += 10
}
// 透過simd執行向量運算,只要一次就可已完成上面的運算結果
// 當node很多時,這樣的差異就會更加的明顯。
let delta = simd_float3(10)
for node in nodes {
node.simdPosition += delta
}