Browse Source

adding more code refactoring

David Catuhe 9 years ago
parent
commit
01a82f5284

File diff suppressed because it is too large
+ 2 - 2
dist/preview release/babylon.core.js


File diff suppressed because it is too large
+ 2955 - 2954
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 2 - 2
dist/preview release/babylon.js


+ 18 - 14
dist/preview release/babylon.max.js

@@ -19860,6 +19860,23 @@ var BABYLON;
             }
             }
             return needNormals;
             return needNormals;
         };
         };
+        StandardMaterial.BindLightShadow = function (light, scene, mesh, lightIndex, effect, defines, depthValuesAlreadySet) {
+            var shadowGenerator = light.getShadowGenerator();
+            if (mesh.receiveShadows && shadowGenerator) {
+                if (!light.needCube()) {
+                    effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
+                }
+                else {
+                    if (!depthValuesAlreadySet) {
+                        depthValuesAlreadySet = true;
+                        effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
+                    }
+                }
+                effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
+                effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
+            }
+            return depthValuesAlreadySet;
+        };
         StandardMaterial.BindLights = function (scene, mesh, effect, defines) {
         StandardMaterial.BindLights = function (scene, mesh, effect, defines) {
             var lightIndex = 0;
             var lightIndex = 0;
             var depthValuesAlreadySet = false;
             var depthValuesAlreadySet = false;
@@ -19895,20 +19912,7 @@ var BABYLON;
                 }
                 }
                 // Shadows
                 // Shadows
                 if (scene.shadowsEnabled) {
                 if (scene.shadowsEnabled) {
-                    var shadowGenerator = light.getShadowGenerator();
-                    if (mesh.receiveShadows && shadowGenerator) {
-                        if (!light.needCube()) {
-                            effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
-                        }
-                        else {
-                            if (!depthValuesAlreadySet) {
-                                depthValuesAlreadySet = true;
-                                effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
-                            }
-                        }
-                        effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                        effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
-                    }
+                    depthValuesAlreadySet = this.BindLightShadow(light, scene, mesh, lightIndex, effect, defines, depthValuesAlreadySet);
                 }
                 }
                 lightIndex++;
                 lightIndex++;
                 if (lightIndex === maxSimultaneousLights)
                 if (lightIndex === maxSimultaneousLights)

File diff suppressed because it is too large
+ 2 - 2
dist/preview release/babylon.noworker.js


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


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


+ 3 - 50
materialsLibrary/dist/babylon.simpleMaterial.js

@@ -285,57 +285,10 @@ var BABYLON;
                 }
                 }
                 this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
                 this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
             }
             }
-            this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
+            this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
+            // Lights
             if (scene.lightsEnabled && !this.disableLighting) {
             if (scene.lightsEnabled && !this.disableLighting) {
-                var lightIndex = 0;
-                var depthValuesAlreadySet = false;
-                for (var index = 0; index < scene.lights.length; index++) {
-                    var light = scene.lights[index];
-                    if (!light.isEnabled()) {
-                        continue;
-                    }
-                    if (!light.canAffectMesh(mesh)) {
-                        continue;
-                    }
-                    if (light instanceof BABYLON.PointLight) {
-                        // Point Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex);
-                    }
-                    else if (light instanceof BABYLON.DirectionalLight) {
-                        // Directional Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex);
-                    }
-                    else if (light instanceof BABYLON.SpotLight) {
-                        // Spot Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
-                    }
-                    else if (light instanceof BABYLON.HemisphericLight) {
-                        // Hemispheric Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
-                    }
-                    light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
-                    this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
-                    // Shadows
-                    if (scene.shadowsEnabled) {
-                        var shadowGenerator = light.getShadowGenerator();
-                        if (mesh.receiveShadows && shadowGenerator) {
-                            if (!light.needCube()) {
-                                this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
-                            }
-                            else {
-                                if (!depthValuesAlreadySet) {
-                                    depthValuesAlreadySet = true;
-                                    this._effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
-                                }
-                            }
-                            this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                            this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
-                        }
-                    }
-                    lightIndex++;
-                    if (lightIndex === maxSimultaneousLights)
-                        break;
-                }
+                BABYLON.StandardMaterial.BindLights(scene, mesh, this._effect, this._defines);
             }
             }
             // View
             // View
             if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
             if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {

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


+ 1 - 13
materialsLibrary/materials/pbr/babylon.pbrMaterial.ts

@@ -291,19 +291,7 @@ module BABYLON {
 
 
                 // Shadows
                 // Shadows
                 if (scene.shadowsEnabled) {
                 if (scene.shadowsEnabled) {
-                    var shadowGenerator = light.getShadowGenerator();
-                    if (mesh.receiveShadows && shadowGenerator) {
-                        if (!(<any>light).needCube()) {
-                            effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
-                        } else {
-                            if (!depthValuesAlreadySet) {
-                                depthValuesAlreadySet = true;
-                                effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
-                            }
-                        }
-                        effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                        effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
-                    }
+                    depthValuesAlreadySet = StandardMaterial.BindLightShadow(light, scene, mesh, lightIndex, effect, defines, depthValuesAlreadySet);
                 }
                 }
 
 
                 lightIndex++;
                 lightIndex++;

+ 14 - 14
materialsLibrary/materials/pbr/pbr.fragment.fx

@@ -513,14 +513,14 @@ float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float dar
 
 
 float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
 float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
 {
 {
-	vec3 directionToLight = vPositionW - lightPosition;
-	float depth = length(directionToLight);
+    vec3 directionToLight = vPositionW - lightPosition;
+    float depth = length(directionToLight);
 
 
-	depth = clamp(depth, 0., 1.0);
-	float diskScale = 2.0 / mapSize;
+    depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
+    depth = clamp(depth, 0., 1.0);
 
 
-	directionToLight = normalize(directionToLight);
-	directionToLight.y = -directionToLight.y;
+    directionToLight = normalize(directionToLight);
+    directionToLight.y = -directionToLight.y;
 
 
     float visibility = 1.;
     float visibility = 1.;
 
 
@@ -533,10 +533,10 @@ float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, fl
     // Poisson Sampling
     // Poisson Sampling
     float biasedDepth = depth - bias;
     float biasedDepth = depth - bias;
 
 
-    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * diskScale)) < biasedDepth) visibility -= 0.25;
-    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * diskScale)) < biasedDepth) visibility -= 0.25;
-    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * diskScale)) < biasedDepth) visibility -= 0.25;
-    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * diskScale)) < biasedDepth) visibility -= 0.25;
+    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
 
 
 #ifdef OVERLOADEDSHADOWVALUES
 #ifdef OVERLOADEDSHADOWVALUES
     return  min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
     return  min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
@@ -593,10 +593,10 @@ float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, flo
     // Poisson Sampling
     // Poisson Sampling
     float biasedDepth = depth.z - bias;
     float biasedDepth = depth.z - bias;
 
 
-    if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
-    if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
-    if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
-    if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
+    if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
 
 
 #ifdef OVERLOADEDSHADOWVALUES
 #ifdef OVERLOADEDSHADOWVALUES
     return  min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
     return  min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));

+ 3 - 53
materialsLibrary/materials/simple/babylon.simpleMaterial.ts

@@ -345,61 +345,11 @@ module BABYLON {
                 this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);                
                 this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);                
             }
             }
 
 
-            this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
+            this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
 
 
+            // Lights
             if (scene.lightsEnabled && !this.disableLighting) {
             if (scene.lightsEnabled && !this.disableLighting) {
-                var lightIndex = 0;
-                var depthValuesAlreadySet = false;
-                for (var index = 0; index < scene.lights.length; index++) {
-                    var light = scene.lights[index];
-
-                    if (!light.isEnabled()) {
-                        continue;
-                    }
-
-                    if (!light.canAffectMesh(mesh)) {
-                        continue;
-                    }
-
-                    if (light instanceof PointLight) {
-                        // Point Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex);
-                    } else if (light instanceof DirectionalLight) {
-                        // Directional Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex);
-                    } else if (light instanceof SpotLight) {
-                        // Spot Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
-                    } else if (light instanceof HemisphericLight) {
-                        // Hemispheric Light
-                        light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
-                    }
-
-                    light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
-                    this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
-
-                    // Shadows
-                    if (scene.shadowsEnabled) {
-                        var shadowGenerator = light.getShadowGenerator();
-                        if (mesh.receiveShadows && shadowGenerator) {
-                            if (!(<any>light).needCube()) {
-                                this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
-                            } else {
-                                if (!depthValuesAlreadySet) {
-                                    depthValuesAlreadySet = true;
-                                    this._effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
-                                }
-                            }
-                            this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                            this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
-                        }
-                    }
-
-                    lightIndex++;
-
-                    if (lightIndex === maxSimultaneousLights)
-                        break;
-                }
+                StandardMaterial.BindLights(scene, mesh, this._effect, this._defines);          
             }
             }
 
 
             // View
             // View

+ 18 - 14
materialsLibrary/test/refs/babylon.max.js

@@ -19860,6 +19860,23 @@ var BABYLON;
             }
             }
             return needNormals;
             return needNormals;
         };
         };
+        StandardMaterial.BindLightShadow = function (light, scene, mesh, lightIndex, effect, defines, depthValuesAlreadySet) {
+            var shadowGenerator = light.getShadowGenerator();
+            if (mesh.receiveShadows && shadowGenerator) {
+                if (!light.needCube()) {
+                    effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
+                }
+                else {
+                    if (!depthValuesAlreadySet) {
+                        depthValuesAlreadySet = true;
+                        effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
+                    }
+                }
+                effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
+                effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
+            }
+            return depthValuesAlreadySet;
+        };
         StandardMaterial.BindLights = function (scene, mesh, effect, defines) {
         StandardMaterial.BindLights = function (scene, mesh, effect, defines) {
             var lightIndex = 0;
             var lightIndex = 0;
             var depthValuesAlreadySet = false;
             var depthValuesAlreadySet = false;
@@ -19895,20 +19912,7 @@ var BABYLON;
                 }
                 }
                 // Shadows
                 // Shadows
                 if (scene.shadowsEnabled) {
                 if (scene.shadowsEnabled) {
-                    var shadowGenerator = light.getShadowGenerator();
-                    if (mesh.receiveShadows && shadowGenerator) {
-                        if (!light.needCube()) {
-                            effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
-                        }
-                        else {
-                            if (!depthValuesAlreadySet) {
-                                depthValuesAlreadySet = true;
-                                effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
-                            }
-                        }
-                        effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                        effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
-                    }
+                    depthValuesAlreadySet = this.BindLightShadow(light, scene, mesh, lightIndex, effect, defines, depthValuesAlreadySet);
                 }
                 }
                 lightIndex++;
                 lightIndex++;
                 if (lightIndex === maxSimultaneousLights)
                 if (lightIndex === maxSimultaneousLights)