Browse Source

Remove from collision cache

The remove function was missing, meshes and geometries would not have
been removed from the collision cache of the worker.
Raanan Weber 10 years ago
parent
commit
c5b145d486
3 changed files with 95 additions and 23 deletions
  1. 33 0
      dist/babylon.js
  2. 27 11
      src/Collisions/babylon.collisionWorker.js
  3. 35 12
      src/Collisions/babylon.collisionWorker.ts

File diff suppressed because it is too large
+ 33 - 0
dist/babylon.js


+ 27 - 11
src/Collisions/babylon.collisionWorker.js

@@ -19,12 +19,18 @@ var BABYLON;
         CollisionCache.prototype.addMesh = function (mesh) {
             this._meshes[mesh.uniqueId] = mesh;
         };
+        CollisionCache.prototype.removeMesh = function (uniqueId) {
+            delete this._meshes[uniqueId];
+        };
         CollisionCache.prototype.getGeometry = function (id) {
             return this._geometries[id];
         };
         CollisionCache.prototype.addGeometry = function (geometry) {
             this._geometries[geometry.id] = geometry;
         };
+        CollisionCache.prototype.removeGeometry = function (id) {
+            delete this._geometries[id];
+        };
         return CollisionCache;
     })();
     BABYLON.CollisionCache = CollisionCache;
@@ -156,20 +162,31 @@ var BABYLON;
             postMessage(reply, undefined);
         };
         CollisionDetectorTransferable.prototype.onUpdate = function (payload) {
-            for (var id in payload.updatedGeometries) {
-                if (payload.updatedGeometries.hasOwnProperty(id)) {
-                    this._collisionCache.addGeometry(payload.updatedGeometries[id]);
-                }
-            }
-            for (var uniqueId in payload.updatedMeshes) {
-                if (payload.updatedMeshes.hasOwnProperty(uniqueId)) {
-                    this._collisionCache.addMesh(payload.updatedMeshes[uniqueId]);
-                }
-            }
             var replay = {
                 error: BABYLON.WorkerReplyType.SUCCESS,
                 taskType: BABYLON.WorkerTaskType.UPDATE
             };
+            try {
+                for (var id in payload.updatedGeometries) {
+                    if (payload.updatedGeometries.hasOwnProperty(id)) {
+                        this._collisionCache.addGeometry(payload.updatedGeometries[id]);
+                    }
+                }
+                for (var uniqueId in payload.updatedMeshes) {
+                    if (payload.updatedMeshes.hasOwnProperty(uniqueId)) {
+                        this._collisionCache.addMesh(payload.updatedMeshes[uniqueId]);
+                    }
+                }
+                payload.removedGeometries.forEach(function (id) {
+                    this._collisionCache.removeGeometry(id);
+                });
+                payload.removedMeshes.forEach(function (uniqueId) {
+                    this._collisionCache.removeMesh(uniqueId);
+                });
+            }
+            catch (x) {
+                replay.error = BABYLON.WorkerReplyType.UNKNOWN_ERROR;
+            }
             postMessage(replay, undefined);
         };
         CollisionDetectorTransferable.prototype.onCollision = function (payload) {
@@ -227,4 +244,3 @@ var BABYLON;
         console.log("single worker init");
     }
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.collisionWorker.js.map

+ 35 - 12
src/Collisions/babylon.collisionWorker.ts

@@ -22,6 +22,10 @@ module BABYLON {
         public addMesh(mesh: SerializedMesh) {
             this._meshes[mesh.uniqueId] = mesh;
         }
+		
+		public removeMesh(uniqueId: number) {
+            delete this._meshes[uniqueId];
+        }
 
         public getGeometry(id: string): SerializedGeometry {
             return this._geometries[id];
@@ -30,6 +34,10 @@ module BABYLON {
         public addGeometry(geometry: SerializedGeometry) {
             this._geometries[geometry.id] = geometry;
         }
+		
+		public removeGeometry(id: string) {
+            delete this._geometries[id];
+        }
     }
 
     export class CollideWorker {
@@ -192,21 +200,36 @@ module BABYLON {
         }
 
         public onUpdate(payload: UpdatePayload) {
-            for (var id in payload.updatedGeometries) {
-                if (payload.updatedGeometries.hasOwnProperty(id)) {
-                    this._collisionCache.addGeometry(payload.updatedGeometries[id]);
-                }
-            }
-            for (var uniqueId in payload.updatedMeshes) {
-                if (payload.updatedMeshes.hasOwnProperty(uniqueId)) {
-                    this._collisionCache.addMesh(payload.updatedMeshes[uniqueId]);
-                }
-            }
-
-            var replay: WorkerReply = {
+			var replay: WorkerReply = {
                 error: WorkerReplyType.SUCCESS,
                 taskType: WorkerTaskType.UPDATE
             }
+			
+			try {
+				for (var id in payload.updatedGeometries) {
+					if (payload.updatedGeometries.hasOwnProperty(id)) {
+						this._collisionCache.addGeometry(payload.updatedGeometries[id]);
+					}
+				}
+				for (var uniqueId in payload.updatedMeshes) {
+					if (payload.updatedMeshes.hasOwnProperty(uniqueId)) {
+						this._collisionCache.addMesh(payload.updatedMeshes[uniqueId]);
+					}
+				}
+				
+				payload.removedGeometries.forEach(function(id) {
+					this._collisionCache.removeGeometry(id);
+				});
+				
+				payload.removedMeshes.forEach(function(uniqueId) {
+					this._collisionCache.removeMesh(uniqueId);
+				});
+				
+			} catch(x) {
+				replay.error = WorkerReplyType.UNKNOWN_ERROR;
+			}
+
+            
             postMessage(replay, undefined);
         }