如何在SceneKit SWIFT中添加对象

时间:2015-10-15 07:10:19

标签: xcode swift blender scenekit

我有两个从Blender(3D设计程序)导入的文件,它们都是.dae,特别是它们是“CampusField1.dae”.CamponField是游戏的底层/地板,“Bob.dae”是人/角色。我的问题是当我将CampusField1设置为场景时,我如何在场景中获得“Bob”。另一个问题是让我说我​​从混合器中导出.dae现在我把文件放在游戏中......每件事都很好但是鲍勃的动画已经附加到Bob.dae文件或者我必须导出一些东西否则从搅拌机,以便我可以运行动画,因为我不知道动画ID是什么或如何实际使其运行,并实际让鲍勃做一些事情。

代码:

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

Full GameController下面!:

  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.
     }

   }

1 个答案:

答案 0 :(得分:1)

首先要告诉你的是你有两个SCNScene; CampusField和Bob。因此,您需要从Bob场景中取出角色节点。

enter image description here

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

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