如何在 SceneKit 中添加对象 SWIFT

How to Added objects in SceneKit SWIFT

我有 2 个文件是从 Blender(3D 设计程序)导入的,它们都是 .dae 具体来说它们是 "CampusField1.dae" CampusField 是游戏的 ground/floor 而 "Bob.dae" 是Man/character。我的问题是,当我将 CampusField1 设置为场景时,如何在场景中也获得 "Bob" 。另一个问题是,假设我从搅拌机中导出 .dae,现在我将文件放入游戏中……一切都很好,但是 Bob 的动画是否已经附加到 Bob.dae 文件,或者我必须从 blender 导出其他东西,这样我就可以 运行 动画,因为我不知道动画 ID 是什么,也不知道如何实际制作它 运行 以及如何让 Bob 做某事。

代码:

import UIKit
import QuartzCore
import SceneKit

class GameViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()

    let scene = SCNScene(named: "art.scnassets/CampusField1.dae")!

    let src = SCNSceneSource(URL: yourSceneURL, options: nil)
    let node = src.entryWithIdentifier("Bob", withClass: SCNNode.self) as SCNNode
    let animation = node.entryWithIdentifier("yourAnimationID", withClass: CAAnimation.self) as CAAnimation

下面是完整的游戏控制器!:

  import UIKit
  import QuartzCore
  import SceneKit

   //============================================================
  class GameViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    //-------------------------
    let scene = SCNScene(named: "art.scnassets/CampusField1.dae")!

    let src = SCNSceneSource(URL: yourSceneURL, options: nil)
    let node = src.entryWithIdentifier("Bob", withClass: SCNNode.self) as SCNNode
    let animation = node.entryWithIdentifier("yourAnimationID", withClass: CAAnimation.self) as CAAnimation
    //--------------------------
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene.rootNode.addChildNode(cameraNode)
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
    //-----------------------------------------------
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)
    //-----------------------------------------------
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    scene.rootNode.addChildNode(ambientLightNode)
    //----------------------------------------------
    //_ = scene.rootNode.childNodeWithName("Bob", recursively: true)!
    // _ = scene.rootNode.childNodeWithName("CampusField1", recursively: true)!

    //--------------------------------------------------------
    // Bob.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))

    let scnView = self.view as! SCNView

    scnView.scene = scene

    scnView.allowsCameraControl = true
    scnView.showsStatistics = false
    scnView.backgroundColor = UIColor.whiteColor()

    let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
    scnView.addGestureRecognizer(tapGesture)
    }

func handleTap(gestureRecognize: UIGestureRecognizer) {
    let scnView = self.view as! SCNView
    let p = gestureRecognize.locationInView(scnView)
    let hitResults = scnView.hitTest(p, options: nil)
    if hitResults.count > 0 {
        let result: AnyObject! = hitResults[0]
        let material = result.node!.geometry!.firstMaterial!
        SCNTransaction.begin()
        SCNTransaction.setAnimationDuration(0.5)
        SCNTransaction.setCompletionBlock {
            SCNTransaction.begin()
            SCNTransaction.setAnimationDuration(0.5)
            material.emission.contents = UIColor.blackColor()
            SCNTransaction.commit()
        }
        material.emission.contents = UIColor.yellowColor()
        SCNTransaction.commit()
    }
}
    //==================================================
override func shouldAutorotate() -> Bool {
    return true
}
 //============================
    override func prefersStatusBarHidden() -> Bool {
       return true
   }
 //==========================
   override func supportedInterfaceOrientations() ->    UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
        } else {
           return .All
      }
     }
      //=============================
       override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
     }

   }

首先要告诉你的是你有两个SCNScene; CampusField 和鲍勃。所以需要把Bob场景中的角色节点取出来

您需要将节点命名为上图中的名称。并从场景中提取该节点:

let bobScene = SCNScene(named: "Bob.dae") 
let bobNode = personScene?.rootNode.childNodeWithName("person", recursively: true) 
let campusFieldScene = SCNScene(named: "CampusField1.dae") 
campusFieldScene.rootNode.addChildNode(bobNode)