相机在正投影中的位置

问题描述:

我正在尝试了解如何将相机与 usesOrthographicProjection = true 一起使用.我需要对其进行设置,以便当我们第一次看到场景时,该对象应该是完整可见的.

I'm trying to understand how to use camera with usesOrthographicProjection = true. I need to set it up so when we first see the scene, the object should be viewable in full.

我使用了Xcode的SceneKit模板,对摄像机进行了一些调整(所有代码都在 viewDidLoad 中).使用默认摄影机(透视投影),看起来像这样:

I used the Xcode's SceneKit template, adjust a little bit about the camera (all the code is in viewDidLoad). Using default camera (perspective projection), it looks like this:

cameraNode.position = SCNVector3(x: 0, y: 0, z: ship.boundingBox.max.z + 20)

接下来,我尝试设置正投影,现在看起来像这样:

Next, I tried to set the orthographic projection, now it looks like this:

cameraNode.camera?.usesOrthographicProjection = true
cameraNode.position = SCNVector3(x: 0, y: 0, z: ship.boundingBox.max.z + 20)

无论我如何尝试更改相机的位置,它的外观仍与上图相同.我尝试使用 GLKMatrix4MakeOrtho 无济于事.什么都行不通.我该如何更改第一视图的印象?

No matter how I tried to change the camera position, it still looks the same as the image above. I've tried using GLKMatrix4MakeOrtho to no avail. Nothing works. What should I do the change the first view impression?

cameraNode.camera?.usesOrthographicProjection = true

//    let width = Float(UIScreen.main.bounds.size.width)
//    let width: Float = 9
//    let glMat = GLKMatrix4MakeOrtho(-width/2, 
//                                    width/2, 
//                                    -width/2, 
//                                    width/2, 
//                                    1, 
//                                    1000)

//    let glMat = GLKMatrix4MakeOrtho(ship.boundingBox.min.x,
//                                    ship.boundingBox.max.x,
//                                    ship.boundingBox.min.y,
//                                    ship.boundingBox.max.y,
//                                    ship.boundingBox.min.z,
//                                    ship.boundingBox.max.z)
//    cameraNode.camera?.projectionTransform = SCNMatrix4FromGLKMatrix4(glMat)

cameraNode.position = SCNVector3(x: 0, 
                                 y: 0, 
                                 z: ship.boundingBox.max.z + 20)

使用正交投影时,使用 orthographicScale 实例属性控制相机的放大倍数.

Use orthographicScale instance property to control camera’s magnification factor when using an orthographic projection.

var orthographicScale: Double { get set }

完整代码版本:

import SceneKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let sceneView = self.view as! SCNView
        sceneView.scene = SCNScene(named: "art.scnassets/ship.scn")!
        sceneView.allowsCameraControl = true
        sceneView.backgroundColor = UIColor.black
        
        sceneView.pointOfView?.camera!.usesOrthographicProjection = true
        sceneView.pointOfView?.camera!.zNear = 0.1
        sceneView.pointOfView?.camera!.zFar = 50.0
        sceneView.pointOfView?.camera!.orthographicScale = 5.0
    }
}

正投影是一种以二维方式表示三维对象的方法.因为我们是二维的,所以Z中没有距离.这就是为什么必须使用 orthographicScale 属性的原因.无论您如何使用平行投影光束移动相机,到物体的距离都将保持不变.

Orthographic projection is a means of representing three-dimensional objects in two dimensions. So there's no distance in Z since we are in two dimensions. That's why you have to use an orthographicScale property. No matter how you move the camera with parallel projection beams, the distance to objects will remain unchanged.

因此,请考虑以下因素:在正交投影中,大小相等的对象将显示大小相同,而不管它们与相机的距离如何.

唯一可控制距离"的参数.在二维空间中(二维中实际上没有 Z位置)是 orthographicScale .

The only parameter that controls a "distance" in two-dimensional space (there's no Z position in reality in 2D) is an orthographicScale.