Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shaders: Add support for transforming instanced mesh tangents #27128

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/renderers/shaders/ShaderChunk/defaultnormal_vertex.glsl.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
export default /* glsl */`

vec3 transformedNormal = objectNormal;
#ifdef USE_TANGENT

vec3 transformedTangent = objectTangent;

#endif

#ifdef USE_BATCHING

// this is in lieu of a per-instance normal-matrix
// shear transforms in the instance matrix are not supported

mat4 batchingMatrix = getBatchingMatrix( batchId );
mat3 bm = mat3( batchingMatrix );
objectNormal /= vec3( dot( bm[ 0 ], bm[ 0 ] ), dot( bm[ 1 ], bm[ 1 ] ), dot( bm[ 2 ], bm[ 2 ] ) );
objectNormal = bm * objectNormal;
transformedNormal /= vec3( dot( bm[ 0 ], bm[ 0 ] ), dot( bm[ 1 ], bm[ 1 ] ), dot( bm[ 2 ], bm[ 2 ] ) );
transformedNormal = bm * transformedNormal;

#ifdef USE_TANGENT

objectTangent /= vec3( dot( bm[ 0 ], bm[ 0 ] ), dot( bm[ 1 ], bm[ 1 ] ), dot( bm[ 2 ], bm[ 2 ] ) );
objectTangent = bm * objectTangent;
transformedTangent /= vec3( dot( bm[ 0 ], bm[ 0 ] ), dot( bm[ 1 ], bm[ 1 ] ), dot( bm[ 2 ], bm[ 2 ] ) );
transformedTangent = bm * transformedTangent;

#endif

#endif

vec3 transformedNormal = objectNormal;

#ifdef USE_INSTANCING

// this is in lieu of a per-instance normal-matrix
// shear transforms in the instance matrix are not supported

mat3 m = mat3( instanceMatrix );
mat3 im = mat3( instanceMatrix );
transformedNormal /= vec3( dot( im[ 0 ], im[ 0 ] ), dot( im[ 1 ], im[ 1 ] ), dot( im[ 2 ], im[ 2 ] ) );
transformedNormal = im * transformedNormal;

transformedNormal /= vec3( dot( m[ 0 ], m[ 0 ] ), dot( m[ 1 ], m[ 1 ] ), dot( m[ 2 ], m[ 2 ] ) );
#ifdef USE_TANGENT

transformedNormal = m * transformedNormal;
transformedTangent /= vec3( dot( im[ 0 ], im[ 0 ] ), dot( im[ 1 ], im[ 1 ] ), dot( im[ 2 ], im[ 2 ] ) );
transformedTangent = im * transformedTangent;

#endif

#endif

Expand All @@ -44,7 +54,7 @@ transformedNormal = normalMatrix * transformedNormal;

#ifdef USE_TANGENT

vec3 transformedTangent = ( modelViewMatrix * vec4( objectTangent, 0.0 ) ).xyz;
transformedTangent = ( modelViewMatrix * vec4( transformedTangent, 0.0 ) ).xyz;

#ifdef FLIP_SIDED

Expand Down