ConvexGeometry.js 1.2 KB

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