|
|
@@ -1,41 +1,11 @@
|
|
|
import TempNode from '../core/TempNode.js';
|
|
|
-import { add } from '../math/OperatorNode.js';
|
|
|
|
|
|
import { normalView, transformNormalToView } from '../accessors/Normal.js';
|
|
|
-import { positionView } from '../accessors/Position.js';
|
|
|
import { TBNViewMatrix } from '../accessors/AccessorsUtils.js';
|
|
|
-import { uv } from '../accessors/UV.js';
|
|
|
-import { faceDirection } from './FrontFacingNode.js';
|
|
|
-import { Fn, nodeProxy, vec3 } from '../tsl/TSLBase.js';
|
|
|
+import { nodeProxy, vec3 } from '../tsl/TSLBase.js';
|
|
|
|
|
|
import { TangentSpaceNormalMap, ObjectSpaceNormalMap } from '../../constants.js';
|
|
|
-
|
|
|
-// Normal Mapping Without Precomputed Tangents
|
|
|
-// http://www.thetenthplanet.de/archives/1180
|
|
|
-
|
|
|
-const perturbNormal2Arb = /*@__PURE__*/ Fn( ( inputs ) => {
|
|
|
-
|
|
|
- const { eye_pos, surf_norm, mapN, uv } = inputs;
|
|
|
-
|
|
|
- const q0 = eye_pos.dFdx();
|
|
|
- const q1 = eye_pos.dFdy();
|
|
|
- const st0 = uv.dFdx();
|
|
|
- const st1 = uv.dFdy();
|
|
|
-
|
|
|
- const N = surf_norm; // normalized
|
|
|
-
|
|
|
- const q1perp = q1.cross( N );
|
|
|
- const q0perp = N.cross( q0 );
|
|
|
-
|
|
|
- const T = q1perp.mul( st0.x ).add( q0perp.mul( st1.x ) );
|
|
|
- const B = q1perp.mul( st0.y ).add( q0perp.mul( st1.y ) );
|
|
|
-
|
|
|
- const det = T.dot( T ).max( B.dot( B ) );
|
|
|
- const scale = faceDirection.mul( det.inverseSqrt() );
|
|
|
-
|
|
|
- return add( T.mul( mapN.x, scale ), B.mul( mapN.y, scale ), N.mul( mapN.z ) ).normalize();
|
|
|
-
|
|
|
-} );
|
|
|
+import { directionToFaceDirection } from './FrontFacingNode.js';
|
|
|
|
|
|
/**
|
|
|
* This class can be used for applying normals maps to materials.
|
|
|
@@ -89,7 +59,7 @@ class NormalMapNode extends TempNode {
|
|
|
|
|
|
}
|
|
|
|
|
|
- setup( builder ) {
|
|
|
+ setup( { material } ) {
|
|
|
|
|
|
const { normalMapType, scaleNode } = this;
|
|
|
|
|
|
@@ -97,44 +67,37 @@ class NormalMapNode extends TempNode {
|
|
|
|
|
|
if ( scaleNode !== null ) {
|
|
|
|
|
|
- normalMap = vec3( normalMap.xy.mul( scaleNode ), normalMap.z );
|
|
|
+ let scale = scaleNode;
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- let outputNode = null;
|
|
|
+ if ( material.flatShading === true ) {
|
|
|
|
|
|
- if ( normalMapType === ObjectSpaceNormalMap ) {
|
|
|
+ scale = directionToFaceDirection( scale );
|
|
|
|
|
|
- outputNode = transformNormalToView( normalMap );
|
|
|
+ }
|
|
|
|
|
|
- } else if ( normalMapType === TangentSpaceNormalMap ) {
|
|
|
+ normalMap = vec3( normalMap.xy.mul( scale ), normalMap.z );
|
|
|
|
|
|
- const tangent = builder.hasGeometryAttribute( 'tangent' );
|
|
|
+ }
|
|
|
|
|
|
- if ( tangent === true ) {
|
|
|
+ let output = null;
|
|
|
|
|
|
- outputNode = TBNViewMatrix.mul( normalMap ).normalize();
|
|
|
+ if ( normalMapType === ObjectSpaceNormalMap ) {
|
|
|
|
|
|
- } else {
|
|
|
+ output = transformNormalToView( normalMap );
|
|
|
|
|
|
- outputNode = perturbNormal2Arb( {
|
|
|
- eye_pos: positionView,
|
|
|
- surf_norm: normalView,
|
|
|
- mapN: normalMap,
|
|
|
- uv: uv()
|
|
|
- } );
|
|
|
+ } else if ( normalMapType === TangentSpaceNormalMap ) {
|
|
|
|
|
|
- }
|
|
|
+ output = TBNViewMatrix.mul( normalMap ).normalize();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error( `THREE.NodeMaterial: Unsupported normal map type: ${ normalMapType }` );
|
|
|
|
|
|
- outputNode = normalView; // Fallback to default normal view
|
|
|
+ output = normalView; // Fallback to default normal view
|
|
|
|
|
|
}
|
|
|
|
|
|
- return outputNode;
|
|
|
+ return output;
|
|
|
|
|
|
}
|
|
|
|