Browse Source

BumpMapNode: Subflows of TextureNode and UVNode (#26865)

* BumpMapNode: Subflows of TextureNode and UVNode

* cleanup
sunag 1 year ago
parent
commit
8d71879573

+ 1 - 2
examples/jsm/nodes/accessors/ExtendedMaterialNode.js

@@ -44,8 +44,7 @@ class ExtendedMaterialNode extends MaterialNode {
 
 			} else if ( material.bumpMap ) {
 
-				// @TODO: Replace material.bumpMap to this.getTexture( 'bumpMap' )
-				node = bumpMap( material.bumpMap, materialReference( 'bumpScale', 'float' ) );
+				node = bumpMap( this.getTexture( 'bumpMap' ).r, materialReference( 'bumpScale', 'float' ) );
 
 			} else {
 

+ 30 - 11
examples/jsm/nodes/display/BumpMapNode.js

@@ -1,26 +1,45 @@
 import TempNode from '../core/TempNode.js';
-import { texture } from '../accessors/TextureNode.js';
 import { addNodeClass } from '../core/Node.js';
 import { uv } from '../accessors/UVNode.js';
 import { normalView } from '../accessors/NormalNode.js';
 import { positionView } from '../accessors/PositionNode.js';
 import { faceDirection } from './FrontFacingNode.js';
-import { tslFn, nodeProxy, vec2 } from '../shadernode/ShaderNode.js';
+import { tslFn, nodeProxy, float, vec2 } from '../shadernode/ShaderNode.js';
 
 // Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen
 // https://mmikk.github.io/papers3d/mm_sfgrad_bump.pdf
 
 // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
 
-const dHdxy_fwd = tslFn( ( { bumpTexture, bumpScale } ) => {
+const dHdxy_fwd = tslFn( ( { textureNode, bumpScale } ) => {
 
-	const uvNode = uv();
+	let texNode = textureNode;
 
-	const Hll = texture( bumpTexture, uvNode ).x;
+	if ( texNode.isTextureNode !== true ) {
+
+		texNode.traverse( ( node ) => {
+
+			if ( node.isTextureNode === true ) texNode = node;
+
+		} );
+
+	}
+
+	if ( texNode.isTextureNode !== true ) {
+
+		throw new Error( 'THREE.TSL: dHdxy_fwd() requires a TextureNode.' );
+
+	}
+
+	const Hll = float( textureNode );
+	const uvNode = texNode.uvNode || uv();
+
+	// It's used to preserve the same TextureNode instance
+	const sampleTexture = ( uv ) => textureNode.cache().context( { getUVNode: () => uv } );
 
 	return vec2(
-		texture( bumpTexture, uvNode.add( uvNode.dFdx() ) ).x.sub( Hll ),
-		texture( bumpTexture, uvNode.add( uvNode.dFdy() ) ).x.sub( Hll )
+		float( sampleTexture( uvNode.add( uvNode.dFdx() ) ) ).sub( Hll ),
+		float( sampleTexture( uvNode.add( uvNode.dFdy() ) ) ).sub( Hll )
 	).mul( bumpScale );
 
 } );
@@ -46,11 +65,11 @@ const perturbNormalArb = tslFn( ( inputs ) => {
 
 class BumpMapNode extends TempNode {
 
-	constructor( texture, scaleNode = null ) {
+	constructor( textureNode, scaleNode = null ) {
 
 		super( 'vec3' );
 
-		this.texture = texture;
+		this.textureNode = textureNode;
 		this.scaleNode = scaleNode;
 
 	}
@@ -58,10 +77,10 @@ class BumpMapNode extends TempNode {
 	setup() {
 
 		const bumpScale = this.scaleNode !== null ? this.scaleNode : 1;
-		const dHdxy = dHdxy_fwd( { bumpTexture: this.texture, bumpScale } );
+		const dHdxy = dHdxy_fwd( { textureNode: this.textureNode, bumpScale } );
 
 		return perturbNormalArb( {
-			surf_pos: positionView.negate(),
+			surf_pos: positionView,
 			surf_norm: normalView,
 			dHdxy
 		} );