Browse Source

RapierPhysics: Support RoundedBoxGeometry (#31351)

林炳权 7 months ago
parent
commit
3e39fccad6
2 changed files with 18 additions and 2 deletions
  1. 10 1
      examples/jsm/physics/RapierPhysics.js
  2. 8 1
      examples/physics_rapier_basic.html

+ 10 - 1
examples/jsm/physics/RapierPhysics.js

@@ -15,7 +15,16 @@ function getShape( geometry ) {
 
 	// TODO change type to is*
 
-	if ( geometry.type === 'BoxGeometry' ) {
+	if ( geometry.type === 'RoundedBoxGeometry' ) {
+
+		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 radius = parameters.radius !== undefined ? parameters.radius : 0.1;
+
+		return RAPIER.ColliderDesc.roundCuboid( sx - radius, sy - radius, sz - radius, radius );
+
+	} else if ( geometry.type === 'BoxGeometry' ) {
 
 		const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
 		const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;

+ 8 - 1
examples/physics_rapier_basic.html

@@ -32,6 +32,7 @@
 			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
 			import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
 			import { RapierHelper } from 'three/addons/helpers/RapierHelper.js';
+			import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
 			import Stats from 'three/addons/libs/stats.module.js';
 
 			let camera, scene, renderer, stats, controls;
@@ -131,9 +132,15 @@
 
 			}
 
+			const geometries = [
+				new THREE.BoxGeometry( 1, 1, 1 ),
+				new THREE.SphereGeometry( 0.5 ),
+				new RoundedBoxGeometry( 1, 1, 1, 2, 0.25 )
+			];
+
 			function addBody( ) {
 
-				const geometry = ( Math.random() > 0.5 ) ? new THREE.SphereGeometry( 0.5 ) : new THREE.BoxGeometry( 1, 1, 1 );
+				const geometry = geometries[ Math.floor( Math.random() * geometries.length ) ];
 				const material = new THREE.MeshStandardMaterial( { color: Math.floor( Math.random() * 0xFFFFFF ) } );
 
 				const mesh = new THREE.Mesh( geometry, material );

粤ICP备19079148号