SceneKit 相關(guān)

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ù)...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 111. [動畫系統(tǒng)]如何將其他類型的動畫轉(zhuǎn)換成關(guān)鍵幀動畫? 動畫->點緩存->關(guān)鍵幀 112. [動畫]Unit...
    胤醚貔貅閱讀 13,510評論 3 88
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,099評論 4 61
  • iOS 蘋果官方Demo合集 字數(shù)10517閱讀21059評論18喜歡144 其實, 開發(fā)了這么久, 不得不說, ...
    bingo居然被占了閱讀 10,587評論 2 31
  • 更新:【面試題含答案】http://bbs.9ria.com/thread-288394-1-1.html 高頻問...
    好怕怕閱讀 5,081評論 3 53
  • AndroidManifest.xml文件中可以設(shè)置 android:largeHeap="true" 分配更多的...
    waven閱讀 529評論 0 0

友情鏈接更多精彩內(nèi)容