Macbook pro (OS X EI Captian 10.11.5)
Xcode 7.3.1
案例以Single View Template 始,依序加入SceneKit Framework, PrimitivesScene.swift(建置繼承SCNView 類別的PrimitivesScene類別),在PrimitivesScene類別建構元方法中建置3D物件,在ViewController的載入方法(viewDidLoad)中加入以PrimitivesScene類別建置的場景物件與,相機等物件.細部步驟:
1. Open New SingleView
2. 加入SceneKit Framework
2.1 選取專案目標(Target)
2.2 選取 Build Phases
2.3 在Link Binary With Libraries 選取+ (加入程式庫)
2.4 搜尋SceneKit.framework
3. 專案中建立新類別(加入檔案 PrimitivesScene.swift)
空白檔案僅有 import Foundation
import Foundation
//---- 依序建立繼承SCNScene類別的場景類別
import SceneKit
// 3.1. Add class 繼承 SCNScene
class PrimitivesScene : SCNScene {
// 3.2. Add init() method and required ... (auto fixed)
override init() {
super.init()
// 3.3. Add 圓
let sphereGeometry = SCNSphere(radius: 0.5)
let sphereNode = SCNNode(geometry: sphereGeometry)
self.rootNode.addChildNode(sphereNode)
// 3.4. Add 2nd circle
let secondSphereGeometry = SCNSphere(radius: 1.0)
let secondSphereNode = SCNNode(geometry: secondSphereGeometry)
secondSphereNode.position = SCNVector3(x: 3.0, y: 0.0, z: 0.0)
self.rootNode.addChildNode(secondSphereNode)
}
// 3.1.2 選取自動修正產生
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
3.5 3.2-3.4 可先測試完再依序加入
4. viewController.swift 建置程序
// 4.2.配合MVC的方式,先在 Main.storyboard 加入 Scene Kit View 物件
import UIKit
// 4.1. Add import
import SceneKit
class ViewController: UIViewController {
// 4. 3, 以輔助視窗將(View)Main.storyboad的控制項連結至(Control)類別
// Connect Scene Kit View
@IBOutlet weak var scnview: SCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 4.2-4.3 取代原範例 let scnview = self.view as! SCNView
scnview.scene = PrimitivesScene()
scnview.backgroundColor = UIColor.blackColor()
scnview.autoenablesDefaultLighting = true
scnview.allowsCameraControl = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
5. Result
沒有留言:
張貼留言