Selaa lähdekoodia

Examples: Intensified ammo and rapier examples.

Mr.doob 2 vuotta sitten
vanhempi
commit
285dd1cb78

+ 27 - 29
examples/jsm/physics/AmmoPhysics.js

@@ -188,58 +188,56 @@ async function AmmoPhysics() {
 
 			const delta = ( time - lastTime ) / 1000;
 
-			// console.time( 'world.step' );
 			world.stepSimulation( delta, 10 );
-			// console.timeEnd( 'world.step' );
 
-		}
+			//
 
-		lastTime = time;
+			for ( let i = 0, l = meshes.length; i < l; i ++ ) {
 
-		//
+				const mesh = meshes[ i ];
 
-		for ( let i = 0, l = meshes.length; i < l; i ++ ) {
+				if ( mesh.isInstancedMesh ) {
 
-			const mesh = meshes[ i ];
+					const array = mesh.instanceMatrix.array;
+					const bodies = meshMap.get( mesh );
 
-			if ( mesh.isInstancedMesh ) {
+					for ( let j = 0; j < bodies.length; j ++ ) {
 
-				const array = mesh.instanceMatrix.array;
-				const bodies = meshMap.get( mesh );
+						const body = bodies[ j ];
 
-				for ( let j = 0; j < bodies.length; j ++ ) {
+						const motionState = body.getMotionState();
+						motionState.getWorldTransform( worldTransform );
 
-					const body = bodies[ j ];
+						const position = worldTransform.getOrigin();
+						const quaternion = worldTransform.getRotation();
 
-					const motionState = body.getMotionState();
-					motionState.getWorldTransform( worldTransform );
+						compose( position, quaternion, array, j * 16 );
 
-					const position = worldTransform.getOrigin();
-					const quaternion = worldTransform.getRotation();
+					}
 
-					compose( position, quaternion, array, j * 16 );
+					mesh.instanceMatrix.needsUpdate = true;
+					mesh.computeBoundingSphere();
 
-				}
+				} else if ( mesh.isMesh ) {
 
-				mesh.instanceMatrix.needsUpdate = true;
-				mesh.computeBoundingSphere();
+					const body = meshMap.get( mesh );
 
-			} else if ( mesh.isMesh ) {
-
-				const body = meshMap.get( mesh );
+					const motionState = body.getMotionState();
+					motionState.getWorldTransform( worldTransform );
 
-				const motionState = body.getMotionState();
-				motionState.getWorldTransform( worldTransform );
+					const position = worldTransform.getOrigin();
+					const quaternion = worldTransform.getRotation();
+					mesh.position.set( position.x(), position.y(), position.z() );
+					mesh.quaternion.set( quaternion.x(), quaternion.y(), quaternion.z(), quaternion.w() );
 
-				const position = worldTransform.getOrigin();
-				const quaternion = worldTransform.getRotation();
-				mesh.position.set( position.x(), position.y(), position.z() );
-				mesh.quaternion.set( quaternion.x(), quaternion.y(), quaternion.z(), quaternion.w() );
+				}
 
 			}
 
 		}
 
+		lastTime = time;
+
 	}
 
 	// animate

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

@@ -153,6 +153,8 @@ async function RapierPhysics() {
 			world.timestep = delta;
 			world.step();
 
+			//
+
 			for ( let i = 0, l = meshes.length; i < l; i ++ ) {
 
 				const mesh = meshes[ i ];

+ 4 - 6
examples/physics_ammo_instancing.html

@@ -34,8 +34,6 @@
 			import { AmmoPhysics } from 'three/addons/physics/AmmoPhysics.js';
 			import Stats from 'three/addons/libs/stats.module.js';
 
-			THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
-
 			let camera, scene, renderer, stats;
 			let physics, position;
 
@@ -85,8 +83,8 @@
 
 				// Boxes
 
-				const geometryBox = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
-				boxes = new THREE.InstancedMesh( geometryBox, material, 100 );
+				const geometryBox = new THREE.BoxGeometry( 0.075, 0.075, 0.075 );
+				boxes = new THREE.InstancedMesh( geometryBox, material, 400 );
 				boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				boxes.castShadow = true;
 				boxes.receiveShadow = true;
@@ -104,8 +102,8 @@
 
 				// Spheres
 
-				const geometrySphere = new THREE.IcosahedronGeometry( 0.075, 3 );
-				spheres = new THREE.InstancedMesh( geometrySphere, material, 100 );
+				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;

+ 4 - 6
examples/physics_rapier_instancing.html

@@ -32,8 +32,6 @@
 			import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
 			import Stats from 'three/addons/libs/stats.module.js';
 
-			THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
-
 			let camera, scene, renderer, stats;
 			let physics, position;
 
@@ -83,8 +81,8 @@
 
 				// Boxes
 
-				const geometryBox = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
-				boxes = new THREE.InstancedMesh( geometryBox, material, 100 );
+				const geometryBox = new THREE.BoxGeometry( 0.075, 0.075, 0.075 );
+				boxes = new THREE.InstancedMesh( geometryBox, material, 400 );
 				boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
 				boxes.castShadow = true;
 				boxes.receiveShadow = true;
@@ -102,8 +100,8 @@
 
 				// Spheres
 
-				const geometrySphere = new THREE.IcosahedronGeometry( 0.075, 3 );
-				spheres = new THREE.InstancedMesh( geometrySphere, material, 100 );
+				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;

BIN
examples/screenshots/physics_ammo_instancing.jpg


BIN
examples/screenshots/physics_rapier_instancing.jpg