Browse Source

Merge pull request #976 from sebavan/PBRUpdate

Pbr update
David Catuhe 9 years ago
parent
commit
c9364c0212

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


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


+ 10 - 8
materialsLibrary/dist/dts/babylon.pbrMaterial.d.ts

@@ -41,22 +41,24 @@ declare module BABYLON {
         albedoColor: Color3;
         reflectivityColor: Color3;
         reflectionColor: Color3;
-        microSurface: number;
         emissiveColor: Color3;
-        useAlphaFromAlbedoTexture: boolean;
-        useEmissiveAsIllumination: boolean;
-        linkEmissiveWithAlbedo: boolean;
-        useSpecularOverAlpha: boolean;
-        disableLighting: boolean;
+        microSurface: number;
         indexOfRefraction: number;
         invertRefractionY: boolean;
-        linkRefractionWithTransparency: boolean;
-        useLightmapAsShadowmap: boolean;
         opacityFresnelParameters: FresnelParameters;
         emissiveFresnelParameters: FresnelParameters;
+        linkRefractionWithTransparency: boolean;
+        linkEmissiveWithAlbedo: boolean;
+        useLightmapAsShadowmap: boolean;
+        useEmissiveAsIllumination: boolean;
+        useAlphaFromAlbedoTexture: boolean;
+        useSpecularOverAlpha: boolean;
         useMicroSurfaceFromReflectivityMapAlpha: boolean;
         useAutoMicroSurfaceFromReflectivityMap: boolean;
         useScalarInLinearSpace: boolean;
+        usePhysicalLightFalloff: boolean;
+        useRadianceOverAlpha: boolean;
+        disableLighting: boolean;
         private _renderTargets;
         private _worldViewProjectionMatrix;
         private _globalAmbientColor;

+ 28 - 10
materialsLibrary/materials/pbr/babylon.pbrMaterial.ts

@@ -89,6 +89,8 @@ module BABYLON {
         public LINKREFRACTIONTOTRANSPARENCY = false;
         public REFRACTIONMAPINLINEARSPACE = false;
         public LODBASEDMICROSFURACE = false;
+        public USEPHYSICALLIGHTFALLOFF = false;
+        public RADIANCEOVERALPHA = false;
 
         constructor() {
             super();
@@ -148,26 +150,28 @@ module BABYLON {
         public albedoColor = new Color3(1, 1, 1);
         public reflectivityColor = new Color3(1, 1, 1);
         public reflectionColor = new Color3(0.5, 0.5, 0.5);
-        public microSurface = 0.5;
         public emissiveColor = new Color3(0, 0, 0);
-        public useAlphaFromAlbedoTexture = false;
-        public useEmissiveAsIllumination = false;
-        public linkEmissiveWithAlbedo = false;
-        public useSpecularOverAlpha = true;
-        public disableLighting = false;
-        
+        public microSurface = 0.5;
         public indexOfRefraction = 0.66;
         public invertRefractionY = false;
-        public linkRefractionWithTransparency = false;
-
-        public useLightmapAsShadowmap = false;
 
         public opacityFresnelParameters: FresnelParameters;
         public emissiveFresnelParameters: FresnelParameters;
 
+        public linkRefractionWithTransparency = false;
+        public linkEmissiveWithAlbedo = false;
+        
+        public useLightmapAsShadowmap = false;
+        public useEmissiveAsIllumination = false;
+        public useAlphaFromAlbedoTexture = false;
+        public useSpecularOverAlpha = true;
         public useMicroSurfaceFromReflectivityMapAlpha = false;
         public useAutoMicroSurfaceFromReflectivityMap = false;
         public useScalarInLinearSpace = false;
+        public usePhysicalLightFalloff = true;
+        public useRadianceOverAlpha = true;
+        
+        public disableLighting = false;
 
         private _renderTargets = new SmartArray<RenderTargetTexture>(16);
         private _worldViewProjectionMatrix = Matrix.Zero();
@@ -561,6 +565,14 @@ module BABYLON {
             if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
                 this._defines.SPECULAROVERALPHA = true;
             }
+            
+            if (this.usePhysicalLightFalloff) {
+                this._defines.USEPHYSICALLIGHTFALLOFF = true;
+            }
+            
+            if (this.useRadianceOverAlpha) {
+                this._defines.RADIANCEOVERALPHA = true;
+            }
 
             // Attribs
             if (mesh) {
@@ -1131,6 +1143,8 @@ module BABYLON {
             newPBRMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
             newPBRMaterial.indexOfRefraction = this.indexOfRefraction;
             newPBRMaterial.invertRefractionY = this.invertRefractionY;
+            newPBRMaterial.usePhysicalLightFalloff = this.usePhysicalLightFalloff;
+            newPBRMaterial.useRadianceOverAlpha = this.useRadianceOverAlpha;
 
             newPBRMaterial.emissiveFresnelParameters = this.emissiveFresnelParameters.clone();
             newPBRMaterial.opacityFresnelParameters = this.opacityFresnelParameters.clone();
@@ -1215,6 +1229,8 @@ module BABYLON {
             serializationObject.useSpecularOverAlpha = this.useSpecularOverAlpha;
             serializationObject.indexOfRefraction = this.indexOfRefraction;
             serializationObject.invertRefractionY = this.invertRefractionY;
+            serializationObject.usePhysicalLightFalloff = this.usePhysicalLightFalloff;
+            serializationObject.useRadianceOverAlpha = this.useRadianceOverAlpha;
 
             serializationObject.emissiveFresnelParameters = this.emissiveFresnelParameters.serialize();
             serializationObject.opacityFresnelParameters = this.opacityFresnelParameters.serialize();
@@ -1312,6 +1328,8 @@ module BABYLON {
             material.useSpecularOverAlpha = source.useSpecularOverAlpha;
             material.indexOfRefraction = source.indexOfRefraction;
             material.invertRefractionY = source.invertRefractionY;
+            material.usePhysicalLightFalloff = source.usePhysicalLightFalloff;
+            material.useRadianceOverAlpha = source.useRadianceOverAlpha;
 
             material.emissiveFresnelParameters = FresnelParameters.Parse(source.emissiveFresnelParameters);
             material.opacityFresnelParameters = FresnelParameters.Parse(source.opacityFresnelParameters);

+ 17 - 10
materialsLibrary/materials/pbr/pbr.fragment.fx

@@ -269,6 +269,17 @@ vec3 toGammaSpace(vec3 color)
     return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));
 }
 
+float computeLightFalloff(vec3 lightOffset, float lightDistanceSquared, float range)
+{
+    #ifdef USEPHYSICALLIGHTFALLOFF
+        float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001));
+        return lightDistanceFalloff;
+    #else
+        float lightFalloff = max(0., 1.0 - length(lightOffset) / range);
+        return lightFalloff;
+    #endif
+}
+
 #ifdef CAMERATONEMAP
     vec3 toneMaps(vec3 color)
     {
@@ -620,11 +631,8 @@ lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData,
     if (lightData.w == 0.)
     {
         vec3 lightOffset = lightData.xyz - vPositionW;
-        
-        // Inverse squared falloff.
         float lightDistanceSquared = dot(lightOffset, lightOffset);
-        float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001) * range);
-        attenuation = lightDistanceFalloff;
+        attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);
         
         lightDistance = sqrt(lightDistanceSquared);
         lightDirection = normalize(lightOffset);
@@ -673,8 +681,7 @@ lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightDa
         
         // Inverse squared falloff.
         float lightDistanceSquared = dot(lightOffset, lightOffset);
-        float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001) * range);
-        float attenuation = lightDistanceFalloff;
+        float attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);
         
         // Directional falloff.
         attenuation *= cosAngle;
@@ -1273,12 +1280,12 @@ vec3 surfaceEmissiveColor = vEmissiveColor;
     vec3 finalSpecular = vec3(0.0);
 #endif
 
-#ifdef OVERLOADEDSHADOWVALUES
-    finalSpecular = mix(finalSpecular, vec3(0.0), (1.0 - vOverloadedShadowIntensity.y));
+#ifdef SPECULAROVERALPHA
+    alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
 #endif
 
-#ifdef SPECULAROVERALPHA
-    alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
+#ifdef RADIANCEOVERALPHA
+    alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
 #endif
 
 // Composition

+ 1 - 18
materialsLibrary/test/index.html

@@ -215,15 +215,6 @@
 					skybox.material = skyboxMaterial;
 					currentMesh.isVisible = true;
 					fur.resetFur();
-					
-                    options.lightRange = 1000;
-                    hemisphericLight.range = 1000;
-                    options.lightRange = 1000;
-                    directionalLight.range = 1000;
-                    options.lightRange = 1000;
-                    pointLight.range = 1000;
-                    options.lightRange = 1000;
-                    spotLight.range = 1000;
                     
 					switch (options.material) {
 						case "simple":
@@ -248,14 +239,6 @@
 							break;
 						case "pbr":
 							currentMaterial = pbr;
-                            options.lightRange = 1;
-                            hemisphericLight.range = 1;
-                            options.lightRange = 1;
-                            directionalLight.range = 1;
-                            options.lightRange = 1;
-                            pointLight.range = 1;
-                            options.lightRange = 1;
-                            spotLight.range = 1;
 							break;
 						case "fur":
 							currentMaterial = fur.material;
@@ -329,7 +312,7 @@
                     directionalLight.range = options.lightRange;
                     pointLight.range = options.lightRange;
                     spotLight.range = options.lightRange;
-                }).listen();
+                });
                 
                 f1.add(options, 'lightRadius').onChange(function() {
                     hemisphericLight.radius = options.lightRadius;

File diff suppressed because it is too large
+ 66 - 27
materialsLibrary/test/refs/babylon.max.js