Macbook pro (OS X EI Captian 10.11.5)
Xcode 7.3.1
案例以Game Template 開始,移除原範本加入的Ship與動畫(Animation),再參考範例加入3D物件
1. 開啟遊戲範本專案
2. GamViewController.swift
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene 1.3 移除 ship 物件
// let scene = SCNScene(named: "art.scnassets/ship.scn")!
// 1.4 add Empty SCNScene 建置空場景
let scene = SCNScene()
// 1.5 加入圓
let sphereGeometry = SCNSphere(radius: 1.0)
sphereGeometry.firstMaterial?.diffuse.contents = UIColor.orangeColor()
let sphereNode = SCNNode(geometry: sphereGeometry)
scene.rootNode.addChildNode(sphereNode)
// create and add a camera to the scene 0.1 加入相機
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 0.2 加入Omni光源
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 1.1 移除 ship 動畫
//let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!
// animate the 3d object 1.2 移除 ship 動畫
//ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
// retrieve the SCNView
let scnView = self.view as! SCNView
// set the scene to the view
scnView.scene = scene
// allows the user to manipulate the camera
scnView.allowsCameraControl = true
// show statistics such as fps and timing information
scnView.showsStatistics = true
// configure the view
scnView.backgroundColor = UIColor.blackColor()
// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
scnView.addGestureRecognizer(tapGesture)
}
3. Result
沒有留言:
張貼留言