Skip to content

Commit

Permalink
RapierPhysics: Added addScene()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Oct 27, 2023
1 parent 92c71d7 commit f956966
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
21 changes: 21 additions & 0 deletions examples/jsm/physics/RapierPhysics.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ async function RapierPhysics() {
const _quaternion = new Quaternion();
const _matrix = new Matrix4();

function addScene( scene ) {

scene.traverse( function ( child ) {

if ( child.isMesh ) {

const physics = child.userData.physics;

if ( physics ) {

addMesh( child, physics.mass, physics.restitution );

}

}

} );

}

function addMesh( mesh, mass = 0, restitution = 0 ) {

const shape = getCollider( mesh.geometry );
Expand Down Expand Up @@ -189,6 +209,7 @@ async function RapierPhysics() {
setInterval( step, 1000 / frameRate );

return {
addScene: addScene,
addMesh: addMesh,
setMeshPosition: setMeshPosition,
setMeshVelocity: setMeshVelocity
Expand Down
8 changes: 4 additions & 4 deletions examples/physics_rapier_instancing.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
);
floor.position.y = - 2.5;
floor.receiveShadow = true;
floor.userData.physics = { mass: 0 };
scene.add( floor );
physics.addMesh( floor );

//

Expand All @@ -81,6 +81,7 @@
boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
boxes.castShadow = true;
boxes.receiveShadow = true;
boxes.userData.physics = { mass: 1 };
scene.add( boxes );

for ( let i = 0; i < boxes.count; i ++ ) {
Expand All @@ -91,15 +92,14 @@

}

physics.addMesh( boxes, 1 );

// Spheres

const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
spheres = new THREE.InstancedMesh( geometrySphere, material, 400 );
spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
spheres.castShadow = true;
spheres.receiveShadow = true;
spheres.userData.physics = { mass: 1 };
scene.add( spheres );

for ( let i = 0; i < spheres.count; i ++ ) {
Expand All @@ -110,7 +110,7 @@

}

physics.addMesh( spheres, 1 );
physics.addScene( scene );

//

Expand Down
20 changes: 13 additions & 7 deletions examples/webxr_xr_ballshooter.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,33 +192,38 @@
// Floor

const geometry = new THREE.BoxGeometry( 6, 2, 6 );
const material = new THREE.MeshNormalMaterial();
const material = new THREE.MeshNormalMaterial( { visible: false } );

const floor = new THREE.Mesh( geometry, material );
floor.position.y = - 1;
physics.addMesh( floor );
floor.userData.physics = { mass: 0 };
scene.add( floor );

// Walls

const wallPX = new THREE.Mesh( geometry, material );
wallPX.position.set( 4, 3, 0 );
wallPX.rotation.z = Math.PI / 2;
physics.addMesh( wallPX );
wallPX.userData.physics = { mass: 0 };
scene.add( wallPX );

const wallNX = new THREE.Mesh( geometry, material );
wallNX.position.set( - 4, 3, 0 );
wallNX.rotation.z = Math.PI / 2;
physics.addMesh( wallNX );
wallNX.userData.physics = { mass: 0 };
scene.add( wallNX );

const wallPZ = new THREE.Mesh( geometry, material );
wallPZ.position.set( 0, 3, 4 );
wallPZ.rotation.x = Math.PI / 2;
physics.addMesh( wallPZ );
wallPZ.userData.physics = { mass: 0 };
scene.add( wallPZ );

const wallNZ = new THREE.Mesh( geometry, material );
wallNZ.position.set( 0, 3, - 4 );
wallNZ.rotation.x = Math.PI / 2;
physics.addMesh( wallNZ );
wallNZ.userData.physics = { mass: 0 };
scene.add( wallNZ );

}

Expand All @@ -229,6 +234,7 @@

spheres = new THREE.InstancedMesh( geometry, material, 800 );
spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
spheres.userData.physics = { mass: 1, restitution: 1.1 };
scene.add( spheres );

const matrix = new THREE.Matrix4();
Expand All @@ -246,7 +252,7 @@

}

physics.addMesh( spheres, 1, 1.1 );
physics.addScene( scene );

}

Expand Down

0 comments on commit f956966

Please sign in to comment.