ConvexGeometry.js 2.0 KB

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