diff --git a/src/nodes/accessors/Bitangent.js b/src/nodes/accessors/Bitangent.js index fcece65095752d..c352db417640e2 100644 --- a/src/nodes/accessors/Bitangent.js +++ b/src/nodes/accessors/Bitangent.js @@ -13,11 +13,11 @@ import { directionToFaceDirection } from '../display/FrontFacingNode.js'; * @param {string} varyingName - The name of the varying to assign the bitangent to. * @returns {Node} The bitangent node. */ -const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], { subBuildFn, material } ) => { +const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], builder ) => { let bitangent = crossNormalTangent.mul( tangentGeometry.w ).xyz; - if ( subBuildFn === 'NORMAL' && material.flatShading !== true ) { + if ( builder.subBuildFn === 'NORMAL' && builder.isFlatShading() !== true ) { bitangent = bitangent.toVarying( varyingName ); @@ -49,11 +49,11 @@ export const bitangentLocal = /*@__PURE__*/ getBitangent( normalLocal.cross( tan * @tsl * @type {Node} */ -export const bitangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, material } ) => { +export const bitangentView = /*@__PURE__*/ ( Fn( ( builder ) => { let node; - if ( subBuildFn === 'VERTEX' || geometry.hasAttribute( 'tangent' ) ) { + if ( builder.subBuildFn === 'VERTEX' || builder.geometry.hasAttribute( 'tangent' ) ) { node = getBitangent( normalView.cross( tangentView ), 'v_bitangentView' ).normalize(); @@ -63,7 +63,7 @@ export const bitangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, mater } - if ( material.flatShading !== true ) { + if ( builder.isFlatShading() !== true ) { node = directionToFaceDirection( node ); diff --git a/src/nodes/accessors/Normal.js b/src/nodes/accessors/Normal.js index a9134c2adf5697..a2c90cc24d9d54 100644 --- a/src/nodes/accessors/Normal.js +++ b/src/nodes/accessors/Normal.js @@ -52,7 +52,7 @@ export const normalViewGeometry = /*@__PURE__*/ ( Fn( ( builder ) => { let node; - if ( builder.material.flatShading === true ) { + if ( builder.isFlatShading() ) { node = normalFlat; @@ -76,7 +76,7 @@ export const normalWorldGeometry = /*@__PURE__*/ ( Fn( ( builder ) => { let normal = normalViewGeometry.transformDirection( cameraViewMatrix ); - if ( builder.material.flatShading !== true ) { + if ( builder.isFlatShading() !== true ) { normal = normal.toVarying( 'v_normalWorldGeometry' ); @@ -92,15 +92,15 @@ export const normalWorldGeometry = /*@__PURE__*/ ( Fn( ( builder ) => { * @tsl * @type {Node} */ -export const normalView = /*@__PURE__*/ ( Fn( ( { subBuildFn, material, context } ) => { +export const normalView = /*@__PURE__*/ ( Fn( ( builder ) => { let node; - if ( subBuildFn === 'NORMAL' || subBuildFn === 'VERTEX' ) { + if ( builder.subBuildFn === 'NORMAL' || builder.subBuildFn === 'VERTEX' ) { node = normalViewGeometry; - if ( material.flatShading !== true ) { + if ( builder.isFlatShading() !== true ) { node = directionToFaceDirection( node ); @@ -110,7 +110,7 @@ export const normalView = /*@__PURE__*/ ( Fn( ( { subBuildFn, material, context // Use getUV context to avoid side effects from nodes overwriting getUV in the context (e.g. EnvironmentNode) - node = context.setupNormal().context( { getUV: null } ); + node = builder.context.setupNormal().context( { getUV: null } ); } diff --git a/src/nodes/accessors/Tangent.js b/src/nodes/accessors/Tangent.js index 2e23c840040104..40a9fabeec6033 100644 --- a/src/nodes/accessors/Tangent.js +++ b/src/nodes/accessors/Tangent.js @@ -27,11 +27,11 @@ export const tangentLocal = /*@__PURE__*/ tangentGeometry.xyz.toVar( 'tangentLoc * @tsl * @type {Node} */ -export const tangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, material } ) => { +export const tangentView = /*@__PURE__*/ ( Fn( ( builder ) => { let node; - if ( subBuildFn === 'VERTEX' || geometry.hasAttribute( 'tangent' ) ) { + if ( builder.subBuildFn === 'VERTEX' || builder.geometry.hasAttribute( 'tangent' ) ) { node = modelViewMatrix.mul( vec4( tangentLocal, 0 ) ).xyz.toVarying( 'v_tangentView' ).normalize(); @@ -41,7 +41,7 @@ export const tangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, materia } - if ( material.flatShading !== true ) { + if ( builder.isFlatShading() !== true ) { node = directionToFaceDirection( node ); diff --git a/src/nodes/core/NodeBuilder.js b/src/nodes/core/NodeBuilder.js index d83ca104d85921..ee2c64bbb7b276 100644 --- a/src/nodes/core/NodeBuilder.js +++ b/src/nodes/core/NodeBuilder.js @@ -467,6 +467,17 @@ class NodeBuilder { } + /** + * Whether the material is using flat shading or not. + * + * @returns {boolean} Whether the material is using flat shading or not. + */ + isFlatShading() { + + return this.material.flatShading === true || this.geometry.hasAttribute( 'normal' ) === false; + + } + /** * Whether the material is opaque or not. * diff --git a/src/nodes/display/NormalMapNode.js b/src/nodes/display/NormalMapNode.js index adf3b3f70d1b7e..a4b9fd4c747043 100644 --- a/src/nodes/display/NormalMapNode.js +++ b/src/nodes/display/NormalMapNode.js @@ -69,7 +69,7 @@ class NormalMapNode extends TempNode { } - setup( { material } ) { + setup( builder ) { const { normalMapType, scaleNode, unpackNormalMode } = this; @@ -105,7 +105,7 @@ class NormalMapNode extends TempNode { let scale = scaleNode; - if ( material.flatShading === true ) { + if ( builder.isFlatShading() === true ) { scale = directionToFaceDirection( scale );