Переглянути джерело

AmmoPhysics: Added addScene()

Mr.doob 1 рік тому
батько
коміт
92c71d7c12

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

@@ -57,6 +57,26 @@ async function AmmoPhysics() {
 	const meshes = [];
 	const meshes = [];
 	const meshMap = new WeakMap();
 	const meshMap = new WeakMap();
 
 
+	function addScene( scene ) {
+
+		scene.traverse( function ( child ) {
+
+			if ( child.isMesh ) {
+
+				const physics = child.userData.physics;
+
+				if ( physics ) {
+
+					addMesh( child, physics.mass );
+
+				}
+
+			}
+
+		} );
+
+	}
+
 	function addMesh( mesh, mass = 0 ) {
 	function addMesh( mesh, mass = 0 ) {
 
 
 		const shape = getShape( mesh.geometry );
 		const shape = getShape( mesh.geometry );
@@ -245,6 +265,7 @@ async function AmmoPhysics() {
 	setInterval( step, 1000 / frameRate );
 	setInterval( step, 1000 / frameRate );
 
 
 	return {
 	return {
+		addScene: addScene,
 		addMesh: addMesh,
 		addMesh: addMesh,
 		setMeshPosition: setMeshPosition
 		setMeshPosition: setMeshPosition
 		// addCompoundMesh
 		// addCompoundMesh

+ 4 - 4
examples/physics_ammo_instancing.html

@@ -66,8 +66,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 );
 
 
 				//
 				//
 
 
@@ -83,6 +83,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 ++ ) {
@@ -93,8 +94,6 @@
 
 
 				}
 				}
 
 
-				physics.addMesh( boxes, 1 );
-
 				// Spheres
 				// Spheres
 
 
 				const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
 				const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
@@ -102,6 +101,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 ++ ) {
@@ -112,7 +112,7 @@
 
 
 				}
 				}
 
 
-				physics.addMesh( spheres, 1 );
+				physics.addScene( scene );
 
 
 				//
 				//