EdgesGeometry.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { BufferGeometry } from '../core/BufferGeometry';
  2. import { Float32BufferAttribute } from '../core/BufferAttribute';
  3. import { Geometry } from '../core/Geometry';
  4. import { _Math } from '../math/Math';
  5. /**
  6. * @author WestLangley / http://github.com/WestLangley
  7. */
  8. function EdgesGeometry( geometry, thresholdAngle ) {
  9. BufferGeometry.call( this );
  10. thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1;
  11. var thresholdDot = Math.cos( _Math.DEG2RAD * thresholdAngle );
  12. var edge = [ 0, 0 ], hash = {};
  13. function sortFunction( a, b ) {
  14. return a - b;
  15. }
  16. var keys = [ 'a', 'b', 'c' ];
  17. var geometry2;
  18. if ( geometry.isBufferGeometry ) {
  19. geometry2 = new Geometry();
  20. geometry2.fromBufferGeometry( geometry );
  21. } else {
  22. geometry2 = geometry.clone();
  23. }
  24. geometry2.mergeVertices();
  25. geometry2.computeFaceNormals();
  26. var vertices = geometry2.vertices;
  27. var faces = geometry2.faces;
  28. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  29. var face = faces[ i ];
  30. for ( var j = 0; j < 3; j ++ ) {
  31. edge[ 0 ] = face[ keys[ j ] ];
  32. edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
  33. edge.sort( sortFunction );
  34. var key = edge.toString();
  35. if ( hash[ key ] === undefined ) {
  36. hash[ key ] = { vert1: edge[ 0 ], vert2: edge[ 1 ], face1: i, face2: undefined };
  37. } else {
  38. hash[ key ].face2 = i;
  39. }
  40. }
  41. }
  42. var coords = [];
  43. for ( var key in hash ) {
  44. var h = hash[ key ];
  45. // A valid edge is only detected if the angle between two consecutive faces is greater than the given threshold angle.
  46. // The implementation detects this via the dot product of two consecutive faces normals.
  47. if ( h.face2 === undefined || faces[ h.face1 ].normal.dot( faces[ h.face2 ].normal ) <= thresholdDot ) {
  48. var vertex = vertices[ h.vert1 ];
  49. coords.push( vertex.x );
  50. coords.push( vertex.y );
  51. coords.push( vertex.z );
  52. vertex = vertices[ h.vert2 ];
  53. coords.push( vertex.x );
  54. coords.push( vertex.y );
  55. coords.push( vertex.z );
  56. }
  57. }
  58. this.addAttribute( 'position', new Float32BufferAttribute( coords, 3 ) );
  59. }
  60. EdgesGeometry.prototype = Object.create( BufferGeometry.prototype );
  61. EdgesGeometry.prototype.constructor = EdgesGeometry;
  62. export { EdgesGeometry };