2

What orthographicProjection does one have to use to be able to make a 2D application in SceneKit with 1:1 SceneKit points to screen points/pixels ratio?

Example: I want to position something at (200, 200) on the screen and I want to use a SCNVector with (200, 200, 0) for it. What orthographicProjection do I need for this?

Max
  • 2,699
  • 2
  • 27
  • 50
  • Hello, what is the relationship between viewport and orthographicScale? I'm confuse about that – ooOlly Sep 20 '18 at 13:48

1 Answers1

7

If you want an orthographic projection where a unit of scene space corresponds to a point of screen space, you need a projection where the left clipping plane is at zero and the right clipping plane is at whatever the screen's width in points is. (Ditto for top/bottom, and near/far doesn't matter so long as you keep objects within whatever near/far you set up.)

For this it's probably easiest to set up your own projection matrix, rather than working out what orthographicScale and camera position correspond to the dimensions you need:

GLKMatrix4 mat = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width,
                                     0, self.view.bounds.size.height,
                                     1, 100); // z range arbitrary
cameraNode.camera.projectionTransform = SCNMatrix4FromGLKMatrix4(mat);
// still need to position the camera for its direction & z range
cameraNode.position = SCNVector3Make(0, 0, 50); 
rickster
  • 124,678
  • 26
  • 272
  • 326
  • This is nice, thank you! I found out in the meantime that one can use the height of the screen / 2.0 as the orthographicScale. I'm wondering if SceneKit is able to do certain optimization if you set the orthographicProjection flag to true. That's why I kept trying. What do you think will lead to better performance? – Max Apr 06 '15 at 22:46
  • 1
    The `orthographicProjection` + `orthographicScale` property combination is probably just a shortcut for setting up a projection matrix based on the camera direction & viewport, so there should be no performance difference. – rickster Apr 06 '15 at 22:55
  • Okay cool, I just don't have enough insight into SceneKit yet, thanks! Where did you learn about it? Just from the docs? – Max Apr 06 '15 at 23:02
  • @Max also need to learn about `orthographicScale` for directional lights so any insight about how you learned to use this would be great. there is scant documentation unfortunately on scenekit. – Crashalot Aug 01 '16 at 16:25
  • @rickster Hello, what is the relationship between viewport and orthographicScale? I'm confuse about that – ooOlly Sep 20 '18 at 13:48
  • @rickster Could you help me out If you have time. My question: https://stackoverflow.com/questions/52428397/confused-about-orthographic-projection-of-camera-in-scenekit – ooOlly Sep 20 '18 at 15:28