使用SceneKit进行命中测试不通过SCNNode返回命中 [英] Using SceneKit for hitTesting not returning a hit with SCNNode

查看:26
本文介绍了使用SceneKit进行命中测试不通过SCNNode返回命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XCode中的文档清楚地指出,当您计划测试3D线段时,可以使用SCNRender、SCNView或SCNNode本身在SceneKit中测试几何图形。我可以使用没有渲染器或视图的SCNScene节点,因此我计划使用SCNNode hitTesting。我创建了一个SCNScene,在其中放了一个SCNNode,并测试了一条通过的简单光线,但我总是得到一个空的命中列表,我不知道为什么:

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. SCNHitTestBoxOnlyKey:是或否不会改变任何事情
  6. SCNHitTestRootNodeKey:场景或boxNode的rootNode不变 任何内容
  7. SCNHitTestIgnoreHiddenNodesKey:是或否不会改变任何事情

我做错了什么?

SCNScene

我已经找到了答案,要么是BUG,要么是功能:使用SCNScene及其节点SCNNode进行3D"hitTestWithSegmentFromPoint(toPoint:options:)"测试,特别是方法:推荐答案不返回命中,除非场景包含在SCNView中。它似乎不能在屏幕外使用。我的猜测是为什么会这样,尽管我可以想象这与在显卡上执行一些相当昂贵的计算有关。

我已经使用Gameview SCNScene starter项目对其进行了测试。关键行是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: 

( hitList![0] )") // assign self.gameView!.scene = scene to reach this point.
    } else {
        println("No hit") // uncomment self.gameView!.scene = scene to reach this point.
    }

}

这篇关于使用SceneKit进行命中测试不通过SCNNode返回命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆