Browse Source

Shaders: Add support for transforming instanced mesh tangents (#27128)

* Add support for transforming instanced mesh tangents

* Remove normal scaling from batching, instancing tangent calculation
Garrett Johnson 1 year ago
parent
commit
fa8539d145
1 changed files with 18 additions and 10 deletions
  1. 18 10
      src/renderers/shaders/ShaderChunk/defaultnormal_vertex.glsl.js

+ 18 - 10
src/renderers/shaders/ShaderChunk/defaultnormal_vertex.glsl.js

@@ -1,5 +1,12 @@
 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
@@ -7,30 +14,31 @@ export default /* glsl */`
 
 	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 = 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 = im * transformedTangent;
+
+	#endif
 
 #endif
 
@@ -44,7 +52,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