ConvexGeometry.js 1.2 KB

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