Skip to content

Commit 2a7cf1f

Browse files
sunagMugen87
andauthored
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 dcd54ba. --------- Co-authored-by: Michael Herzog <[email protected]>
1 parent 19d07ee commit 2a7cf1f

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/nodes/accessors/Bitangent.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,71 @@
11
import { varying } from '../core/VaryingNode.js';
2+
import { Fn } from '../tsl/TSLCore.js';
23
import { cameraViewMatrix } from './Camera.js';
34
import { normalGeometry, normalLocal, normalView, normalWorld, transformedNormalView } from './Normal.js';
45
import { tangentGeometry, tangentLocal, tangentView, tangentWorld, transformedTangentView } from './Tangent.js';
56

6-
const getBitangent = ( crossNormalTangent ) => crossNormalTangent.mul( tangentGeometry.w ).xyz;
7+
/**
8+
* Returns the bitangent node and assigns it to a varying if the material is not flat shaded.
9+
*
10+
* @tsl
11+
* @private
12+
* @param {Node<vec3>} crossNormalTangent - The cross product of the normal and tangent vectors.
13+
* @param {string} varyingName - The name of the varying to assign the bitangent to.
14+
* @returns {Node<vec3>} The bitangent node.
15+
*/
16+
const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], builder ) => {
17+
18+
let bitangent = crossNormalTangent.mul( tangentGeometry.w ).xyz;
19+
20+
if ( builder.material.flatShading !== true ) {
21+
22+
bitangent = varying( crossNormalTangent, varyingName );
23+
24+
}
25+
26+
return bitangent;
27+
28+
} ).once();
729

830
/**
931
* TSL object that represents the bitangent attribute of the current rendered object.
1032
*
1133
* @tsl
1234
* @type {Node<vec3>}
1335
*/
14-
export const bitangentGeometry = /*@__PURE__*/ varying( getBitangent( normalGeometry.cross( tangentGeometry ) ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
36+
export const bitangentGeometry = /*@__PURE__*/ getBitangent( normalGeometry.cross( tangentGeometry ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
1537

1638
/**
1739
* TSL object that represents the vertex bitangent in local space of the current rendered object.
1840
*
1941
* @tsl
2042
* @type {Node<vec3>}
2143
*/
22-
export const bitangentLocal = /*@__PURE__*/ varying( getBitangent( normalLocal.cross( tangentLocal ) ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
44+
export const bitangentLocal = /*@__PURE__*/ getBitangent( normalLocal.cross( tangentLocal ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
2345

2446
/**
2547
* TSL object that represents the vertex bitangent in view space of the current rendered object.
2648
*
2749
* @tsl
28-
* @type {Node<vec4>}
50+
* @type {Node<vec3>}
2951
*/
30-
export const bitangentView = /*@__PURE__*/ varying( getBitangent( normalView.cross( tangentView ) ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
52+
export const bitangentView = getBitangent( normalView.cross( tangentView ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
3153

3254
/**
3355
* TSL object that represents the vertex bitangent in world space of the current rendered object.
3456
*
3557
* @tsl
36-
* @type {Node<vec4>}
58+
* @type {Node<vec3>}
3759
*/
38-
export const bitangentWorld = /*@__PURE__*/ varying( getBitangent( normalWorld.cross( tangentWorld ) ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
60+
export const bitangentWorld = /*@__PURE__*/ getBitangent( normalWorld.cross( tangentWorld ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
3961

4062
/**
4163
* TSL object that represents the transformed vertex bitangent in view space of the current rendered object.
4264
*
4365
* @tsl
44-
* @type {Node<vec4>}
66+
* @type {Node<vec3>}
4567
*/
46-
export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ) ).normalize().toVar( 'transformedBitangentView' );
68+
export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ), 'v_transformedBitangentView' ).normalize().toVar( 'transformedBitangentView' );
4769

4870
/**
4971
* TSL object that represents the transformed vertex bitangent in world space of the current rendered object.

src/nodes/accessors/Normal.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,19 @@ export const normalView = /*@__PURE__*/ ( Fn( ( builder ) => {
7171
* @tsl
7272
* @type {Node<vec3>}
7373
*/
74-
export const normalWorld = /*@__PURE__*/ varying( normalView.transformDirection( cameraViewMatrix ), 'v_normalWorld' ).normalize().toVar( 'normalWorld' );
74+
export const normalWorld = /*@__PURE__*/ ( Fn( ( builder ) => {
75+
76+
let normal = normalView.transformDirection( cameraViewMatrix );
77+
78+
if ( builder.material.flatShading !== true ) {
79+
80+
normal = varying( normal, 'v_normalWorld' );
81+
82+
}
83+
84+
return normal;
85+
86+
}, 'vec3' ).once() )().normalize().toVar( 'normalWorld' );
7587

7688
/**
7789
* TSL object that represents the transformed vertex normal in view space of the current rendered object.

src/nodes/accessors/TextureNode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class TextureNode extends UniformNode {
328328

329329
if ( ( uvNode === null || builder.context.forceUVContext === true ) && builder.context.getUV ) {
330330

331-
uvNode = builder.context.getUV( this );
331+
uvNode = builder.context.getUV( this, builder );
332332

333333
}
334334

src/renderers/webgpu/nodes/WGSLNodeBuilder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class WGSLNodeBuilder extends NodeBuilder {
516516
const textureDimension = this.generateTextureDimension( texture, textureProperty, levelSnippet );
517517

518518
const vecType = texture.isData3DTexture ? 'vec3' : 'vec2';
519-
const coordSnippet = `${ vecType }<u32>(${ wrapFunction }(${ uvSnippet }) * ${ vecType }<f32>(${ textureDimension }))`;
519+
const coordSnippet = `${ vecType }<u32>( ${ wrapFunction }( ${ uvSnippet } ) * ${ vecType }<f32>( ${ textureDimension } ) )`;
520520

521521
return this.generateTextureLoad( texture, textureProperty, coordSnippet, depthSnippet, levelSnippet );
522522

0 commit comments

Comments
 (0)