2
0
Эх сурвалжийг харах

RapierPhysics: Added addScene()

Mr.doob 1 жил өмнө
parent
commit
f956966499

+ 21 - 0
examples/jsm/physics/RapierPhysics.js

@@ -55,6 +55,26 @@ async function RapierPhysics() {
 	const _quaternion = new Quaternion();
 	const _quaternion = new Quaternion();
 	const _matrix = new Matrix4();
 	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 ) {
 	function addMesh( mesh, mass = 0, restitution = 0 ) {
 
 
 		const shape = getCollider( mesh.geometry );
 		const shape = getCollider( mesh.geometry );
@@ -189,6 +209,7 @@ async function RapierPhysics() {
 	setInterval( step, 1000 / frameRate );
 	setInterval( step, 1000 / frameRate );
 
 
 	return {
 	return {
+		addScene: addScene,
 		addMesh: addMesh,
 		addMesh: addMesh,
 		setMeshPosition: setMeshPosition,
 		setMeshPosition: setMeshPosition,
 		setMeshVelocity: setMeshVelocity
 		setMeshVelocity: setMeshVelocity

+ 4 - 4
examples/physics_rapier_instancing.html

@@ -64,8 +64,8 @@
 				);
 				);
 				floor.position.y = - 2.5;
 				floor.position.y = - 2.5;
 				floor.receiveShadow = true;
 				floor.receiveShadow = true;
+				floor.userData.physics = { mass: 0 };
 				scene.add( floor );
 				scene.add( floor );
-				physics.addMesh( floor );
 
 
 				//
 				//
 
 
@@ -81,6 +81,7 @@
 				boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				boxes.castShadow = true;
 				boxes.castShadow = true;
 				boxes.receiveShadow = true;
 				boxes.receiveShadow = true;
+				boxes.userData.physics = { mass: 1 };
 				scene.add( boxes );
 				scene.add( boxes );
 
 
 				for ( let i = 0; i < boxes.count; i ++ ) {
 				for ( let i = 0; i < boxes.count; i ++ ) {
@@ -91,8 +92,6 @@
 
 
 				}
 				}
 
 
-				physics.addMesh( boxes, 1 );
-
 				// Spheres
 				// Spheres
 
 
 				const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
 				const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
@@ -100,6 +99,7 @@
 				spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				spheres.castShadow = true;
 				spheres.castShadow = true;
 				spheres.receiveShadow = true;
 				spheres.receiveShadow = true;
+				spheres.userData.physics = { mass: 1 };
 				scene.add( spheres );
 				scene.add( spheres );
 
 
 				for ( let i = 0; i < spheres.count; i ++ ) {
 				for ( let i = 0; i < spheres.count; i ++ ) {
@@ -110,7 +110,7 @@
 
 
 				}
 				}
 
 
-				physics.addMesh( spheres, 1 );
+				physics.addScene( scene );
 
 
 				//
 				//
 
 

+ 13 - 7
examples/webxr_xr_ballshooter.html

@@ -192,33 +192,38 @@
 					// Floor
 					// Floor
 
 
 					const geometry = new THREE.BoxGeometry( 6, 2, 6 );
 					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 );
 					const floor = new THREE.Mesh( geometry, material );
 					floor.position.y = - 1;
 					floor.position.y = - 1;
-					physics.addMesh( floor );
+					floor.userData.physics = { mass: 0 };
+					scene.add( floor );
 
 
 					// Walls
 					// Walls
 
 
 					const wallPX = new THREE.Mesh( geometry, material );
 					const wallPX = new THREE.Mesh( geometry, material );
 					wallPX.position.set( 4, 3, 0 );
 					wallPX.position.set( 4, 3, 0 );
 					wallPX.rotation.z = Math.PI / 2;
 					wallPX.rotation.z = Math.PI / 2;
-					physics.addMesh( wallPX );
+					wallPX.userData.physics = { mass: 0 };
+					scene.add( wallPX );
 
 
 					const wallNX = new THREE.Mesh( geometry, material );
 					const wallNX = new THREE.Mesh( geometry, material );
 					wallNX.position.set( - 4, 3, 0 );
 					wallNX.position.set( - 4, 3, 0 );
 					wallNX.rotation.z = Math.PI / 2;
 					wallNX.rotation.z = Math.PI / 2;
-					physics.addMesh( wallNX );
+					wallNX.userData.physics = { mass: 0 };
+					scene.add( wallNX );
 
 
 					const wallPZ = new THREE.Mesh( geometry, material );
 					const wallPZ = new THREE.Mesh( geometry, material );
 					wallPZ.position.set( 0, 3, 4 );
 					wallPZ.position.set( 0, 3, 4 );
 					wallPZ.rotation.x = Math.PI / 2;
 					wallPZ.rotation.x = Math.PI / 2;
-					physics.addMesh( wallPZ );
+					wallPZ.userData.physics = { mass: 0 };
+					scene.add( wallPZ );
 
 
 					const wallNZ = new THREE.Mesh( geometry, material );
 					const wallNZ = new THREE.Mesh( geometry, material );
 					wallNZ.position.set( 0, 3, - 4 );
 					wallNZ.position.set( 0, 3, - 4 );
 					wallNZ.rotation.x = Math.PI / 2;
 					wallNZ.rotation.x = Math.PI / 2;
-					physics.addMesh( wallNZ );
+					wallNZ.userData.physics = { mass: 0 };
+					scene.add( wallNZ );
 
 
 				}
 				}
 
 
@@ -229,6 +234,7 @@
 
 
 				spheres = new THREE.InstancedMesh( geometry, material, 800 );
 				spheres = new THREE.InstancedMesh( geometry, material, 800 );
 				spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
+				spheres.userData.physics = { mass: 1, restitution: 1.1 };
 				scene.add( spheres );
 				scene.add( spheres );
 
 
 				const matrix = new THREE.Matrix4();
 				const matrix = new THREE.Matrix4();
@@ -246,7 +252,7 @@
 
 
 				}
 				}
 
 
-				physics.addMesh( spheres, 1, 1.1 );
+				physics.addScene( scene );
 
 
 			}
 			}