Skip to content

Commit 404125a

Browse files
authored
Merge pull request #17584 from sunag/dev-node-sides
NodeMaterial - Fix DoubleSided and BackSided NormalMap flipped
2 parents c798e80 + 7e5e3fd commit 404125a

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

examples/jsm/nodes/misc/NormalMapNode.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* @author sunag / http://www.sunag.com.br/
33
*/
44

5+
import {
6+
BackSide
7+
} from '../../../../build/three.module.js';
8+
59
import { TempNode } from '../core/TempNode.js';
610
import { Vector2Node } from '../inputs/Vector2Node.js';
711
import { FunctionNode } from '../core/FunctionNode.js';
@@ -25,7 +29,7 @@ NormalMapNode.Nodes = ( function () {
2529
// Per-Pixel Tangent Space Normal Mapping
2630
// http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
2731

28-
`vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 map, vec2 vUv, vec2 normalScale ) {
32+
`vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 map, vec2 vUv, vec2 normalScale ) {
2933
3034
// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
3135
@@ -47,14 +51,8 @@ NormalMapNode.Nodes = ( function () {
4751
#ifdef DOUBLE_SIDED
4852
4953
// Workaround for Adreno GPUs gl_FrontFacing bug. See #15850 and #10331
50-
// http://hacksoflife.blogspot.com/2009/11/per-pixel-tangent-space-normal-mapping.html?showComment=1522254677437#c5087545147696715943
51-
vec3 NfromST = cross( S, T );
52-
if( dot( NfromST, N ) > 0.0 ) {
53-
54-
S *= -1.0;
55-
T *= -1.0;
5654
57-
}
55+
if ( dot( cross( S, T ), N ) < 0.0 ) mapN.xy *= - 1.0;
5856
5957
#else
6058
@@ -87,11 +85,19 @@ NormalMapNode.prototype.generate = function ( builder, output ) {
8785
this.position = this.position || new PositionNode( PositionNode.VIEW );
8886
this.uv = this.uv || new UVNode();
8987

88+
var scale = this.scale.build( builder, 'v2' );
89+
90+
if ( builder.material.side === BackSide ) {
91+
92+
scale = '-' + scale;
93+
94+
}
95+
9096
return builder.format( perturbNormal2Arb + '( -' + this.position.build( builder, 'v3' ) + ', ' +
9197
this.normal.build( builder, 'v3' ) + ', ' +
9298
this.value.build( builder, 'v3' ) + ', ' +
9399
this.uv.build( builder, 'v2' ) + ', ' +
94-
this.scale.build( builder, 'v2' ) + ' )', this.getType( builder ), output );
100+
scale + ' )', this.getType( builder ), output );
95101

96102
} else {
97103

examples/webgl_materials_nodes.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,18 @@
724724

725725
}, false );
726726

727+
addGui( 'side', {
728+
DoubleSided: THREE.DoubleSide,
729+
FrontSided: THREE.FrontSide,
730+
BackSided: THREE.BackSide
731+
}, function ( val ) {
732+
733+
defaultSide = Number( val );
734+
735+
updateMaterial();
736+
737+
} );
738+
727739
break;
728740

729741
case 'physical':

0 commit comments

Comments
 (0)