ソースを参照

Improving shadows bias

David Catuhe 10 年 前
コミット
3a43c9c6bc

+ 4 - 2
Babylon/Loading/Plugins/babylon.babylonFileLoader.js

@@ -778,8 +778,10 @@ var BABYLON;
                 }
                 }
                 else
                 else
                     triggerParams = BABYLON.ActionManager[trigger.name];
                     triggerParams = BABYLON.ActionManager[trigger.name];
-                for (var j = 0; j < trigger.children.length; j++)
-                    traverse(trigger.children[j], triggerParams, null, null);
+                for (var j = 0; j < trigger.children.length; j++) {
+                    if (!trigger.detached)
+                        traverse(trigger.children[j], triggerParams, null, null);
+                }
             }
             }
         };
         };
         var parseSound = function (parsedSound, scene, rootUrl) {
         var parseSound = function (parsedSound, scene, rootUrl) {

+ 4 - 2
Babylon/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -968,8 +968,10 @@
             else
             else
                 triggerParams = BABYLON.ActionManager[trigger.name];
                 triggerParams = BABYLON.ActionManager[trigger.name];
 
 
-            for (var j = 0; j < trigger.children.length; j++)
-                traverse(trigger.children[j], triggerParams, null, null);
+            for (var j = 0; j < trigger.children.length; j++) {
+                if (!trigger.detached)
+                    traverse(trigger.children[j], triggerParams, null, null);
+            }
         }
         }
 
 
     };
     };

+ 40 - 5
Babylon/PostProcess/babylon.volumetricLightScatteringPostProcess.js

@@ -78,6 +78,8 @@ var BABYLON;
                     defines.push("#define ALPHATEST");
                     defines.push("#define ALPHATEST");
                 if (material.opacityTexture !== undefined) {
                 if (material.opacityTexture !== undefined) {
                     defines.push("#define OPACITY");
                     defines.push("#define OPACITY");
+                    if (material.opacityTexture.getAlphaFromRGB)
+                        defines.push("#define OPACITYRGB");
                     if (!needUV)
                     if (!needUV)
                         defines.push("#define NEED_UV");
                         defines.push("#define NEED_UV");
                 }
                 }
@@ -109,7 +111,7 @@ var BABYLON;
             var join = defines.join("\n");
             var join = defines.join("\n");
             if (this._cachedDefines !== join) {
             if (this._cachedDefines !== join) {
                 this._cachedDefines = join;
                 this._cachedDefines = join;
-                this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect({ vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" }, attribs, ["world", "mBones", "viewProjection", "diffuseMatrix"], ["diffuseSampler", "opacitySampler"], join);
+                this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect({ vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" }, attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "opacityLevel"], ["diffuseSampler", "opacitySampler"], join);
             }
             }
             return this._volumetricLightScatteringPass.isReady();
             return this._volumetricLightScatteringPass.isReady();
         };
         };
@@ -189,8 +191,10 @@ var BABYLON;
                         if (alphaTexture) {
                         if (alphaTexture) {
                             _this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                             _this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                         }
                         }
-                        if (material.opacityTexture !== undefined)
+                        if (material.opacityTexture !== undefined) {
                             _this._volumetricLightScatteringPass.setTexture("opacitySampler", material.opacityTexture);
                             _this._volumetricLightScatteringPass.setTexture("opacitySampler", material.opacityTexture);
+                            _this._volumetricLightScatteringPass.setFloat("opacityLevel", material.opacityTexture.level);
+                        }
                     }
                     }
                     // Bones
                     // Bones
                     if (mesh.useBones) {
                     if (mesh.useBones) {
@@ -202,7 +206,7 @@ var BABYLON;
             };
             };
             // Render target texture callbacks
             // Render target texture callbacks
             var savedSceneClearColor;
             var savedSceneClearColor;
-            var sceneClearColor = new BABYLON.Color3(0.0, 0.0, 0.0);
+            var sceneClearColor = new BABYLON.Color4(0.0, 0.0, 0.0, 1.0);
             this._volumetricLightScatteringRTT.onBeforeRender = function () {
             this._volumetricLightScatteringRTT.onBeforeRender = function () {
                 savedSceneClearColor = scene.clearColor;
                 savedSceneClearColor = scene.clearColor;
                 scene.clearColor = sceneClearColor;
                 scene.clearColor = sceneClearColor;
@@ -211,15 +215,46 @@ var BABYLON;
                 scene.clearColor = savedSceneClearColor;
                 scene.clearColor = savedSceneClearColor;
             };
             };
             this._volumetricLightScatteringRTT.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
             this._volumetricLightScatteringRTT.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
+                var engine = scene.getEngine();
                 var index;
                 var index;
                 for (index = 0; index < opaqueSubMeshes.length; index++) {
                 for (index = 0; index < opaqueSubMeshes.length; index++) {
                     renderSubMesh(opaqueSubMeshes.data[index]);
                     renderSubMesh(opaqueSubMeshes.data[index]);
                 }
                 }
+                engine.setAlphaTesting(true);
                 for (index = 0; index < alphaTestSubMeshes.length; index++) {
                 for (index = 0; index < alphaTestSubMeshes.length; index++) {
                     renderSubMesh(alphaTestSubMeshes.data[index]);
                     renderSubMesh(alphaTestSubMeshes.data[index]);
                 }
                 }
-                for (index = 0; index < transparentSubMeshes.length; index++) {
-                    renderSubMesh(transparentSubMeshes.data[index]);
+                engine.setAlphaTesting(false);
+                if (transparentSubMeshes.length) {
+                    for (index = 0; index < transparentSubMeshes.length; index++) {
+                        var submesh = transparentSubMeshes.data[index];
+                        submesh._alphaIndex = submesh.getMesh().alphaIndex;
+                        submesh._distanceToCamera = submesh.getBoundingInfo().boundingSphere.centerWorld.subtract(scene.activeCamera.position).length();
+                    }
+                    var sortedArray = transparentSubMeshes.data.slice(0, transparentSubMeshes.length);
+                    sortedArray.sort(function (a, b) {
+                        // Alpha index first
+                        if (a._alphaIndex > b._alphaIndex) {
+                            return 1;
+                        }
+                        if (a._alphaIndex < b._alphaIndex) {
+                            return -1;
+                        }
+                        // Then distance to camera
+                        if (a._distanceToCamera < b._distanceToCamera) {
+                            return 1;
+                        }
+                        if (a._distanceToCamera > b._distanceToCamera) {
+                            return -1;
+                        }
+                        return 0;
+                    });
+                    // Render sub meshes
+                    engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
+                    for (index = 0; index < sortedArray.length; index++) {
+                        renderSubMesh(sortedArray[index]);
+                    }
+                    engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
                 }
                 }
             };
             };
         };
         };

+ 47 - 7
Babylon/PostProcess/babylon.volumetricLightScatteringPostProcess.ts

@@ -92,6 +92,8 @@
 
 
                 if (material.opacityTexture !== undefined) {
                 if (material.opacityTexture !== undefined) {
                     defines.push("#define OPACITY");
                     defines.push("#define OPACITY");
+                    if (material.opacityTexture.getAlphaFromRGB)
+                        defines.push("#define OPACITYRGB");
                     if (!needUV)
                     if (!needUV)
                         defines.push("#define NEED_UV");
                         defines.push("#define NEED_UV");
                 }
                 }
@@ -130,7 +132,7 @@
                 this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect(
                 this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect(
                     { vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" },
                     { vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" },
                     attribs,
                     attribs,
-                    ["world", "mBones", "viewProjection", "diffuseMatrix"],
+                    ["world", "mBones", "viewProjection", "diffuseMatrix", "opacityLevel"],
                     ["diffuseSampler", "opacitySampler"], join);
                     ["diffuseSampler", "opacitySampler"], join);
             }
             }
 
 
@@ -231,8 +233,10 @@
                             this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                             this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                         }
                         }
 
 
-                        if (material.opacityTexture !== undefined)
+                        if (material.opacityTexture !== undefined) {
                             this._volumetricLightScatteringPass.setTexture("opacitySampler", material.opacityTexture);
                             this._volumetricLightScatteringPass.setTexture("opacitySampler", material.opacityTexture);
+                            this._volumetricLightScatteringPass.setFloat("opacityLevel", material.opacityTexture.level);
+                        }
                     }
                     }
 
 
                     // Bones
                     // Bones
@@ -247,8 +251,8 @@
             };
             };
 
 
             // Render target texture callbacks
             // Render target texture callbacks
-            var savedSceneClearColor: Color3;
-            var sceneClearColor = new Color3(0.0, 0.0, 0.0);
+            var savedSceneClearColor: Color4;
+            var sceneClearColor = new Color4(0.0, 0.0, 0.0, 1.0);
 
 
             this._volumetricLightScatteringRTT.onBeforeRender = (): void => {
             this._volumetricLightScatteringRTT.onBeforeRender = (): void => {
                 savedSceneClearColor = scene.clearColor;
                 savedSceneClearColor = scene.clearColor;
@@ -260,18 +264,54 @@
             };
             };
 
 
             this._volumetricLightScatteringRTT.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>): void => {
             this._volumetricLightScatteringRTT.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>): void => {
-                var index;
+                var engine = scene.getEngine();
+                var index: number;
 
 
                 for (index = 0; index < opaqueSubMeshes.length; index++) {
                 for (index = 0; index < opaqueSubMeshes.length; index++) {
                     renderSubMesh(opaqueSubMeshes.data[index]);
                     renderSubMesh(opaqueSubMeshes.data[index]);
                 }
                 }
 
 
+                engine.setAlphaTesting(true);
                 for (index = 0; index < alphaTestSubMeshes.length; index++) {
                 for (index = 0; index < alphaTestSubMeshes.length; index++) {
                     renderSubMesh(alphaTestSubMeshes.data[index]);
                     renderSubMesh(alphaTestSubMeshes.data[index]);
                 }
                 }
+                engine.setAlphaTesting(false);
+
+                if (transparentSubMeshes.length) {
+                    // Sort sub meshes
+                    for (index = 0; index < transparentSubMeshes.length; index++) {
+                        var submesh = transparentSubMeshes.data[index];
+                        submesh._alphaIndex = submesh.getMesh().alphaIndex;
+                        submesh._distanceToCamera = submesh.getBoundingInfo().boundingSphere.centerWorld.subtract(scene.activeCamera.position).length();
+                    }
+
+                    var sortedArray = transparentSubMeshes.data.slice(0, transparentSubMeshes.length);
+                    sortedArray.sort((a, b) => {
+                        // Alpha index first
+                        if (a._alphaIndex > b._alphaIndex) {
+                            return 1;
+                        }
+                        if (a._alphaIndex < b._alphaIndex) {
+                            return -1;
+                        }
 
 
-                for (index = 0; index < transparentSubMeshes.length; index++) {
-                    renderSubMesh(transparentSubMeshes.data[index]);
+                        // Then distance to camera
+                        if (a._distanceToCamera < b._distanceToCamera) {
+                            return 1;
+                        }
+                        if (a._distanceToCamera > b._distanceToCamera) {
+                            return -1;
+                        }
+
+                        return 0;
+                    });
+
+                    // Render sub meshes
+                    engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
+                    for (index = 0; index < sortedArray.length; index++) {
+                        renderSubMesh(sortedArray[index]);
+                    }
+                    engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
                 }
                 }
             };
             };
         }
         }

+ 1 - 1
Babylon/Shaders/default.fragment.fx

@@ -270,7 +270,7 @@ float ChebychevInequality(vec2 moments, float t)
 	}
 	}
 
 
 	float variance = moments.y - (moments.x * moments.x);
 	float variance = moments.y - (moments.x * moments.x);
-	variance = max(variance, 0.02);
+	variance = max(variance, 0.002);
 
 
 	float d = t - moments.x;
 	float d = t - moments.x;
 
 

+ 13 - 5
Babylon/Shaders/volumetricLightScatteringPass.fragment.fx

@@ -12,6 +12,7 @@ uniform sampler2D diffuseSampler;
 
 
 #if defined(OPACITY)
 #if defined(OPACITY)
 uniform sampler2D opacitySampler;
 uniform sampler2D opacitySampler;
+uniform float opacityLevel;
 #endif
 #endif
 
 
 void main(void)
 void main(void)
@@ -27,15 +28,22 @@ void main(void)
 
 
 #ifdef OPACITY
 #ifdef OPACITY
 	vec4 opacityColor = texture2D(opacitySampler, vUV);
 	vec4 opacityColor = texture2D(opacitySampler, vUV);
+	float alpha = 1.0;
 
 
-	#if defined(BASIC_RENDER)
-	gl_FragColor = diffuseColor * opacityColor;
+	#ifdef OPACITYRGB
+	opacityColor.rgb = opacityColor.rgb * vec3(0.3, 0.59, 0.11);
+	alpha *= (opacityColor.x + opacityColor.y + opacityColor.z) * opacityLevel;
 	#else
 	#else
-	if (opacityColor.r == 0.0)
-		discard;
+	alpha *= opacityColor.a * opacityLevel;
+	#endif
 
 
-	gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
+	#if defined(BASIC_RENDER)
+	gl_FragColor = vec4(diffuseColor.rgb, alpha);
+	#else
+	gl_FragColor = vec4(0.0, 0.0, 0.0, alpha);
 	#endif
 	#endif
+
+	gl_FragColor.a = alpha;
 #else
 #else
 	#ifndef BASIC_RENDER
 	#ifndef BASIC_RENDER
 	gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
 	gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);

ファイルの差分が大きいため隠しています
+ 46 - 9
babylon.2.1-alpha.debug.js


ファイルの差分が大きいため隠しています
+ 5 - 5
babylon.2.1-alpha.js