使用SceneKit for hitTesting不返回带有SCNNode的命中

时间:2015-03-22 19:09:33

标签: swift scenekit scnnode

XCode中的文档明确指出,当计划测试3D线段时,可以使用SCNRender,SCNView或SCNNode自己完成在SceneKit中使用hitTesting几何体。我有一个SCNScene的用途,它的节点没有渲染器或视图,因此我打算使用SCNNode hitTesting。我创建了一个SCNScene,在其中放入一个SCNNode并测试一条经过的简单光线,但我总是得到一个空的hitList,我不明白为什么:

import Swift
import SceneKit

let boxGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0)
let boxNode = SCNNode(geometry: boxGeometry)

var scene = SCNScene()
scene.rootNode.addChildNode(boxNode)

let from = SCNVector3(x: 0, y: -2, z: 0)
let to = SCNVector3(x: 0, y: 2 , z: 0)

var hits = scene.rootNode.hitTestWithSegmentFromPoint(from, toPoint: to, options:nil) // this is always empty
if hits != nil {
    if hits!.count > 0 {
        var hit = (hits!.first as! SCNHitTestResult).node as SCNNode
    }
}

我尝试过各种形式的选项,但没有任何改变。

  1. SCNHitTestFirstFoundOnlyKey:是或否不会改变任何内容
  2. SCNHitTestSortResultsKey:是或否不会改变任何内容
  3. SCNHitTestClipToZRangeKey:对SCNNode无效
  4. SCNHitTestBackFaceCullingKey:是或否不会改变任何内容
  5. SCNHitTestBoundingBoxOnlyKey:是或否不会改变任何内容
  6. SCNHitTestRootNodeKey:场景的rootNOde或boxNode不会改变
  7. SCNHitTestIgnoreHiddenNodesKey:是或否不会改变任何内容
  8. 我做错了什么?

2 个答案:

答案 0 :(得分:1)

我找到了答案,这是一个错误或一个功能:使用SCNScene及其节点SCNNode进行3D hitTesting,特别是方法:" hitTestWithSegmentFromPoint(toPoint:options :)"除非场景包含在SCNView中,否则不会返回命中。它似乎无法在屏幕外使用。我的猜测是你的原因,虽然我可以想象它与在显卡上执行一些非常昂贵的计算有关。

我使用GameView SCNScene启动项目对此进行了测试。关键线是self.gameView!.scene = scene

override func awakeFromNib(){
    let scene = SCNScene()
    let boxGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
    let boxNode = SCNNode(geometry: boxGeometry)
    boxNode.position=SCNVector3(x: 0, y: 0, z: 0)
    scene.rootNode.addChildNode(boxNode)

    // create and add a camera to the scene
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene.rootNode.addChildNode(cameraNode)

    // place the camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

    // create and add a light to the scene
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)

    // create and add an ambient light to the scene
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = NSColor.darkGrayColor()
    scene.rootNode.addChildNode(ambientLightNode)

    // set the scene to the view
    // uncomment this to fail
    self.gameView!.scene = scene

    // allows the user to manipulate the camera
    self.gameView!.allowsCameraControl = true

    // show statistics such as fps and timing information
    self.gameView!.showsStatistics = true

    // configure the view
    self.gameView!.backgroundColor = NSColor.blackColor()

    let hitList = scene.rootNode.hitTestWithSegmentFromPoint(SCNVector3(x:-10,y:0,z:0), toPoint: SCNVector3(x:10,y:0,z:0), options:[SCNHitTestBackFaceCullingKey:false, SCNHitTestSortResultsKey:true, SCNHitTestIgnoreHiddenNodesKey:false])

    if hitList?.count > 0 {
        println("Hit found: \n\n\( hitList![0] )") // assign self.gameView!.scene = scene to reach this point.
    } else {
        println("No hit") // uncomment self.gameView!.scene = scene to reach this point.
    }

}

答案 1 :(得分:0)

我在使用hitTestWithSegmentFromPoint时遇到了麻烦。

我在viewDidLoad()中调用它并且它返回了一个0元素数组,但我确信它有一个命中。

在viewDidAppear()(或更高版本)中调用它解决了我的问题。