ios - 对 SceneKit 中相机的正交投影感到困惑

标签 ios objective-c swift opengl-es scenekit

我想在我的应用中使用正交投影来显示 3D 场景。在我的代码中,我在场景中放了一个盒子,并像打击一样设置了 Point of View 的正交投影。 (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中表示不同的viewing box,会显示不同的场景分区。

感谢任何帮助!

最佳答案

我想,您无法使用 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/

相关文章:

objective-c - 获得前导零的时间

objective-c - 在 Objective-C 中将数字舍入到指定精度

java - 如何监控 iOS/Android 设备中的 https/ssl 流量

ios - UILabel长按识别

ios - iOS 上的 SSL 固定

ios - UIButton 垂直底部对齐不起作用

objective-c - Objective C 前向类属性

ios - 如何只为 UIView 的左下角、右下角和左上角设置 cornerRadius?

Xcode 单元测试,等待 `class setUp()`

swift - 如何将 SwiftUI 框架中 DatePicker 的外观更改为仅月份和年份?