ConvexGeometry.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. console.warn( "THREE.ConvexGeometry: As part of the transition to ES6 Modules, the files in 'examples/js' have been deprecated in r117 (May 2020) and will be deleted in r124 (December 2020). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. // ConvexGeometry
  6. THREE.ConvexGeometry = function ( points ) {
  7. THREE.Geometry.call( this );
  8. this.fromBufferGeometry( new THREE.ConvexBufferGeometry( points ) );
  9. this.mergeVertices();
  10. };
  11. THREE.ConvexGeometry.prototype = Object.create( THREE.Geometry.prototype );
  12. THREE.ConvexGeometry.prototype.constructor = THREE.ConvexGeometry;
  13. // ConvexBufferGeometry
  14. THREE.ConvexBufferGeometry = function ( points ) {
  15. THREE.BufferGeometry.call( this );
  16. // buffers
  17. var vertices = [];
  18. var normals = [];
  19. if ( THREE.ConvexHull === undefined ) {
  20. console.error( 'THREE.ConvexBufferGeometry: ConvexBufferGeometry relies on THREE.ConvexHull' );
  21. }
  22. var convexHull = new THREE.ConvexHull().setFromPoints( points );
  23. // generate vertices and normals
  24. var faces = convexHull.faces;
  25. for ( var i = 0; i < faces.length; i ++ ) {
  26. var face = faces[ i ];
  27. var edge = face.edge;
  28. // we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
  29. do {
  30. var point = edge.head().point;
  31. vertices.push( point.x, point.y, point.z );
  32. normals.push( face.normal.x, face.normal.y, face.normal.z );
  33. edge = edge.next;
  34. } while ( edge !== face.edge );
  35. }
  36. // build geometry
  37. this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  38. this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  39. };
  40. THREE.ConvexBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  41. THREE.ConvexBufferGeometry.prototype.constructor = THREE.ConvexBufferGeometry;