Browse Source

MeshPhysicalMaterial: Promote ior to a material property (#22238)

* Clean up

* Promote .ior to a material property
WestLangley 4 years ago
parent
commit
1fc786900a

+ 1 - 1
examples/webgl_furnace_test.html

@@ -91,7 +91,7 @@
 							color: 0xffffff,
 							envMap: radianceMap,
 							envMapIntensity: 1,
-							reflectivity: 0.5
+							ior: 1.5
 						} );
 
 						const mesh = new THREE.Mesh( geometry, material );

+ 4 - 4
examples/webgl_materials_physical_transmission.html

@@ -25,7 +25,7 @@
 				opacity: 1,
 				metalness: 0,
 				roughness: 0,
-				reflectivity: 0.5,
+				ior: 1.5,
 				thickness: 0.01,
 				specularIntensity: 1,
 				specularTint: 0xffffff,
@@ -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,
@@ -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();
 
 					} );

+ 7 - 7
src/materials/MeshPhysicalMaterial.js

@@ -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>,
  *
@@ -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 );
 
 			}
 		} );
@@ -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 ) {
 

+ 2 - 2
src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js

@@ -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
 
@@ -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
 

+ 0 - 1
src/renderers/shaders/ShaderChunk/transmission_fragment.glsl.js

@@ -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,

+ 3 - 3
src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js

@@ -2,7 +2,7 @@ export default /* glsl */`
 #define STANDARD
 
 #ifdef PHYSICAL
-	#define REFLECTIVITY
+	#define IOR
 	#define CLEARCOAT
 	#define SPECULAR
 #endif
@@ -20,8 +20,8 @@ uniform float opacity;
 	uniform vec3 attenuationTint;
 #endif
 
-#ifdef REFLECTIVITY
-	uniform float reflectivity;
+#ifdef IOR
+	uniform float ior;
 #endif
 
 #ifdef SPECULAR

+ 2 - 1
src/renderers/shaders/UniformsLib.js

@@ -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 }
 

+ 2 - 1
src/renderers/webgl/WebGLMaterials.js

@@ -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;
@@ -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;