ConvexGeometry.js 1.3 KB

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