访问几何子节点Scenekit

时间:2017-05-12 23:18:02

标签: swift scenekit scene child-nodes

我正在尝试访问childnode:boxNode,然后使用平移手势识别器的输入旋转节点。我应该如何访问此节点以便操作它?我应该在全局范围内声明多维数据集节点并修改这种方式吗?我是Swift的新手,所以如果这段代码看起来很草率,我会道歉。我想在我的panGesture()函数中添加旋转动作。谢谢!

import UIKit
import SceneKit

class GameViewController: UIViewController {

var scnView: SCNView!
var scnScene: SCNScene!

override func viewDidLoad() {
    super.viewDidLoad()
    setupView()
    setupScene()
}

//  override var shouldAutorotate: Bool {
//    return true
//  }
//
//  override var prefersStatusBarHidden: Bool {
//    return true
//  }

func setupView() {
    scnView = self.view as! SCNView
}

func setupScene() {
    scnScene = SCNScene()
    scnView.scene = scnScene
    scnView.backgroundColor = UIColor.blueColor()
    initCube()
    initLight()
    initCamera()
}

func initCube () {
    var cubeGeometry:SCNGeometry
    cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
    let boxNode = SCNNode(geometry: cubeGeometry)
    scnScene.rootNode.addChildNode(boxNode)
}

func initLight () {
    let light = SCNLight()
    light.type = SCNLightTypeAmbient
    let lightNode = SCNNode()
    lightNode.light = light
    light.castsShadow = false
    lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)
    scnScene.rootNode.addChildNode(lightNode)
}

func initCamera () {
    let camera = SCNCamera()
    let cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3(x: 0.0, y: 0.0, z: 5.0)
    scnScene.rootNode.addChildNode(cameraNode)
}

func initRecognizer () {
    let panTestRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GameViewController.panGesture(_:)))
    scnView.addGestureRecognizer(panTestRecognizer)
}

//TAKE GESTURE INPUT AND ROTATE CUBE ACCORDINGLY
func panGesture(sender: UIPanGestureRecognizer) {
    //let translation = sender.translationInView(sender.view!)
}

}

1 个答案:

答案 0 :(得分:0)

首先,您应该更新您的Xcode。看起来这是您正在使用的较旧的Swift版本。 Swift 3中file_put_contents()SCNLightTypeAmbient

所以你应该采用的方法是给你的节点名称来识别它们:

.ambient

然后你可以调用你的父节点

myChildNode.name = "FooChildBar"

您也可以在场景的根节点上使用它

let theNodeYouAreLookingFor = parentNode.childNode(withName: "FooChildBar", recursively: true)

将查看场景中的所有节点并返回您提供名称的节点。