|
@@ -3,6 +3,8 @@ import { reference } from './ReferenceNode.js';
|
|
|
import { materialReference } from './MaterialReferenceNode.js';
|
|
|
import { nodeImmutable, float } from '../shadernode/ShaderNode.js';
|
|
|
|
|
|
+const cache = new WeakMap();
|
|
|
+
|
|
|
class MaterialNode extends Node {
|
|
|
|
|
|
constructor( scope ) {
|
|
@@ -13,29 +15,49 @@ class MaterialNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
- getFloat( property ) {
|
|
|
+ getCache( builder, property, type ) {
|
|
|
|
|
|
- //@TODO: Check if it can be cached by property name.
|
|
|
+ const material = builder.context.material;
|
|
|
|
|
|
- return materialReference( property, 'float' );
|
|
|
+ let cacheMaterial = cache.get( material );
|
|
|
|
|
|
- }
|
|
|
+ if ( cacheMaterial === undefined ) {
|
|
|
+
|
|
|
+ cacheMaterial = {};
|
|
|
+
|
|
|
+ cache.set( material, cacheMaterial );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ let node = cacheMaterial[ property ];
|
|
|
+
|
|
|
+ if ( node === undefined ) {
|
|
|
|
|
|
- getColor( property ) {
|
|
|
+ node = materialReference( property, type );
|
|
|
|
|
|
- //@TODO: Check if it can be cached by property name.
|
|
|
+ cacheMaterial[ property ] = node;
|
|
|
|
|
|
- return materialReference( property, 'color' );
|
|
|
+ }
|
|
|
+
|
|
|
+ return node;
|
|
|
|
|
|
}
|
|
|
|
|
|
- getTexture( property ) {
|
|
|
+ getFloat( builder, property ) {
|
|
|
|
|
|
- //@TODO: Check if it can be cached by property name.
|
|
|
+ return this.getCache( builder, property, 'float' );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ getColor( builder, property ) {
|
|
|
+
|
|
|
+ return this.getCache( builder, property, 'color' );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- const textureRefNode = materialReference( property, 'texture' );
|
|
|
+ getTexture( builder, property ) {
|
|
|
|
|
|
- return textureRefNode;
|
|
|
+ return this.getCache( builder, property, 'texture' );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -48,19 +70,19 @@ class MaterialNode extends Node {
|
|
|
|
|
|
if ( scope === MaterialNode.ALPHA_TEST || scope === MaterialNode.SHININESS || scope === MaterialNode.REFLECTIVITY || scope === MaterialNode.ROTATION || scope === MaterialNode.IRIDESCENCE || scope === MaterialNode.IRIDESCENCE_IOR ) {
|
|
|
|
|
|
- node = this.getFloat( scope );
|
|
|
+ node = this.getFloat( builder, scope );
|
|
|
|
|
|
} else if ( scope === MaterialNode.SPECULAR_COLOR ) {
|
|
|
|
|
|
- node = this.getColor( 'specular' );
|
|
|
+ node = this.getColor( builder, 'specular' );
|
|
|
|
|
|
} else if ( scope === MaterialNode.COLOR ) {
|
|
|
|
|
|
- const colorNode = this.getColor( 'color' );
|
|
|
+ const colorNode = this.getColor( builder, 'color' );
|
|
|
|
|
|
if ( material.map && material.map.isTexture === true ) {
|
|
|
|
|
|
- node = colorNode.mul( this.getTexture( 'map' ) );
|
|
|
+ node = colorNode.mul( this.getTexture( builder, 'map' ) );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -70,11 +92,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.OPACITY ) {
|
|
|
|
|
|
- const opacityNode = this.getFloat( 'opacity' );
|
|
|
+ const opacityNode = this.getFloat( builder, 'opacity' );
|
|
|
|
|
|
if ( material.alphaMap && material.alphaMap.isTexture === true ) {
|
|
|
|
|
|
- node = opacityNode.mul( this.getTexture( 'alphaMap' ) );
|
|
|
+ node = opacityNode.mul( this.getTexture( builder, 'alphaMap' ) );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -86,7 +108,7 @@ class MaterialNode extends Node {
|
|
|
|
|
|
if ( material.specularMap && material.specularMap.isTexture === true ) {
|
|
|
|
|
|
- node = this.getTexture( 'specularMap' ).r;
|
|
|
+ node = this.getTexture( builder, 'specularMap' ).r;
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -96,11 +118,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.ROUGHNESS ) {
|
|
|
|
|
|
- const roughnessNode = this.getFloat( 'roughness' );
|
|
|
+ const roughnessNode = this.getFloat( builder, 'roughness' );
|
|
|
|
|
|
if ( material.roughnessMap && material.roughnessMap.isTexture === true ) {
|
|
|
|
|
|
- node = roughnessNode.mul( this.getTexture( 'roughnessMap' ).g );
|
|
|
+ node = roughnessNode.mul( this.getTexture( builder, 'roughnessMap' ).g );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -110,11 +132,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.METALNESS ) {
|
|
|
|
|
|
- const metalnessNode = this.getFloat( 'metalness' );
|
|
|
+ const metalnessNode = this.getFloat( builder, 'metalness' );
|
|
|
|
|
|
if ( material.metalnessMap && material.metalnessMap.isTexture === true ) {
|
|
|
|
|
|
- node = metalnessNode.mul( this.getTexture( 'metalnessMap' ).b );
|
|
|
+ node = metalnessNode.mul( this.getTexture( builder, 'metalnessMap' ).b );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -124,11 +146,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.EMISSIVE ) {
|
|
|
|
|
|
- const emissiveNode = this.getColor( 'emissive' );
|
|
|
+ const emissiveNode = this.getColor( builder, 'emissive' );
|
|
|
|
|
|
if ( material.emissiveMap && material.emissiveMap.isTexture === true ) {
|
|
|
|
|
|
- node = emissiveNode.mul( this.getTexture( 'emissiveMap' ) );
|
|
|
+ node = emissiveNode.mul( this.getTexture( builder, 'emissiveMap' ) );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -138,11 +160,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.CLEARCOAT ) {
|
|
|
|
|
|
- const clearcoatNode = this.getFloat( 'clearcoat' );
|
|
|
+ const clearcoatNode = this.getFloat( builder, 'clearcoat' );
|
|
|
|
|
|
if ( material.clearcoatMap && material.clearcoatMap.isTexture === true ) {
|
|
|
|
|
|
- node = clearcoatNode.mul( this.getTexture( 'clearcoatMap' ).r );
|
|
|
+ node = clearcoatNode.mul( this.getTexture( builder, 'clearcoatMap' ).r );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -152,11 +174,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.CLEARCOAT_ROUGHNESS ) {
|
|
|
|
|
|
- const clearcoatRoughnessNode = this.getFloat( 'clearcoatRoughness' );
|
|
|
+ const clearcoatRoughnessNode = this.getFloat( builder, 'clearcoatRoughness' );
|
|
|
|
|
|
if ( material.clearcoatRoughnessMap && material.clearcoatRoughnessMap.isTexture === true ) {
|
|
|
|
|
|
- node = clearcoatRoughnessNode.mul( this.getTexture( 'clearcoatRoughnessMap' ).r );
|
|
|
+ node = clearcoatRoughnessNode.mul( this.getTexture( builder, 'clearcoatRoughnessMap' ).r );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -166,11 +188,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.SHEEN ) {
|
|
|
|
|
|
- const sheenNode = this.getColor( 'sheenColor' ).mul( this.getFloat( 'sheen' ) ); // Move this mul() to CPU
|
|
|
+ const sheenNode = this.getColor( builder, 'sheenColor' ).mul( this.getFloat( builder, 'sheen' ) ); // Move this mul() to CPU
|
|
|
|
|
|
if ( material.sheenColorMap && material.sheenColorMap.isTexture === true ) {
|
|
|
|
|
|
- node = sheenNode.mul( this.getTexture( 'sheenColorMap' ).rgb );
|
|
|
+ node = sheenNode.mul( this.getTexture( builder, 'sheenColorMap' ).rgb );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -180,11 +202,11 @@ class MaterialNode extends Node {
|
|
|
|
|
|
} else if ( scope === MaterialNode.SHEEN_ROUGHNESS ) {
|
|
|
|
|
|
- const sheenRoughnessNode = this.getFloat( 'sheenRoughness' );
|
|
|
+ const sheenRoughnessNode = this.getFloat( builder, 'sheenRoughness' );
|
|
|
|
|
|
if ( material.sheenRoughnessMap && material.sheenRoughnessMap.isTexture === true ) {
|
|
|
|
|
|
- node = sheenRoughnessNode.mul( this.getTexture( 'sheenRoughnessMap' ).a );
|
|
|
+ node = sheenRoughnessNode.mul( this.getTexture( builder, 'sheenRoughnessMap' ).a );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -202,7 +224,7 @@ class MaterialNode extends Node {
|
|
|
|
|
|
const iridescenceThicknessMinimum = reference( 0, 'float', material.iridescenceThicknessRange );
|
|
|
|
|
|
- node = iridescenceThicknessMaximum.sub( iridescenceThicknessMinimum ).mul( this.getTexture( 'iridescenceThicknessMap' ).g ).add( iridescenceThicknessMinimum );
|
|
|
+ node = iridescenceThicknessMaximum.sub( iridescenceThicknessMinimum ).mul( this.getTexture( builder, 'iridescenceThicknessMap' ).g ).add( iridescenceThicknessMinimum );
|
|
|
|
|
|
} else {
|
|
|
|