Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MeshBasicNodeMaterial: Refactor env map support. #28798

Merged
merged 8 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
55 changes: 55 additions & 0 deletions src/nodes/functions/BasicLightingModel.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/nodes/functions/PhongLightingModel.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -31,7 +31,7 @@ const BRDF_BlinnPhong = tslFn( ( { lightDirection } ) => {

} );

class PhongLightingModel extends LightingModel {
class PhongLightingModel extends BasicLightingModel {

constructor( specular = true ) {

Expand Down
26 changes: 26 additions & 0 deletions src/nodes/lighting/BasicEnvironmentNode.js
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions src/nodes/lighting/LightingNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class LightingNode extends Node {

super( 'vec3' );

this.isLightingNode = true;

}

generate( /*builder*/ ) {
Expand Down
37 changes: 9 additions & 28 deletions src/nodes/materials/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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 );
Expand All @@ -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();

}

Expand Down
9 changes: 9 additions & 0 deletions src/nodes/materials/MeshStandardNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions src/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -330,7 +329,7 @@ class NodeMaterial extends Material {

}

getEnvNode( builder ) {
setupEnvironment( builder ) {

let node = null;

Expand All @@ -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 );

}

Expand Down