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)
But I can see nothing. I find out If set orthographicScale
to width/2.0
will works correctly.
pov.orthographicScale = Double(width/2);
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.
Why I need magnify orthographic projection? I already set the dimension of it through GLKMatrix4MakeOrtho
.
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
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)
In OpenGL it means different viewing box and will display different partition of scene.
Any help is appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…