Scene Kit:计算出的projectPoint被置换

Scene Kit: projectPoint calculated is displaced

我正在尝试在场景的父视图上的球体上绘制框架,但由于某种原因,通过 SCNScene.projectPoint 方法转换的点被置换了。

为简单起见,我在场景中心创建了一个半径为 2 的球体。球体附加到根节点,因此世界坐标的左上角位于 (-2,-2,0) 点。

这里是完整的代码:

func sphereWithFrame(){

    var v1 = SCNVector3(x: 0,y: 0,z: 0)
    var v2 = SCNVector3(x: 0,y: 0,z: 0)

    let topSphere = SCNSphere(radius: 2.0)
    topSphere.firstMaterial!.diffuse.contents = UIColor.greenColor()


    let topSphereNode = SCNNode(geometry: topSphere)
    topSphereNode.position = SCNVector3Make(0, 0, 0)
    scene.rootNode.addChildNode(topSphereNode)

    topSphereNode.getBoundingBoxMin(&v1, max: &v2)

    //world coordinates
    let v1w =  topSphereNode.convertPosition(v1, toNode: scene.rootNode)
    let v2w =  topSphereNode.convertPosition(v2, toNode: scene.rootNode)

    //projected coordinates
    let v1p = scnview.projectPoint(v1w)
    let v2p = scnview.projectPoint(v2w)

    //frame rectangle
    var rect = CGRectMake(CGFloat(v1p.x), CGFloat(v2p.y), CGFloat(v2p.x - v1p.x), CGFloat(v1p.y - v2p.y))
    let rectView = UIView(frame: rect)
    rectView.alpha = 0.3
    rectView.backgroundColor = UIColor.blueColor()
    scnview.addSubview(rectView)

    println("v1 \(v1.toString()), v2\(v2.toString())")
    println("v1w \(v1w.toString()), v2w\(v2w.toString())")
    println("v1p \(v1p.toString()), v2w\(v2p.toString())")
    println("rect\(rect)")

}

控制台:

v1 x:-2.0,y:-2.0,z:-2.0, v2x:2.0,y:2.0,z:2.0
v1w x:-2.0,y:-2.0,z:-2.0, v2wx:2.0,y:2.0,z:2.0
v1p x:90.718,y:309.282,z:0.925926, v2wx:263.923,y:136.077,z:0.883838
rect(90.718, 136.077, 173.205, 173.205)

屏幕上的结果是一个矩形,从应该在的位置右上一点。

我尽量按照post” How do I find my mouse point in a scene using SceneKit?”。 我错了什么?

必须在两个世界坐标上将 z 设置为零:

v1w.z = 0
v2w.z = 0

解释事情有帮助