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

RapierPhysics: Clean up (#26042)

* RapierPhysics: Clean up

* Update RapierPhysics.js

* Update RapierPhysics.js

* Update RapierPhysics.js

* Fix

* Match the original behaviour

* Test reverting clock

* Bring Clock back

* Update screenshot

* Fix screenshot

* Fix again

* Add to exceptions list
Levi Pesin 2 жил өмнө
parent
commit
dd4a418d04

+ 76 - 124
examples/jsm/physics/RapierPhysics.js

@@ -1,79 +1,72 @@
-async function RapierPhysics() {
-
-	const RAPIER = await import( 'https://cdn.skypack.dev/@dimforge/[email protected]' );
-	
-	await RAPIER.init();
-
-	const frameRate = 60;
+import { Clock, Vector3, Quaternion, Matrix4 } from 'three';
 
-	// Docs: https://rapier.rs/docs/api/javascript/JavaScript3D/	
-
-	const gravity = { x: 0.0, y: - 9.81, z: 0.0 };
-	const world = new RAPIER.World( gravity );
+const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/[email protected]';
 
-	function getCollider( geometry ) {
+const frameRate = 60;
 
-		const parameters = geometry.parameters;
+const _scale = new Vector3( 1, 1, 1 );
+const ZERO = new Vector3();
 
-		// TODO change type to is*
+let RAPIER = null;
 
-		if ( geometry.type === 'BoxGeometry' ) {
+function getCollider( geometry ) {
 
-			const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
-			const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
-			const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
+	const parameters = geometry.parameters;
 
-			return RAPIER.ColliderDesc.cuboid( sx, sy, sz );
+	// TODO change type to is*
 
-		} else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
+	if ( geometry.type === 'BoxGeometry' ) {
 
-			const radius = parameters.radius !== undefined ? parameters.radius : 1;
+		const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
+		const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
+		const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
 
-			return RAPIER.ColliderDesc.ball( radius );
+		return RAPIER.ColliderDesc.cuboid( sx, sy, sz );
 
-		}
+	} else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
 
-		return null;
+		const radius = parameters.radius !== undefined ? parameters.radius : 1;
+		return RAPIER.ColliderDesc.ball( radius );
 
 	}
 
-	const meshes = [];
-	const meshMap = new WeakMap();
+	return null;
 
-	function addMesh( mesh, mass = 0, restitution = 0 ) {
+}
 
-		const shape = getCollider( mesh.geometry );
+async function RapierPhysics() {
 
-		if ( shape !== null ) {
+	if ( RAPIER === null ) {
 
-			shape.setMass( mass );
-			shape.setRestitution( restitution );
-	
-			if ( mesh.isInstancedMesh ) {
+		RAPIER = await import( RAPIER_PATH );
+		await RAPIER.init();
 
-				handleInstancedMesh( mesh, mass, shape );
+	}
 
-			} else if ( mesh.isMesh ) {
+	// Docs: https://rapier.rs/docs/api/javascript/JavaScript3D/	
 
-				handleMesh( mesh, mass, shape );
+	const gravity = new Vector3( 0.0, - 9.81, 0.0 );
+	const world = new RAPIER.World( gravity );
 
-			}
+	const meshes = [];
+	const meshMap = new WeakMap();
 
-		}
+	const _vector = new Vector3();
+	const _quaternion = new Quaternion();
+	const _matrix = new Matrix4();
 
-	}
+	function addMesh( mesh, mass = 0, restitution = 0 ) {
 
-	function handleMesh( mesh, mass, shape ) {
+		const shape = getCollider( mesh.geometry );
 
-		const position = mesh.position;
-		const quaternion = mesh.quaternion;
+		if ( shape === null ) return;
 
-		const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
-		desc.setTranslation( position.x, position.y, position.z );
-		desc.setRotation( quaternion );
+		shape.setMass( mass );
+		shape.setRestitution( restitution );
 
-		const body = world.createRigidBody( desc );
-		world.createCollider( shape, body );
+		const body = mesh.isInstancedMesh
+							? createInstancedBody( mesh, mass, shape )
+							: createBody( mesh.position, mesh.quaternion, mass, shape );
 
 		if ( mass > 0 ) {
 
@@ -84,7 +77,7 @@ async function RapierPhysics() {
 
 	}
 
-	function handleInstancedMesh( mesh, mass, shape ) {
+	function createInstancedBody( mesh, mass, shape ) {
 
 		const array = mesh.instanceMatrix.array;
 
@@ -92,32 +85,31 @@ async function RapierPhysics() {
 
 		for ( let i = 0; i < mesh.count; i ++ ) {
 
-			const index = i * 16;
+			const position = _vector.fromArray( array, i * 16 + 12 );
+			bodies.push( createBody( position, null, mass, shape ) );
 
-			const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
-			desc.setTranslation( array[ index + 12 ], array[ index + 13 ], array[ index + 14 ] );
+		}
 
-			const body = world.createRigidBody( desc );
-			world.createCollider( shape, body );
+		return bodies;
 
-			bodies.push( body );
+	}
 
-		}
+	function createBody( position, quaternion, mass, shape ) {
 
-		if ( mass > 0 ) {
+		const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
+		desc.setTranslation( ...position );
+		if ( quaternion !== null ) desc.setRotation( quaternion );
 
-			meshes.push( mesh );
-			meshMap.set( mesh, bodies );
+		const body = world.createRigidBody( desc );
+		world.createCollider( shape, body );
 
-		}
+		return body;
 
 	}
 
-	const ZERO = { x: 0, y: 0, z: 0 };
-
 	function setMeshPosition( mesh, position, index = 0 ) {
 
-		let  body = meshMap.get( mesh );
+		let body = meshMap.get( mesh );
 
 		if ( mesh.isInstancedMesh ) {
 
@@ -133,7 +125,7 @@ async function RapierPhysics() {
 
 	function setMeshVelocity( mesh, velocity, index = 0 ) {
 
-		let  body = meshMap.get( mesh );
+		let body = meshMap.get( mesh );
 
 		if ( mesh.isInstancedMesh ) {
 
@@ -147,59 +139,49 @@ async function RapierPhysics() {
 
 	//
 
-	let lastTime = 0;
+	const clock = new Clock();
 
 	function step() {
 
-		const time = performance.now();
-
-		if ( lastTime > 0 ) {
-
-			const delta = ( time - lastTime ) / 1000;
+		world.timestep = clock.getDelta();
+		world.step();
 
-			world.timestep = delta;
-			world.step();
+		//
 
-			//
+		for ( let i = 0, l = meshes.length; i < l; i ++ ) {
 
-			for ( let i = 0, l = meshes.length; i < l; i ++ ) {
+			const mesh = meshes[ i ];
 
-				const mesh = meshes[ i ];
-
-				if ( mesh.isInstancedMesh ) {
-
-					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 position = body.translation();
-						const quaternion = body.rotation();
+					const body = bodies[ j ];
 
-						compose( position, quaternion, array, j * 16 );
+					const position = body.translation();
+					_quaternion.copy( body.rotation() );
 
-					}
+					_matrix.compose( position, _quaternion, _scale ).toArray( 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 {
 
-					mesh.position.copy( body.translation() );
-					mesh.quaternion.copy( body.rotation() );
+				const body = meshMap.get( mesh );
 
-				}
+				mesh.position.copy( body.translation() );
+				mesh.quaternion.copy( body.rotation() );
 
 			}
 
 		}
 
-		lastTime = time;
-
 	}
 
 	// animate
@@ -214,34 +196,4 @@ async function RapierPhysics() {
 
 }
 
-function compose( position, quaternion, array, index ) {
-
-	const x = quaternion.x, y = quaternion.y, z = quaternion.z, w = quaternion.w;
-	const x2 = x + x, y2 = y + y, z2 = z + z;
-	const xx = x * x2, xy = x * y2, xz = x * z2;
-	const yy = y * y2, yz = y * z2, zz = z * z2;
-	const wx = w * x2, wy = w * y2, wz = w * z2;
-
-	array[ index + 0 ] = ( 1 - ( yy + zz ) );
-	array[ index + 1 ] = ( xy + wz );
-	array[ index + 2 ] = ( xz - wy );
-	array[ index + 3 ] = 0;
-
-	array[ index + 4 ] = ( xy - wz );
-	array[ index + 5 ] = ( 1 - ( xx + zz ) );
-	array[ index + 6 ] = ( yz + wx );
-	array[ index + 7 ] = 0;
-
-	array[ index + 8 ] = ( xz + wy );
-	array[ index + 9 ] = ( yz - wx );
-	array[ index + 10 ] = ( 1 - ( xx + yy ) );
-	array[ index + 11 ] = 0;
-
-	array[ index + 12 ] = position.x;
-	array[ index + 13 ] = position.y;
-	array[ index + 14 ] = position.z;
-	array[ index + 15 ] = 1;
-
-}
-
 export { RapierPhysics };

BIN
examples/screenshots/physics_rapier_instancing.jpg


+ 4 - 0
test/e2e/puppeteer.js

@@ -90,6 +90,10 @@ const exceptionList = [
 	'webgl_test_memory2',
 	'webgl_tiled_forward',
 
+	// TODO: implement determinism for setTimeout and setInterval
+	// could it fix some examples from above?
+	'physics_rapier_instancing',
+
 	// Awaiting for WebGPU support
 	'webgpu_audio_processing',
 	'webgpu_backdrop',