Shading models using incorrect view direction for orthographic projections.
See original GitHub issue#17379 exposed a problem with the current orthographic behavior of light. However it was quickly dismissed as a help question, but I don’t think this is the case.
It was thought to be a problem with physical and standard models, but I believe the real issue at hand is the incorrect calculation of the view direction for orthographic cameras.
Currently the view direction is being calculated as follows vViewPosition = - mvPosition.xyz;
However this is not a correct model for an orthographic view. This is supposed to be fixed as the camera direction, disregarding the object position.
This is causing multiple unexpected behaviours. Given a fixed orthographic camera and a scene being lit by a directional light, the specular light component shouldn’t move if the object moves. This is not what’s happening on Dev - dev example
From the image, you can see the fresnel component is being incorrectly placed on the object. As previously noted, the specular component is also incorrectly moving when it shouldn’t be.
In order to fix this behavior, I just replaced the vViewPosition
calculation for every shading model with the following:
vViewPosition = isPerspectiveMatrix( projectionMatrix ) ? - mvPosition.xyz : vec3( 0, 0, 1 );
This is the example of what should be actually happening - fix example
Would appreciate if other contributors, that are more familiar with orthographic projection, could confirm this is indeed the correct way of handling the issue at hand.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (1 by maintainers)
Top GitHub Comments
I’ve studied a bit more about how orthographic projections are supposed to work.
The only real difference is that you can assume that lighting rays read by the camera are the ones with exact opposite direction from the camera view direction. You can think of it as if there was a filter, on top of the screen space, blocking any incident rays coming from lateral directions.
With that being said, the lighting calculations should stay exactly the same. Only the view direction calculations need to be adjusted accordingly.
I’ve implemented this behavior on environment reflections / refractions and the result looks correct now.
dev example fix example
( Note that you can rotate the camera and both the environment reflections and the specular component still behaves as you would expect )
I’ve tried looking for how other engines are handling this behavior:
Babylon.js is incorrectly calculating the view directions exactly as we are on dev. example Blender and Cinema4D, however, are behaving exactly as the proposed fix.
I have a partial PR ready, in case you guys want to proceed with this change. There’s just one minor varying collision with
vIsPerspective
that needs to be solved, but I’ll leave this discussion for this possible PR.While you are thinking about this, see if you can also study reflections from an environment map.
It is not clear to me how we should be handling lighting with an orthographic camera.
It might be of interest to compare with other engines.