Skip to content

Commit 8636675

Browse files
committed
feat: disposal methods
1 parent 441fd60 commit 8636675

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

examples/jsm/physics/RapierPhysics.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ export class Physic {
355355
this.world.step(delta);
356356

357357
for (let i = 0, l = this.dynamicObjects.length; i < l; i++) {
358-
const mesh = this.dynamicObjects[i];
358+
const object = this.dynamicObjects[i];
359359

360-
if (mesh instanceof InstancedMesh) {
361-
const array = mesh.instanceMatrix.array;
360+
if (object instanceof InstancedMesh) {
361+
const array = object.instanceMatrix.array;
362362
/** @type {PhysicProperties[]} */
363-
const bodies = this.dynamicObjectMap.get(mesh);
363+
const bodies = this.dynamicObjectMap.get(object);
364364

365365
for (let j = 0; j < bodies.length; j++) {
366366
const physicProperties = bodies[j];
@@ -375,17 +375,41 @@ export class Physic {
375375
.toArray(array, j * 16);
376376
}
377377

378-
mesh.instanceMatrix.needsUpdate = true;
379-
mesh.computeBoundingSphere();
378+
object.instanceMatrix.needsUpdate = true;
379+
object.computeBoundingSphere();
380380
} else {
381381
/** @type {PhysicProperties} */
382-
const physicProperties = this.dynamicObjectMap.get(mesh);
382+
const physicProperties = this.dynamicObjectMap.get(object);
383383

384-
mesh.position.copy(physicProperties.rigidBody.translation());
385-
mesh.quaternion.copy(physicProperties.rigidBody.rotation());
384+
object.position.copy(physicProperties.rigidBody.translation());
385+
object.quaternion.copy(physicProperties.rigidBody.rotation());
386386
}
387387
}
388388
}
389+
390+
/**
391+
* @description Remove the specified object to the physic `world`.
392+
*
393+
* @param {Object3D} object Object3D based.
394+
*/
395+
removeFromWorld(object) {
396+
for (let i = 0; i < this.dynamicObjects.length; i++) {
397+
const dynamicObject = this.dynamicObjects[i];
398+
if (dynamicObject.id === object.id) {
399+
this.dynamicObjectMap.delete(dynamicObject);
400+
this.dynamicObjects.splice(i, 1);
401+
return;
402+
}
403+
}
404+
}
405+
406+
/**
407+
* @description remove all the stored physical objects.
408+
*/
409+
dispose() {
410+
this.dynamicObjects = [];
411+
this.dynamicObjectMap = new WeakMap();
412+
}
389413
}
390414

391415
export async function RapierPhysics() {

0 commit comments

Comments
 (0)