ConvexGeometry.js 1.6 KB

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