ConvexGeometry.js 1.5 KB

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