ConvexGeometry.js 1.1 KB

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