Browse Source

TSL: Fix conditional cache and introduce `isolate()` (#31973)

* cleanup

* Update FunctionCallNode.js

* fix conditional cache

* add custom parent scope

* Rename `CacheNode` -> `IsolateNode`, `cache()` -> `isolate()`

* Update IsolateNode.js
sunag 6 months ago
parent
commit
e80d168055

+ 1 - 1
src/nodes/Nodes.js

@@ -6,7 +6,7 @@ export { default as ArrayNode } from './core/ArrayNode.js';
 export { default as AssignNode } from './core/AssignNode.js';
 export { default as AttributeNode } from './core/AttributeNode.js';
 export { default as BypassNode } from './core/BypassNode.js';
-export { default as CacheNode } from './core/CacheNode.js';
+export { default as IsolateNode } from './core/IsolateNode.js';
 export { default as ConstNode } from './core/ConstNode.js';
 export { default as ContextNode } from './core/ContextNode.js';
 export { default as IndexNode } from './core/IndexNode.js';

+ 1 - 1
src/nodes/TSL.js

@@ -5,7 +5,7 @@ export * from './core/constants.js';
 export * from './core/AssignNode.js';
 export * from './core/AttributeNode.js';
 export * from './core/BypassNode.js';
-export * from './core/CacheNode.js';
+export * from './core/IsolateNode.js';
 export * from './core/ContextNode.js';
 export * from './core/IndexNode.js';
 export * from './core/ParameterNode.js';

+ 2 - 2
src/nodes/accessors/TextureNode.js

@@ -349,9 +349,9 @@ class TextureNode extends UniformNode {
 
 		//
 
-		let uvNode = Fn( () => {
+		const uvNode = Fn( () => {
 
-			uvNode = this.uvNode;
+			let uvNode = this.uvNode;
 
 			if ( ( uvNode === null || builder.context.forceUVContext === true ) && builder.context.getUV ) {
 

+ 1 - 1
src/nodes/code/FunctionCallNode.js

@@ -180,7 +180,7 @@ export const call = ( func, ...params ) => {
 
 	params = params.length > 1 || ( params[ 0 ] && params[ 0 ].isNode === true ) ? nodeArray( params ) : nodeObjects( params[ 0 ] );
 
-	return nodeObject( new FunctionCallNode( nodeObject( func ), params ) );
+	return new FunctionCallNode( nodeObject( func ), params );
 
 };
 

+ 43 - 9
src/nodes/core/CacheNode.js → src/nodes/core/IsolateNode.js

@@ -1,5 +1,6 @@
 import Node from './Node.js';
 import { addMethodChaining, nodeObject } from '../tsl/TSLCore.js';
+import { warn } from '../../utils.js';
 
 /**
  * This node can be used as a cache management component for another node.
@@ -8,11 +9,11 @@ import { addMethodChaining, nodeObject } from '../tsl/TSLCore.js';
  *
  * @augments Node
  */
-class CacheNode extends Node {
+class IsolateNode extends Node {
 
 	static get type() {
 
-		return 'CacheNode';
+		return 'IsolateNode';
 
 	}
 
@@ -20,9 +21,9 @@ class CacheNode extends Node {
 	 * Constructs a new cache node.
 	 *
 	 * @param {Node} node - The node that should be cached.
-	 * @param {boolean} [parent=true] - Whether this node refers to a shared parent cache or not.
+	 * @param {Node} [scope=null] - The scope node that defines the cache context.
 	 */
-	constructor( node, parent = true ) {
+	constructor( node ) {
 
 		super();
 
@@ -48,7 +49,7 @@ class CacheNode extends Node {
 		 * @readonly
 		 * @default true
 		 */
-		this.isCacheNode = true;
+		this.isIsolateNode = true;
 
 	}
 
@@ -82,9 +83,23 @@ class CacheNode extends Node {
 
 	}
 
+	setParent( parent ) {
+
+		this.parent = parent;
+
+		return this;
+
+	}
+
+	getParent() {
+
+		return this.parent;
+
+	}
+
 }
 
-export default CacheNode;
+export default IsolateNode;
 
 /**
  * TSL function for creating a cache node.
@@ -92,9 +107,28 @@ export default CacheNode;
  * @tsl
  * @function
  * @param {Node} node - The node that should be cached.
- * @param {boolean} [parent] - Whether this node refers to a shared parent cache or not.
- * @returns {CacheNode}
+ * @returns {IsolateNode}
  */
-export const cache = ( node, parent ) => nodeObject( new CacheNode( nodeObject( node ), parent ) );
+export const isolate = ( node ) => new IsolateNode( nodeObject( node ) );
+
+
+/**
+ * TSL function for creating a cache node.
+ *
+ * @tsl
+ * @function
+ * @deprecated
+ * @param {Node} node - The node that should be cached.
+ * @param {boolean} [parent=true] - Whether this node refers to a shared parent cache or not.
+ * @returns {IsolateNode}
+ */
+export function cache( node, parent = true ) {
+
+	warn( 'TSL: "cache()" has been deprecated. Use "isolate()" instead.' ); // @deprecated r181
+
+	return isolate( node ).setParent( parent );
+
+}
 
 addMethodChaining( 'cache', cache );
+addMethodChaining( 'isolate', isolate );

+ 1 - 1
src/nodes/display/BumpMapNode.js

@@ -11,7 +11,7 @@ import { Fn, nodeProxy, float, vec2 } from '../tsl/TSLBase.js';
 const dHdxy_fwd = Fn( ( { textureNode, bumpScale } ) => {
 
 	// It's used to preserve the same TextureNode instance
-	const sampleTexture = ( callback ) => textureNode.cache().context( { getUV: ( texNode ) => callback( texNode.uvNode || uv() ), forceUVContext: true } );
+	const sampleTexture = ( callback ) => textureNode.isolate().context( { getUV: ( texNode ) => callback( texNode.uvNode || uv() ), forceUVContext: true } );
 
 	const Hll = float( sampleTexture( ( uvNode ) => uvNode ) );
 

+ 4 - 4
src/nodes/lighting/EnvironmentNode.js

@@ -1,5 +1,5 @@
 import LightingNode from './LightingNode.js';
-import { cache } from '../core/CacheNode.js';
+import { isolate } from '../core/IsolateNode.js';
 import { roughness, clearcoatRoughness } from '../core/PropertyNode.js';
 import { cameraViewMatrix } from '../accessors/Camera.js';
 import { normalView, clearcoatNormalView, normalWorld } from '../accessors/Normal.js';
@@ -77,8 +77,8 @@ class EnvironmentNode extends LightingNode {
 		const radiance = envNode.context( createRadianceContext( roughness, radianceNormalView ) ).mul( materialEnvIntensity );
 		const irradiance = envNode.context( createIrradianceContext( normalWorld ) ).mul( Math.PI ).mul( materialEnvIntensity );
 
-		const isolateRadiance = cache( radiance );
-		const isolateIrradiance = cache( irradiance );
+		const isolateRadiance = isolate( radiance );
+		const isolateIrradiance = isolate( irradiance );
 
 		//
 
@@ -93,7 +93,7 @@ class EnvironmentNode extends LightingNode {
 		if ( clearcoatRadiance ) {
 
 			const clearcoatRadianceContext = envNode.context( createRadianceContext( clearcoatRoughness, clearcoatNormalView ) ).mul( materialEnvIntensity );
-			const isolateClearcoatRadiance = cache( clearcoatRadianceContext );
+			const isolateClearcoatRadiance = isolate( clearcoatRadianceContext );
 
 			clearcoatRadiance.addAssign( isolateClearcoatRadiance );
 

+ 3 - 3
src/nodes/math/ConditionalNode.js

@@ -100,9 +100,9 @@ class ConditionalNode extends Node {
 
 	setup( builder ) {
 
-		const condNode = this.condNode.cache();
-		const ifNode = this.ifNode.cache();
-		const elseNode = this.elseNode ? this.elseNode.cache() : null;
+		const condNode = this.condNode;
+		const ifNode = this.ifNode.isolate();
+		const elseNode = this.elseNode ? this.elseNode.isolate() : null;
 
 		//
 

+ 1 - 1
src/nodes/tsl/TSLBase.js

@@ -19,7 +19,7 @@ export * from '../display/ColorSpaceNode.js'; // .toColorSpace()
 export * from '../display/ToneMappingNode.js'; // .toToneMapping()
 export * from '../accessors/BufferAttributeNode.js'; // .toAttribute()
 export * from '../gpgpu/ComputeNode.js'; // .compute()
-export * from '../core/CacheNode.js'; // .cache()
+export * from '../core/IsolateNode.js'; // .isolate()
 export * from '../core/BypassNode.js'; // .bypass()
 export * from '../utils/RemapNode.js'; // .remap(), .remapClamp()
 export * from '../code/ExpressionNode.js'; // expression()

粤ICP备19079148号