From c59b972f1f033fc33bbe33b01c0a234de0aca07d Mon Sep 17 00:00:00 2001 From: linbingquan <695601626@qq.com> Date: Sat, 21 Jun 2025 11:10:16 +0800 Subject: [PATCH 1/2] RapierPhysics: Add removeMesh() --- examples/jsm/physics/RapierPhysics.js | 44 ++++++++++++++++++++++++++- examples/physics_rapier_basic.html | 23 ++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/examples/jsm/physics/RapierPhysics.js b/examples/jsm/physics/RapierPhysics.js index b19bf480e9de84..c8426a85554561 100644 --- a/examples/jsm/physics/RapierPhysics.js +++ b/examples/jsm/physics/RapierPhysics.js @@ -1,6 +1,6 @@ import { Clock, Vector3, Quaternion, Matrix4 } from 'three'; -const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/rapier3d-compat@0.12.0'; +const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/rapier3d-compat@0.17.3'; const frameRate = 60; @@ -149,6 +149,39 @@ async function RapierPhysics() { } + function removeMesh( mesh ) { + + const index = meshes.indexOf( mesh ); + + if ( index !== - 1 ) { + + meshes.splice( index, 1 ); + meshMap.delete( mesh ); + + if ( ! mesh.userData.physics ) return; + + const body = mesh.userData.physics.body; + + if ( ! body ) return; + + if ( Array.isArray( body ) ) { + + for ( let i = 0; i < body.length; i ++ ) { + + world.removeRigidBody( body[ i ] ); + + } + + } else { + + world.removeRigidBody( body ); + + } + + } + + } + function createInstancedBody( mesh, mass, shape ) { const array = mesh.instanceMatrix.array; @@ -306,6 +339,15 @@ async function RapierPhysics() { */ addMesh: addMesh, + /** + * Removes the given mesh from this physics simulation. + * + * @method + * @name RapierPhysics#addMesh + * @param {Mesh} mesh The mesh to remove. + */ + removeMesh: removeMesh, + /** * Set the position of the given mesh which is part of the physics simulation. Calling this * method will reset the current simulated velocity of the mesh. diff --git a/examples/physics_rapier_basic.html b/examples/physics_rapier_basic.html index 8f0d0d8b8bde0c..4c67068f842edc 100644 --- a/examples/physics_rapier_basic.html +++ b/examples/physics_rapier_basic.html @@ -34,7 +34,7 @@ import { RapierHelper } from 'three/addons/helpers/RapierHelper.js'; import Stats from 'three/addons/libs/stats.module.js'; - let camera, scene, renderer, stats; + let camera, scene, renderer, stats, controls; let physics, physicsHelper; init(); @@ -77,7 +77,8 @@ document.body.appendChild( renderer.domElement ); renderer.setAnimationLoop( animate ); - const controls = new OrbitControls( camera, renderer.domElement ); + controls = new OrbitControls( camera, renderer.domElement ); + controls.enableDamping = true; controls.target = new THREE.Vector3( 0, 2, 0 ); controls.update(); @@ -156,11 +157,27 @@ } - function animate() { + for ( const object of scene.children ) { + + if ( object.isMesh ) { + + if ( object.position.y < - 10 ) { + + scene.remove( object ); + physics.removeMesh( object ); + + } + + } + + } + if ( physicsHelper ) physicsHelper.update(); + controls.update(); + renderer.render( scene, camera ); stats.update(); From 14c0589e00446601e49560b9ea02b68d0107c67a Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Sat, 21 Jun 2025 10:22:38 +0200 Subject: [PATCH 2/2] Update RapierPhysics.js Fix JSDoc. --- examples/jsm/physics/RapierPhysics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/jsm/physics/RapierPhysics.js b/examples/jsm/physics/RapierPhysics.js index c8426a85554561..423e2ac7f3b279 100644 --- a/examples/jsm/physics/RapierPhysics.js +++ b/examples/jsm/physics/RapierPhysics.js @@ -343,7 +343,7 @@ async function RapierPhysics() { * Removes the given mesh from this physics simulation. * * @method - * @name RapierPhysics#addMesh + * @name RapierPhysics#removeMesh * @param {Mesh} mesh The mesh to remove. */ removeMesh: removeMesh,