1
0

ConvexGeometry.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. const convexHull = new ConvexHull().setFromPoints( points );
  13. // generate vertices and normals
  14. const faces = convexHull.faces;
  15. for ( let i = 0; i < faces.length; i ++ ) {
  16. const face = faces[ i ];
  17. let edge = face.edge;
  18. // we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
  19. do {
  20. const point = edge.head().point;
  21. vertices.push( point.x, point.y, point.z );
  22. normals.push( face.normal.x, face.normal.y, face.normal.z );
  23. edge = edge.next;
  24. } while ( edge !== face.edge );
  25. }
  26. // build geometry
  27. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  28. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  29. }
  30. }
  31. export { ConvexGeometry };