Skip to content

Commit

Permalink
MeshPhysicalMaterial: Promote ior to a material property (#22238)
Browse files Browse the repository at this point in the history
* Clean up

* Promote .ior to a material property
  • Loading branch information
WestLangley authored Aug 2, 2021
1 parent d7601e1 commit 1fc7869
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/webgl_furnace_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
color: 0xffffff,
envMap: radianceMap,
envMapIntensity: 1,
reflectivity: 0.5
ior: 1.5
} );

const mesh = new THREE.Mesh( geometry, material );
Expand Down
8 changes: 4 additions & 4 deletions examples/webgl_materials_physical_transmission.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
opacity: 1,
metalness: 0,
roughness: 0,
reflectivity: 0.5,
ior: 1.5,
thickness: 0.01,
specularIntensity: 1,
specularTint: 0xffffff,
Expand Down Expand Up @@ -86,7 +86,7 @@
color: params.color,
metalness: params.metalness,
roughness: params.roughness,
reflectivity: params.reflectivity,
ior: params.ior,
alphaMap: texture,
envMap: hdrEquirect,
envMapIntensity: params.envMapIntensity,
Expand Down Expand Up @@ -154,10 +154,10 @@

} );

gui.add( params, 'reflectivity', 0, 1, 0.01 )
gui.add( params, 'ior', 1, 2, 0.01 )
.onChange( function () {

material.reflectivity = params.reflectivity;
material.ior = params.ior;
render();

} );
Expand Down
14 changes: 7 additions & 7 deletions src/materials/MeshPhysicalMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import * as MathUtils from '../math/MathUtils.js';
* clearcoatNormalScale: <Vector2>,
* clearcoatNormalMap: new THREE.Texture( <Image> ),
*
* reflectivity: <float>,
* ior: <float>,
* reflectivity: <float>,
*
* sheen: <Color>,
*
Expand Down Expand Up @@ -54,17 +54,17 @@ class MeshPhysicalMaterial extends MeshStandardMaterial {
this.clearcoatNormalScale = new Vector2( 1, 1 );
this.clearcoatNormalMap = null;

this.reflectivity = 0.5; // maps to F0 = 0.04
this.ior = 1.5;

Object.defineProperty( this, 'ior', {
Object.defineProperty( this, 'reflectivity', {
get: function () {

return ( 1 + 0.4 * this.reflectivity ) / ( 1 - 0.4 * this.reflectivity );
return ( MathUtils.clamp( 2.5 * ( this.ior - 1 ) / ( this.ior + 1 ), 0, 1 ) );

},
set: function ( ior ) {
set: function ( reflectivity ) {

this.reflectivity = MathUtils.clamp( 2.5 * ( ior - 1 ) / ( ior + 1 ), 0, 1 );
this.ior = ( 1 + 0.4 * reflectivity ) / ( 1 - 0.4 * reflectivity );

}
} );
Expand Down Expand Up @@ -106,7 +106,7 @@ class MeshPhysicalMaterial extends MeshStandardMaterial {
this.clearcoatNormalMap = source.clearcoatNormalMap;
this.clearcoatNormalScale.copy( source.clearcoatNormalScale );

this.reflectivity = source.reflectivity;
this.ior = source.ior;

if ( source.sheen ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ material.specularRoughness = max( roughnessFactor, 0.0525 );// 0.0525 correspond
material.specularRoughness += geometryRoughness;
material.specularRoughness = min( material.specularRoughness, 1.0 );
#ifdef REFLECTIVITY
#ifdef IOR
#ifdef SPECULAR
Expand Down Expand Up @@ -38,7 +38,7 @@ material.specularRoughness = min( material.specularRoughness, 1.0 );
#endif
material.specularColor = mix( min( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ) * specularTintFactor, vec3( 1.0 ) ) * specularIntensityFactor, diffuseColor.rgb, metalnessFactor );
material.specularColor = mix( min( pow2( ( ior - 1.0 ) / ( ior + 1.0 ) ) * specularTintFactor, vec3( 1.0 ) ) * specularIntensityFactor, diffuseColor.rgb, metalnessFactor );
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default /* glsl */`
vec3 pos = vWorldPosition.xyz / vWorldPosition.w;
vec3 v = normalize( cameraPosition - pos );
vec3 n = inverseTransformDirection( normal, viewMatrix );
float ior = ( 1.0 + 0.4 * reflectivity ) / ( 1.0 - 0.4 * reflectivity );
vec3 transmission = transmissionFactor * getIBLVolumeRefraction(
n, v, roughnessFactor, material.diffuseColor, material.specularColor,
Expand Down
6 changes: 3 additions & 3 deletions src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default /* glsl */`
#define STANDARD
#ifdef PHYSICAL
#define REFLECTIVITY
#define IOR
#define CLEARCOAT
#define SPECULAR
#endif
Expand All @@ -20,8 +20,8 @@ uniform float opacity;
uniform vec3 attenuationTint;
#endif
#ifdef REFLECTIVITY
uniform float reflectivity;
#ifdef IOR
uniform float ior;
#endif
#ifdef SPECULAR
Expand Down
3 changes: 2 additions & 1 deletion src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const UniformsLib = {

envMap: { value: null },
flipEnvMap: { value: - 1 },
reflectivity: { value: 1.0 },
reflectivity: { value: 1.0 }, // basic, lambert, phong
ior: { value: 1.5 }, // standard, physical
refractionRatio: { value: 0.98 },
maxMipLevel: { value: 0 }

Expand Down
3 changes: 2 additions & 1 deletion src/renderers/webgl/WebGLMaterials.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function WebGLMaterials( properties ) {
uniforms.flipEnvMap.value = ( envMap.isCubeTexture && envMap.isRenderTargetTexture === false ) ? - 1 : 1;

uniforms.reflectivity.value = material.reflectivity;
uniforms.ior.value = material.ior;
uniforms.refractionRatio.value = material.refractionRatio;

const maxMipLevel = properties.get( envMap ).__maxMipLevel;
Expand Down Expand Up @@ -567,7 +568,7 @@ function WebGLMaterials( properties ) {

refreshUniformsStandard( uniforms, material );

uniforms.reflectivity.value = material.reflectivity; // also part of uniforms common
uniforms.ior.value = material.ior; // also part of uniforms common

uniforms.clearcoat.value = material.clearcoat;
uniforms.clearcoatRoughness.value = material.clearcoatRoughness;
Expand Down

0 comments on commit 1fc7869

Please sign in to comment.