如何从主art.scnassets文件夹向SceneKit添加SCNode

时间:2015-10-25 00:27:28

标签: ios swift scenekit

我想添加一个位于art.assets文件夹中的.dae文件。到了现场。多数民众赞成在洛杉矶很快,但我也会采用Objective-C。在这里感谢一些代码,它只是Xcode为您提供的基本场景工具包文件。

代码:

 import UIKit
 import QuartzCore
 import SceneKit

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

    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)



    //--------------------------
    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)))

    }

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)

我使用此代码

获取了场景中的对象

代码:

    let scene = SCNScene(named: "art.scnassets/GenricFootball.dae")!
    let characterscene = SCNScene(named: "art.scnassets/untitled.dae")!
    let monkey: SCNNode = characterscene.rootNode.childNodeWithName("Cube_001", recursively: true)!
    scene.rootNode.addChildNode(monkey)
    monkey.position = SCNVector3(x: 5, y: 0, z: 5)