Browse Source

Adding support for point light shadowing

David catuhe 9 năm trước cách đây
mục cha
commit
da863d9e1b

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 20 - 20
dist/preview release/babylon.core.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 531 - 519
dist/preview release/babylon.d.ts


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 27 - 26
dist/preview release/babylon.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 107 - 26
dist/preview release/babylon.max.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 27 - 26
dist/preview release/babylon.noworker.js


+ 2 - 1
dist/preview release/what's new.md

@@ -1,9 +1,10 @@
 - 2.3.0:
   - **Major updates**
+    - Point lights shadow mapping. [Demo here](http://www.babylonjs-playground.com/#LYCSQ#12) ([deltakosh](https://github.com/deltakosh))
     - Introducing [Materials Library](https://github.com/BabylonJS/Babylon.js/materialsLibrary) ([deltakosh](https://github.com/deltakosh))
     - New cache mecanism for StandardMaterial ([deltakosh](https://github.com/deltakosh))
     - New Solid Particle System ([jerome](https://github.com/jbousquie))
-    - New `StandardMaterial.lightmapTexture` which can be controlled with `StandardMaterial.useLightmapAsShadowMap`. [Demo here](#NEEDDEMO) ([deltakosh](https://github.com/deltakosh))
+    - New `StandardMaterial.lightmapTexture` which can be controlled with `StandardMaterial.useLightmapAsShadowMap` ([deltakosh](https://github.com/deltakosh))
     - Support for reflection probes. [See documentation here](http://doc.babylonjs.com/tutorials/How_to_use_Reflection_probes) ([deltakosh](https://github.com/deltakosh))
     - New serializers [folder](https://github.com/BabylonJS/Babylon.js/serializers) to host .babylon serializers ([deltakosh](https://github.com/deltakosh))
       - New .obj serializer ([BitOfGold](https://github.com/BitOfGold))

+ 0 - 1
src/Audio/babylon.sound.js

@@ -442,4 +442,3 @@ var BABYLON;
     })();
     BABYLON.Sound = Sound;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.sound.js.map

+ 0 - 2
src/Debug/babylon.debugLayer.js

@@ -238,7 +238,6 @@ var BABYLON;
             this._scene.unregisterBeforeRender(this._syncData);
             this._scene.unregisterAfterRender(this._syncUI);
             this._rootElement.removeChild(this._globalDiv);
-            window.removeEventListener("resize", this._syncPositions);
             this._scene.forceShowBoundingBoxes = false;
             this._scene.forceWireframe = false;
             BABYLON.StandardMaterial.DiffuseTextureEnabled = true;
@@ -281,7 +280,6 @@ var BABYLON;
             this._rootElement = rootElement || document.body;
             this._rootElement.appendChild(this._globalDiv);
             this._generateDOMelements();
-            window.addEventListener("resize", this._syncPositions);
             engine.getRenderingCanvas().addEventListener("click", this._onCanvasClick);
             this._syncPositions();
             this._scene.registerBeforeRender(this._syncData);

+ 2 - 5
src/Debug/babylon.debugLayer.ts

@@ -21,7 +21,7 @@
         private _drawingContext: CanvasRenderingContext2D;
         private _rootElement: HTMLElement;
 
-        private _syncPositions: () => void;
+        public _syncPositions: () => void;
         private _syncData: () => void;
         private _syncUI: () => void;
         private _onCanvasClick: (evt: MouseEvent) => void;
@@ -343,8 +343,6 @@
             this._scene.unregisterAfterRender(this._syncUI);
             this._rootElement.removeChild(this._globalDiv);
 
-            window.removeEventListener("resize", this._syncPositions);
-
             this._scene.forceShowBoundingBoxes = false;
             this._scene.forceWireframe = false;
 
@@ -395,8 +393,7 @@
             this._rootElement.appendChild(this._globalDiv);
 
             this._generateDOMelements();
-
-            window.addEventListener("resize", this._syncPositions);
+            
             engine.getRenderingCanvas().addEventListener("click", this._onCanvasClick);
 
             this._syncPositions();

+ 13 - 5
src/Lights/Shadows/babylon.shadowGenerator.js

@@ -15,17 +15,24 @@ var BABYLON;
             this._projectionMatrix = BABYLON.Matrix.Zero();
             this._transformMatrix = BABYLON.Matrix.Zero();
             this._worldViewProjection = BABYLON.Matrix.Zero();
+            this._currentFaceIndex = 0;
+            this._currentFaceIndexCache = 0;
             this._light = light;
             this._scene = light.getScene();
             this._mapSize = mapSize;
             light._shadowGenerator = this;
             // Render target
-            this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
+            this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false, true, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT, light.needCube());
             this._shadowMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
             this._shadowMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
             this._shadowMap.anisotropicFilteringLevel = 1;
-            this._shadowMap.updateSamplingMode(BABYLON.Texture.NEAREST_SAMPLINGMODE);
+            if (!light.needCube()) {
+                this._shadowMap.updateSamplingMode(BABYLON.Texture.NEAREST_SAMPLINGMODE);
+            }
             this._shadowMap.renderParticles = false;
+            this._shadowMap.onBeforeRender = function (faceIndex) {
+                _this._currentFaceIndex = faceIndex;
+            };
             this._shadowMap.onAfterUnbind = function () {
                 if (!_this.useBlurVarianceShadowMap) {
                     return;
@@ -273,13 +280,14 @@ var BABYLON;
         // Methods
         ShadowGenerator.prototype.getTransformMatrix = function () {
             var scene = this._scene;
-            if (this._currentRenderID === scene.getRenderId()) {
+            if (this._currentRenderID === scene.getRenderId() && this._currentFaceIndexCache === this._currentFaceIndex) {
                 return this._transformMatrix;
             }
             this._currentRenderID = scene.getRenderId();
+            this._currentFaceIndexCache = this._currentFaceIndex;
             var lightPosition = this._light.position;
-            BABYLON.Vector3.NormalizeToRef(this._light.direction, this._lightDirection);
-            if (Math.abs(BABYLON.Vector3.Dot(this._lightDirection, BABYLON.Vector3.Up())) == 1.0) {
+            BABYLON.Vector3.NormalizeToRef(this._light.getShadowDirection(this._currentFaceIndex), this._lightDirection);
+            if (Math.abs(BABYLON.Vector3.Dot(this._lightDirection, BABYLON.Vector3.Up())) === 1.0) {
                 this._lightDirection.z = 0.0000000000001; // Need to avoid perfectly perpendicular light
             }
             if (this._light.computeTransformedPosition()) {

+ 15 - 5
src/Lights/Shadows/babylon.shadowGenerator.ts

@@ -121,6 +121,8 @@
         private _downSamplePostprocess: PassPostProcess;
         private _boxBlurPostprocess: PostProcess;
         private _mapSize: number;
+        private _currentFaceIndex = 0;
+        private _currentFaceIndexCache = 0;
 
         constructor(mapSize: number, light: IShadowLight) {
             this._light = light;
@@ -130,13 +132,20 @@
             light._shadowGenerator = this;
 
             // Render target
-            this._shadowMap = new RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
+            this._shadowMap = new RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false, true, Engine.TEXTURETYPE_UNSIGNED_INT, light.needCube());
             this._shadowMap.wrapU = Texture.CLAMP_ADDRESSMODE;
             this._shadowMap.wrapV = Texture.CLAMP_ADDRESSMODE;
             this._shadowMap.anisotropicFilteringLevel = 1;
-            this._shadowMap.updateSamplingMode(Texture.NEAREST_SAMPLINGMODE);
+            if (!light.needCube()) {
+                this._shadowMap.updateSamplingMode(Texture.NEAREST_SAMPLINGMODE);
+            }
             this._shadowMap.renderParticles = false;
 
+            
+            this._shadowMap.onBeforeRender = (faceIndex: number) => {
+                this._currentFaceIndex = faceIndex;
+            }
+
             this._shadowMap.onAfterUnbind = () => {
                 if (!this.useBlurVarianceShadowMap) {
                     return;
@@ -306,16 +315,17 @@
         // Methods
         public getTransformMatrix(): Matrix {
             var scene = this._scene;
-            if (this._currentRenderID === scene.getRenderId()) {
+            if (this._currentRenderID === scene.getRenderId() && this._currentFaceIndexCache === this._currentFaceIndex) {
                 return this._transformMatrix;
             }
 
             this._currentRenderID = scene.getRenderId();
+            this._currentFaceIndexCache = this._currentFaceIndex;
 
             var lightPosition = this._light.position;
-            Vector3.NormalizeToRef(this._light.direction, this._lightDirection);
+            Vector3.NormalizeToRef(this._light.getShadowDirection(this._currentFaceIndex), this._lightDirection);
 
-            if (Math.abs(Vector3.Dot(this._lightDirection, Vector3.Up())) == 1.0) {
+            if (Math.abs(Vector3.Dot(this._lightDirection, Vector3.Up())) === 1.0) {
                 this._lightDirection.z = 0.0000000000001; // Need to avoid perfectly perpendicular light
             }
                 

+ 6 - 0
src/Lights/babylon.directionalLight.js

@@ -68,6 +68,12 @@ var BABYLON;
         DirectionalLight.prototype.needRefreshPerFrame = function () {
             return true;
         };
+        DirectionalLight.prototype.needCube = function () {
+            return false;
+        };
+        DirectionalLight.prototype.getShadowDirection = function (faceIndex) {
+            return this.direction;
+        };
         DirectionalLight.prototype.computeTransformedPosition = function () {
             if (this.parent && this.parent.getWorldMatrix) {
                 if (!this.transformedPosition) {

+ 8 - 0
src/Lights/babylon.directionalLight.ts

@@ -90,6 +90,14 @@
             return true;
         }
 
+        public needCube(): boolean {
+            return false;
+        }
+
+        public getShadowDirection(faceIndex?: number): Vector3 {
+            return this.direction;
+        }
+
         public computeTransformedPosition(): boolean {
             if (this.parent && this.parent.getWorldMatrix) {
                 if (!this.transformedPosition) {

+ 3 - 1
src/Lights/babylon.light.ts

@@ -2,7 +2,6 @@
 
     export interface IShadowLight {
         position: Vector3;
-        direction: Vector3;
         transformedPosition: Vector3;
         name: string;
 
@@ -13,6 +12,9 @@
 
         supportsVSM(): boolean;
         needRefreshPerFrame(): boolean;
+        needCube(): boolean;
+
+        getShadowDirection(faceIndex?: number): Vector3;
 
         _shadowGenerator: ShadowGenerator;
     }

+ 42 - 8
src/Lights/babylon.pointLight.js

@@ -12,21 +12,55 @@ var BABYLON;
             this.position = position;
         }
         PointLight.prototype.getAbsolutePosition = function () {
-            return this._transformedPosition ? this._transformedPosition : this.position;
+            return this.transformedPosition ? this.transformedPosition : this.position;
         };
-        PointLight.prototype.transferToEffect = function (effect, positionUniformName) {
+        PointLight.prototype.computeTransformedPosition = function () {
             if (this.parent && this.parent.getWorldMatrix) {
-                if (!this._transformedPosition) {
-                    this._transformedPosition = BABYLON.Vector3.Zero();
+                if (!this.transformedPosition) {
+                    this.transformedPosition = BABYLON.Vector3.Zero();
                 }
-                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
-                effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, 0);
+                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
+                return true;
+            }
+            return false;
+        };
+        PointLight.prototype.transferToEffect = function (effect, positionUniformName) {
+            if (this.parent && this.parent.getWorldMatrix) {
+                this.computeTransformedPosition();
+                effect.setFloat4(positionUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, 0);
                 return;
             }
             effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, 0);
         };
-        PointLight.prototype.getShadowGenerator = function () {
-            return null;
+        PointLight.prototype.needCube = function () {
+            return true;
+        };
+        PointLight.prototype.supportsVSM = function () {
+            return false;
+        };
+        PointLight.prototype.needRefreshPerFrame = function () {
+            return false;
+        };
+        PointLight.prototype.getShadowDirection = function (faceIndex) {
+            switch (faceIndex) {
+                case 0:
+                    return new BABYLON.Vector3(1, 0, 0);
+                case 1:
+                    return new BABYLON.Vector3(-1, 0, 0);
+                case 2:
+                    return new BABYLON.Vector3(0, -1, 0);
+                case 3:
+                    return new BABYLON.Vector3(0, 1, 0);
+                case 4:
+                    return new BABYLON.Vector3(0, 0, 1);
+                case 5:
+                    return new BABYLON.Vector3(0, 0, -1);
+            }
+            return BABYLON.Vector3.Zero();
+        };
+        PointLight.prototype.setShadowProjectionMatrix = function (matrix, viewMatrix, renderList) {
+            var activeCamera = this.getScene().activeCamera;
+            BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2, 1.0, activeCamera.minZ, activeCamera.maxZ, matrix);
         };
         PointLight.prototype._getWorldMatrix = function () {
             if (!this._worldMatrix) {

+ 53 - 10
src/Lights/babylon.pointLight.ts

@@ -1,24 +1,35 @@
 module BABYLON {
-    export class PointLight extends Light {
+    export class PointLight extends Light implements IShadowLight {
         private _worldMatrix: Matrix;
-        private _transformedPosition: Vector3;
+        public transformedPosition: Vector3;
 
         constructor(name: string, public position: Vector3, scene: Scene) {
             super(name, scene);
         }
 
         public getAbsolutePosition(): Vector3 {
-            return this._transformedPosition ? this._transformedPosition : this.position;
+            return this.transformedPosition ? this.transformedPosition : this.position;
         }
 
-        public transferToEffect(effect: Effect, positionUniformName: string): void {
+        public computeTransformedPosition(): boolean {
             if (this.parent && this.parent.getWorldMatrix) {
-                if (!this._transformedPosition) {
-                    this._transformedPosition = Vector3.Zero();
+                if (!this.transformedPosition) {
+                    this.transformedPosition = Vector3.Zero();
                 }
 
-                Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
-                effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, 0);
+                Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
+
+                return true;
+            }
+
+            return false;
+        }
+
+        public transferToEffect(effect: Effect, positionUniformName: string): void {
+            if (this.parent && this.parent.getWorldMatrix) {
+                this.computeTransformedPosition();
+
+                effect.setFloat4(positionUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, 0);
 
                 return;
             }
@@ -26,8 +37,40 @@
             effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, 0);
         }
 
-        public getShadowGenerator(): ShadowGenerator {
-            return null;
+        public needCube(): boolean {
+            return true;
+        }
+
+        public supportsVSM(): boolean {
+            return false;
+        }
+
+        public needRefreshPerFrame(): boolean {
+            return false;
+        }
+
+        public getShadowDirection(faceIndex?: number): Vector3 {
+            switch (faceIndex) {
+                case 0:
+                    return new Vector3(1, 0, 0);
+                case 1:
+                    return new Vector3(-1, 0, 0);
+                case 2:
+                    return new Vector3(0, -1, 0);
+                case 3:
+                    return new Vector3(0, 1, 0);
+                case 4:
+                    return new Vector3(0, 0, 1);
+                case 5:
+                    return new Vector3(0, 0, -1);
+            }
+
+            return Vector3.Zero();
+        }
+
+        public setShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
+            var activeCamera = this.getScene().activeCamera;
+            Matrix.PerspectiveFovLHToRef(Math.PI / 2, 1.0, activeCamera.minZ, activeCamera.maxZ, matrix);
         }
 
         public _getWorldMatrix(): Matrix {

+ 6 - 0
src/Lights/babylon.spotLight.js

@@ -21,12 +21,18 @@ var BABYLON;
             var activeCamera = this.getScene().activeCamera;
             BABYLON.Matrix.PerspectiveFovLHToRef(this.angle, 1.0, activeCamera.minZ, activeCamera.maxZ, matrix);
         };
+        SpotLight.prototype.needCube = function () {
+            return false;
+        };
         SpotLight.prototype.supportsVSM = function () {
             return true;
         };
         SpotLight.prototype.needRefreshPerFrame = function () {
             return false;
         };
+        SpotLight.prototype.getShadowDirection = function (faceIndex) {
+            return this.direction;
+        };
         SpotLight.prototype.setDirectionToTarget = function (target) {
             this.direction = BABYLON.Vector3.Normalize(target.subtract(this.position));
             return this.direction;

+ 7 - 0
src/Lights/babylon.spotLight.ts

@@ -19,6 +19,9 @@
             Matrix.PerspectiveFovLHToRef(this.angle, 1.0, activeCamera.minZ, activeCamera.maxZ, matrix);
         }
 
+        public needCube(): boolean {
+            return false;
+        }
 
         public supportsVSM(): boolean {
             return true;
@@ -28,6 +31,10 @@
             return false;
         }
 
+        public getShadowDirection(faceIndex?: number): Vector3 {
+            return this.direction;
+        }
+
         public setDirectionToTarget(target: Vector3): Vector3 {
             this.direction = Vector3.Normalize(target.subtract(this.position));
             return this.direction;

+ 15 - 6
src/Materials/babylon.standardMaterial.js

@@ -52,10 +52,14 @@ var BABYLON;
             this.HEMILIGHT1 = false;
             this.HEMILIGHT2 = false;
             this.HEMILIGHT3 = false;
-            this.POINTDIRLIGHT0 = false;
-            this.POINTDIRLIGHT1 = false;
-            this.POINTDIRLIGHT2 = false;
-            this.POINTDIRLIGHT3 = false;
+            this.POINTLIGHT0 = false;
+            this.POINTLIGHT1 = false;
+            this.POINTLIGHT2 = false;
+            this.POINTLIGHT3 = false;
+            this.DIRLIGHT0 = false;
+            this.DIRLIGHT1 = false;
+            this.DIRLIGHT2 = false;
+            this.DIRLIGHT3 = false;
             this.SPECULARTERM = false;
             this.SHADOW0 = false;
             this.SHADOW1 = false;
@@ -359,8 +363,11 @@ var BABYLON;
                     else if (light instanceof BABYLON.HemisphericLight) {
                         type = "HEMILIGHT" + lightIndex;
                     }
+                    else if (light instanceof BABYLON.PointLight) {
+                        type = "POINTLIGHT" + lightIndex;
+                    }
                     else {
-                        type = "POINTDIRLIGHT" + lightIndex;
+                        type = "DIRLIGHT" + lightIndex;
                     }
                     this._defines[type] = true;
                     // Specular
@@ -697,7 +704,9 @@ var BABYLON;
                     if (scene.shadowsEnabled) {
                         var shadowGenerator = light.getShadowGenerator();
                         if (mesh.receiveShadows && shadowGenerator) {
-                            this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
+                            if (!light.needCube()) {
+                                this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
+                            }
                             this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
                             this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
                         }

+ 14 - 6
src/Materials/babylon.standardMaterial.ts

@@ -44,10 +44,14 @@
         public HEMILIGHT1 = false;
         public HEMILIGHT2 = false;
         public HEMILIGHT3 = false;
-        public POINTDIRLIGHT0 = false;
-        public POINTDIRLIGHT1 = false;
-        public POINTDIRLIGHT2 = false;
-        public POINTDIRLIGHT3 = false;
+        public POINTLIGHT0 = false;
+        public POINTLIGHT1 = false;
+        public POINTLIGHT2 = false;
+        public POINTLIGHT3 = false;
+        public DIRLIGHT0 = false;
+        public DIRLIGHT1 = false;
+        public DIRLIGHT2 = false;
+        public DIRLIGHT3 = false;
         public SPECULARTERM = false;
         public SHADOW0 = false;
         public SHADOW1 = false;
@@ -411,8 +415,10 @@
                         type = "SPOTLIGHT" + lightIndex;
                     } else if (light instanceof HemisphericLight) {
                         type = "HEMILIGHT" + lightIndex;
+                    } else if (light instanceof PointLight) {
+                        type = "POINTLIGHT" + lightIndex;
                     } else {
-                        type = "POINTDIRLIGHT" + lightIndex;
+                        type = "DIRLIGHT" + lightIndex;
                     }
 
                     this._defines[type] = true;
@@ -837,7 +843,9 @@
                     if (scene.shadowsEnabled) {
                         var shadowGenerator = light.getShadowGenerator();
                         if (mesh.receiveShadows && shadowGenerator) {
-                            this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
+                            if (!(<any>light).needCube()) {
+                                this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
+                            }
                             this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
                             this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
                         }

+ 106 - 8
src/Shaders/default.fragment.fx

@@ -30,8 +30,12 @@ uniform vec4 vLightDiffuse0;
 uniform vec3 vLightSpecular0;
 #endif
 #ifdef SHADOW0
+#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
 varying vec4 vPositionFromLight0;
 uniform sampler2D shadowSampler0;
+#else
+uniform samplerCube shadowSampler0;
+#endif
 uniform vec3 shadowsInfo0;
 #endif
 #ifdef SPOTLIGHT0
@@ -49,8 +53,12 @@ uniform vec4 vLightDiffuse1;
 uniform vec3 vLightSpecular1;
 #endif
 #ifdef SHADOW1
+#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
 varying vec4 vPositionFromLight1;
 uniform sampler2D shadowSampler1;
+#else
+uniform samplerCube shadowSampler1;
+#endif
 uniform vec3 shadowsInfo1;
 #endif
 #ifdef SPOTLIGHT1
@@ -68,8 +76,12 @@ uniform vec4 vLightDiffuse2;
 uniform vec3 vLightSpecular2;
 #endif
 #ifdef SHADOW2
+#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
 varying vec4 vPositionFromLight2;
 uniform sampler2D shadowSampler2;
+#else
+uniform samplerCube shadowSampler2;
+#endif
 uniform vec3 shadowsInfo2;
 #endif
 #ifdef SPOTLIGHT2
@@ -87,8 +99,12 @@ uniform vec4 vLightDiffuse3;
 uniform vec3 vLightSpecular3;
 #endif
 #ifdef SHADOW3
+#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
 varying vec4 vPositionFromLight3;
 uniform sampler2D shadowSampler3;
+#else
+uniform samplerCube shadowSampler3;
+#endif
 uniform vec3 shadowsInfo3;
 #endif
 #ifdef SPOTLIGHT3
@@ -252,11 +268,55 @@ float unpack(vec4 color)
 	return dot(color, bit_shift);
 }
 
-float unpackHalf(vec2 color)
+#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
+float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
 {
-	return color.x + (color.y / 255.0);
+	vec3 directionToLight = vPositionW - lightPosition;
+	float depth = length(directionToLight);
+
+	depth = clamp(depth, 0., 1.);
+
+	directionToLight.y = 1.0 - directionToLight.y;
+
+	float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
+
+	if (depth > shadow)
+	{
+		return darkness;
+	}
+	return 1.0;
 }
 
+float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float bias, float darkness)
+{
+	vec3 directionToLight = vPositionW - lightPosition;
+	float depth = length(directionToLight);
+
+	depth = clamp(depth, 0., 1.);
+
+	directionToLight.y = 1.0 - directionToLight.y;
+
+	float visibility = 1.;
+
+	vec3 poissonDisk[4];
+	poissonDisk[0] = vec3(-0.094201624, 0.04, -0.039906216);
+	poissonDisk[1] = vec3(0.094558609, -0.04, -0.076890725);
+	poissonDisk[2] = vec3(-0.094184101, 0.01, -0.092938870);
+	poissonDisk[3] = vec3(0.034495938, -0.01, 0.029387760);
+
+	// Poisson Sampling
+	float biasedDepth = depth - bias;
+
+	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0])) < biasedDepth) visibility -= 0.25;
+	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1])) < biasedDepth) visibility -= 0.25;
+	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2])) < biasedDepth) visibility -= 0.25;
+	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3])) < biasedDepth) visibility -= 0.25;
+
+	return  min(1.0, visibility + darkness);
+}
+#endif
+
+#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) ||  defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
 float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
 {
 	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
@@ -308,6 +368,11 @@ float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, flo
 }
 
 // Thanks to http://devmaster.net/
+float unpackHalf(vec2 color)
+{
+	return color.x + (color.y / 255.0);
+}
+
 float linstep(float low, float high, float v) {
 	return clamp((v - low) / (high - low), 0.0, 1.0);
 }
@@ -339,6 +404,7 @@ float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, flo
 	return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
 }
 #endif
+#endif
 
 // Bump
 #ifdef BUMP
@@ -605,7 +671,7 @@ void main(void) {
 #ifdef HEMILIGHT0
 	lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness);
 #endif
-#ifdef POINTDIRLIGHT0
+#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
 	lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
 #endif
 #ifdef SHADOW0
@@ -613,9 +679,17 @@ void main(void) {
 	shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
 #else
 #ifdef SHADOWPCF0
+	#if defined(POINTLIGHT0)
+	shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
+	#else
 	shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
+	#endif
 #else
+	#if defined(POINTLIGHT0)
+	shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
+	#else
 	shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
+	#endif
 #endif
 #endif
 #else
@@ -637,7 +711,7 @@ void main(void) {
 #ifdef HEMILIGHT1
 	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, glossiness);
 #endif
-#ifdef POINTDIRLIGHT1
+#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
 	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
 #endif
 #ifdef SHADOW1
@@ -645,9 +719,17 @@ void main(void) {
 	shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
 #else
 #ifdef SHADOWPCF1
+#if defined(POINTLIGHT1)
+	shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
+#else
 	shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
+#endif
 #else
+	#if defined(POINTLIGHT1)
+	shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
+	#else
 	shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
+	#endif
 #endif
 #endif
 #else
@@ -669,7 +751,7 @@ void main(void) {
 #ifdef HEMILIGHT2
 	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness);
 #endif
-#ifdef POINTDIRLIGHT2
+#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
 	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
 #endif
 #ifdef SHADOW2
@@ -677,9 +759,17 @@ void main(void) {
 	shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
 #else
 #ifdef SHADOWPCF2
+#if defined(POINTLIGHT2)
+	shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
+#else
 	shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
+#endif
 #else
+	#if defined(POINTLIGHT2)
+	shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
+	#else
 	shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
+	#endif
 #endif	
 #endif	
 #else
@@ -701,7 +791,7 @@ void main(void) {
 #ifdef HEMILIGHT3
 	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness);
 #endif
-#ifdef POINTDIRLIGHT3
+#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
 	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
 #endif
 #ifdef SHADOW3
@@ -709,9 +799,17 @@ void main(void) {
 		shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
 #else
 #ifdef SHADOWPCF3
-		shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
+#if defined(POINTLIGHT3)
+	shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
+#else
+	shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
+#endif
 #else
-		shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
+	#if defined(POINTLIGHT3)
+	shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
+	#else
+	shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
+	#endif
 #endif	
 #endif	
 #else

+ 8 - 8
src/Shaders/default.vertex.fx

@@ -103,19 +103,19 @@ varying float fFogDistance;
 #endif
 
 #ifdef SHADOWS
-#ifdef LIGHT0
+#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
 uniform mat4 lightMatrix0;
 varying vec4 vPositionFromLight0;
 #endif
-#ifdef LIGHT1
+#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
 uniform mat4 lightMatrix1;
 varying vec4 vPositionFromLight1;
 #endif
-#ifdef LIGHT2
+#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
 uniform mat4 lightMatrix2;
 varying vec4 vPositionFromLight2;
 #endif
-#ifdef LIGHT3
+#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
 uniform mat4 lightMatrix3;
 varying vec4 vPositionFromLight3;
 #endif
@@ -265,16 +265,16 @@ void main(void) {
 
 	// Shadows
 #ifdef SHADOWS
-#ifdef LIGHT0
+#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
 	vPositionFromLight0 = lightMatrix0 * worldPos;
 #endif
-#ifdef LIGHT1
+#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
 	vPositionFromLight1 = lightMatrix1 * worldPos;
 #endif
-#ifdef LIGHT2
+#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
 	vPositionFromLight2 = lightMatrix2 * worldPos;
 #endif
-#ifdef LIGHT3
+#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
 	vPositionFromLight3 = lightMatrix3 * worldPos;
 #endif
 #endif

+ 95 - 35
src/Shaders/legacydefault.fragment.fx

@@ -27,8 +27,13 @@ uniform vec4 vLightDiffuse0;
 uniform vec3 vLightSpecular0;
 #endif
 #ifdef SHADOW0
+#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
 varying vec4 vPositionFromLight0;
 uniform sampler2D shadowSampler0;
+#else
+uniform samplerCube shadowSampler0;
+#endif
+uniform vec3 shadowsInfo0;
 #endif
 #ifdef SPOTLIGHT0
 uniform vec4 vLightDirection0;
@@ -45,8 +50,13 @@ uniform vec4 vLightDiffuse1;
 uniform vec3 vLightSpecular1;
 #endif
 #ifdef SHADOW1
+#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
 varying vec4 vPositionFromLight1;
 uniform sampler2D shadowSampler1;
+#else
+uniform samplerCube shadowSampler1;
+#endif
+uniform vec3 shadowsInfo1;
 #endif
 #ifdef SPOTLIGHT1
 uniform vec4 vLightDirection1;
@@ -63,8 +73,13 @@ uniform vec4 vLightDiffuse2;
 uniform vec3 vLightSpecular2;
 #endif
 #ifdef SHADOW2
+#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
 varying vec4 vPositionFromLight2;
 uniform sampler2D shadowSampler2;
+#else
+uniform samplerCube shadowSampler2;
+#endif
+uniform vec3 shadowsInfo2;
 #endif
 #ifdef SPOTLIGHT2
 uniform vec4 vLightDirection2;
@@ -81,8 +96,13 @@ uniform vec4 vLightDiffuse3;
 uniform vec3 vLightSpecular3;
 #endif
 #ifdef SHADOW3
+#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
 varying vec4 vPositionFromLight3;
 uniform sampler2D shadowSampler3;
+#else
+uniform samplerCube shadowSampler3;
+#endif
+uniform vec3 shadowsInfo3;
 #endif
 #ifdef SPOTLIGHT3
 uniform vec4 vLightDirection3;
@@ -166,55 +186,78 @@ uniform vec4 emissiveRightColor;
 
 float unpack(vec4 color)
 {
-	const vec4 bitShift = vec4(1. / (255. * 255. * 255.), 1. / (255. * 255.), 1. / 255., 1.);
-	return dot(color, bitShift);
+	const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
+	return dot(color, bit_shift);
 }
 
-float unpackHalf(vec2 color)
+#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
+float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
 {
-	return color.x + (color.y / 255.0);
+	vec3 directionToLight = vPositionW - lightPosition;
+	float depth = length(directionToLight);
+
+	depth = clamp(depth, 0., 1.);
+
+	directionToLight.y = 1.0 - directionToLight.y;
+
+	float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
+
+	if (depth > shadow)
+	{
+		return darkness;
+	}
+	return 1.0;
 }
+#endif
 
-float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler)
+#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) ||  defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
+float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
 {
 	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
+	depth = 0.5 * depth + vec3(0.5);
+	vec2 uv = depth.xy;
 
 	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
 	{
 		return 1.0;
 	}
 
-	float shadow = unpack(texture2D(shadowSampler, uv));
+	float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
 
 	if (depth.z > shadow)
 	{
-		return 0.;
+		return darkness;
 	}
 	return 1.;
 }
 
 // Thanks to http://devmaster.net/
-float ChebychevInequality(vec2 moments, float t)
+float unpackHalf(vec2 color)
 {
-	if (t <= moments.x)
-	{
-		return 1.0;
-	}
+	return color.x + (color.y / 255.0);
+}
+
+float linstep(float low, float high, float v) {
+	return clamp((v - low) / (high - low), 0.0, 1.0);
+}
 
-	float variance = moments.y - (moments.x * moments.x);
-	variance = max(variance, 0.);
+float ChebychevInequality(vec2 moments, float compare, float bias)
+{
+	float p = smoothstep(compare - bias, compare, moments.x);
+	float variance = max(moments.y - moments.x * moments.x, 0.02);
+	float d = compare - moments.x;
+	float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
 
-	float d = t - moments.x;
-	return variance / (variance + d * d);
+	return clamp(max(p, p_max), 0.0, 1.0);
 }
 
-float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler)
+float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
 {
 	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
+	depth = 0.5 * depth + vec3(0.5);
+	vec2 uv = depth.xy;
 
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
+	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
 	{
 		return 1.0;
 	}
@@ -222,9 +265,10 @@ float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler)
 	vec4 texel = texture2D(shadowSampler, uv);
 
 	vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
-	return clamp(1.3 - ChebychevInequality(moments, depth.z), 0., 1.0);
+	return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
 }
 #endif
+#endif
 
 #ifdef CLIPPLANE
 varying float fClipDistance;
@@ -417,14 +461,18 @@ void main(void) {
 #ifdef HEMILIGHT0
 	mat3 info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0, vLightGround0);
 #endif
-#ifdef POINTDIRLIGHT0
+#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
 	mat3 info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0);
 #endif
 #ifdef SHADOW0
 #ifdef SHADOWVSM0
-	shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0);
+	shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
+#else
+#if defined(POINTLIGHT0)
+	shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
 #else
-	shadow = computeShadow(vPositionFromLight0, shadowSampler0);
+	shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
+#endif
 #endif
 #else
 	shadow = 1.;
@@ -445,14 +493,18 @@ void main(void) {
 #ifdef HEMILIGHT1
 	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1, vLightGround1);
 #endif
-#ifdef POINTDIRLIGHT1
+#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
 	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1);
 #endif
 #ifdef SHADOW1
 #ifdef SHADOWVSM1
-	shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1);
+	shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
+#else
+#if defined(POINTLIGHT1)
+	shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
 #else
-	shadow = computeShadow(vPositionFromLight1, shadowSampler1);
+	shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
+#endif
 #endif
 #else
 	shadow = 1.;
@@ -473,15 +525,19 @@ void main(void) {
 #ifdef HEMILIGHT2
 	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2, vLightGround2);
 #endif
-#ifdef POINTDIRLIGHT2
+#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
 	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2);
 #endif
 #ifdef SHADOW2
 #ifdef SHADOWVSM2
-	shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2);
+	shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
 #else
-	shadow = computeShadow(vPositionFromLight2, shadowSampler2);
-#endif	
+#if defined(POINTLIGHT2)
+	shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
+#else
+	shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
+#endif
+#endif
 #else
 	shadow = 1.;
 #endif
@@ -501,15 +557,19 @@ void main(void) {
 #ifdef HEMILIGHT3
 	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3, vLightGround3);
 #endif
-#ifdef POINTDIRLIGHT3
+#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
 	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3);
 #endif
 #ifdef SHADOW3
 #ifdef SHADOWVSM3
-	shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3);
+	shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
+#else
+#if defined(POINTLIGHT3)
+	shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
 #else
-	shadow = computeShadow(vPositionFromLight3, shadowSampler3);
-#endif	
+	shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
+#endif
+#endif
 #else
 	shadow = 1.;
 #endif

+ 8 - 8
src/Shaders/legacydefault.vertex.fx

@@ -80,19 +80,19 @@ varying float fFogDistance;
 #endif
 
 #ifdef SHADOWS
-#ifdef LIGHT0
+#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
 uniform mat4 lightMatrix0;
 varying vec4 vPositionFromLight0;
 #endif
-#ifdef LIGHT1
+#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
 uniform mat4 lightMatrix1;
 varying vec4 vPositionFromLight1;
 #endif
-#ifdef LIGHT2
+#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
 uniform mat4 lightMatrix2;
 varying vec4 vPositionFromLight2;
 #endif
-#ifdef LIGHT3
+#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
 uniform mat4 lightMatrix3;
 varying vec4 vPositionFromLight3;
 #endif
@@ -256,16 +256,16 @@ void main(void) {
 
 	// Shadows
 #ifdef SHADOWS
-#ifdef LIGHT0
+#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
 	vPositionFromLight0 = lightMatrix0 * worldPos;
 #endif
-#ifdef LIGHT1
+#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
 	vPositionFromLight1 = lightMatrix1 * worldPos;
 #endif
-#ifdef LIGHT2
+#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
 	vPositionFromLight2 = lightMatrix2 * worldPos;
 #endif
-#ifdef LIGHT3
+#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
 	vPositionFromLight3 = lightMatrix3 * worldPos;
 #endif
 #endif

+ 6 - 0
src/babylon.engine.js

@@ -819,6 +819,12 @@ var BABYLON;
             var width = navigator.isCocoonJS ? window.innerWidth : this._renderingCanvas.clientWidth;
             var height = navigator.isCocoonJS ? window.innerHeight : this._renderingCanvas.clientHeight;
             this.setSize(width / this._hardwareScalingLevel, height / this._hardwareScalingLevel);
+            for (var index = 0; index < this.scenes.length; index++) {
+                var scene = this.scenes[index];
+                if (scene.debugLayer.isVisible()) {
+                    scene.debugLayer._syncPositions();
+                }
+            }
         };
         /**
          * force a specific size of the canvas

+ 7 - 0
src/babylon.engine.ts

@@ -935,6 +935,13 @@
             var height = navigator.isCocoonJS ? window.innerHeight : this._renderingCanvas.clientHeight;
 
             this.setSize(width / this._hardwareScalingLevel, height / this._hardwareScalingLevel);
+
+            for (var index = 0; index < this.scenes.length; index++) {
+                var scene = this.scenes[index];
+                if (scene.debugLayer.isVisible()) {
+                    scene.debugLayer._syncPositions();
+                }
+            }
         }
 
         /**