Skip to content

Commit 8b4ee6e

Browse files
authored
TSL: Force flat shading for geometries without normals for normal* nodes (mrdoob#32848)
1 parent c58d044 commit 8b4ee6e

5 files changed

Lines changed: 27 additions & 16 deletions

File tree

src/nodes/accessors/Bitangent.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { directionToFaceDirection } from '../display/FrontFacingNode.js';
1313
* @param {string} varyingName - The name of the varying to assign the bitangent to.
1414
* @returns {Node<vec3>} The bitangent node.
1515
*/
16-
const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], { subBuildFn, material } ) => {
16+
const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], builder ) => {
1717

1818
let bitangent = crossNormalTangent.mul( tangentGeometry.w ).xyz;
1919

20-
if ( subBuildFn === 'NORMAL' && material.flatShading !== true ) {
20+
if ( builder.subBuildFn === 'NORMAL' && builder.isFlatShading() !== true ) {
2121

2222
bitangent = bitangent.toVarying( varyingName );
2323

@@ -49,11 +49,11 @@ export const bitangentLocal = /*@__PURE__*/ getBitangent( normalLocal.cross( tan
4949
* @tsl
5050
* @type {Node<vec3>}
5151
*/
52-
export const bitangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, material } ) => {
52+
export const bitangentView = /*@__PURE__*/ ( Fn( ( builder ) => {
5353

5454
let node;
5555

56-
if ( subBuildFn === 'VERTEX' || geometry.hasAttribute( 'tangent' ) ) {
56+
if ( builder.subBuildFn === 'VERTEX' || builder.geometry.hasAttribute( 'tangent' ) ) {
5757

5858
node = getBitangent( normalView.cross( tangentView ), 'v_bitangentView' ).normalize();
5959

@@ -63,7 +63,7 @@ export const bitangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, mater
6363

6464
}
6565

66-
if ( material.flatShading !== true ) {
66+
if ( builder.isFlatShading() !== true ) {
6767

6868
node = directionToFaceDirection( node );
6969

src/nodes/accessors/Normal.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const normalViewGeometry = /*@__PURE__*/ ( Fn( ( builder ) => {
5252

5353
let node;
5454

55-
if ( builder.material.flatShading === true ) {
55+
if ( builder.isFlatShading() ) {
5656

5757
node = normalFlat;
5858

@@ -76,7 +76,7 @@ export const normalWorldGeometry = /*@__PURE__*/ ( Fn( ( builder ) => {
7676

7777
let normal = normalViewGeometry.transformDirection( cameraViewMatrix );
7878

79-
if ( builder.material.flatShading !== true ) {
79+
if ( builder.isFlatShading() !== true ) {
8080

8181
normal = normal.toVarying( 'v_normalWorldGeometry' );
8282

@@ -92,15 +92,15 @@ export const normalWorldGeometry = /*@__PURE__*/ ( Fn( ( builder ) => {
9292
* @tsl
9393
* @type {Node<vec3>}
9494
*/
95-
export const normalView = /*@__PURE__*/ ( Fn( ( { subBuildFn, material, context } ) => {
95+
export const normalView = /*@__PURE__*/ ( Fn( ( builder ) => {
9696

9797
let node;
9898

99-
if ( subBuildFn === 'NORMAL' || subBuildFn === 'VERTEX' ) {
99+
if ( builder.subBuildFn === 'NORMAL' || builder.subBuildFn === 'VERTEX' ) {
100100

101101
node = normalViewGeometry;
102102

103-
if ( material.flatShading !== true ) {
103+
if ( builder.isFlatShading() !== true ) {
104104

105105
node = directionToFaceDirection( node );
106106

@@ -110,7 +110,7 @@ export const normalView = /*@__PURE__*/ ( Fn( ( { subBuildFn, material, context
110110

111111
// Use getUV context to avoid side effects from nodes overwriting getUV in the context (e.g. EnvironmentNode)
112112

113-
node = context.setupNormal().context( { getUV: null } );
113+
node = builder.context.setupNormal().context( { getUV: null } );
114114

115115
}
116116

src/nodes/accessors/Tangent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export const tangentLocal = /*@__PURE__*/ tangentGeometry.xyz.toVar( 'tangentLoc
2727
* @tsl
2828
* @type {Node<vec3>}
2929
*/
30-
export const tangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, material } ) => {
30+
export const tangentView = /*@__PURE__*/ ( Fn( ( builder ) => {
3131

3232
let node;
3333

34-
if ( subBuildFn === 'VERTEX' || geometry.hasAttribute( 'tangent' ) ) {
34+
if ( builder.subBuildFn === 'VERTEX' || builder.geometry.hasAttribute( 'tangent' ) ) {
3535

3636
node = modelViewMatrix.mul( vec4( tangentLocal, 0 ) ).xyz.toVarying( 'v_tangentView' ).normalize();
3737

@@ -41,7 +41,7 @@ export const tangentView = /*@__PURE__*/ ( Fn( ( { subBuildFn, geometry, materia
4141

4242
}
4343

44-
if ( material.flatShading !== true ) {
44+
if ( builder.isFlatShading() !== true ) {
4545

4646
node = directionToFaceDirection( node );
4747

src/nodes/core/NodeBuilder.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,17 @@ class NodeBuilder {
467467

468468
}
469469

470+
/**
471+
* Whether the material is using flat shading or not.
472+
*
473+
* @returns {boolean} Whether the material is using flat shading or not.
474+
*/
475+
isFlatShading() {
476+
477+
return this.material.flatShading === true || this.geometry.hasAttribute( 'normal' ) === false;
478+
479+
}
480+
470481
/**
471482
* Whether the material is opaque or not.
472483
*

src/nodes/display/NormalMapNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class NormalMapNode extends TempNode {
6969

7070
}
7171

72-
setup( { material } ) {
72+
setup( builder ) {
7373

7474
const { normalMapType, scaleNode, unpackNormalMode } = this;
7575

@@ -105,7 +105,7 @@ class NormalMapNode extends TempNode {
105105

106106
let scale = scaleNode;
107107

108-
if ( material.flatShading === true ) {
108+
if ( builder.isFlatShading() === true ) {
109109

110110
scale = directionToFaceDirection( scale );
111111

0 commit comments

Comments
 (0)