Xcode 如何在SceneKit SWIFT中添加对象

Xcode 如何在SceneKit SWIFT中添加对象,xcode,swift,blender,scenekit,Xcode,Swift,Blender,Scenekit,我有两个从Blender(3D设计程序)导入的文件,它们都是。具体来说,它们是“CampusField1.dae”。CampusField是游戏的底层/底层,“Bob.dae”是人/角色。我的问题是,当我把CampusField1设置为场景时,如何让“Bob”也出现在场景中。另一个问题是,假设我从blender导出.dae,现在我将文件放入游戏中。。。一切都很好,但是Bob的动画是否已附加到Bob.dae文件,或者我是否必须从blender中导出其他内容,以便我可以运行动画,因为我不知道动画ID

我有两个从Blender(3D设计程序)导入的文件,它们都是。具体来说,它们是“CampusField1.dae”。CampusField是游戏的底层/底层,“Bob.dae”是人/角色。我的问题是,当我把CampusField1设置为场景时,如何让“Bob”也出现在场景中。另一个问题是,假设我从blender导出.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
    //--------------------------
    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;坎普斯菲尔德和鲍勃。因此,需要从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)

太棒了,你有没有可能知道如何给皮肤角色设置动画?我看了一下,我想我真的很接近了。我想我会把我的角色放在场景中,然后从那里加载一个单独的.dae/Collada文件中的动画,其中包含同一个角色,就像一个行走动画一样。idk?有两种方法可以制作行走动画:1。可以从三维建模软件(如blender或maya)导出动画角色。2.或者命名各个节点并使用核心动画设置这些节点的动画…:]
let bobScene = SCNScene(named: "Bob.dae") 
let bobNode = personScene?.rootNode.childNodeWithName("person", recursively: true) 
let campusFieldScene = SCNScene(named: "CampusField1.dae") 
campusFieldScene.rootNode.addChildNode(bobNode)