SceneKit
SCNNode 結(jié)構(gòu)圖

SCNNode 1.png

SCNNode 2.png
通過兩張圖片,更好地了解SCNNode的結(jié)構(gòu)
一個渲染循環(huán)
渲染循環(huán)內(nèi)做的事

Render Cycle.png
SCNGeometry 的內(nèi)建對象

內(nèi)建對象1.jpg

內(nèi)建對象2.jpg

內(nèi)建對象3.jpg
.dae 文件全稱Data Asset Exchange,是Collada 的輸出文件

Collada file specifies.jpg
上帝說有光,于是SCNLight亮了
四種光照效果

1.jpg

2.jpg
光照的規(guī)則

光照規(guī)則.jpg
亮起來
func setupLighting(scene:SCNScene) {
let ambientLight = SCNNode()
ambientLight.light = SCNLight()
ambientLight.light!.type = SCNLightTypeAmbient
ambientLight.light!.color = UIColor(white: 0.3, alpha: 1.0)
scene.rootNode.addChildNode(ambientLight)
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeSpot
lightNode.light!.castsShadow = true
lightNode.light!.color = UIColor(white: 0.8, alpha: 1.0)
lightNode.position = SCNVector3Make(0, 80, 30)
lightNode.rotation = SCNVector4Make(1, 0, 0, Float(-M_PI/2.8))
lightNode.light!.spotInnerAngle = 0
lightNode.light!.spotOuterAngle = 50
lightNode.light!.shadowColor = UIColor.blackColor()
lightNode.light!.zFar = 500
lightNode.light!.zNear = 50
scene.rootNode.addChildNode(lightNode)
//Save for later
spotLight = lightNode
}
老板,你是土豪金的么SCNMaterial
八種不同的材質(zhì)




藍色貼圖
pyramidNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blueColor()
pyramidNode.geometry?.firstMaterial?.specular.contents = UIColor.blueColor()
pyramidNode.geometry?.firstMaterial?.shininess = 1.0
地球貼圖
globeNode.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "earthDiffuse")
globeNode.geometry?.firstMaterial?.ambient.contents = UIImage(named: "earthAmbient")
globeNode.geometry?.firstMaterial?.specular.contents = UIImage(named: "earthSpecular")
globeNode.geometry?.firstMaterial?.normal.contents = UIImage(named: "earthNormal")
globeNode.geometry?.firstMaterial?.diffuse.mipFilter = SCNFilterMode.Linear
相機走起 -- SCNCamera
添加一個cameraNode
cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.camera!.zFar = 500
cameraNode.position = SCNVector3Make(0, 30, 200)
cameraNode.rotation = SCNVector4Make(1, 0, 0, Float(-M_PI_4*0.75))
scene.rootNode.addChildNode(cameraNode)

SCNCamera.jpg
添加動畫
用CoreAnimation就好啦
直接上代碼,繞y軸旋轉(zhuǎn)
// Spin around the Y-Axis
rotation.fromValue = NSValue(SCNVector4:SCNVector4Make(0, 0, 0, 0))
rotation.toValue = NSValue(SCNVector4:SCNVector4Make(0, 1, 0, Float(2.0*M_PI)))
rotation.duration = 5
rotation.repeatCount = .infinity
pyramidNode.addAnimation(rotation, forKey: "rotation")```
#####添加SCNAction 實現(xiàn)動畫
實現(xiàn)上下跳動
```swift
//上下跳動
let moveGlobeUp = SCNAction.moveByX(0.0, y: 10.0, z: 0.0, duration: 1.0)
let moveGlobeDown = SCNAction.moveByX(0.0, y: -10.0, z: 0.0, duration: 1.0)
let sequence = SCNAction.sequence([moveGlobeUp, moveGlobeDown])
let repeateSequence = SCNAction.repeatActionForever(sequence)
globeNode.runAction(repeateSequence)
放大縮小
let scaleToZero = SCNAction.scaleTo(0.0, duration: 0)
let scaleUp = SCNAction.scaleTo(1.0, duration: 5)
let opacityToZero = SCNAction.fadeOutWithDuration(5)
let sequence = SCNAction.sequence([scaleToZero, scaleUp, opacityToZero])
let repeateSequence = SCNAction.repeatAction(sequence, count: 10)
cylinderNode.runAction(repeateSequence)
動起來
鏡頭動作
讓鏡頭跟著移動
//follow the camera
let spaceman = spaceManNode.presentationNode
let spacemanPosition = spaceman.position
let cameraDamping:Float = 0.05
let targetPosition = SCNVector3Make(spacemanPosition.x, 30.0, spacemanPositio
var cameraPosition = cameraNode.position
cameraPosition = SCNVector3Make(cameraPosition.x * (1.0 - cameraDamping) + tar
* cameraDamping,
cameraPosition.y * (1.0 - cameraDamping) + targetPosition.y * cameraDampin
cameraPosition.z * (1.0 - cameraDamping) + targetPosition.z * cameraDampin
cameraNode.position = cameraPosition
添加.dae 文件到scene
func setupEnemy(scene:SCNScene) -> SCNNode {
let enemyScene = SCNScene(named: "art.scnassets/Enemy.dae")
let enemyNode = enemyScene!.rootNode.childNodeWithName("enemy", recursively: false)
enemyNode!.name = "enemy"
enemyNode!.position = SCNVector3Make(40, 10, 30)
enemyNode!.rotation = SCNVector4Make(0, 1, 0, Float(M_PI))
scene.rootNode.addChildNode(enemyNode!)
return enemyNode!
}
碰撞檢測SCNPhysicsBody
三種碰撞體

三種碰撞體.jpg
在SceneKit 里面使用SpriteKit
繪制2d效果
import SpriteKit
import SceneKit
class GameOverlay: SKScene {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
碰撞這一塊實現(xiàn)的不是很好,暫時先不研究了
以上代碼圖像來自:Beginning Swift Games Development for iOS 一書,沒搜到中文翻譯版,建議做3D開發(fā)的童鞋自行下載此書,查詞典看完(本書提供的source code 不太全,得配合著書進行完善)
Scenekit內(nèi)容很豐富,后續(xù)應該還會繼續(xù)補充...
待續(xù)...