Просмотр исходного кода

RapierPhysics: Add triangular mesh collider (#28993)

* RapierPhysics: Add triangular mesh collider

* Formatting

* Add support for interleaved buffers
Fermin Lozano 1 год назад
Родитель
Сommit
a839af717d
1 измененных файлов с 20 добавлено и 0 удалено
  1. 20 0
      examples/jsm/physics/RapierPhysics.js

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

@@ -28,6 +28,26 @@ function getShape( geometry ) {
 		const radius = parameters.radius !== undefined ? parameters.radius : 1;
 		return RAPIER.ColliderDesc.ball( radius );
 
+	} else if ( geometry.type === 'BufferGeometry' ) {
+
+		const vertices = [];
+		const vertex = new Vector3();
+		const position = geometry.getAttribute( 'position' );
+
+		for ( let i = 0; i < position.count; i ++ ) {
+
+			vertex.fromBufferAttribute( position, i );
+			vertices.push( vertex.x, vertex.y, vertex.z );
+
+		}
+
+		// if the buffer is non-indexed, generate an index buffer
+		const indices = geometry.getIndex() === null
+							? Uint32Array.from( Array( parseInt( vertices.length / 3 ) ).keys() )
+							: geometry.getIndex().array;
+
+		return RAPIER.ColliderDesc.trimesh( vertices, indices );
+
 	}
 
 	return null;

粤ICP备19079148号