Skip to content

Commit d4a7c78

Browse files
authored
Merge pull request #17163 from sunag/dev-energy-preservation
NodeMaterial: Added energy preservation flag
2 parents 5902347 + 7a6666b commit d4a7c78

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

examples/jsm/nodes/materials/nodes/StandardNode.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ function StandardNode() {
2020
this.roughness = new FloatNode( 0.5 );
2121
this.metalness = new FloatNode( 0.5 );
2222

23+
this.energyPreservation = true;
24+
2325
}
2426

2527
StandardNode.prototype = Object.create( Node.prototype );
@@ -32,6 +34,8 @@ StandardNode.prototype.build = function ( builder ) {
3234

3335
builder.define( this.clearCoat || this.clearCoatRoughness ? 'PHYSICAL' : 'STANDARD' );
3436

37+
if ( this.energyPreservation ) builder.define( 'ENERGY_PRESERVATION' );
38+
3539
builder.requires.lights = true;
3640

3741
builder.extensions.shaderTextureLOD = true;

src/materials/MeshStandardMaterial.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface MeshStandardMaterialParameters extends MaterialParameters {
2929
alphaMap?: Texture;
3030
envMap?: Texture;
3131
envMapIntensity?: number;
32+
energyPreservation: boolean;
3233
refractionRatio?: number;
3334
wireframe?: boolean;
3435
wireframeLinewidth?: number;
@@ -66,6 +67,7 @@ export class MeshStandardMaterial extends Material {
6667
alphaMap: Texture | null;
6768
envMap: Texture | null;
6869
envMapIntensity: number;
70+
energyPreservation: boolean;
6971
refractionRatio: number;
7072
wireframe: boolean;
7173
wireframeLinewidth: number;

src/materials/MeshStandardMaterial.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import { Color } from '../math/Color.js';
4444
* envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
4545
* envMapIntensity: <float>
4646
*
47+
* energyPreservation: <bool>,
48+
*
4749
* refractionRatio: <float>,
4850
*
4951
* wireframe: <boolean>,
@@ -99,6 +101,8 @@ function MeshStandardMaterial( parameters ) {
99101
this.envMap = null;
100102
this.envMapIntensity = 1.0;
101103

104+
this.energyPreservation = true;
105+
102106
this.refractionRatio = 0.98;
103107

104108
this.wireframe = false;
@@ -161,6 +165,8 @@ MeshStandardMaterial.prototype.copy = function ( source ) {
161165
this.envMap = source.envMap;
162166
this.envMapIntensity = source.envMapIntensity;
163167

168+
this.energyPreservation = source.energyPreservation;
169+
164170
this.refractionRatio = source.refractionRatio;
165171

166172
this.wireframe = source.wireframe;

src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricCo
9898
9999
// Defer to the IndirectSpecular function to compute
100100
// the indirectDiffuse if energy preservation is enabled.
101-
#ifndef ENVMAP_TYPE_CUBE_UV
101+
#ifndef ENERGY_PRESERVATION
102102
103103
reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
104104
@@ -120,7 +120,7 @@ void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradia
120120
121121
// Both indirect specular and diffuse light accumulate here
122122
// if energy preservation enabled, and PMREM provided.
123-
#if defined( ENVMAP_TYPE_CUBE_UV )
123+
#if defined( ENERGY_PRESERVATION )
124124
125125
vec3 singleScattering = vec3( 0.0 );
126126
vec3 multiScattering = vec3( 0.0 );

src/renderers/webgl/WebGLProgram.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ function WebGLProgram( renderer, extensions, code, material, shader, parameters,
527527

528528
parameters.physicallyCorrectLights ? '#define PHYSICALLY_CORRECT_LIGHTS' : '',
529529

530+
parameters.energyPreservation ? '#define ENERGY_PRESERVATION' : '',
531+
530532
parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
531533
parameters.logarithmicDepthBuffer && ( capabilities.isWebGL2 || extensions.get( 'EXT_frag_depth' ) ) ? '#define USE_LOGDEPTHBUF_EXT' : '',
532534

src/renderers/webgl/WebGLPrograms.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
205205
toneMapping: renderer.toneMapping,
206206
physicallyCorrectLights: renderer.physicallyCorrectLights,
207207

208+
energyPreservation: material.energyPreservation,
209+
208210
premultipliedAlpha: material.premultipliedAlpha,
209211

210212
alphaTest: material.alphaTest,

0 commit comments

Comments
 (0)