ConvexGeometry.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ( function () {
  2. class ConvexGeometry extends THREE.BufferGeometry {
  3. constructor( points ) {
  4. super(); // buffers
  5. const vertices = [];
  6. const normals = [];
  7. if ( THREE.ConvexHull === undefined ) {
  8. console.error( 'THREE.ConvexBufferGeometry: ConvexBufferGeometry relies on THREE.ConvexHull' );
  9. }
  10. const convexHull = new THREE.ConvexHull().setFromPoints( points ); // generate vertices and normals
  11. const faces = convexHull.faces;
  12. for ( let i = 0; i < faces.length; i ++ ) {
  13. const face = faces[ i ];
  14. let edge = face.edge; // we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
  15. do {
  16. const point = edge.head().point;
  17. vertices.push( point.x, point.y, point.z );
  18. normals.push( face.normal.x, face.normal.y, face.normal.z );
  19. edge = edge.next;
  20. } while ( edge !== face.edge );
  21. } // build geometry
  22. this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  23. this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  24. }
  25. }
  26. THREE.ConvexGeometry = ConvexGeometry;
  27. } )();