Browse Source

Improved PBR (demo included)

David Catuhe 9 years ago
parent
commit
6e3564892c

File diff suppressed because it is too large
+ 34 - 21
materialsLibrary/dist/babylon.pbrMaterial.js


File diff suppressed because it is too large
+ 3 - 3
materialsLibrary/dist/babylon.pbrMaterial.min.js


+ 36 - 21
materialsLibrary/materials/pbr/babylon.pbrMaterial.ts

@@ -81,6 +81,7 @@ module BABYLON {
         public CAMERATONEMAP = false;
         public CAMERACONTRAST = false;
         public OVERLOADEDVALUES = false;
+        public OVERLOADEDSHADOWVALUES = false;
 
         constructor() {
             super();
@@ -95,9 +96,9 @@ module BABYLON {
         public environmentIntensity: number = 1.0;
         private _lightingInfos: Vector4 = new Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, 0.0);
 
-        public shadowIntensity: number = 1.0;
-        public shadeIntensity: number = 1.0;
-        private _shadowInfos: Vector4 = new Vector4(this.shadowIntensity, this.shadeIntensity, 0.0, 0.0);
+        public overloadedShadowIntensity: number = 1.0;
+        public overloadedShadeIntensity: number = 1.0;
+        private _overloadedShadowInfos: Vector4 = new Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
 
         public cameraExposure: number = 1.0;
         public cameraContrast: number = 1.0;
@@ -108,14 +109,17 @@ module BABYLON {
         public overloadedSpecularIntensity: number = 0.0;
         public overloadedEmissiveIntensity: number = 0.0;
         private _overloadedIntensity: Vector4 = new Vector4(this.overloadedAmbientIntensity, this.overloadedDiffuseIntensity, this.overloadedSpecularIntensity, this.overloadedEmissiveIntensity);
+
         public overloadedAmbient: Color3 = BABYLON.Color3.White();
         public overloadedDiffuse: Color3 = BABYLON.Color3.White();
         public overloadedSpecular: Color3 = BABYLON.Color3.White();
         public overloadedEmissive: Color3 = BABYLON.Color3.White();
+        public overloadedReflection: Color3 = BABYLON.Color3.White();
 
-        public overloadedSmoothness: number = 0.0;
-        public overloadedSmoothnessIntensity: number = 0.0;
-        private _overloadedSmoothness: Vector3 = new Vector3(this.overloadedSmoothness, this.overloadedSmoothnessIntensity, 0.0);
+        public overloadedGlossiness: number = 0.0;
+        public overloadedGlossinessIntensity: number = 0.0;
+        public overloadedReflectionIntensity: number = 0.0;
+        private _overloadedGlossiness: Vector3 = new Vector3(this.overloadedGlossiness, this.overloadedGlossinessIntensity, this.overloadedReflectionIntensity);
        
         public disableBumpMap: boolean = false;
 
@@ -533,11 +537,17 @@ module BABYLON {
                 this._defines.CAMERATONEMAP = true;
             }
 
-            if (this.overloadedSmoothnessIntensity > 0 ||
+            if (this.overloadedShadeIntensity != 1 ||
+                this.overloadedShadowIntensity != 1) {
+                this._defines.OVERLOADEDSHADOWVALUES = true;
+            }
+
+            if (this.overloadedGlossinessIntensity > 0 ||
                 this.overloadedEmissiveIntensity > 0 ||
                 this.overloadedSpecularIntensity > 0 ||
                 this.overloadedDiffuseIntensity > 0 ||
-                this.overloadedAmbientIntensity > 0) {
+                this.overloadedAmbientIntensity > 0 ||
+                this.overloadedReflectionIntensity > 0) {
                 this._defines.OVERLOADEDVALUES = true;
             }
 
@@ -740,7 +750,7 @@ module BABYLON {
                         "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix",
                         "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
                         "opacityParts", "emissiveLeftColor", "emissiveRightColor",
-                        "vLightingIntensity", "vShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedDiffuse", "vOverloadedSpecular", "vOverloadedEmissive", "vOverloadedSmoothness",
+                        "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedDiffuse", "vOverloadedReflection", "vOverloadedSpecular", "vOverloadedEmissive", "vOverloadedGlossiness",
                         "logarithmicDepthConstant"
                     ],
                     ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler",
@@ -928,9 +938,9 @@ module BABYLON {
             this._lightingInfos.z = this.environmentIntensity;
             this._effect.setVector4("vLightingIntensity", this._lightingInfos);
 
-            this._shadowInfos.x = this.shadowIntensity;
-            this._shadowInfos.y = this.shadeIntensity;
-            this._effect.setVector4("vShadowIntensity", this._shadowInfos);
+            this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
+            this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
+            this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
 
             this._cameraInfos.x = this.cameraExposure;
             this._cameraInfos.y = this.cameraContrast;
@@ -950,10 +960,13 @@ module BABYLON {
             this._effect.setColor3("vOverloadedSpecular", this._tempColor);
             this.overloadedEmissive.toLinearSpaceToRef(this._tempColor);
             this._effect.setColor3("vOverloadedEmissive", this._tempColor);
+            this.overloadedReflection.toLinearSpaceToRef(this._tempColor);
+            this._effect.setColor3("vOverloadedReflection", this._tempColor);
 
-            this._overloadedSmoothness.x = this.overloadedSmoothness;
-            this._overloadedSmoothness.y = this.overloadedSmoothnessIntensity;
-            this._effect.setVector3("vOverloadedSmoothness", this._overloadedSmoothness);
+            this._overloadedGlossiness.x = this.overloadedGlossiness;
+            this._overloadedGlossiness.y = this.overloadedGlossinessIntensity;
+            this._overloadedGlossiness.z = this.overloadedReflectionIntensity;
+            this._effect.setVector3("vOverloadedGlossiness", this._overloadedGlossiness);
 
             // Log. depth
             if (this._defines.LOGARITHMICDEPTH) {
@@ -1041,11 +1054,11 @@ module BABYLON {
             newPBRMaterial.emissiveIntensity = this.emissiveIntensity;
             newPBRMaterial.environmentIntensity = this.environmentIntensity;
         
-            newPBRMaterial.shadowIntensity = this.shadowIntensity;
-            newPBRMaterial.shadeIntensity = this.shadeIntensity;
-        
             newPBRMaterial.cameraExposure = this.cameraExposure;
             newPBRMaterial.cameraContrast = this.cameraContrast;
+
+            newPBRMaterial.overloadedShadowIntensity = this.overloadedShadowIntensity;
+            newPBRMaterial.overloadedShadeIntensity = this.overloadedShadeIntensity;
         
             newPBRMaterial.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
             newPBRMaterial.overloadedDiffuseIntensity = this.overloadedDiffuseIntensity;
@@ -1055,9 +1068,11 @@ module BABYLON {
             newPBRMaterial.overloadedDiffuse = this.overloadedDiffuse;
             newPBRMaterial.overloadedSpecular = this.overloadedSpecular;
             newPBRMaterial.overloadedEmissive = this.overloadedEmissive;
+            newPBRMaterial.overloadedReflection = this.overloadedReflection;
 
-            newPBRMaterial.overloadedSmoothness = this.overloadedSmoothness;
-            newPBRMaterial.overloadedSmoothnessIntensity = this.overloadedSmoothnessIntensity;
+            newPBRMaterial.overloadedGlossiness = this.overloadedGlossiness;
+            newPBRMaterial.overloadedGlossinessIntensity = this.overloadedGlossinessIntensity;
+            newPBRMaterial.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
         
             newPBRMaterial.disableBumpMap = this.disableBumpMap;
 
@@ -1105,4 +1120,4 @@ module BABYLON {
             return newPBRMaterial;
         }
     }
-} 
+}

+ 53 - 31
materialsLibrary/materials/pbr/legacypbr.fragment.fx

@@ -2,15 +2,15 @@
 
 // Constants
 #define RECIPROCAL_PI2 0.15915494
+#define FRESNEL_MAXIMUM_ON_ROUGH 0.25
 
 uniform vec3 vEyePosition;
 uniform vec3 vAmbientColor;
 uniform vec4 vDiffuseColor;
+uniform vec3 vReflectionColor;
 
 // CUSTOM CONTROLS
 uniform vec4 vLightingIntensity;
-uniform vec4 vShadowIntensity;
-uniform vec3 vReflectionColor;
 uniform vec4 vCameraInfos;
 
 #ifdef OVERLOADEDVALUES
@@ -19,12 +19,16 @@ uniform vec3 vOverloadedAmbient;
 uniform vec3 vOverloadedDiffuse;
 uniform vec3 vOverloadedSpecular;
 uniform vec3 vOverloadedEmissive;
-uniform vec3 vOverloadedSmoothness;
+uniform vec3 vOverloadedReflection;
+uniform vec3 vOverloadedGlossiness;
+#endif
+
+#ifdef OVERLOADEDSHADOWVALUES
+uniform vec4 vOverloadedShadowIntensity;
 #endif
 
 // PBR CUSTOM CONSTANTS
 const float kPi = 3.1415926535897932384626433832795;
-#define FRESNEL_MAXIMUM_ON_ROUGH 0.25;
 
 // PBR HELPER METHODS
 float Square(float value)
@@ -463,14 +467,15 @@ void main(void) {
     baseColor.rgb *= vDiffuseInfos.y;
 #endif
 
-#ifdef OVERLOADEDVALUES
-    baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
-#endif
-
 #ifdef VERTEXCOLOR
     baseColor.rgb *= vColor.rgb;
 #endif
 
+#ifdef OVERLOADEDVALUES
+    baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
+    diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
+#endif
+
     // Bump
 #ifdef NORMAL
     vec3 normalW = normalize(vNormalW);
@@ -506,19 +511,19 @@ void main(void) {
         #endif
 
         #ifdef GLOSSINESSFROMSPECULARMAP
-                glossiness = specularMapColor.a;
+            glossiness = specularMapColor.a;
         #else
-                glossiness = computeDefaultGlossiness(glossiness, specularColor);
+            glossiness = computeDefaultGlossiness(glossiness, specularColor);
         #endif
     #endif
 
     #ifdef OVERLOADEDVALUES
-            glossiness = mix(glossiness, vOverloadedSmoothness.x, vOverloadedSmoothness.y);
+        glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);
     #endif
 #else
     float glossiness = 0.;
     #ifdef OVERLOADEDVALUES
-            glossiness = mix(glossiness, vOverloadedSmoothness.x, vOverloadedSmoothness.y);
+        glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);
     #endif
 
     vec3 specularColor = vec3(0., 0., 0);
@@ -542,8 +547,9 @@ void main(void) {
 
     // Lighting
     vec3 diffuseBase = vec3(0., 0., 0.);
-#ifdef OVERLOADEDVALUES
-    vec3 shadowedOnly = vOverloadedDiffuse;
+
+#ifdef OVERLOADEDSHADOWVALUES
+    vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);
 #endif
 
 #ifdef SPECULARTERM
@@ -567,8 +573,8 @@ void main(void) {
 
     shadow = 1.;
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -592,8 +598,8 @@ void main(void) {
 
     shadow = 1.;
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -617,8 +623,8 @@ void main(void) {
 
     shadow = 1.;
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -642,8 +648,8 @@ void main(void) {
 
     shadow = 1.;
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -664,6 +670,11 @@ vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
 vec3 specularEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(glossiness));
 reflectionColor *= specularEnvironmentReflectanceViewer;
 
+#ifdef OVERLOADEDVALUES
+    ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);
+    reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);
+#endif
+
 #ifdef OPACITY
     vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
 
@@ -685,22 +696,37 @@ reflectionColor *= specularEnvironmentReflectanceViewer;
 #ifdef EMISSIVE
     vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
     emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;
-    #ifdef OVERLOADEDVALUES
-        emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
-    #endif
+#endif
+
+#ifdef OVERLOADEDVALUES
+    emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
 #endif
 
     // Composition
 #ifdef EMISSIVEASILLUMINATION
     vec3 finalDiffuse = max(diffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+
+    #ifdef OVERLOADEDSHADOWVALUES
+        shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+    #endif
 #else
     #ifdef LINKEMISSIVEWITHDIFFUSE
-            vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+        vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+        #ifdef OVERLOADEDSHADOWVALUES
+                shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+        #endif
     #else
-            vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
+        vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
+        #ifdef OVERLOADEDSHADOWVALUES
+            shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
+        #endif
     #endif
 #endif
 
+#ifdef OVERLOADEDSHADOWVALUES
+      finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));
+#endif
+
 // diffuse lighting from environment 0.2 replaces Harmonic...
 // Ambient Reflection already includes the environment intensity.
 finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
@@ -735,9 +761,5 @@ finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
     color = contrasts(color);
 #endif
 
-#ifdef OVERLOADEDVALUES
-    color.rgb = mix(color.rgb, shadowedOnly, (1.0 - vShadowIntensity.y));
-#endif
-
     gl_FragColor = color;
 }

+ 82 - 35
materialsLibrary/materials/pbr/pbr.fragment.fx

@@ -19,7 +19,6 @@ uniform vec4 vDiffuseColor;
 
 // CUSTOM CONTROLS
 uniform vec4 vLightingIntensity;
-uniform vec4 vShadowIntensity;
 uniform vec4 vCameraInfos;
 
 #ifdef OVERLOADEDVALUES
@@ -28,7 +27,12 @@ uniform vec4 vCameraInfos;
     uniform vec3 vOverloadedDiffuse;
     uniform vec3 vOverloadedSpecular;
     uniform vec3 vOverloadedEmissive;
-    uniform vec3 vOverloadedSmoothness;
+    uniform vec3 vOverloadedReflection;
+    uniform vec3 vOverloadedGlossiness;
+#endif
+
+#ifdef OVERLOADEDSHADOWVALUES
+    uniform vec4 vOverloadedShadowIntensity;
 #endif
 
 // PBR CUSTOM CONSTANTS
@@ -447,7 +451,11 @@ float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float dar
 
     if (depth > shadow)
     {
-        return mix(1.0, darkness, vShadowIntensity.x);
+#ifdef OVERLOADEDSHADOWVALUES
+        return mix(1.0, darkness, vOverloadedShadowIntensity.x);
+#else
+        return darkness;
+#endif
     }
     return 1.0;
 }
@@ -478,7 +486,11 @@ float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, fl
     if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * diskScale)) < biasedDepth) visibility -= 0.25;
     if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * diskScale)) < biasedDepth) visibility -= 0.25;
 
-    return  min(1.0, mix(1.0, visibility + darkness, vShadowIntensity.x));
+#ifdef OVERLOADEDSHADOWVALUES
+    return  min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
+#else
+    return  min(1.0, visibility + darkness);
+#endif
 }
 #endif
 
@@ -498,7 +510,11 @@ float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float dark
 
     if (depth.z > shadow)
     {
-        return mix(1.0, darkness, vShadowIntensity.x);
+#ifdef OVERLOADEDSHADOWVALUES
+        return mix(1.0, darkness, vOverloadedShadowIntensity.x);
+#else
+        return darkness;
+#endif
     }
     return 1.;
 }
@@ -530,7 +546,11 @@ float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, flo
     if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
     if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
 
-    return  min(1.0, mix(1.0, visibility + darkness, vShadowIntensity.x));
+#ifdef OVERLOADEDSHADOWVALUES
+    return  min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
+#else
+    return  min(1.0, visibility + darkness);
+#endif
 }
 
 // Thanks to http://devmaster.net/
@@ -567,7 +587,11 @@ float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, flo
     vec4 texel = texture2D(shadowSampler, uv);
 
     vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
-    return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vShadowIntensity.x));
+#ifdef OVERLOADEDSHADOWVALUES
+    return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vOverloadedShadowIntensity.x));
+#else
+    return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
+#endif
 }
 #endif
 
@@ -797,14 +821,15 @@ void main(void) {
     baseColor.rgb *= vDiffuseInfos.y;
 #endif
 
-#ifdef OVERLOADEDVALUES
-    baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
-#endif
-
 #ifdef VERTEXCOLOR
     baseColor.rgb *= vColor.rgb;
 #endif
 
+#ifdef OVERLOADEDVALUES
+    baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
+    diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
+#endif
+
     // Bump
 #ifdef NORMAL
     vec3 normalW = normalize(vNormalW);
@@ -853,12 +878,12 @@ void main(void) {
     #endif
 
     #ifdef OVERLOADEDVALUES
-        glossiness = mix(glossiness, vOverloadedSmoothness.x, vOverloadedSmoothness.y);
+        glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);
     #endif
 #else
     float glossiness = 0.;
     #ifdef OVERLOADEDVALUES
-        glossiness = mix(glossiness, vOverloadedSmoothness.x, vOverloadedSmoothness.y);
+        glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);
     #endif
     
     vec3 specularColor = vec3(0., 0., 0);
@@ -882,8 +907,9 @@ void main(void) {
 
     // Lighting
     vec3 diffuseBase = vec3(0., 0., 0.);
-#ifdef OVERLOADEDVALUES
-    vec3 shadowedOnly = vOverloadedDiffuse;
+    
+#ifdef OVERLOADEDSHADOWVALUES
+    vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);
 #endif
 
 #ifdef SPECULARTERM
@@ -926,8 +952,8 @@ void main(void) {
     shadow = 1.;
 #endif
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -971,8 +997,8 @@ void main(void) {
 #endif
 
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -1016,8 +1042,8 @@ void main(void) {
 #endif
 
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -1061,8 +1087,8 @@ void main(void) {
 #endif
 
     diffuseBase += info.diffuse * shadow;
-#ifdef OVERLOADEDVALUES
-    shadowedOnly *= shadow;
+#ifdef OVERLOADEDSHADOWVALUES
+    shadowedOnlyDiffuseBase *= shadow;
 #endif
 
 #ifdef SPECULARTERM
@@ -1107,6 +1133,11 @@ vec3 ambientReflectionColor = vReflectionColor.rgb;
     #endif
 #endif
 
+#ifdef OVERLOADEDVALUES
+    ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);
+    reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);
+#endif
+
 reflectionColor *= vLightingIntensity.z;
 ambientReflectionColor *= vLightingIntensity.z;
 
@@ -1143,10 +1174,10 @@ reflectionColor *= specularEnvironmentReflectanceViewer;
 #ifdef EMISSIVE
     vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
     emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;
+#endif
 
-    #ifdef OVERLOADEDVALUES
-        emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
-    #endif
+#ifdef OVERLOADEDVALUES
+    emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
 #endif
 
 #ifdef EMISSIVEFRESNEL
@@ -1158,14 +1189,30 @@ reflectionColor *= specularEnvironmentReflectanceViewer;
     // Composition
 #ifdef EMISSIVEASILLUMINATION
     vec3 finalDiffuse = max(diffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+    
+    #ifdef OVERLOADEDSHADOWVALUES
+        shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+    #endif
 #else
     #ifdef LINKEMISSIVEWITHDIFFUSE
         vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+
+        #ifdef OVERLOADEDSHADOWVALUES
+            shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
+        #endif
     #else
         vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
+
+        #ifdef OVERLOADEDSHADOWVALUES
+            shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
+        #endif
     #endif
 #endif
 
+#ifdef OVERLOADEDSHADOWVALUES
+    finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));
+#endif
+
 // diffuse lighting from environment 0.2 replaces Harmonic...
 // Ambient Reflection already includes the environment intensity.
 finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
@@ -1176,6 +1223,10 @@ finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
     vec3 finalSpecular = vec3(0.0);
 #endif
 
+#ifdef OVERLOADEDSHADOWVALUES
+    finalSpecular = mix(finalSpecular, vec3(0.0), (1.0 - vOverloadedShadowIntensity.y));
+#endif
+
 #ifdef SPECULAROVERALPHA
     alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
 #endif
@@ -1191,11 +1242,11 @@ finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
 #ifdef LIGHTMAP
     vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
 
-#ifdef USELIGHTMAPASSHADOWMAP
-    color.rgb *= lightmapColor;
-#else
-    color.rgb += lightmapColor;
-#endif
+    #ifdef USELIGHTMAPASSHADOWMAP
+        color.rgb *= lightmapColor;
+    #else
+        color.rgb += lightmapColor;
+    #endif
 #endif
 
 #ifdef FOG
@@ -1215,10 +1266,6 @@ finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
     color = contrasts(color);
 #endif
 
-#ifdef OVERLOADEDVALUES
-    color.rgb = mix(color.rgb, shadowedOnly, (1.0 - vShadowIntensity.y));
-#endif
-
     // Normal Display.
     // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
 

+ 139 - 20
materialsLibrary/test/add/addpbr.js

@@ -3,57 +3,176 @@ window.preparePBR = function() {
 	pbr.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
 	pbr.diffuseTexture.uScale = 5;
 	pbr.diffuseTexture.vScale = 5;
-	pbr.specularColor = BABYLON.Color3.Gray();
-	pbr.glossiness = 0.5;
+	pbr.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);	
+	pbr.specularColor = new BABYLON.Color3(0.3, 0.3, 0.3);
+	pbr.glossiness = 0.9;
 	
-	pbr.reflectionFresnelParameters = new BABYLON.FresnelParameters();
-	pbr.useReflectionFresnelFromSpecular = true;
-	pbr.reflectionFresnelParameters.power = 1.0;
-	pbr.reflectionFresnelParameters.bias = 0.0;
-	
-	pbr.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
-					
-	registerRangeUI("pbr", "directIntensity", 0, 1, function(value) {
+	registerButtonUI("pbr", "Default", function() {
+		setRangeValues({
+		  "directIntensity": 1,
+		  "emissiveIntensity": 1,
+		  "environmentIntensity": 1,
+		  "ShadowIntensity": 1,
+		  "ShadeIntensity": 1,
+		  "cameraExposure": 1,
+		  "cameraContrast": 1,
+		  "glossiness": 0.9,
+		  "specularColorR": 0.3,
+		  "specularColorG": 0.3,
+		  "specularColorB": 0.3,
+		  "diffuseColorR": 1,
+		  "diffuseColorG": 1,
+		  "diffuseColorB": 1,
+		  "diffuseColorLevel": 0
+		});
+	});
+	registerButtonUI("pbr", "Rough Gold", function() {
+		setRangeValues({
+		  "directIntensity": 1.3439461727881254,
+		  "emissiveIntensity": 1,
+		  "environmentIntensity": 0.3685013699580344,
+		  "ShadowIntensity": 1,
+		  "ShadeIntensity": 1,
+		  "cameraExposure": 0.7153261887420668,
+		  "cameraContrast": 1.6474178892241538,
+		  "glossiness": 0.42269274789303946,
+		  "specularColorR": 1,
+		  "specularColorG": 0.8453854957860789,
+		  "specularColorB": 0.5093989525890475,
+		  "diffuseColorR": 0,
+		  "diffuseColorG": 0,
+		  "diffuseColorB": 0,
+		  "diffuseColorLevel": 1
+		});
+	});
+	registerButtonUI("pbr", "Plastic", function() {
+		setRangeValues({
+		  "directIntensity": 0.9971213540040931,
+		  "emissiveIntensity": 1,
+		  "environmentIntensity": 0.3685013699580344,
+		  "ShadowIntensity": 0.975444802830091,
+		  "ShadeIntensity": 0.8020323934380749,
+		  "cameraExposure": 0.7586792910900708,
+		  "cameraContrast": 1.5823882357021477,
+		  "glossiness": 0.8562237713730799,
+		  "specularColorR": 0.05,
+		  "specularColorG": 0.05,
+		  "specularColorB": 0.05,
+		  "diffuseColorR": 0.20592723615301922,
+		  "diffuseColorG": 0.942929976069088,
+		  "diffuseColorB": 1,
+		  "diffuseColorLevel": 1
+		});
+	});
+	registerButtonUI("pbr", "Shiny Copper", function() {
+		setRangeValues({
+		  "directIntensity": 1.2355634169181153,
+		  "emissiveIntensity": 0.910415149308085,
+		  "environmentIntensity": 0.21676551174002023,
+		  "ShadowIntensity": 1.018797905178095,
+		  "ShadeIntensity": 0.975444802830091,
+		  "cameraExposure": 1.0621510075260991,
+		  "cameraContrast": 1.0404744563520971,
+		  "glossiness": 0.888738598134083,
+		  "specularColorR": 0.98,
+		  "specularColorG": 0.78,
+		  "specularColorB": 0.706,
+		  "diffuseColorR": 0.1,
+		  "diffuseColorG": 0.1,
+		  "diffuseColorB": 0.1,
+		  "diffuseColorLevel": 1
+		});
+	});
+
+	registerRangeUI("pbr", "directIntensity", 0, 2, function(value) {
 		pbr.directIntensity = value;
 	}, function() {
 		return pbr.directIntensity;
 	});
 	
-	registerRangeUI("pbr", "emissiveIntensity", 0, 1, function(value) {
+	registerRangeUI("pbr", "emissiveIntensity", 0, 2, function(value) {
 		pbr.emissiveIntensity = value;
 	}, function() {
 		return pbr.emissiveIntensity;
 	});
 	
-	registerRangeUI("pbr", "environmentIntensity", 0, 1, function(value) {
+	registerRangeUI("pbr", "environmentIntensity", 0, 2, function(value) {
 		pbr.environmentIntensity = value;
 	}, function() {
 		return pbr.environmentIntensity;
 	});
 	
-	registerRangeUI("pbr", "shadowIntensity", 0, 1, function(value) {
-		pbr.shadowIntensity = value;
+	registerRangeUI("pbr", "ShadowIntensity", 0, 2, function(value) {
+		pbr.overloadedShadowIntensity = value;
 	}, function() {
-		return pbr.shadowIntensity;
+		return pbr.overloadedShadowIntensity;
 	});
 	
-	registerRangeUI("pbr", "shadeIntensity", 0, 1, function(value) {
-		pbr.shadeIntensity = value;
+	registerRangeUI("pbr", "ShadeIntensity", 0, 2, function(value) {
+		pbr.overloadedShadeIntensity = value;
 	}, function() {
-		return pbr.shadeIntensity;
+		return pbr.overloadedShadeIntensity;
 	});
 	
-	registerRangeUI("pbr", "cameraExposure", 0, 1, function(value) {
+	registerRangeUI("pbr", "cameraExposure", 0, 2, function(value) {
 		pbr.cameraExposure = value;
 	}, function() {
 		return pbr.cameraExposure;
 	});
+
+	registerRangeUI("pbr", "cameraContrast", 0, 2, function(value) {
+		pbr.cameraContrast = value;
+	}, function() {
+		return pbr.cameraContrast;
+	});
 	
 	registerRangeUI("pbr", "glossiness", 0, 1, function(value) {
 		pbr.glossiness = value;
 	}, function() {
 		return pbr.glossiness;
 	});
-		
+
+	registerRangeUI("pbr", "specularColorR", 0, 1, function(value) {
+		pbr.specularColor.r = value;
+	}, function() {
+		return pbr.specularColor.r;
+	});
+
+	registerRangeUI("pbr", "specularColorG", 0, 1, function(value) {
+		pbr.specularColor.g = value;
+	}, function() {
+		return pbr.specularColor.g;
+	});
+
+	registerRangeUI("pbr", "specularColorB", 0, 1, function(value) {
+		pbr.specularColor.b = value;
+	}, function() {
+		return pbr.specularColor.b;
+	});
+
+	registerRangeUI("pbr", "diffuseColorR", 0, 1, function(value) {
+		pbr.overloadedDiffuse.r = value;
+	}, function() {
+		return pbr.overloadedDiffuse.r;
+	});
+
+	registerRangeUI("pbr", "diffuseColorG", 0, 1, function(value) {
+		pbr.overloadedDiffuse.g = value;
+	}, function() {
+		return pbr.overloadedDiffuse.g;
+	});
+
+	registerRangeUI("pbr", "diffuseColorB", 0, 1, function(value) {
+		pbr.overloadedDiffuse.b = value;
+	}, function() {
+		return pbr.overloadedDiffuse.b;
+	});
+
+	registerRangeUI("pbr", "diffuseColorLevel", 0, 1, function(value) {
+		pbr.overloadedDiffuseIntensity = value;
+	}, function() {
+		return pbr.overloadedDiffuseIntensity;
+	});
+
 	return pbr;
 }

+ 45 - 6
materialsLibrary/test/index.js

@@ -26,18 +26,57 @@ window.registerRangeUI = function(material, name, minValue, maxValue, onChange,
 		minValue: minValue,
 		maxValue: maxValue,
 		onChange: onChange,
-		onSet: onSet
+		onSet: onSet,
+		type: "Range"
 	});
 }
 
-var setUi = function(ui) {
-	options[ui.name] = ui.onSet();
+window.setRangeValues = function(json) {
+	for (var key in json) {
+		if (json.hasOwnProperty(key)) {
+			setRangeValue(key, json[key]);
+		}
+	}
+}
+
+window.setRangeValue = function(name, value) {
+	if (!materialgui) {
+		return;
+	}
+	
+	var controllers = materialgui.__controllers;
+	for (var i = 0; i < controllers.length; i++) {
+		if (controllers[i].property == name) {
+			controllers[i].setValue(value);
+		}
+	}
+}
+
+window.registerButtonUI = function(material, name, onClick) {
+	if (!registeredUIs[material]) {
+		registeredUIs[material] = [];
+	}
 	
-	materialgui.add(options, ui.name, ui.minValue, ui.maxValue).onChange(function(value) {
-		ui.onChange(value);
+	registeredUIs[material].push({
+		name: name, 
+		onClick: onClick,
+		type: "Button"
 	});
 }
 
+var setUi = function(ui) {
+	if (ui.type == "Range") {
+		options[ui.name] = ui.onSet();
+		var test = materialgui.add(options, ui.name, ui.minValue, ui.maxValue).onChange(function(value) {
+			ui.onChange(value);
+		});
+	}
+	else if (ui.type == "Button") {
+		options[ui.name] = ui.onClick;
+		materialgui.add(options, ui.name);
+	}
+}
+
 window.enableMaterial = function(material) {
 	if (materialgui) {
 		materialgui.domElement.parentElement.removeChild(materialgui.domElement);	
@@ -50,6 +89,6 @@ window.enableMaterial = function(material) {
 			var ui = registeredUIs[material][index];
 			
 			setUi(ui);
-		}	
+		}
 	}
 }