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: Add lightMap support. #28821

Merged
merged 3 commits into from
Jul 6, 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
25 changes: 23 additions & 2 deletions src/nodes/functions/BasicLightingModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';
import { vec4 } from '../shadernode/ShaderNode.js';

class BasicLightingModel extends LightingModel {

Expand All @@ -12,12 +13,32 @@ class BasicLightingModel extends LightingModel {

}

indirect( { ambientOcclusion, reflectedLight } ) {
indirect( context, stack, builder ) {

reflectedLight.indirectDiffuse.addAssign( diffuseColor.rgb );
const ambientOcclusion = context.ambientOcclusion;
const reflectedLight = context.reflectedLight;
const irradianceLightMap = builder.context.irradianceLightMap;

reflectedLight.indirectDiffuse.assign( vec4( 0.0 ) );

// accumulation (baked indirect lighting only)

if ( irradianceLightMap ) {

reflectedLight.indirectDiffuse.addAssign( irradianceLightMap );

} else {

reflectedLight.indirectDiffuse.addAssign( vec4( 1.0, 1.0, 1.0, 0.0 ) );

}

// modulation

reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );

reflectedLight.indirectDiffuse.mulAssign( diffuseColor.rgb );

}

finish( context, stack, builder ) {
Expand Down
29 changes: 29 additions & 0 deletions src/nodes/lighting/BasicLightMapNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import LightingNode from './LightingNode.js';
import { addNodeClass } from '../core/Node.js';
import { float } from '../shadernode/ShaderNode.js';

class BasicLightMapNode extends LightingNode {

constructor( lightMapNode = null ) {

super();

this.lightMapNode = lightMapNode;

}

setup( builder ) {

// irradianceLightMap property is used in the indirectDiffuse() method of BasicLightingModel

const RECIPROCAL_PI = float( 1 / Math.PI );

builder.context.irradianceLightMap = this.lightMapNode.mul( RECIPROCAL_PI );

}

}

export default BasicLightMapNode;

addNodeClass( 'BasicLightMapNode', BasicLightMapNode );
16 changes: 16 additions & 0 deletions src/nodes/materials/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { materialLightMap } from '../accessors/MaterialNode.js';
import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial.js';
import BasicEnvironmentNode from '../lighting/BasicEnvironmentNode.js';
import BasicLightMapNode from '../lighting/BasicLightMapNode.js';
import BasicLightingModel from '../functions/BasicLightingModel.js';

const defaultValues = new MeshBasicMaterial();
Expand Down Expand Up @@ -30,6 +32,20 @@ class MeshBasicNodeMaterial extends NodeMaterial {

}

setupLightMap( builder ) {

let node = null;

if ( builder.material.lightMap ) {

node = new BasicLightMapNode( materialLightMap );

}

return node;

}

setupLightingModel() {

return new BasicLightingModel();
Expand Down
20 changes: 18 additions & 2 deletions src/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ class NodeMaterial extends Material {

}

setupLightMap( builder ) {

let node = null;

if ( builder.material.lightMap ) {

node = new IrradianceNode( materialLightMap );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we sould move this to StandardMeshNodeMaterial like setupEnvironment()?

Copy link
Collaborator Author

@Mugen87 Mugen87 Jul 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked this myself this as well but then I've realized when I do this setupLightMap() in NodeMaterial will be empty. The basic and standard materials share no common code in context of light maps like the setup of the environment map node in NodeMaterial.setupEnvironment(). Besides, we would have to implement setupLightMap() in phong, lambert and toon not just in MeshStandardNodeMaterial.

The light map handling in MeshBasicNodeMaterial is quite an exception and I thought it's more clear to completely overwrite the standard implementation from NodeMaterial .

That said, I'm not feeling strong about this issue. If you think it's more consistent to follow the approach of env maps, I'll create a setupLightMap() method for each material that supports light maps.


}

return node;

}

setupLights( builder ) {

const materialLightsNode = [];
Expand All @@ -365,9 +379,11 @@ class NodeMaterial extends Material {

}

if ( builder.material.lightMap ) {
const lightMapNode = this.setupLightMap( builder );

if ( lightMapNode && lightMapNode.isLightingNode ) {

materialLightsNode.push( new IrradianceNode( materialLightMap ) );
materialLightsNode.push( lightMapNode );

}

Expand Down