对SceneKit中相机的正交投影感到困惑 [英] Confused about Orthographic Projection of camera in SceneKit

查看:232
本文介绍了对SceneKit中相机的正交投影感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正交投影在我的应用程序中显示3D场景.在我的代码中,我在场景中放了一个盒子,并像打击一样设置了视点的正投影. (0,0,500)处的相机注视-z方向,并在世界原点处选择框.因此相机应该能够捕捉盒子.

I want to use orthographic projection to display 3D scene in my app. In my code I put a box in scene and set orthographic projection of Point of view like blow. Camera at (0,0,500) look at -z direction, and box at origin of world. So camera should be able to capture the box.

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可以正常工作.

But I can see nothing. I find out If set orthographicScale to width/2.0 will works correctly.

pov.orthographicScale = Double(width/2);

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

Question 1 : I don't know why this works. I read docs of apple but still confused. https://developer.apple.com/documentation/scenekit/scncamera/1436612-orthographicscale?language=objc

orthographicScale

指定使用正交投影时相机的放大倍数.

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

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

Why I need magnify orthographic projection? I already set the dimension of it through GLKMatrix4MakeOrtho.

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

I'm not sure whether it is relate to viewport transform? Because viewport transform in OpenGL computed as follows: https://docs.microsoft.com/en-us/windows/desktop/opengl/glviewport

问题2:我还发现,如果我像打击一样创建投影矩阵,则其工作原理与以前相同.

Question 2 : I also find If I create projection matrix like blow it works same as before.

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

在OpenGL中,这意味着不同的查看框,并且将显示不同的场景分区.

In OpenGL it means different viewing box and will display different partition of scene.

感谢您的帮助!

推荐答案

我想,您无法使用GLKMatrix4MakeOrtho在场景中看到您的3D对象,原因是iOS和macOS的GLKit类别太多了现在已弃用.

I suppose, you can't see your 3D objects in the scene using GLKMatrix4MakeOrtho, due to the fact that so many classes of GLKit for iOS and macOS are deprecated now.

要查看3D对象,您需要在Attributes Inspector中手动设置 Far Z Clipping 参数.这两个字段是正交摄影机的平截头体的 near far 剪切平面.默认值Far=100不允许您在100个单位以外的地方看到场景.

In order to see your 3D object, you need to manually set Far Z Clipping parameter in Attributes Inspector. These two fields are near and far clipping planes of the frustum of your Orthographic Camera. A default value of Far=100 doesn't allow you see the scene further than 100 units away.

然后使用Scale属性为正交视图中的对象设置比例因子.

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

要通过GLKMatrix4MakeOrtho以编程方式使用它,首先需要将GLKitOpenGL模块(以及所有必要的委托协议)导入项目中.

In order to use it programmatically via GLKMatrix4MakeOrtho, you need to import GLKit and OpenGL modules (as well as all the necessary delegate protocols) into your project at first.

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;
}

此外,在iOS上,GLKit需要OpenGL ES 2.0上下文.在macOS中,GLKit需要支持OpenGL 3.2 Core Profile的OpenGL上下文.

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 框架. 查看已弃用的GLKit类.

But remember! Many GL Features are deprecated, so you'd better use Metal framework. Look at deprecated classes of GLKit.

这篇关于对SceneKit中相机的正交投影感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆