Macbook pro (OS X EI Captian 10.11.5)
Xcode 7.3.1案例以Game Template 開始,移除原範本加入的Ship與動畫(Animation),再參考範例以迴圈加入內建3D物件
進階
1. 開啟遊戲範本專案
2. GameViewController.swft
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
//let scene = SCNScene(named: "art.scnassets/ship.scn")! 移除Ship
let scene = SCNScene(); //建立空白場景
// 建立內建3D物件
var geometries = [SCNSphere(radius: 1.0),
SCNPlane(width: 1.0, height: 1.5),
SCNBox(width: 1.0, height: 1.5, length: 2.0, chamferRadius: 0.0),
SCNPyramid(width: 2.0, height: 1.5, length: 1.0),
SCNCylinder(radius: 1.0, height: 1.5),
SCNCone(topRadius: 0.5, bottomRadius: 1.0, height: 1.5),
SCNTorus(ringRadius: 1.0, pipeRadius: 0.2),
SCNTube(innerRadius: 0.5, outerRadius: 1.0, height: 1.5),
SCNCapsule(capRadius: 0.5, height: 2.0)]
var x:Float = 0.0
for index in 0..<geometries.count {
let hue:CGFloat = CGFloat(index) / CGFloat(geometries.count)
let color = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
let geometry = geometries[index]
geometry.firstMaterial?.diffuse.contents = color
let node = SCNNode(geometry: geometry)
node.position = SCNVector3(x: x, y: 0.0, z: 0.0)
scene.rootNode.addChildNode(node)
x += 2.5
}
// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
// create and add a light to the scene
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)
// retrieve the ship node
//let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!
// animate the 3d object
//ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
...
3. 結果:
4. 加入動畫(可在加入3D物件進入場景前設定)
if index == 0 {
// Animation
let moveUp = SCNAction.moveByX(0.0, y: 1.0, z: 0.0, duration: 1.0)
let moveDown = SCNAction.moveByX(0.0, y: -1.0, z: 0.0, duration: 1.0)
let sequence = SCNAction.sequence([moveUp,moveDown])
node.runAction(SCNAction.repeatActionForever(sequence))
//let moveUp = SCNAction.moveByX(0.0, y: 1.0, z: 0.0, duration: 1.0)
//node.runAction(SCNAction.repeatActionForever(moveUp))
}
scene.rootNode.addChildNode(node)
沒有留言:
張貼留言