ConvexGeometry.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. THREE.ConvexBufferGeometry = function ( points ) {
  2. THREE.BufferGeometry.call( this );
  3. // 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 );
  10. // generate vertices and normals
  11. var faces = convexHull.faces;
  12. for ( var i = 0; i < faces.length; i ++ ) {
  13. var face = faces[ i ];
  14. var edge = face.edge;
  15. // we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
  16. do {
  17. var point = edge.head().point;
  18. vertices.push( point.x, point.y, point.z );
  19. normals.push( face.normal.x, face.normal.y, face.normal.z );
  20. edge = edge.next;
  21. } while ( edge !== face.edge );
  22. }
  23. // build geometry
  24. this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  25. this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  26. };
  27. THREE.ConvexBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  28. THREE.ConvexBufferGeometry.prototype.constructor = THREE.ConvexBufferGeometry;