Parcourir la source

MeshBasicNodeMaterial: Refactor env map support. (#28798)

* MeshBasicNodeMaterial: Refactor env map support.

* MeshBasicNodeMaterial: Move env map logic to lighting model.

* NodeMaterial: Clean up.

* MeshLambertNodeMaterial: Fix creation of lighting model.

* Update MeshLambertNodeMaterial.js

* update

* cleanup

* revision

---------

Co-authored-by: sunag <[email protected]>
Michael Herzog il y a 1 an
Parent
commit
2b5e50041c

+ 1 - 0
src/nodes/Nodes.js

@@ -169,6 +169,7 @@ export { default as LightingNode /* @TODO: lighting (abstract), light */ } from
 export { default as LightingContextNode, lightingContext } from './lighting/LightingContextNode.js';
 export { default as HemisphereLightNode } from './lighting/HemisphereLightNode.js';
 export { default as EnvironmentNode } from './lighting/EnvironmentNode.js';
+export { default as BasicEnvironmentNode } from './lighting/BasicEnvironmentNode.js';
 export { default as IrradianceNode } from './lighting/IrradianceNode.js';
 export { default as AONode } from './lighting/AONode.js';
 export { default as AnalyticLightNode } from './lighting/AnalyticLightNode.js';

+ 55 - 0
src/nodes/functions/BasicLightingModel.js

@@ -0,0 +1,55 @@
+import LightingModel from '../core/LightingModel.js';
+import { diffuseColor } from '../core/PropertyNode.js';
+import { MultiplyOperation, MixOperation, AddOperation } from '../../constants.js';
+import { materialSpecularStrength, materialReflectivity } from '../accessors/MaterialNode.js';
+import { mix } from '../math/MathNode.js';
+
+class BasicLightingModel extends LightingModel {
+
+	constructor() {
+
+		super();
+
+	}
+
+	indirectDiffuse( { reflectedLight } ) {
+
+		reflectedLight.indirectDiffuse.assign( diffuseColor.rgb );
+
+	}
+
+	finish( context, stack, builder ) {
+
+		const material = builder.material;
+		const outgoingLight = context.outgoingLight;
+		const envNode = builder.context.environment;
+
+		if ( envNode ) {
+
+			switch ( material.combine ) {
+
+				case MultiplyOperation:
+					outgoingLight.rgb.assign( mix( outgoingLight.rgb, outgoingLight.rgb.mul( envNode.rgb ), materialSpecularStrength.mul( materialReflectivity ) ) );
+					break;
+
+				case MixOperation:
+					outgoingLight.rgb.assign( mix( outgoingLight.rgb, envNode.rgb, materialSpecularStrength.mul( materialReflectivity ) ) );
+					break;
+
+				case AddOperation:
+					outgoingLight.rgb.addAssign( envNode.rgb.mul( materialSpecularStrength.mul( materialReflectivity ) ) );
+					break;
+
+				default:
+					console.warn( 'THREE.BasicLightingModel: Unsupported .combine value:', material.combine );
+					break;
+
+			}
+
+		}
+
+	}
+
+}
+
+export default BasicLightingModel;

+ 2 - 2
src/nodes/functions/PhongLightingModel.js

@@ -1,4 +1,4 @@
-import LightingModel from '../core/LightingModel.js';
+import BasicLightingModel from './BasicLightingModel.js';
 import F_Schlick from './BSDF/F_Schlick.js';
 import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
 import { diffuseColor } from '../core/PropertyNode.js';
@@ -31,7 +31,7 @@ const BRDF_BlinnPhong = tslFn( ( { lightDirection } ) => {
 
 } );
 
-class PhongLightingModel extends LightingModel {
+class PhongLightingModel extends BasicLightingModel {
 
 	constructor( specular = true ) {
 

+ 26 - 0
src/nodes/lighting/BasicEnvironmentNode.js

@@ -0,0 +1,26 @@
+import LightingNode from './LightingNode.js';
+import { addNodeClass } from '../core/Node.js';
+
+class BasicEnvironmentNode extends LightingNode {
+
+	constructor( envNode = null ) {
+
+		super();
+
+		this.envNode = envNode;
+
+	}
+
+	setup( builder ) {
+
+		// environment property is used in the finish() method of BasicLightingModel
+
+		builder.context.environment = this.envNode;
+
+	}
+
+}
+
+export default BasicEnvironmentNode;
+
+addNodeClass( 'BasicEnvironmentNode', BasicEnvironmentNode );

+ 2 - 0
src/nodes/lighting/LightingNode.js

@@ -6,6 +6,8 @@ class LightingNode extends Node {
 
 		super( 'vec3' );
 
+		this.isLightingNode = true;
+
 	}
 
 	generate( /*builder*/ ) {

+ 9 - 28
src/nodes/materials/MeshBasicNodeMaterial.js

@@ -1,9 +1,7 @@
 import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
-import { diffuseColor } from '../core/PropertyNode.js';
-import { materialSpecularStrength, materialReflectivity } from '../accessors/MaterialNode.js';
 import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial.js';
-import { MultiplyOperation, MixOperation, AddOperation } from '../../constants.js';
-import { mix } from '../math/MathNode.js';
+import BasicEnvironmentNode from '../lighting/BasicEnvironmentNode.js';
+import BasicLightingModel from '../functions/BasicLightingModel.js';
 
 const defaultValues = new MeshBasicMaterial();
 
@@ -15,7 +13,7 @@ class MeshBasicNodeMaterial extends NodeMaterial {
 
 		this.isMeshBasicNodeMaterial = true;
 
-		this.lights = false;
+		this.lights = true;
 		//this.normals = false; @TODO: normals usage by context
 
 		this.setDefaultValues( defaultValues );
@@ -24,34 +22,17 @@ class MeshBasicNodeMaterial extends NodeMaterial {
 
 	}
 
-	setupVariants( builder ) {
+	setupEnvironment( builder ) {
 
-		const envNode = this.getEnvNode( builder );
+		const envNode = super.setupEnvironment( builder );
 
-		if ( envNode !== null ) {
+		return envNode ? new BasicEnvironmentNode( envNode ) : null;
 
-			switch ( this.combine ) {
-
-				case MultiplyOperation:
-					diffuseColor.assign( mix( diffuseColor.rgb, diffuseColor.rgb.mul( envNode.rgb ), materialSpecularStrength.mul( materialReflectivity ) ) );
-					break;
-
-				case MixOperation:
-					diffuseColor.assign( mix( diffuseColor.rgb, envNode.rgb, materialSpecularStrength.mul( materialReflectivity ) ) );
-					break;
-
-				case AddOperation:
-					diffuseColor.addAssign( envNode.rgb.mul( materialSpecularStrength.mul( materialReflectivity ) ) );
-					break;
-
-				default:
-					console.warn( 'THREE.MeshBasicNodeMaterial: Unsupported .combine value:', this.combine );
-					break;
-
-			}
+	}
 
-		}
+	setupLightingModel() {
 
+		return new BasicLightingModel();
 
 	}
 

+ 9 - 0
src/nodes/materials/MeshStandardNodeMaterial.js

@@ -4,6 +4,7 @@ import { mix } from '../math/MathNode.js';
 import { materialRoughness, materialMetalness } from '../accessors/MaterialNode.js';
 import getRoughness from '../functions/material/getRoughness.js';
 import PhysicalLightingModel from '../functions/PhysicalLightingModel.js';
+import EnvironmentNode from '../lighting/EnvironmentNode.js';
 import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
 
 import { MeshStandardMaterial } from '../../materials/MeshStandardMaterial.js';
@@ -29,6 +30,14 @@ class MeshStandardNodeMaterial extends NodeMaterial {
 
 	}
 
+	setupEnvironment( builder ) {
+
+		const envNode = super.setupEnvironment( builder );
+
+		return envNode ? new EnvironmentNode( envNode ) : null;
+
+	}
+
 	setupLightingModel( /*builder*/ ) {
 
 		return new PhysicalLightingModel();

+ 5 - 6
src/nodes/materials/NodeMaterial.js

@@ -20,7 +20,6 @@ import { mix } from '../math/MathNode.js';
 import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
 import AONode from '../lighting/AONode.js';
 import { lightingContext } from '../lighting/LightingContextNode.js';
-import EnvironmentNode from '../lighting/EnvironmentNode.js';
 import IrradianceNode from '../lighting/IrradianceNode.js';
 import { depth } from '../display/ViewportDepthNode.js';
 import { cameraLogDepth } from '../accessors/CameraNode.js';
@@ -330,7 +329,7 @@ class NodeMaterial extends Material {
 
 	}
 
-	getEnvNode( builder ) {
+	setupEnvironment( builder ) {
 
 		let node = null;
 
@@ -354,15 +353,15 @@ class NodeMaterial extends Material {
 
 	setupLights( builder ) {
 
-		const envNode = this.getEnvNode( builder );
+		const materialLightsNode = [];
 
 		//
 
-		const materialLightsNode = [];
+		const envNode = this.setupEnvironment( builder );
 
-		if ( envNode ) {
+		if ( envNode && envNode.isLightingNode ) {
 
-			materialLightsNode.push( new EnvironmentNode( envNode ) );
+			materialLightsNode.push( envNode );
 
 		}