From 7a6666b8f42b6359c1cdb0f1efa4037c891da9e6 Mon Sep 17 00:00:00 2001 From: sunag Date: Fri, 2 Aug 2019 23:49:07 -0300 Subject: [PATCH] energy-preservation flag --- examples/jsm/nodes/materials/nodes/StandardNode.js | 4 ++++ src/materials/MeshStandardMaterial.d.ts | 2 ++ src/materials/MeshStandardMaterial.js | 6 ++++++ .../ShaderChunk/lights_physical_pars_fragment.glsl.js | 4 ++-- src/renderers/webgl/WebGLProgram.js | 2 ++ src/renderers/webgl/WebGLPrograms.js | 2 ++ 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/jsm/nodes/materials/nodes/StandardNode.js b/examples/jsm/nodes/materials/nodes/StandardNode.js index 380373c85c3a3a..3e1cf90f8b833c 100644 --- a/examples/jsm/nodes/materials/nodes/StandardNode.js +++ b/examples/jsm/nodes/materials/nodes/StandardNode.js @@ -20,6 +20,8 @@ function StandardNode() { this.roughness = new FloatNode( 0.5 ); this.metalness = new FloatNode( 0.5 ); + this.energyPreservation = true; + } StandardNode.prototype = Object.create( Node.prototype ); @@ -32,6 +34,8 @@ StandardNode.prototype.build = function ( builder ) { builder.define( this.clearCoat || this.clearCoatRoughness ? 'PHYSICAL' : 'STANDARD' ); + if ( this.energyPreservation ) builder.define( 'ENERGY_PRESERVATION' ); + builder.requires.lights = true; builder.extensions.shaderTextureLOD = true; diff --git a/src/materials/MeshStandardMaterial.d.ts b/src/materials/MeshStandardMaterial.d.ts index a9c1eb1f4ce0d4..f7c01ec0182772 100644 --- a/src/materials/MeshStandardMaterial.d.ts +++ b/src/materials/MeshStandardMaterial.d.ts @@ -29,6 +29,7 @@ export interface MeshStandardMaterialParameters extends MaterialParameters { alphaMap?: Texture; envMap?: Texture; envMapIntensity?: number; + energyPreservation: boolean; refractionRatio?: number; wireframe?: boolean; wireframeLinewidth?: number; @@ -66,6 +67,7 @@ export class MeshStandardMaterial extends Material { alphaMap: Texture | null; envMap: Texture | null; envMapIntensity: number; + energyPreservation: boolean; refractionRatio: number; wireframe: boolean; wireframeLinewidth: number; diff --git a/src/materials/MeshStandardMaterial.js b/src/materials/MeshStandardMaterial.js index cbbe32dc34d1cf..2fe5e314851854 100644 --- a/src/materials/MeshStandardMaterial.js +++ b/src/materials/MeshStandardMaterial.js @@ -44,6 +44,8 @@ import { Color } from '../math/Color.js'; * envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ), * envMapIntensity: * + * energyPreservation: , + * * refractionRatio: , * * wireframe: , @@ -99,6 +101,8 @@ function MeshStandardMaterial( parameters ) { this.envMap = null; this.envMapIntensity = 1.0; + this.energyPreservation = true; + this.refractionRatio = 0.98; this.wireframe = false; @@ -161,6 +165,8 @@ MeshStandardMaterial.prototype.copy = function ( source ) { this.envMap = source.envMap; this.envMapIntensity = source.envMapIntensity; + this.energyPreservation = source.energyPreservation; + this.refractionRatio = source.refractionRatio; this.wireframe = source.wireframe; diff --git a/src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js index ba2bed2623f978..0be2ea59e8c762 100644 --- a/src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js +++ b/src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js @@ -98,7 +98,7 @@ void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricCo // Defer to the IndirectSpecular function to compute // the indirectDiffuse if energy preservation is enabled. - #ifndef ENVMAP_TYPE_CUBE_UV + #ifndef ENERGY_PRESERVATION reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); @@ -120,7 +120,7 @@ void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradia // Both indirect specular and diffuse light accumulate here // if energy preservation enabled, and PMREM provided. - #if defined( ENVMAP_TYPE_CUBE_UV ) + #if defined( ENERGY_PRESERVATION ) vec3 singleScattering = vec3( 0.0 ); vec3 multiScattering = vec3( 0.0 ); diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index d8ae626dc67b45..325dbecbc07b89 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -522,6 +522,8 @@ function WebGLProgram( renderer, extensions, code, material, shader, parameters, parameters.physicallyCorrectLights ? '#define PHYSICALLY_CORRECT_LIGHTS' : '', + parameters.energyPreservation ? '#define ENERGY_PRESERVATION' : '', + parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '', parameters.logarithmicDepthBuffer && ( capabilities.isWebGL2 || extensions.get( 'EXT_frag_depth' ) ) ? '#define USE_LOGDEPTHBUF_EXT' : '', diff --git a/src/renderers/webgl/WebGLPrograms.js b/src/renderers/webgl/WebGLPrograms.js index d790e453a53c04..9cbd7ccf60ef4c 100644 --- a/src/renderers/webgl/WebGLPrograms.js +++ b/src/renderers/webgl/WebGLPrograms.js @@ -201,6 +201,8 @@ function WebGLPrograms( renderer, extensions, capabilities ) { toneMapping: renderer.toneMapping, physicallyCorrectLights: renderer.physicallyCorrectLights, + energyPreservation: material.energyPreservation, + premultipliedAlpha: material.premultipliedAlpha, alphaTest: material.alphaTest,