Skip to content

Commit 5c6374b

Browse files
mrdoobclaude
andauthored
MeshPhongMaterial: Add support for scene.environment IBL. (#32795)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 77097b7 commit 5c6374b

File tree

10 files changed

+30
-12
lines changed

10 files changed

+30
-12
lines changed
-46 KB
Loading

examples/webxr_vr_sandbox.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
scene.add( torus );
7979

8080
const cylinderGeometry = new THREE.CylinderGeometry( 1, 1, 0.1, 50 );
81-
const cylinderMaterial = new THREE.MeshStandardMaterial();
81+
const cylinderMaterial = new THREE.MeshLambertMaterial();
8282
const cylinder = new THREE.Mesh( cylinderGeometry, cylinderMaterial );
8383
cylinder.position.z = - 2;
8484
scene.add( cylinder );
@@ -96,7 +96,7 @@
9696
scene.add( reflector );
9797

9898
const frameGeometry = new THREE.BoxGeometry( 2.1, 2.1, 0.1 );
99-
const frameMaterial = new THREE.MeshPhongMaterial();
99+
const frameMaterial = new THREE.MeshLambertMaterial( { color: 0x888888 } );
100100
const frame = new THREE.Mesh( frameGeometry, frameMaterial );
101101
frame.position.z = - 0.07;
102102
reflector.add( frame );

src/materials/MeshPhongMaterial.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ class MeshPhongMaterial extends Material {
284284
*/
285285
this.reflectivity = 1;
286286

287+
/**
288+
* Scales the effect of the environment map by multiplying its color.
289+
*
290+
* @type {number}
291+
* @default 1
292+
*/
293+
this.envMapIntensity = 1.0;
294+
287295
/**
288296
* The index of refraction (IOR) of air (approximately 1) divided by the
289297
* index of refraction of the material. It is used with environment mapping
@@ -392,6 +400,7 @@ class MeshPhongMaterial extends Material {
392400
this.envMapRotation.copy( source.envMapRotation );
393401
this.combine = source.combine;
394402
this.reflectivity = source.reflectivity;
403+
this.envMapIntensity = source.envMapIntensity;
395404
this.refractionRatio = source.refractionRatio;
396405

397406
this.wireframe = source.wireframe;

src/renderers/WebGLRenderer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,10 +2132,10 @@ class WebGLRenderer {
21322132

21332133
// always update environment and fog - changing these trigger an getProgram call, but it's possible that the program doesn't change
21342134

2135-
materialProperties.environment = ( material.isMeshStandardMaterial || material.isMeshLambertMaterial ) ? scene.environment : null;
2135+
materialProperties.environment = ( material.isMeshStandardMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial ) ? scene.environment : null;
21362136
materialProperties.fog = scene.fog;
21372137

2138-
const usePMREM = material.isMeshStandardMaterial || ( material.isMeshLambertMaterial && ! material.envMap );
2138+
const usePMREM = material.isMeshStandardMaterial || ( material.isMeshLambertMaterial && ! material.envMap ) || ( material.isMeshPhongMaterial && ! material.envMap );
21392139
materialProperties.envMap = environments.get( material.envMap || materialProperties.environment, usePMREM );
21402140
materialProperties.envMapRotation = ( materialProperties.environment !== null && material.envMap === null ) ? scene.environmentRotation : material.envMapRotation;
21412141

@@ -2267,9 +2267,9 @@ class WebGLRenderer {
22672267
textures.resetTextureUnits();
22682268

22692269
const fog = scene.fog;
2270-
const environment = ( material.isMeshStandardMaterial || material.isMeshLambertMaterial ) ? scene.environment : null;
2270+
const environment = ( material.isMeshStandardMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial ) ? scene.environment : null;
22712271
const colorSpace = ( _currentRenderTarget === null ) ? _this.outputColorSpace : ( _currentRenderTarget.isXRRenderTarget === true ? _currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace );
2272-
const usePMREM = material.isMeshStandardMaterial || ( material.isMeshLambertMaterial && ! material.envMap );
2272+
const usePMREM = material.isMeshStandardMaterial || ( material.isMeshLambertMaterial && ! material.envMap ) || ( material.isMeshPhongMaterial && ! material.envMap );
22732273
const envMap = environments.get( material.envMap || environment, usePMREM );
22742274
const vertexAlphas = material.vertexColors === true && !! geometry.attributes.color && geometry.attributes.color.itemSize === 4;
22752275
const vertexTangents = !! geometry.attributes.tangent && ( !! material.normalMap || material.anisotropy > 0 );
@@ -2595,7 +2595,7 @@ class WebGLRenderer {
25952595

25962596
}
25972597

2598-
if ( ( material.isMeshStandardMaterial || material.isMeshLambertMaterial ) && material.envMap === null && scene.environment !== null ) {
2598+
if ( ( material.isMeshStandardMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial ) && material.envMap === null && scene.environment !== null ) {
25992599

26002600
m_uniforms.envMapIntensity.value = scene.environmentIntensity;
26012601

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default /* glsl */`
22
#if defined( RE_IndirectDiffuse )
33
4-
#ifdef LAMBERT
4+
#if defined( LAMBERT ) || defined( PHONG )
55
66
irradiance += iblIrradiance;
77

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default /* glsl */`
1212
1313
#if defined( USE_ENVMAP ) && defined( ENVMAP_TYPE_CUBE_UV )
1414
15-
#if defined( STANDARD ) || defined( LAMBERT )
15+
#if defined( STANDARD ) || defined( LAMBERT ) || defined( PHONG )
1616
1717
iblIrradiance += getIBLIrradiance( geometryNormal );
1818

src/renderers/shaders/ShaderLib.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const ShaderLib = {
6666
{
6767
emissive: { value: /*@__PURE__*/ new Color( 0x000000 ) },
6868
specular: { value: /*@__PURE__*/ new Color( 0x111111 ) },
69-
shininess: { value: 30 }
69+
shininess: { value: 30 },
70+
envMapIntensity: { value: 1 }
7071
}
7172
] ),
7273

src/renderers/shaders/ShaderLib/meshphong.glsl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ uniform float opacity;
7070
#include <aomap_pars_fragment>
7171
#include <lightmap_pars_fragment>
7272
#include <emissivemap_pars_fragment>
73+
#include <cube_uv_reflection_fragment>
7374
#include <envmap_common_pars_fragment>
7475
#include <envmap_pars_fragment>
76+
#include <envmap_physical_pars_fragment>
7577
#include <fog_pars_fragment>
7678
#include <bsdfs>
7779
#include <lights_pars_begin>

src/renderers/webgl/WebGLMaterials.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ function WebGLMaterials( renderer, properties ) {
6363
refreshUniformsCommon( uniforms, material );
6464
refreshUniformsPhong( uniforms, material );
6565

66+
if ( material.envMap ) {
67+
68+
uniforms.envMapIntensity.value = material.envMapIntensity;
69+
70+
}
71+
6672
} else if ( material.isMeshStandardMaterial ) {
6773

6874
refreshUniformsCommon( uniforms, material );

src/renderers/webgl/WebGLPrograms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ function WebGLPrograms( renderer, environments, extensions, capabilities, bindin
5151

5252
const fog = scene.fog;
5353
const geometry = object.geometry;
54-
const environment = ( material.isMeshStandardMaterial || material.isMeshLambertMaterial ) ? scene.environment : null;
54+
const environment = ( material.isMeshStandardMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial ) ? scene.environment : null;
5555

56-
const usePMREM = material.isMeshStandardMaterial || ( material.isMeshLambertMaterial && ! material.envMap );
56+
const usePMREM = material.isMeshStandardMaterial || ( material.isMeshLambertMaterial && ! material.envMap ) || ( material.isMeshPhongMaterial && ! material.envMap );
5757
const envMap = environments.get( material.envMap || environment, usePMREM );
5858
const envMapCubeUVHeight = ( !! envMap ) && ( envMap.mapping === CubeUVReflectionMapping ) ? envMap.image.height : null;
5959

0 commit comments

Comments
 (0)