ConvexGeometry.js 1.7 KB

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