Преглед изворни кода

TSL: Fix `bitangent*` if used `material.flatShading` (#30837)

* fix bitangent if used flat shading

* cleanup

* cleanup

* Update Bitangent.js

* Update Bitangent.js

* Update Bitangent.js

* Update Bitangent.js

* fix extrusion side of normal map if tangent used

* Revert "fix extrusion side of normal map if tangent used"

This reverts commit dcd54ba8044485f3681a3f84d16161ff32d1976a.

---------

Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
sunag пре 10 месеци
родитељ
комит
2a7cf1f506

+ 31 - 9
src/nodes/accessors/Bitangent.js

@@ -1,9 +1,31 @@
 import { varying } from '../core/VaryingNode.js';
+import { Fn } from '../tsl/TSLCore.js';
 import { cameraViewMatrix } from './Camera.js';
 import { normalGeometry, normalLocal, normalView, normalWorld, transformedNormalView } from './Normal.js';
 import { tangentGeometry, tangentLocal, tangentView, tangentWorld, transformedTangentView } from './Tangent.js';
 
-const getBitangent = ( crossNormalTangent ) => crossNormalTangent.mul( tangentGeometry.w ).xyz;
+/**
+ * Returns the bitangent node and assigns it to a varying if the material is not flat shaded.
+ *
+ * @tsl
+ * @private
+ * @param {Node<vec3>} crossNormalTangent - The cross product of the normal and tangent vectors.
+ * @param {string} varyingName - The name of the varying to assign the bitangent to.
+ * @returns {Node<vec3>} The bitangent node.
+ */
+const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], builder ) => {
+
+	let bitangent = crossNormalTangent.mul( tangentGeometry.w ).xyz;
+
+	if ( builder.material.flatShading !== true ) {
+
+		bitangent = varying( crossNormalTangent, varyingName );
+
+	}
+
+	return bitangent;
+
+} ).once();
 
 /**
  * TSL object that represents the bitangent attribute of the current rendered object.
@@ -11,7 +33,7 @@ const getBitangent = ( crossNormalTangent ) => crossNormalTangent.mul( tangentGe
  * @tsl
  * @type {Node<vec3>}
  */
-export const bitangentGeometry = /*@__PURE__*/ varying( getBitangent( normalGeometry.cross( tangentGeometry ) ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
+export const bitangentGeometry = /*@__PURE__*/ getBitangent( normalGeometry.cross( tangentGeometry ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
 
 /**
  * TSL object that represents the vertex bitangent in local space of the current rendered object.
@@ -19,31 +41,31 @@ export const bitangentGeometry = /*@__PURE__*/ varying( getBitangent( normalGeom
  * @tsl
  * @type {Node<vec3>}
  */
-export const bitangentLocal = /*@__PURE__*/ varying( getBitangent( normalLocal.cross( tangentLocal ) ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
+export const bitangentLocal = /*@__PURE__*/ getBitangent( normalLocal.cross( tangentLocal ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
 
 /**
  * TSL object that represents the vertex bitangent in view space of the current rendered object.
  *
  * @tsl
- * @type {Node<vec4>}
+ * @type {Node<vec3>}
  */
-export const bitangentView = /*@__PURE__*/ varying( getBitangent( normalView.cross( tangentView ) ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
+export const bitangentView = getBitangent( normalView.cross( tangentView ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
 
 /**
  * TSL object that represents the vertex bitangent in world space of the current rendered object.
  *
  * @tsl
- * @type {Node<vec4>}
+ * @type {Node<vec3>}
  */
-export const bitangentWorld = /*@__PURE__*/ varying( getBitangent( normalWorld.cross( tangentWorld ) ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
+export const bitangentWorld = /*@__PURE__*/ getBitangent( normalWorld.cross( tangentWorld ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
 
 /**
  * TSL object that represents the transformed vertex bitangent in view space of the current rendered object.
  *
  * @tsl
- * @type {Node<vec4>}
+ * @type {Node<vec3>}
  */
-export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ) ).normalize().toVar( 'transformedBitangentView' );
+export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ), 'v_transformedBitangentView' ).normalize().toVar( 'transformedBitangentView' );
 
 /**
  * TSL object that represents the transformed vertex bitangent in world space of the current rendered object.

+ 13 - 1
src/nodes/accessors/Normal.js

@@ -71,7 +71,19 @@ export const normalView = /*@__PURE__*/ ( Fn( ( builder ) => {
  * @tsl
  * @type {Node<vec3>}
  */
-export const normalWorld = /*@__PURE__*/ varying( normalView.transformDirection( cameraViewMatrix ), 'v_normalWorld' ).normalize().toVar( 'normalWorld' );
+export const normalWorld = /*@__PURE__*/ ( Fn( ( builder ) => {
+
+	let normal = normalView.transformDirection( cameraViewMatrix );
+
+	if ( builder.material.flatShading !== true ) {
+
+		normal = varying( normal, 'v_normalWorld' );
+
+	}
+
+	return normal;
+
+}, 'vec3' ).once() )().normalize().toVar( 'normalWorld' );
 
 /**
  * TSL object that represents the transformed vertex normal in view space of the current rendered object.

+ 1 - 1
src/nodes/accessors/TextureNode.js

@@ -328,7 +328,7 @@ class TextureNode extends UniformNode {
 
 		if ( ( uvNode === null || builder.context.forceUVContext === true ) && builder.context.getUV ) {
 
-			uvNode = builder.context.getUV( this );
+			uvNode = builder.context.getUV( this, builder );
 
 		}
 

+ 1 - 1
src/renderers/webgpu/nodes/WGSLNodeBuilder.js

@@ -516,7 +516,7 @@ class WGSLNodeBuilder extends NodeBuilder {
 		const textureDimension = this.generateTextureDimension( texture, textureProperty, levelSnippet );
 
 		const vecType = texture.isData3DTexture ? 'vec3' : 'vec2';
-		const coordSnippet = `${ vecType }<u32>(${ wrapFunction }(${ uvSnippet }) * ${ vecType }<f32>(${ textureDimension }))`;
+		const coordSnippet = `${ vecType }<u32>( ${ wrapFunction }( ${ uvSnippet } ) * ${ vecType }<f32>( ${ textureDimension } ) )`;
 
 		return this.generateTextureLoad( texture, textureProperty, coordSnippet, depthSnippet, levelSnippet );
 

粤ICP备19079148号