OGeek|极客世界-中国程序员成长平台

标题: ios - 对 SceneKit 中相机的正交投影感到困惑 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:37
标题: ios - 对 SceneKit 中相机的正交投影感到困惑

我想使用正交投影在我的应用中显示 3D 场景。在我的代码中,我在场景中放置了一个框,并设置了视点的正交投影,如打击。 (0,0,500) 处的相机看 -z 方向,并在世界的原点框。所以相机应该能够捕捉到盒子。

let cameraNode = SCNNode()
let pov = SCNCamera()
pov.usesOrthographicProjection = true
let width = UISreen.main.bounds.size.width
let glMat = GLKMatrix4MakeOrtho(-width/2, width/2, -width/2, width/2, 1, 1000)
pov.projectionTransform = SCNMatrix4FromGLKMatrix4(glMat)
cameraNode.camera = pov
cameraNode.position = SCNVector3.init(0, 0, 500)
scene.rootNode.addChildNode(cameraNode)

let boxGeo = SCNBox.init(width: 100, height: 100, length: 1, chamferRadius: 0)
let box = SCNNode.init(geometry: boxGeo)
scene.rootNode.addChildNode(box)

但我什么也看不见。我发现如果将 orthographicScale 设置为 width/2.0 将正常工作。

pov.orthographicScale = Double(width/2);

问题 1:我不知道为什么会这样。我阅读了苹果的文档,但仍然感到困惑。 https://developer.apple.com/documentation/scenekit/scncamera/1436612-orthographicscale?language=objc

orthographicScale

Specifies the camera’s magnification factor when using an orthographic projection.

为什么需要放大正交投影?我已经通过 GLKMatrix4MakeOrtho 设置了它的尺寸。

我不确定它是否与视口(viewport)变换有关?因为 OpenGL 中的视口(viewport)变换计算如下:https://learn.microsoft.com/en-us/windows/desktop/opengl/glviewport

enter image description here

问题 2: 我还发现如果我创建像 Blow 这样的投影矩阵,它的工作原理和以前一样。

let glMat = GLKMatrix4MakeOrtho(0, width, 0, width, 1, 1000)

在OpenGL中表示不同的观察框,会显示不同的场景分区。

感谢任何帮助!



Best Answer-推荐答案


我想,您无法使用 GLKMatrix4MakeOrtho 在场景中看到您的 3D 对象,因为现在很多用于 iOS 和 macOS 的 GLKit 类已被弃用。

为了查看您的 3D 对象,您需要在 Attributes Inspector 中手动设置 Far Z Clipping 参数。这两个字段分别是正交相机截头体的 nearfar 剪切平面。默认值 Far=100 不允许您看到 100 单位以外的场景。

enter image description here

Then use a Scale property to set a scale factor for your objects in an ortho view.

为了通过 GLKMatrix4MakeOrtho 以编程方式使用它,您需要导入 GLKitOpenGL 模块(以及所有必要的委托(delegate)协议(protocol)) 首先进入您的项目。

import GLKit
import OpenGL

// Obj-C GLKMatrix4

GLK_INLINE GLKMatrix4 GLKMatrix4MakeOrtho(float left, 
                                          float right,
                                          float bottom, 
                                          float top,
                                          float nearZ, 
                                          float farZ) {
    float ral = right + left;
    float rsl = right - left;
    float tab = top + bottom;
    float tsb = top - bottom;
    float fan = farZ + nearZ;
    float fsn = farZ - nearZ;

    GLKMatrix4 m = { 2.0f / rsl, 0.0f, 0.0f, 0.0f,
                     0.0f, 2.0f / tsb, 0.0f, 0.0f,
                     0.0f, 0.0f, -2.0f / fsn, 0.0f,
                     -ral / rsl, -tab / tsb, -fan / fsn, 1.0f };

    return m;
}

Also, on iOS, GLKit requires an OpenGL ES 2.0 context. In macOS, GLKit requires an OpenGL context that supports the OpenGL 3.2 Core Profile.

但请记住!许多 GL 功能已弃用,因此您最好使用 Metal 框架。 Look at deprecated classes of GLKit .

关于ios - 对 SceneKit 中相机的正交投影感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52428397/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4