Jelajahi Sumber

expose energyConserving as a material parameter on MeshPhongMaterial.

Ben Houston 9 tahun lalu
induk
melakukan
3f4804977b

+ 17 - 0
editor/js/Sidebar.Material.js

@@ -163,6 +163,16 @@ Sidebar.Material = function ( editor ) {
 
 	container.add( materialVertexColorsRow );
 
+	// energy conserving
+
+	var materialEnergyConservingRow = new UI.Panel();
+	var materialEnergyConserving = new UI.Checkbox( false ).onChange( update );
+
+	materialEnergyConservingRow.add( new UI.Text( 'EnergyConserving' ).setWidth( '90px' ) );
+	materialEnergyConservingRow.add( materialEnergyConserving );
+
+	container.add( materialEnergyConservingRow );
+
 	// skinning
 
 	var materialSkinningRow = new UI.Panel();
@@ -462,6 +472,12 @@ Sidebar.Material = function ( editor ) {
 
 			}
 
+			if ( material.energyConserving !== undefined ) {
+
+				material.energyConserving = materialEnergyConserving.getValue();
+
+			}
+
 			if ( material.map !== undefined ) {
 
 				var mapEnabled = materialMapEnabled.getValue() === true;
@@ -687,6 +703,7 @@ Sidebar.Material = function ( editor ) {
 			'vertexShader': materialProgramRow,
 			'vertexColors': materialVertexColorsRow,
 			'skinning': materialSkinningRow,
+			'energyConserving': materialEnergyConservingRow,
 			'map': materialMapRow,
 			'alphaMap': materialAlphaMapRow,
 			'bumpMap': materialBumpMapRow,

+ 6 - 0
src/materials/MeshPhongMaterial.js

@@ -11,6 +11,8 @@
  *
  *  map: new THREE.Texture( <Image> ),
  *
+ *  energyConverving: false
+ *
  *  lightMap: new THREE.Texture( <Image> ),
  *  lightMapIntensity: <float>
  *
@@ -71,6 +73,8 @@ THREE.MeshPhongMaterial = function ( parameters ) {
 
 	this.map = null;
 
+	this.energyConserving = false;
+
 	this.lightMap = null;
 	this.lightMapIntensity = 1.0;
 
@@ -133,6 +137,8 @@ THREE.MeshPhongMaterial.prototype.copy = function ( source ) {
 
 	this.map = source.map;
 
+	this.energyConserving = source.energyConserving;
+
 	this.lightMap = source.lightMap;
 	this.lightMapIntensity = source.lightMapIntensity;
 

+ 4 - 4
src/renderers/shaders/ShaderChunk/bsdfs.glsl

@@ -62,8 +62,8 @@ vec3 F_Schlick( const in vec3 F0, const in float dotLH ) {
 
 float G_SmithSchlick( float roughness2, float dotNL, float dotNV ) {
 
-	float termL = ( dotNL + sqrt( roughness2 + ( 1.0 - roughness2 ) * square( dotNL ) ) );
-	float termV = ( dotNV + sqrt( roughness2 + ( 1.0 - roughness2 ) * square( dotNV ) ) );
+	float termL = ( dotNL + sqrt( roughness2 + ( 1.0 - roughness2 ) * dotNL * dotNL ) );
+	float termV = ( dotNV + sqrt( roughness2 + ( 1.0 - roughness2 ) * dotNV * dotNV ) );
 	
 	return 1.0 / ( abs( termL * termV ) + 0.0000001 );
 
@@ -72,8 +72,8 @@ float G_SmithSchlick( float roughness2, float dotNL, float dotNV ) {
 float D_GGX( float roughness2, float dotNH ) {
 
 	// should 1/PI be in this distribution?
-	float denom = square( dotNH ) * ( roughness2 - 1.0 ) + 1.0;
-	return roughness2 / ( PI * square( denom ) + 0.0000001 );
+	float denom = dotNH * dotNH * ( roughness2 - 1.0 ) + 1.0;
+	return roughness2 / ( PI * denom * denom + 0.0000001 );
 
 }
 

+ 10 - 12
src/renderers/shaders/ShaderChunk/common.glsl

@@ -1,15 +1,13 @@
-const float PI = 3.14159;
-const float PI2 = 6.28318;
-const float RECIPROCAL_PI = 0.31830988618;
-const float RECIPROCAL_PI2 = 0.15915494;
-const float LOG2 = 1.442695;
-const float EPSILON = 1e-6;
-
-float	square( const in float a ) { return a*a; }
-float	saturate( const in float a ) { return clamp( a, 0.0, 1.0 ); }
-vec3	saturate( const in vec3 a ) { return clamp( a, 0.0, 1.0 ); }
-vec3	whiteCompliment( const in vec3 color ) { return 1.0 - saturate( color ); }
-float	luminance( const in vec3 color ) { return dot( color, vec3( 0.2125, 0.7154, 0.0721 ) ); }
+#define PI 3.14159
+#define PI2 6.28318
+#define RECIPROCAL_PI 0.31830988618
+#define RECIPROCAL_PI2 0.15915494
+#define LOG2 1.442695
+#define EPSILON 1e-6
+
+#define saturate(a) clamp( a, 0.0, 1.0 )
+#define whiteCompliment(a) ( 1.0 - saturate( a ) )
+
 float	average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
 
 

+ 0 - 96
src/renderers/shaders/ShaderChunk/lights.glsl

@@ -113,99 +113,3 @@ float calcLightAttenuation( float lightDistance, float cutoffDistance, float dec
 	}
 
 #endif
-
-
-#define MAX_LIGHT_PROBES 0
-
-#if MAX_LIGHT_PROBES > 0
-
-	struct ProbeLight {
-	  vec3 position;
-	  vec3 sh[9];
-	};
-
-	uniform LightProbes lightProbes[ MAX_LIGHT_PROBES ];
-
-	void getLightProbeIncidentLight( const in GeometricContext geometry, out IncidentLight incidentLight ) { 
-
-		// TODO: loop through all light probes and calculate their contributions
-		// based on an inverse distance weighting.	
-
-		incidentLight.color = vec3( 0.0 );
-		incidentLight.direction = geometry.normal;
-
-	}
-
-#endif
-
-
-#define MAX_AREA_LIGHTS 0 
-
-#if MAX_AREA_LIGHTS > 0
-
-	struct HemisphereLight {
-	  vec3 position;	// NOTE: top left of area light, not the center
-	  vec3 width;
-	  vec3 height;
-	  vec3 color;
-	  //sampler2D image;
-	  float distance;
-	  float decay;
-	};
-
-	uniform AreaLight areaLights[ MAX_AREA_LIGHTS ];
-
-	vec3 clampToAreaLight( const in mat3 worldToPlaneMat, const in vec3 lightPositionPlaneCoord, const in vec3 point ) {
-
-		// convert into "plane space" from world space
-		var pointPlaneCoord = worldToPlaneMat * point;
-
-		// clamp point plane coords to positive unit in plane space.
-		pointPlaneCoord.xy = clamp( positionPlaneCoord.xy - lightPositionPlaneCoord.xy, 0.0, 1.0 ) + lightPositionPlaneCoord.xy;
-		pointPlaneCoord.z = lightPositionPlaneOffset; // project onto plane in plane coordinate space
-
-		// convert out of "plane space" into world space
-		return positionPlaneCoord * worldToPlaneMat;
-
-	}
-
-	void getAreaIncidentLight( const in AreaLight areaLight, const in GeometricContext geometry, out IncidentLight diffuseIncidentLight, out IncidentLight specularIncidentLight ) {
-	
-		vec3 areaLightDirection = normalize( cross( widthDir, heightDir ) );
-		// NOTE: width and height are purposely not normalized, this is necessary because plane space is scaled based on width/height size.
-		mat3 worldToPlaneMat = mat3( areaLight.width, areaLight.height, areaLightDirection );
-		vec3 lightPositionPlaneCoord = worldToPlaneMat * areaLight.position;
-
-		vec3 clampedPlanePosition = clampToAreaLight( worldToPlaneMat, lightPositionPlaneCoord, geometry.position );
-
-		vec3 positionToLight = ( clampedPlanePosition - geometry.position );
-		float lightDistance = length( positionToLight );
-
-		diffuseIncidentLight.color = areaLight.color[ i ] * calcLightAttenuation( lightDistance, areaLight.distance, areaLight.decay ) * 0.01;
-		diffuseIncidentLight.direction = positionToLight / lightDistance;
-
-		vec3 reflectDir = reflect( geometry.viewDir, geometry.normal );
-		float specAngle = dot( reflectDir, direction );
-		
-		if( dot( geometry.position - areaLight.position, areaLightDirection ) >= 0.0 && specAngle > 0.0 ) {
-
-			lightPositionPlaneCoord = linePlaneIntersect( geometry.position, reflectDir, areaLight.position, areaLight.normal );
-			clampedPlanePosition = clampToAreaLight( worldToPlaneMat, lightPositionPlaneCoord, lightPositionPlaneCoord );
-
-			positionToLight = ( clampedPlanePosition - geometry.position );
-			lightDistance = length( positionToLight );
-
-			specularIncidentLight.color = areaLight.color[ i ] * calcLightAttenuation( lightDistance, areaLight.distance, areaLight.decay );
-			specularIncidentLight.direction = positionToLight / lightDistance;
-
-		}
-		else {
-
-			specularIncidentLight.color = vec3( 0.0 );
-			specularIncidentLight.direction = vec3( 1.0, 0.0, 0.0 );
-
-		}
-
-	}
-
-#endif

+ 4 - 0
src/renderers/webgl/WebGLProgram.js

@@ -249,6 +249,10 @@ THREE.WebGLProgram = ( function () {
 				parameters.alphaMap ? '#define USE_ALPHAMAP' : '',
 				parameters.vertexColors ? '#define USE_COLOR' : '',
 
+				parameters.energyConserving ? '#define ENERGY_PRESERVING_MONOCHROME' : '',
+
+
+
 				parameters.flatShading ? '#define FLAT_SHADED' : '',
 
 				parameters.skinning ? '#define USE_SKINNING' : '',

+ 2 - 0
src/renderers/webgl/WebGLPrograms.js

@@ -149,6 +149,8 @@ THREE.WebGLPrograms = function ( renderer, capabilities ) {
 			specularMap: !! material.specularMap,
 			alphaMap: !! material.alphaMap,
 
+			energyConserving: !! material.energyConserving,
+
 			combine: material.combine,
 
 			vertexColors: material.vertexColors,