ConvexGeometry.js 1.1 KB

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