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 3 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
57 changes: 57 additions & 0 deletions src/nodes/functions/BasicLightingModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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( envNode = null, combine = MultiplyOperation ) {

super();

this.envNode = envNode;
this.combine = combine;

}

indirectDiffuse( { reflectedLight } ) {

reflectedLight.indirectDiffuse.assign( diffuseColor.rgb );

}

finish( context ) {

const outgoingLight = context.outgoingLight;
const envNode = this.envNode;

if ( envNode ) {

switch ( this.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:', this.combine );
break;

}

}

}

}

export default BasicLightingModel;
9 changes: 5 additions & 4 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 All @@ -7,6 +7,7 @@ import { materialSpecularStrength } from '../accessors/MaterialNode.js';
import { shininess, specularColor } from '../core/PropertyNode.js';
import { positionViewDirection } from '../accessors/PositionNode.js';
import { tslFn, float } from '../shadernode/ShaderNode.js';
import { MultiplyOperation } from '../../constants.js';

const G_BlinnPhong_Implicit = () => float( 0.25 );

Expand All @@ -31,11 +32,11 @@ const BRDF_BlinnPhong = tslFn( ( { lightDirection } ) => {

} );

class PhongLightingModel extends LightingModel {
class PhongLightingModel extends BasicLightingModel {

constructor( specular = true ) {
constructor( envNode = null, combine = MultiplyOperation, specular = true ) {

super();
super( envNode, combine );

this.specular = specular;

Expand Down
34 changes: 4 additions & 30 deletions src/nodes/materials/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 BasicLightingModel from '../functions/BasicLightingModel.js';

const defaultValues = new MeshBasicMaterial();

Expand All @@ -15,7 +12,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 +21,11 @@ class MeshBasicNodeMaterial extends NodeMaterial {

}

setupVariants( builder ) {
setupLightingModel( builder ) {

const envNode = this.getEnvNode( builder );

if ( 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;

}

}

return new BasicLightingModel( envNode, this.combine );

}

Expand Down
6 changes: 4 additions & 2 deletions src/nodes/materials/MeshLambertNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class MeshLambertNodeMaterial extends NodeMaterial {

}

setupLightingModel( /*builder*/ ) {
setupLightingModel( builder ) {

return new PhongLightingModel( false ); // ( specular ) -> force lambert
const envNode = this.getEnvNode( builder );

return new PhongLightingModel( envNode, false, ); // ( specular ) -> force lambert
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

}

Expand Down
6 changes: 4 additions & 2 deletions src/nodes/materials/MeshPhongNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ class MeshPhongNodeMaterial extends NodeMaterial {

}

setupLightingModel( /*builder*/ ) {
setupLightingModel( builder ) {

return new PhongLightingModel();
const envNode = this.getEnvNode( builder );

return new PhongLightingModel( envNode, this.combine );

}

Expand Down