Skip to content

Commit

Permalink
Fix view dir in ortho projection (#1962)
Browse files Browse the repository at this point in the history
* fix: view dir in ortho projection
  • Loading branch information
zhuxudong authored Jan 12, 2024
1 parent 2e8b172 commit e1a511c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ export class Camera extends Component {
set isOrthographic(value: boolean) {
this._virtualCamera.isOrthographic = value;
this._projectionMatrixChange();

if (value) {
this.shaderData.enableMacro("CAMERA_ORTHOGRAPHIC");
} else {
this.shaderData.disableMacro("CAMERA_ORTHOGRAPHIC");
}
}

/**
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/shaderlib/begin_viewdir_frag.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifdef MATERIAL_NEED_WORLD_POS
vec3 V = normalize( camera_Position - v_pos );
#endif
#ifdef CAMERA_ORTHOGRAPHIC
vec3 V = -camera_Forward;
#else
#ifdef MATERIAL_NEED_WORLD_POS
vec3 V = normalize( camera_Position - v_pos );
#endif
#endif
3 changes: 2 additions & 1 deletion packages/core/src/shaderlib/camera_declare.glsl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
uniform vec3 camera_Position;
uniform vec3 camera_Position;
uniform vec3 camera_Forward;
7 changes: 5 additions & 2 deletions packages/core/src/shaderlib/pbr/pbr_helper.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ float getAARoughnessFactor(vec3 normal) {

void initGeometry(out Geometry geometry, bool isFrontFacing){
geometry.position = v_pos;
geometry.viewDir = normalize(camera_Position - v_pos);

#ifdef CAMERA_ORTHOGRAPHIC
geometry.viewDir = -camera_Forward;
#else
geometry.viewDir = normalize(camera_Position - v_pos);
#endif
#if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY)
mat3 tbn = getTBN(isFrontFacing);
#endif
Expand Down

0 comments on commit e1a511c

Please sign in to comment.