Explorar o código

Merge pull request #453 from RaananW/geometry-callbacks

callbacks for geometry changes
David Catuhe %!s(int64=10) %!d(string=hai) anos
pai
achega
c105e91238

+ 11 - 5
Babylon/Mesh/babylon.geometry.js

@@ -41,6 +41,7 @@ var BABYLON;
         };
         Geometry.prototype.setAllVerticesData = function (vertexData, updatable) {
             vertexData.applyToGeometry(this, updatable);
+            this.notifyUpdate();
         };
         Geometry.prototype.setVerticesData = function (kind, data, updatable, stride) {
             this._vertexBuffers = this._vertexBuffers || {};
@@ -62,6 +63,7 @@ var BABYLON;
                     mesh.computeWorldMatrix(true);
                 }
             }
+            this.notifyUpdate(kind);
         };
         Geometry.prototype.updateVerticesDataDirectly = function (kind, data, offset) {
             var vertexBuffer = this.getVertexBuffer(kind);
@@ -69,6 +71,7 @@ var BABYLON;
                 return;
             }
             vertexBuffer.updateDirectly(data, offset);
+            this.notifyUpdate(kind);
         };
         Geometry.prototype.updateVerticesData = function (kind, data, updateExtends) {
             var vertexBuffer = this.getVertexBuffer(kind);
@@ -97,6 +100,7 @@ var BABYLON;
                     }
                 }
             }
+            this.notifyUpdate(kind);
         };
         Geometry.prototype.getTotalVertices = function () {
             if (!this.isReady()) {
@@ -162,6 +166,7 @@ var BABYLON;
             for (var index = 0; index < numOfMeshes; index++) {
                 meshes[index]._createGlobalSubMesh();
             }
+            this.notifyUpdate();
         };
         Geometry.prototype.getTotalIndices = function () {
             if (!this.isReady()) {
@@ -243,6 +248,11 @@ var BABYLON;
                 this._indexBuffer.references = numOfMeshes;
             }
         };
+        Geometry.prototype.notifyUpdate = function (kind) {
+            if (this.onGeometryUpdated) {
+                this.onGeometryUpdated(this, kind);
+            }
+        };
         Geometry.prototype.load = function (scene, onLoaded) {
             var _this = this;
             if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
@@ -298,11 +308,7 @@ var BABYLON;
             this._delayLoadingFunction = null;
             this._delayInfo = [];
             this._boundingInfo = null; // todo: .dispose()
-            var geometries = this._scene.getGeometries();
-            index = geometries.indexOf(this);
-            if (index > -1) {
-                geometries.splice(index, 1);
-            }
+            this._scene.removeGeometry(this);
             this._isDisposed = true;
         };
         Geometry.prototype.copy = function (id) {

+ 13 - 6
Babylon/Mesh/babylon.geometry.ts

@@ -4,6 +4,7 @@
         public id: string;
         public delayLoadState = Engine.DELAYLOADSTATE_NONE;
         public delayLoadingFile: string;
+        public onGeometryUpdated: (geometry: Geometry, kind?: string) => void;
 
         // Private
         private _scene: Scene;
@@ -54,6 +55,7 @@
 
         public setAllVerticesData(vertexData: VertexData, updatable?: boolean): void {
             vertexData.applyToGeometry(this, updatable);
+            this.notifyUpdate();
         }
 
         public setVerticesData(kind: string, data: number[], updatable?: boolean, stride?: number): void {
@@ -83,6 +85,7 @@
                     mesh.computeWorldMatrix(true);
                 }
             }
+            this.notifyUpdate(kind);
         }
 
         public updateVerticesDataDirectly(kind: string, data: Float32Array, offset: number): void {
@@ -93,6 +96,7 @@
             }
 
             vertexBuffer.updateDirectly(data, offset);
+            this.notifyUpdate(kind);
         }
 
         public updateVerticesData(kind: string, data: number[], updateExtends?: boolean): void {
@@ -132,6 +136,7 @@
                     }
                 }
             }
+            this.notifyUpdate(kind);
         }
 
         public getTotalVertices(): number {
@@ -209,6 +214,7 @@
             for (var index = 0; index < numOfMeshes; index++) {
                 meshes[index]._createGlobalSubMesh();
             }
+            this.notifyUpdate();
         }
 
         public getTotalIndices(): number {
@@ -316,6 +322,12 @@
             }
         }
 
+        private notifyUpdate(kind?: string) {
+            if (this.onGeometryUpdated) {
+                this.onGeometryUpdated(this, kind);
+            }
+        }
+
         public load(scene: Scene, onLoaded?: () => void): void {
             if (this.delayLoadState === Engine.DELAYLOADSTATE_LOADING) {
                 return;
@@ -383,12 +395,7 @@
 
             this._boundingInfo = null; // todo: .dispose()
 
-            var geometries = this._scene.getGeometries();
-            index = geometries.indexOf(this);
-
-            if (index > -1) {
-                geometries.splice(index, 1);
-            }
+            this._scene.removeGeometry(this);
             this._isDisposed = true;
         }
 

+ 19 - 0
Babylon/babylon.scene.js

@@ -728,8 +728,27 @@ var BABYLON;
                 return false;
             }
             this._geometries.push(geometry);
+            if (this.onGeometryAdded) {
+                this.onGeometryAdded(geometry);
+            }
             return true;
         };
+        /**
+         * Removes an existing geometry
+         * @param {BABYLON.Geometry} geometry - the geometry to be removed from the scene.
+         * @return {boolean} was the geometry removed or not
+         */
+        Scene.prototype.removeGeometry = function (geometry) {
+            var index = this._geometries.indexOf(geometry);
+            if (index > -1) {
+                this._geometries.splice(index, 1);
+                if (this.onGeometryRemoved) {
+                    this.onGeometryRemoved(geometry);
+                }
+                return true;
+            }
+            return false;
+        };
         Scene.prototype.getGeometries = function () {
             return this._geometries;
         };

+ 23 - 0
Babylon/babylon.scene.ts

@@ -129,6 +129,8 @@
 
         // Geometries
         private _geometries = new Array<Geometry>();
+        public onGeometryAdded: (newGeometry?: Geometry) => void;
+        public onGeometryRemoved: (removedGeometry?: Geometry) => void;
 
         public materials = new Array<Material>();
         public multiMaterials = new Array<MultiMaterial>();
@@ -951,10 +953,31 @@
             }
 
             this._geometries.push(geometry);
+            if (this.onGeometryAdded) {
+                this.onGeometryAdded(geometry);
+            }
 
             return true;
         }
 
+        /**
+         * Removes an existing geometry
+         * @param {BABYLON.Geometry} geometry - the geometry to be removed from the scene.
+         * @return {boolean} was the geometry removed or not
+         */
+        public removeGeometry(geometry: Geometry): boolean {
+            var index = this._geometries.indexOf(geometry);
+
+            if (index > -1) {
+                this._geometries.splice(index, 1);
+                if (this.onGeometryRemoved) {
+                    this.onGeometryRemoved(geometry);
+                }
+                return true;
+            }
+            return false;
+        }
+
         public getGeometries(): Geometry[] {
             return this._geometries;
         }