Orthographic and perspective projection in the one TCastleViewport

Good day!
In the game I realize the sprite world. And, as I think, it seems better in the orthographic projection.
But I also want to make the sky like big sphere with the stars texture.
So, when the camera will be rotated, sprites move left or right without parallax and sphere with sky texture will rotate. Like a player looking around.

So, can I use the different projection types for the different objects in the one TCastleViewport?

I may be wrong but to my knowledge:

  1. Projection is property of camera, not of object. I.e. this is the way 3D coordinates of objects are converted into 2D coordinates of screen. If we’ll be converting coordinates of one object one way, and other object in a different way it may result in weird look, IMHO.
  2. I wonder why you don’t want to have the skybox/skysphere in orthographic projection too? I don’t see any reason why it wouldn’t work. E.g. testing it in Blender on icosphere now and it looks quite meaningful (see attached video - background is an icosphere, whole scene is in orthographic projection). The only drawback I can see in this - is that only a small fraction of the icosphere is visible on the screen, actually “wasting” texture space, or in other words, needing a high-res texture to make it look bearable.
1 Like

Yes, that could be a optimal solution in my case, thanks!

Indeed, you cannot render in one TCastleViewport (which always has exactly one "current’ camera, determined by TCastleViewport.Camera) both perspective and orthographic rendering. As Eugene says, whether the world is “perspective” or “orthographic” is a property of the camera. Mixing different projections for different objects would (or at least could) result in weird rendering.

That said, you can:

  1. Have 2 TCastleViewport instances, one on top of each other. The background TCastleViewport could display your skybox / sky sphere. The foreground TCastleViewport could display everything else. Set TCastleViewport.Transparent for the foreground viewport to see the background viewport behind it. Both viewports could have a completely different cameras, with different projections (from code you will want to synchronize camera rotation from one viewport to anther; do it e.g. in overridden BeforeRender in view).

  2. Or, stick to 1 viewport, and just render your skybox / sky sphere also in orthographic. As Eugene says, it should work.

  3. Note that CGE also supports background (from X3D TBackgroundNode, from CGE TCastleBackground component) in orthographic mode. You can “just” use it.

    Though we make a simple cheat (see castle-engine/src/scene/castleinternalbackgroundrenderer.pas at 404f1933db10b9641640b48253825ecf6a81e718 · castle-engine/castle-engine · GitHub ): when projection is orthographic, we set it temporarily to perspective. The reason for this cheat is small, it mattered for CGE (where it has to work with various possible orthographic setting), it may not matter to you in a particular application.

    All in all, you probably don’t need to “replicate” this cheat if you will go with option AD 2.

1 Like

In general, I just think that the standard game compass make navigation in the game world too artificial.
Therefore, I decided to try to create an informative skybox with landmarks from celestial bodies, by which the player will determine the direction.
And the otographic projection is completely enough for the implementation of this plan.

So, I use 2D Viewport. It is work, but I have one problem: X and Y axis are locked. How can I unlock them?

The “2D viewport” is really just a regular TCastleViewport with some default config more suited for 2D, see Designing a 2D world | Manual | Castle Game Engine . To do what you want:

  • You can start from “3D Viewport” and just change camera type to orthographic (adjusting Camera1.ProjectionType, Camera1.Orthographic.Height / Camera1.Orthographic.Width). This will change the camera used once you run the game (run-time).

    Then in editor use “Viewport → Align View To Camera” to also have orthographic camera at design-time.

  • Or you can start from “2D Viewport” which has by default orthographic camera, and change the navigation (which is by default “2D” locking you to XY). At design-time, switch your navigation to “Viewport → Fly” (there’s a shortctrl Ctrl+2 to toggle between 2D and Fly navigation).

    If you want to change navigation at runtime, remove TCastle2DNavigation and add TCastleWalkNavigation.

1 Like