如何将2个SCNNode水平对齐?

时间:2017-07-16 20:07:28

标签: swift scenekit scnnode

我正在尝试将两个SCNNode对齐,但似乎无法弄清楚如何。这就是我现在所拥有的:

cube.scale = SCNVector3(x: 0.1, y: 0.1, z: 0.1)
cube.position = SCNVector3(0, 3, -3)

cubeTwo.scale = SCNVector3(x: 0.15, y: 0.15, z: 0.15)
cubeTwo.position = SCNVector3(0.5, 3, -3)

cubeThree.scale = SCNVector3(x: 0.2, y: 0.2, z: 0.2)
cubeThree.position = SCNVector3(1, 3, -3)

我怎样才能做到这一点?谢谢!!

2 个答案:

答案 0 :(得分:0)

要水平对齐它们,请将position Y值设置为相同。 X是左右,Y是上下,Z是近远(直到你开始移动相机!)。

答案 1 :(得分:0)

parentNode.addChildNode(cube)
parentNode.addChildNode(cubeTwo)
parentNode.addChildNode(cubeThree)


func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

    parentNode.position = self.getPositionRelativeToCameraView(distance: 1.0).position


}


func getPositionRelativeToCameraView(distance: Float) -> (position: SCNVector3, rotation: SCNVector4) {
    var x = Float()
    var y = Float()
    var z = Float()

    let cameraLocation = self.sceneView.pointOfView!.position //else { return (SCNVector3Zero) }
    let rotation = self.sceneView.pointOfView!.rotation //else { return (SCNVector3Zero) }
    let direction = calculateCameraDirection(cameraNode: rotation)

    x = cameraLocation.x + distance * direction.x
    y = cameraLocation.y + distance * direction.y
    z = cameraLocation.z + distance * direction.z

    let position = SCNVector3Make(x, y, z)
    return (position, rotation)
}

func calculateCameraDirection(cameraNode: SCNVector4) -> SCNVector3 {
    let x = -cameraNode.x
    let y = -cameraNode.y
    let z = -cameraNode.z
    let w = cameraNode.w
    let cameraRotationMatrix = GLKMatrix3Make(cos(w) + pow(x, 2) * (1 - cos(w)),
                                              x * y * (1 - cos(w)) - z * sin(w),
                                              x * z * (1 - cos(w)) + y*sin(w),

                                              y*x*(1-cos(w)) + z*sin(w),
                                              cos(w) + pow(y, 2) * (1 - cos(w)),
                                              y*z*(1-cos(w)) - x*sin(w),

                                              z*x*(1 - cos(w)) - y*sin(w),
                                              z*y*(1 - cos(w)) + x*sin(w),
                                              cos(w) + pow(z, 2) * ( 1 - cos(w)))

    let cameraDirection = GLKMatrix3MultiplyVector3(cameraRotationMatrix, GLKVector3Make(0.0, 0.0, -1.0))
    return SCNVector3FromGLKVector3(cameraDirection)
}