瀏覽代碼

DepthRenderer
AudioEngine

David Catuhe 10 年之前
父節點
當前提交
71f1a4eb0d

+ 7 - 0
Babylon/Audio/babylon.audioengine.js

@@ -24,6 +24,13 @@
                 this.masterGain.connect(this.audioContext.destination);
             }
         }
+        AudioEngine.prototype.dispose = function () {
+            this.canUseWebAudio = false;
+            this.masterGain.disconnect();
+            this.masterGain = null;
+            this.audioContext = null;
+        };
+
         AudioEngine.prototype.getGlobalVolume = function () {
             if (this.canUseWebAudio) {
                 return this.masterGain.gain.value;

+ 7 - 0
Babylon/Audio/babylon.audioengine.ts

@@ -27,6 +27,13 @@
             }
         }
 
+        public dispose() {
+            this.canUseWebAudio = false;
+            this.masterGain.disconnect();
+            this.masterGain = null;
+            this.audioContext = null;
+        }
+
         public getGlobalVolume(): number {
             if (this.canUseWebAudio) {
                 return this.masterGain.gain.value;

+ 36 - 7
Babylon/Audio/babylon.sound.js

@@ -19,8 +19,8 @@
             this.maxDistance = 100;
             this.distanceModel = "linear";
             this.panningModel = "HRTF";
-            this.startTime = 0;
-            this.startOffset = 0;
+            this._startTime = 0;
+            this._startOffset = 0;
             this._position = BABYLON.Vector3.Zero();
             this._localDirection = new BABYLON.Vector3(1, 0, 0);
             this._volume = 1;
@@ -81,6 +81,34 @@
                 }
             }
         }
+        Sound.prototype.dispose = function () {
+            if (this._isReadyToPlay) {
+                if (this._isPlaying) {
+                    this.stop();
+                }
+                this._isReadyToPlay = false;
+                if (this.soundTrackId === -1) {
+                    this._scene.mainSoundTrack.RemoveSound(this);
+                } else {
+                    this._scene.soundTracks[this.soundTrackId].RemoveSound(this);
+                }
+                this._soundGain.disconnect();
+                this._soundSource.disconnect();
+                this._audioBuffer = null;
+                this._soundGain = null;
+                this._soundSource = null;
+                if (this._soundPanner) {
+                    this._soundPanner.disconnect();
+                    this._soundPanner = null;
+                }
+                this._audioNode.disconnect();
+                if (this._connectedMesh) {
+                    this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
+                    this._connectedMesh = null;
+                }
+            }
+        };
+
         Sound.prototype._soundLoaded = function (audioData) {
             var _this = this;
             this._isLoaded = true;
@@ -221,8 +249,8 @@
                     }
                     this._soundSource.connect(this._audioNode);
                     this._soundSource.loop = this.loop;
-                    this.startTime = startTime;
-                    this._soundSource.start(startTime, this.startOffset % this._soundSource.buffer.duration);
+                    this._startTime = startTime;
+                    this._soundSource.start(startTime, this._startOffset % this._soundSource.buffer.duration);
                     this._isPlaying = true;
                 } catch (ex) {
                     BABYLON.Tools.Error("Error while trying to play audio: " + this._name + ", " + ex.message);
@@ -245,7 +273,7 @@
         Sound.prototype.pause = function () {
             if (this._isPlaying) {
                 this._soundSource.stop(0);
-                this.startOffset += this._audioEngine.audioContext.currentTime - this.startTime;
+                this._startOffset += this._audioEngine.audioContext.currentTime - this._startTime;
             }
         };
 
@@ -271,9 +299,10 @@
                     this.play();
                 }
             }
-            meshToConnectTo.registerAfterWorldMatrixUpdate(function (connectedMesh) {
+            this._registerFunc = function (connectedMesh) {
                 return _this._onRegisterAfterWorldMatrixUpdate(connectedMesh);
-            });
+            };
+            meshToConnectTo.registerAfterWorldMatrixUpdate(this._registerFunc);
         };
 
         Sound.prototype._onRegisterAfterWorldMatrixUpdate = function (connectedMesh) {

+ 37 - 6
Babylon/Audio/babylon.sound.ts

@@ -10,8 +10,8 @@
         public maxDistance: number = 100;
         public distanceModel: string = "linear";
         public panningModel: string = "HRTF";
-        private startTime: number = 0;
-        private startOffset: number = 0;
+        private _startTime: number = 0;
+        private _startOffset: number = 0;
         private _position: Vector3 = Vector3.Zero();
         private _localDirection: Vector3 = new Vector3(1, 0, 0);
         private _volume: number = 1;
@@ -35,6 +35,7 @@
         private _name: string;
         private _connectedMesh: BABYLON.AbstractMesh;
         private _customAttenuationFunction: (currentVolume: number, currentDistance: number, maxDistance: number, refDistance: number, rolloffFactor: number) => number;
+        private _registerFunc;
 
         /**
         * Create a sound and attach it to a scene
@@ -94,6 +95,35 @@
             }
         }
 
+        public dispose() {
+            if (this._isReadyToPlay) {
+                if (this._isPlaying) {
+                    this.stop();
+                }
+                this._isReadyToPlay = false;
+                if (this.soundTrackId === -1) {
+                    this._scene.mainSoundTrack.RemoveSound(this);
+                }
+                else {
+                    this._scene.soundTracks[this.soundTrackId].RemoveSound(this);
+                }
+                this._soundGain.disconnect();
+                this._soundSource.disconnect();
+                this._audioBuffer = null;
+                this._soundGain = null;
+                this._soundSource = null;
+                if (this._soundPanner) {
+                    this._soundPanner.disconnect();
+                    this._soundPanner = null;
+                }
+                this._audioNode.disconnect();
+                if (this._connectedMesh) {
+                    this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
+                    this._connectedMesh = null;
+                }
+            }
+        }
+
         private _soundLoaded(audioData: ArrayBuffer) {
             this._isLoaded = true;
             this._audioEngine.audioContext.decodeAudioData(audioData,(buffer) => {
@@ -231,8 +261,8 @@
                     }
                     this._soundSource.connect(this._audioNode);
                     this._soundSource.loop = this.loop;
-                    this.startTime = startTime;
-                    this._soundSource.start(startTime, this.startOffset % this._soundSource.buffer.duration);
+                    this._startTime = startTime;
+                    this._soundSource.start(startTime, this._startOffset % this._soundSource.buffer.duration);
                     this._isPlaying = true;
                 }
                 catch (ex) {
@@ -256,7 +286,7 @@
         public pause() {
             if (this._isPlaying) {
                 this._soundSource.stop(0);
-                this.startOffset += this._audioEngine.audioContext.currentTime - this.startTime;
+                this._startOffset += this._audioEngine.audioContext.currentTime - this._startTime;
             }
         }
 
@@ -281,7 +311,8 @@
                     this.play();
                 }
             }
-            meshToConnectTo.registerAfterWorldMatrixUpdate((connectedMesh: BABYLON.AbstractMesh) => this._onRegisterAfterWorldMatrixUpdate(connectedMesh));
+            this._registerFunc = (connectedMesh: BABYLON.AbstractMesh) => this._onRegisterAfterWorldMatrixUpdate(connectedMesh);
+            meshToConnectTo.registerAfterWorldMatrixUpdate(this._registerFunc);
         }
 
         private _onRegisterAfterWorldMatrixUpdate(connectedMesh: BABYLON.AbstractMesh) {

+ 8 - 3
Babylon/Audio/babylon.soundtrack.js

@@ -9,9 +9,6 @@
             this.soundCollection = new Array();
             if (this._audioEngine.canUseWebAudio) {
                 this._trackGain = this._audioEngine.audioContext.createGain();
-
-                //this._trackConvolver = this._audioEngine.audioContext.createConvolver();
-                //this._trackConvolver.connect(this._trackGain);
                 this._trackGain.connect(this._audioEngine.masterGain);
 
                 if (options) {
@@ -28,6 +25,14 @@
                 this.id = this._scene.soundTracks.length - 1;
             }
         }
+        SoundTrack.prototype.dispose = function () {
+            while (this.soundCollection.length) {
+                this.soundCollection[0].dispose();
+            }
+            this._trackGain.disconnect();
+            this._trackGain = null;
+        };
+
         SoundTrack.prototype.AddSound = function (sound) {
             sound.connectToSoundTrackAudioNode(this._trackGain);
             if (sound.soundTrackId) {

+ 8 - 2
Babylon/Audio/babylon.soundtrack.ts

@@ -14,8 +14,6 @@
             this.soundCollection = new Array();
             if (this._audioEngine.canUseWebAudio) {
                 this._trackGain = this._audioEngine.audioContext.createGain();
-                //this._trackConvolver = this._audioEngine.audioContext.createConvolver();
-                //this._trackConvolver.connect(this._trackGain);
                 this._trackGain.connect(this._audioEngine.masterGain);
 
                 if (options) {
@@ -29,6 +27,14 @@
             }
         }
 
+        public dispose() {
+            while (this.soundCollection.length) {
+                this.soundCollection[0].dispose();
+            }
+            this._trackGain.disconnect();
+            this._trackGain = null;
+        }
+
         public AddSound(sound: BABYLON.Sound) {
             sound.connectToSoundTrackAudioNode(this._trackGain);
             if (sound.soundTrackId) {

+ 9 - 7
Babylon/Materials/Textures/babylon.renderTargetTexture.js

@@ -54,7 +54,7 @@ var BABYLON;
                 return true;
             }
 
-            if (this.refreshRate == this._currentRefreshId) {
+            if (this.refreshRate === this._currentRefreshId) {
                 this._currentRefreshId = 1;
                 return true;
             }
@@ -107,7 +107,7 @@ var BABYLON;
                 delete this._waitingRenderList;
             }
 
-            if (!this.renderList) {
+            if (this.renderList && this.renderList.length === 0) {
                 return;
             }
 
@@ -121,8 +121,10 @@ var BABYLON;
 
             this._renderingManager.reset();
 
-            for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
-                var mesh = this.renderList[meshIndex];
+            var currentRenderList = this.renderList ? this.renderList : scene.getActiveMeshes().data;
+
+            for (var meshIndex = 0; meshIndex < currentRenderList.length; meshIndex++) {
+                var mesh = currentRenderList[meshIndex];
 
                 if (mesh) {
                     if (!mesh.isReady() || (mesh.material && !mesh.material.isReady())) {
@@ -131,7 +133,7 @@ var BABYLON;
                         continue;
                     }
 
-                    if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) != 0)) {
+                    if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) !== 0)) {
                         mesh._activate(scene.getRenderId());
 
                         for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
@@ -152,7 +154,7 @@ var BABYLON;
             }
 
             // Render
-            this._renderingManager.render(this.customRenderFunction, this.renderList, this.renderParticles, this.renderSprites);
+            this._renderingManager.render(this.customRenderFunction, currentRenderList, this.renderParticles, this.renderSprites);
 
             if (useCameraPostProcess) {
                 scene.postProcessManager._finalizeFrame(false, this._texture);
@@ -172,7 +174,7 @@ var BABYLON;
 
         RenderTargetTexture.prototype.clone = function () {
             var textureSize = this.getSize();
-            var newTexture = new BABYLON.RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
+            var newTexture = new RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
 
             // Base texture
             newTexture.hasAlpha = this.hasAlpha;

+ 11 - 9
Babylon/Materials/Textures/babylon.renderTargetTexture.ts

@@ -3,7 +3,7 @@
         public renderList = new Array<AbstractMesh>();
         public renderParticles = true;
         public renderSprites = false;
-        public coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
+        public coordinatesMode = Texture.PROJECTION_MODE;
         public onBeforeRender: () => void;
         public onAfterRender: () => void;
         public activeCamera: Camera;
@@ -29,7 +29,7 @@
             this._texture = scene.getEngine().createRenderTargetTexture(size, { generateMipMaps: generateMipMaps, type: type });
 
             // Rendering groups
-            this._renderingManager = new BABYLON.RenderingManager(scene);
+            this._renderingManager = new RenderingManager(scene);
         }
 
         public resetRefreshCounter(): void {
@@ -52,7 +52,7 @@
                 return true;
             }
 
-            if (this.refreshRate == this._currentRefreshId) {
+            if (this.refreshRate === this._currentRefreshId) {
                 this._currentRefreshId = 1;
                 return true;
             }
@@ -101,7 +101,7 @@
                 delete this._waitingRenderList;
             }
 
-            if (!this.renderList) {
+            if (this.renderList && this.renderList.length === 0) {
                 return;
             }
 
@@ -115,8 +115,10 @@
 
             this._renderingManager.reset();
 
-            for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
-                var mesh = this.renderList[meshIndex];
+            var currentRenderList = this.renderList ? this.renderList : scene.getActiveMeshes().data;
+
+            for (var meshIndex = 0; meshIndex < currentRenderList.length; meshIndex++) {
+                var mesh = currentRenderList[meshIndex];
 
                 if (mesh) {
                     if (!mesh.isReady() || (mesh.material && !mesh.material.isReady())) {
@@ -125,7 +127,7 @@
                         continue;
                     }
 
-                    if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) != 0)) {
+                    if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) !== 0)) {
                         mesh._activate(scene.getRenderId());
 
                         for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
@@ -146,7 +148,7 @@
             }
 
             // Render
-            this._renderingManager.render(this.customRenderFunction, this.renderList, this.renderParticles, this.renderSprites);
+            this._renderingManager.render(this.customRenderFunction, currentRenderList, this.renderParticles, this.renderSprites);
 
             if (useCameraPostProcess) {
                 scene.postProcessManager._finalizeFrame(false, this._texture);
@@ -166,7 +168,7 @@
 
         public clone(): RenderTargetTexture {
             var textureSize = this.getSize();
-            var newTexture = new BABYLON.RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
+            var newTexture = new RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
 
             // Base texture
             newTexture.hasAlpha = this.hasAlpha;

+ 2 - 2
Babylon/Mesh/babylon.InstancedMesh.js

@@ -163,7 +163,7 @@ var BABYLON;
                 for (var index = 0; index < this.getScene().meshes.length; index++) {
                     var mesh = this.getScene().meshes[index];
 
-                    if (mesh.parent == this) {
+                    if (mesh.parent === this) {
                         mesh.clone(mesh.name, result);
                     }
                 }
@@ -186,4 +186,4 @@ var BABYLON;
     })(BABYLON.AbstractMesh);
     BABYLON.InstancedMesh = InstancedMesh;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.InstancedMesh.js.map
+//# sourceMappingURL=babylon.instancedMesh.js.map

+ 5 - 5
Babylon/Mesh/babylon.InstancedMesh.ts

@@ -72,11 +72,11 @@
         }
 
         public refreshBoundingInfo(): void {
-            var data = this._sourceMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
+            var data = this._sourceMesh.getVerticesData(VertexBuffer.PositionKind);
 
             if (data) {
-                var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._sourceMesh.getTotalVertices());
-                this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
+                var extend = Tools.ExtractMinAndMax(data, 0, this._sourceMesh.getTotalVertices());
+                this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
             }
 
             this._updateBoundingInfo();
@@ -122,7 +122,7 @@
             var result = this._sourceMesh.createInstance(name);
 
             // Deep copy
-            BABYLON.Tools.DeepCopy(this, result, ["name"], []);
+            Tools.DeepCopy(this, result, ["name"], []);
 
             // Bounding info
             this.refreshBoundingInfo();
@@ -137,7 +137,7 @@
                 for (var index = 0; index < this.getScene().meshes.length; index++) {
                     var mesh = this.getScene().meshes[index];
 
-                    if (mesh.parent == this) {
+                    if (mesh.parent === this) {
                         mesh.clone(mesh.name, result);
                     }
                 }

+ 57 - 0
Babylon/Mesh/babylon.abstractMesh.js

@@ -11,6 +11,7 @@ var BABYLON;
         function AbstractMesh(name, scene) {
             _super.call(this, name, scene);
             // Properties
+            this.definedFacingForward = true;
             this.position = new BABYLON.Vector3(0, 0, 0);
             this.rotation = new BABYLON.Vector3(0, 0, 0);
             this.scaling = new BABYLON.Vector3(1, 1, 1);
@@ -258,6 +259,62 @@ var BABYLON;
             }
         };
 
+        // ================================== Point of View Movement =================================
+        /**
+        * Perform relative position change from the point of view of behind the front of the mesh.
+        * This is performed taking into account the meshes current rotation, so you do not have to care.
+        * Supports definition of mesh facing forward or backward.
+        * @param {number} amountRight
+        * @param {number} amountUp
+        * @param {number} amountForward
+        */
+        AbstractMesh.prototype.movePOV = function (amountRight, amountUp, amountForward) {
+            this.position.addInPlace(this.calcMovePOV(amountRight, amountUp, amountForward));
+        };
+
+        /**
+        * Calculate relative position change from the point of view of behind the front of the mesh.
+        * This is performed taking into account the meshes current rotation, so you do not have to care.
+        * Supports definition of mesh facing forward or backward.
+        * @param {number} amountRight
+        * @param {number} amountUp
+        * @param {number} amountForward
+        */
+        AbstractMesh.prototype.calcMovePOV = function (amountRight, amountUp, amountForward) {
+            var rotMatrix = new BABYLON.Matrix();
+            var rotQuaternion = (this.rotationQuaternion) ? this.rotationQuaternion : BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
+            rotQuaternion.toRotationMatrix(rotMatrix);
+
+            var translationDelta = BABYLON.Vector3.Zero();
+            var defForwardMult = this.definedFacingForward ? -1 : 1;
+            BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(amountRight * defForwardMult, amountUp, amountForward * defForwardMult, rotMatrix, translationDelta);
+            return translationDelta;
+        };
+
+        // ================================== Point of View Rotation =================================
+        /**
+        * Perform relative rotation change from the point of view of behind the front of the mesh.
+        * Supports definition of mesh facing forward or backward.
+        * @param {number} flipBack
+        * @param {number} twirlClockwise
+        * @param {number} tiltRight
+        */
+        AbstractMesh.prototype.rotatePOV = function (flipBack, twirlClockwise, tiltRight) {
+            this.rotation.addInPlace(this.calcRotatePOV(flipBack, twirlClockwise, tiltRight));
+        };
+
+        /**
+        * Calculate relative rotation change from the point of view of behind the front of the mesh.
+        * Supports definition of mesh facing forward or backward.
+        * @param {number} flipBack
+        * @param {number} twirlClockwise
+        * @param {number} tiltRight
+        */
+        AbstractMesh.prototype.calcRotatePOV = function (flipBack, twirlClockwise, tiltRight) {
+            var defForwardMult = this.definedFacingForward ? 1 : -1;
+            return new BABYLON.Vector3(flipBack * defForwardMult, twirlClockwise, tiltRight * defForwardMult);
+        };
+
         AbstractMesh.prototype.setPivotMatrix = function (matrix) {
             this._pivotMatrix = matrix;
             this._cache.pivotMatrixUpdated = true;

+ 0 - 3
Babylon/Mesh/babylon.mesh.js

@@ -80,9 +80,6 @@ var BABYLON;
                 this.parent = parent;
             }
         }
-        Mesh.prototype._clone = function () {
-        };
-
         Object.defineProperty(Mesh.prototype, "hasLODLevels", {
             // Methods
             get: function () {

+ 0 - 3
Babylon/Mesh/babylon.mesh.ts

@@ -83,9 +83,6 @@
 
         }
 
-        private _clone() {
-        }
-
         // Methods
         public get hasLODLevels(): boolean {
             return this._LODLevels.length > 0;

+ 160 - 0
Babylon/Rendering/babylon.depthRenderer.js

@@ -0,0 +1,160 @@
+var BABYLON;
+(function (BABYLON) {
+    var DepthRenderer = (function () {
+        function DepthRenderer(scene, type) {
+            if (typeof type === "undefined") { type = BABYLON.Engine.TEXTURETYPE_FLOAT; }
+            var _this = this;
+            this._viewMatrix = BABYLON.Matrix.Zero();
+            this._projectionMatrix = BABYLON.Matrix.Zero();
+            this._transformMatrix = BABYLON.Matrix.Zero();
+            this._worldViewProjection = BABYLON.Matrix.Zero();
+            this._scene = scene;
+            var engine = scene.getEngine();
+
+            // Render target
+            this._depthMap = new BABYLON.RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type);
+            this._depthMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
+            this._depthMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
+            this._depthMap.refreshRate = 1;
+            this._depthMap.renderParticles = false;
+            this._depthMap.renderList = null;
+
+            // Custom render function
+            var renderSubMesh = function (subMesh) {
+                var mesh = subMesh.getRenderingMesh();
+                var scene = _this._scene;
+                var engine = scene.getEngine();
+
+                // Culling
+                engine.setState(subMesh.getMaterial().backFaceCulling);
+
+                // Managing instances
+                var batch = mesh._getInstancesRenderList(subMesh._id);
+
+                if (batch.mustReturn) {
+                    return;
+                }
+
+                var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
+
+                if (_this.isReady(subMesh, hardwareInstancedRendering)) {
+                    engine.enableEffect(_this._effect);
+                    mesh._bind(subMesh, _this._effect, BABYLON.Material.TriangleFillMode);
+                    var material = subMesh.getMaterial();
+
+                    _this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
+
+                    _this._effect.setFloat("far", scene.activeCamera.maxZ);
+
+                    // Alpha test
+                    if (material && material.needAlphaTesting()) {
+                        var alphaTexture = material.getAlphaTestTexture();
+                        _this._effect.setTexture("diffuseSampler", alphaTexture);
+                        _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
+                    }
+
+                    // Bones
+                    var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
+
+                    if (useBones) {
+                        _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
+                    }
+
+                    if (hardwareInstancedRendering) {
+                        mesh._renderWithInstances(subMesh, BABYLON.Material.TriangleFillMode, batch, _this._effect, engine);
+                    } else {
+                        if (batch.renderSelf[subMesh._id]) {
+                            _this._effect.setMatrix("world", mesh.getWorldMatrix());
+
+                            // Draw
+                            mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
+                        }
+
+                        if (batch.visibleInstances[subMesh._id]) {
+                            for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
+                                var instance = batch.visibleInstances[subMesh._id][instanceIndex];
+
+                                _this._effect.setMatrix("world", instance.getWorldMatrix());
+
+                                // Draw
+                                mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
+                            }
+                        }
+                    }
+                }
+            };
+
+            this._depthMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
+                var index;
+
+                for (index = 0; index < opaqueSubMeshes.length; index++) {
+                    renderSubMesh(opaqueSubMeshes.data[index]);
+                }
+
+                for (index = 0; index < alphaTestSubMeshes.length; index++) {
+                    renderSubMesh(alphaTestSubMeshes.data[index]);
+                }
+            };
+        }
+        DepthRenderer.prototype.isReady = function (subMesh, useInstances) {
+            var defines = [];
+
+            var attribs = [BABYLON.VertexBuffer.PositionKind];
+
+            var mesh = subMesh.getMesh();
+            var scene = mesh.getScene();
+            var material = subMesh.getMaterial();
+
+            // Alpha test
+            if (material && material.needAlphaTesting()) {
+                defines.push("#define ALPHATEST");
+                if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
+                    attribs.push(BABYLON.VertexBuffer.UVKind);
+                    defines.push("#define UV1");
+                }
+                if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
+                    attribs.push(BABYLON.VertexBuffer.UV2Kind);
+                    defines.push("#define UV2");
+                }
+            }
+
+            // Bones
+            if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
+                attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
+                attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
+                defines.push("#define BONES");
+                defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
+            }
+
+            // Instances
+            if (useInstances) {
+                defines.push("#define INSTANCES");
+                attribs.push("world0");
+                attribs.push("world1");
+                attribs.push("world2");
+                attribs.push("world3");
+            }
+
+            // Get correct effect
+            var join = defines.join("\n");
+            if (this._cachedDefines !== join) {
+                this._cachedDefines = join;
+                this._effect = this._scene.getEngine().createEffect("depth", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler"], join);
+            }
+
+            return this._effect.isReady();
+        };
+
+        DepthRenderer.prototype.getDepthMap = function () {
+            return this._depthMap;
+        };
+
+        // Methods
+        DepthRenderer.prototype.dispose = function () {
+            this._depthMap.dispose();
+        };
+        return DepthRenderer;
+    })();
+    BABYLON.DepthRenderer = DepthRenderer;
+})(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.depthRenderer.js.map

+ 165 - 0
Babylon/Rendering/babylon.depthRenderer.ts

@@ -0,0 +1,165 @@
+module BABYLON {
+    export class DepthRenderer {
+        private _scene: Scene;
+        private _depthMap: RenderTargetTexture;
+        private _effect: Effect;
+
+        private _viewMatrix = Matrix.Zero();
+        private _projectionMatrix = Matrix.Zero();
+        private _transformMatrix = Matrix.Zero();
+        private _worldViewProjection = Matrix.Zero();
+
+        private _cachedDefines: string;
+
+        constructor(scene: Scene, type: number = Engine.TEXTURETYPE_FLOAT) {
+            this._scene = scene;
+            var engine = scene.getEngine();
+
+            // Render target
+            this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight()}, this._scene, false, true, type);
+            this._depthMap.wrapU = Texture.CLAMP_ADDRESSMODE;
+            this._depthMap.wrapV = Texture.CLAMP_ADDRESSMODE;
+            this._depthMap.refreshRate = 1;
+            this._depthMap.renderParticles = false;
+            this._depthMap.renderList = null;
+
+            // Custom render function
+            var renderSubMesh = (subMesh: SubMesh): void => {
+                var mesh = subMesh.getRenderingMesh();
+                var scene = this._scene;
+                var engine = scene.getEngine();
+
+                // Culling
+                engine.setState(subMesh.getMaterial().backFaceCulling);
+
+                // Managing instances
+                var batch = mesh._getInstancesRenderList(subMesh._id);
+
+                if (batch.mustReturn) {
+                    return;
+                }
+
+                var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
+
+                if (this.isReady(subMesh, hardwareInstancedRendering)) {
+                    engine.enableEffect(this._effect);
+                    mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
+                    var material = subMesh.getMaterial();
+
+                    this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
+
+                    this._effect.setFloat("far", scene.activeCamera.maxZ);
+
+                    // Alpha test
+                    if (material && material.needAlphaTesting()) {
+                        var alphaTexture = material.getAlphaTestTexture();
+                        this._effect.setTexture("diffuseSampler", alphaTexture);
+                        this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
+                    }
+
+                    // Bones
+                    var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind);
+
+                    if (useBones) {
+                        this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
+                    }
+
+                    if (hardwareInstancedRendering) {
+                        mesh._renderWithInstances(subMesh, Material.TriangleFillMode, batch, this._effect, engine);
+                    } else {
+                        if (batch.renderSelf[subMesh._id]) {
+                            this._effect.setMatrix("world", mesh.getWorldMatrix());
+
+                            // Draw
+                            mesh._draw(subMesh, Material.TriangleFillMode);
+                        }
+
+                        if (batch.visibleInstances[subMesh._id]) {
+                            for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
+                                var instance = batch.visibleInstances[subMesh._id][instanceIndex];
+
+                                this._effect.setMatrix("world", instance.getWorldMatrix());
+
+                                // Draw
+                                mesh._draw(subMesh, Material.TriangleFillMode);
+                            }
+                        }
+                    }
+                }
+            };
+
+            this._depthMap.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>): void => {
+                var index;
+
+                for (index = 0; index < opaqueSubMeshes.length; index++) {
+                    renderSubMesh(opaqueSubMeshes.data[index]);
+                }
+
+                for (index = 0; index < alphaTestSubMeshes.length; index++) {
+                    renderSubMesh(alphaTestSubMeshes.data[index]);
+                }
+            };
+        }
+
+        public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
+            var defines = [];
+
+            var attribs = [VertexBuffer.PositionKind];
+
+            var mesh = subMesh.getMesh();
+            var scene = mesh.getScene();
+            var material = subMesh.getMaterial();
+
+            // Alpha test
+            if (material && material.needAlphaTesting()) {
+                defines.push("#define ALPHATEST");
+                if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
+                    attribs.push(VertexBuffer.UVKind);
+                    defines.push("#define UV1");
+                }
+                if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
+                    attribs.push(VertexBuffer.UV2Kind);
+                    defines.push("#define UV2");
+                }
+            }
+
+            // Bones
+            if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
+                attribs.push(VertexBuffer.MatricesIndicesKind);
+                attribs.push(VertexBuffer.MatricesWeightsKind);
+                defines.push("#define BONES");
+                defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
+            }
+
+            // Instances
+            if (useInstances) {
+                defines.push("#define INSTANCES");
+                attribs.push("world0");
+                attribs.push("world1");
+                attribs.push("world2");
+                attribs.push("world3");
+            }
+
+            // Get correct effect      
+            var join = defines.join("\n");
+            if (this._cachedDefines !== join) {
+                this._cachedDefines = join;
+                this._effect = this._scene.getEngine().createEffect("depth",
+                    attribs,
+                    ["world", "mBones", "viewProjection", "diffuseMatrix", "far"],
+                    ["diffuseSampler"], join);
+            }
+
+            return this._effect.isReady();
+        }
+
+        public getDepthMap(): RenderTargetTexture {
+            return this._depthMap;
+        }
+
+        // Methods
+        public dispose(): void {
+            this._depthMap.dispose();
+        }
+    }
+} 

+ 1 - 1
Babylon/Rendering/babylon.renderingManager.js

@@ -56,7 +56,7 @@
         };
 
         RenderingManager.prototype.render = function (customRenderFunction, activeMeshes, renderParticles, renderSprites) {
-            for (var index = 0; index < BABYLON.RenderingManager.MAX_RENDERINGGROUPS; index++) {
+            for (var index = 0; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
                 this._depthBufferAlreadyCleaned = false;
                 var renderingGroup = this._renderingGroups[index];
 

+ 2 - 2
Babylon/Rendering/babylon.renderingManager.ts

@@ -62,7 +62,7 @@
 
         public render(customRenderFunction: (opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>) => void,
             activeMeshes: AbstractMesh[], renderParticles: boolean, renderSprites: boolean): void {
-            for (var index = 0; index < BABYLON.RenderingManager.MAX_RENDERINGGROUPS; index++) {
+            for (var index = 0; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
                 this._depthBufferAlreadyCleaned = false;
                 var renderingGroup = this._renderingGroups[index];
 
@@ -91,7 +91,7 @@
             var renderingGroupId = mesh.renderingGroupId || 0;
 
             if (!this._renderingGroups[renderingGroupId]) {
-                this._renderingGroups[renderingGroupId] = new BABYLON.RenderingGroup(renderingGroupId, this._scene);
+                this._renderingGroups[renderingGroupId] = new RenderingGroup(renderingGroupId, this._scene);
             }
 
             this._renderingGroups[renderingGroupId].dispatch(subMesh);

+ 21 - 0
Babylon/Shaders/depth.fragment.fx

@@ -0,0 +1,21 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+#ifdef ALPHATEST
+varying vec2 vUV;
+uniform sampler2D diffuseSampler;
+#endif
+
+uniform float far;
+
+void main(void)
+{
+#ifdef ALPHATEST
+	if (texture2D(diffuseSampler, vUV).a < 0.4)
+		discard;
+#endif
+
+	float depth = gl_FragCoord.z / far;
+	gl_FragColor = vec4(depth, depth * depth, 0.0, 1.0);
+}

+ 65 - 0
Babylon/Shaders/depth.vertex.fx

@@ -0,0 +1,65 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+// Attribute
+attribute vec3 position;
+#ifdef BONES
+attribute vec4 matricesIndices;
+attribute vec4 matricesWeights;
+#endif
+
+// Uniform
+#ifdef INSTANCES
+attribute vec4 world0;
+attribute vec4 world1;
+attribute vec4 world2;
+attribute vec4 world3;
+#else
+uniform mat4 world;
+#endif
+
+uniform mat4 viewProjection;
+#ifdef BONES
+uniform mat4 mBones[BonesPerMesh];
+#endif
+
+#ifdef ALPHATEST
+varying vec2 vUV;
+uniform mat4 diffuseMatrix;
+#ifdef UV1
+attribute vec2 uv;
+#endif
+#ifdef UV2
+attribute vec2 uv2;
+#endif
+#endif
+
+void main(void)
+{
+#ifdef INSTANCES
+	mat4 finalWorld = mat4(world0, world1, world2, world3);
+#else
+	mat4 finalWorld = world;
+#endif
+
+#ifdef BONES
+	mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
+	mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
+	mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
+	mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
+	finalWorld = finalWorld * (m0 + m1 + m2 + m3);
+	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
+#else
+	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
+#endif
+
+#ifdef ALPHATEST
+#ifdef UV1
+	vUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));
+#endif
+#ifdef UV2
+	vUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));
+#endif
+#endif
+}

+ 3 - 0
Babylon/babylon.engine.js

@@ -1984,6 +1984,9 @@
                 this.scenes[0].dispose();
             }
 
+            // Release audio engine
+            this._audioEngine.dispose();
+
             for (var name in this._compiledEffects) {
                 this._gl.deleteProgram(this._compiledEffects[name]._program);
             }

+ 12 - 9
Babylon/babylon.engine.ts

@@ -222,7 +222,7 @@
     var compileShader = (gl: WebGLRenderingContext, source: string, type: string, defines: string): WebGLShader => {
         var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
 
-        gl.shaderSource(shader,(defines ? defines + "\n" : "") + source);
+        gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
         gl.compileShader(shader);
 
         if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
@@ -620,10 +620,10 @@
             // Pointer lock
             this._onPointerLockChange = () => {
                 this.isPointerLock = (document.mozPointerLockElement === canvas ||
-                    document.webkitPointerLockElement === canvas ||
-                    document.msPointerLockElement === canvas ||
-                    document.pointerLockElement === canvas
-                    );
+                document.webkitPointerLockElement === canvas ||
+                document.msPointerLockElement === canvas ||
+                document.pointerLockElement === canvas
+                );
             };
 
             document.addEventListener("pointerlockchange", this._onPointerLockChange, false);
@@ -1144,7 +1144,7 @@
             return effect;
         }
 
-        public createEffectForParticles(fragmentName: string, uniformsNames: string[] = [], samplers: string[] = [], defines = "", fallbacks?: EffectFallbacks,
+        public createEffectForParticles(fragmentName: string, uniformsNames: string[]= [], samplers: string[]= [], defines = "", fallbacks?: EffectFallbacks,
             onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void): Effect {
 
             return this.createEffect(
@@ -1455,7 +1455,7 @@
 
                     var header = Internals.TGATools.GetTGAHeader(data);
 
-                    prepareWebGLTexture(texture, this._gl, scene, header.width, header.height, invertY, noMipmap, false,() => {
+                    prepareWebGLTexture(texture, this._gl, scene, header.width, header.height, invertY, noMipmap, false, () => {
                         Internals.TGATools.UploadContent(this._gl, data);
 
                         if (onLoad) {
@@ -1476,7 +1476,7 @@
                     var info = Internals.DDSTools.GetDDSInfo(data);
 
                     var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) === 1);
-                    prepareWebGLTexture(texture, this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC,() => {
+                    prepareWebGLTexture(texture, this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, () => {
 
                         Internals.DDSTools.UploadDDSLevels(this._gl, this.getCaps().s3tc, data, info, loadMipmap, 1);
 
@@ -1495,7 +1495,7 @@
 
             } else {
                 var onload = (img) => {
-                    prepareWebGLTexture(texture, this._gl, scene, img.width, img.height, invertY, noMipmap, false,(potWidth, potHeight) => {
+                    prepareWebGLTexture(texture, this._gl, scene, img.width, img.height, invertY, noMipmap, false, (potWidth, potHeight) => {
                         var isPot = (img.width === potWidth && img.height === potHeight);
                         if (!isPot) {
                             this._workingCanvas.width = potWidth;
@@ -1981,6 +1981,9 @@
                 this.scenes[0].dispose();
             }
 
+            // Release audio engine
+            this._audioEngine.dispose();
+
             // Release effects
             for (var name in this._compiledEffects) {
                 this._gl.deleteProgram(this._compiledEffects[name]._program);

+ 37 - 2
Babylon/babylon.scene.js

@@ -1278,6 +1278,11 @@
                 }
             }
 
+            // Depth renderer
+            if (this._depthRenderer) {
+                this._renderTargets.push(this._depthRenderer.getDepthMap());
+            }
+
             // RenderPipeline
             this.postProcessRenderPipelineManager.update();
 
@@ -1344,9 +1349,9 @@
                         sound.updateDistanceFromListener();
                     }
                 }
-                for (var i = 0; i < this.soundTracks.length; i++) {
+                for (i = 0; i < this.soundTracks.length; i++) {
                     for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
-                        var sound = this.soundTracks[i].soundCollection[j];
+                        sound = this.soundTracks[i].soundCollection[j];
                         if (sound.useCustomAttenuation) {
                             sound.updateDistanceFromListener();
                         }
@@ -1355,6 +1360,25 @@
             }
         };
 
+        Scene.prototype.enableDepthRenderer = function () {
+            if (this._depthRenderer) {
+                return this._depthRenderer;
+            }
+
+            this._depthRenderer = new BABYLON.DepthRenderer(this);
+
+            return this._depthRenderer;
+        };
+
+        Scene.prototype.disableDepthRenderer = function () {
+            if (!this._depthRenderer) {
+                return;
+            }
+
+            this._depthRenderer.dispose();
+            this._depthRenderer = null;
+        };
+
         Scene.prototype.dispose = function () {
             this.beforeRender = null;
             this.afterRender = null;
@@ -1363,6 +1387,10 @@
 
             this._boundingBoxRenderer.dispose();
 
+            if (this._depthRenderer) {
+                this._depthRenderer.dispose();
+            }
+
             // Debug layer
             this.debugLayer.hide();
 
@@ -1376,6 +1404,13 @@
 
             this.detachControl();
 
+            // Release sounds & sounds tracks
+            this.mainSoundTrack.dispose();
+
+            for (var scIndex = 0; scIndex < this.soundTracks.length; scIndex++) {
+                this.soundTracks[scIndex].dispose();
+            }
+
             // Detach cameras
             var canvas = this._engine.getRenderingCanvas();
             var index;

+ 39 - 2
Babylon/babylon.scene.ts

@@ -230,6 +230,8 @@
 
         private _debugLayer: DebugLayer;
 
+        private _depthRenderer: DepthRenderer;
+
         /**
          * @constructor
          * @param {BABYLON.Engine} engine - the engine to be used to render this scene.
@@ -1374,6 +1376,11 @@
                 }
             }
 
+            // Depth renderer
+            if (this._depthRenderer) {
+                this._renderTargets.push(this._depthRenderer.getDepthMap());
+            }
+
             // RenderPipeline
             this.postProcessRenderPipelineManager.update();
 
@@ -1442,9 +1449,9 @@
                         sound.updateDistanceFromListener();
                     }
                 }
-                for (var i = 0; i < this.soundTracks.length; i++) {
+                for (i = 0; i < this.soundTracks.length; i++) {
                     for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
-                        var sound = this.soundTracks[i].soundCollection[j];
+                        sound = this.soundTracks[i].soundCollection[j];
                         if (sound.useCustomAttenuation) {
                             sound.updateDistanceFromListener();
                         }
@@ -1453,6 +1460,25 @@
             }
         }
 
+        public enableDepthRenderer(): DepthRenderer {
+            if (this._depthRenderer) {
+                return this._depthRenderer;
+            }
+
+            this._depthRenderer = new DepthRenderer(this);
+
+            return this._depthRenderer;
+        }
+
+        public disableDepthRenderer(): void {
+            if (!this._depthRenderer) {
+                return;
+            }
+
+            this._depthRenderer.dispose();
+            this._depthRenderer = null;
+        }
+
         public dispose(): void {
             this.beforeRender = null;
             this.afterRender = null;
@@ -1461,6 +1487,10 @@
 
             this._boundingBoxRenderer.dispose();
 
+            if (this._depthRenderer) {
+                this._depthRenderer.dispose();
+            }
+
             // Debug layer
             this.debugLayer.hide();
 
@@ -1474,6 +1504,13 @@
 
             this.detachControl();
 
+            // Release sounds & sounds tracks
+            this.mainSoundTrack.dispose();
+
+            for (var scIndex = 0; scIndex < this.soundTracks.length; scIndex++) {
+                this.soundTracks[scIndex].dispose();
+            }
+
             // Detach cameras
             var canvas = this._engine.getRenderingCanvas();
             var index;

+ 3 - 3
References/waa.d.ts

@@ -637,19 +637,19 @@ declare enum DistanceModelType {
      * A linear distance model which calculates distanceGain according to:
      *     1 - rolloffFactor * (distance - refDistance) / (maxDistance - refDistance)
      */
-    linear,
+    'linear',
 
     /**
      * An inverse distance model which calculates distanceGain according to:
      *     refDistance / (refDistance + rolloffFactor * (distance - refDistance))
      */
-    inverse,
+    'inverse',
 
     /**
      * An exponential distance model which calculates distanceGain according to:
      *     pow(distance / refDistance, -rolloffFactor)
      */
-    exponential
+    'exponential'
 }
 
 /**

File diff suppressed because it is too large
+ 379 - 24
babylon.2.0-beta.debug.js


File diff suppressed because it is too large
+ 9 - 9
babylon.2.0-beta.js


File diff suppressed because it is too large
+ 2565 - 2190
babylon.2.0.d.ts