EdgesGeometry.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { BufferGeometry } from '../core/BufferGeometry.js';
  2. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  3. import * as MathUtils from '../math/MathUtils.js';
  4. import { Triangle } from '../math/Triangle.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. const _v0 = /*@__PURE__*/ new Vector3();
  7. const _v1 = /*@__PURE__*/ new Vector3();
  8. const _normal = /*@__PURE__*/ new Vector3();
  9. const _triangle = /*@__PURE__*/ new Triangle();
  10. class EdgesGeometry extends BufferGeometry {
  11. constructor( geometry = null, thresholdAngle = 1 ) {
  12. super();
  13. this.type = 'EdgesGeometry';
  14. this.parameters = {
  15. geometry: geometry,
  16. thresholdAngle: thresholdAngle
  17. };
  18. if ( geometry !== null ) {
  19. const precisionPoints = 4;
  20. const precision = Math.pow( 10, precisionPoints );
  21. const thresholdDot = Math.cos( MathUtils.DEG2RAD * thresholdAngle );
  22. const indexAttr = geometry.getIndex();
  23. const positionAttr = geometry.getAttribute( 'position' );
  24. const indexCount = indexAttr ? indexAttr.count : positionAttr.count;
  25. const indexArr = [ 0, 0, 0 ];
  26. const vertKeys = [ 'a', 'b', 'c' ];
  27. const hashes = new Array( 3 );
  28. const edgeData = {};
  29. const vertices = [];
  30. for ( let i = 0; i < indexCount; i += 3 ) {
  31. if ( indexAttr ) {
  32. indexArr[ 0 ] = indexAttr.getX( i );
  33. indexArr[ 1 ] = indexAttr.getX( i + 1 );
  34. indexArr[ 2 ] = indexAttr.getX( i + 2 );
  35. } else {
  36. indexArr[ 0 ] = i;
  37. indexArr[ 1 ] = i + 1;
  38. indexArr[ 2 ] = i + 2;
  39. }
  40. const { a, b, c } = _triangle;
  41. a.fromBufferAttribute( positionAttr, indexArr[ 0 ] );
  42. b.fromBufferAttribute( positionAttr, indexArr[ 1 ] );
  43. c.fromBufferAttribute( positionAttr, indexArr[ 2 ] );
  44. _triangle.getNormal( _normal );
  45. // create hashes for the edge from the vertices
  46. hashes[ 0 ] = `${ Math.round( a.x * precision ) },${ Math.round( a.y * precision ) },${ Math.round( a.z * precision ) }`;
  47. hashes[ 1 ] = `${ Math.round( b.x * precision ) },${ Math.round( b.y * precision ) },${ Math.round( b.z * precision ) }`;
  48. hashes[ 2 ] = `${ Math.round( c.x * precision ) },${ Math.round( c.y * precision ) },${ Math.round( c.z * precision ) }`;
  49. // skip degenerate triangles
  50. if ( hashes[ 0 ] === hashes[ 1 ] || hashes[ 1 ] === hashes[ 2 ] || hashes[ 2 ] === hashes[ 0 ] ) {
  51. continue;
  52. }
  53. // iterate over every edge
  54. for ( let j = 0; j < 3; j ++ ) {
  55. // get the first and next vertex making up the edge
  56. const jNext = ( j + 1 ) % 3;
  57. const vecHash0 = hashes[ j ];
  58. const vecHash1 = hashes[ jNext ];
  59. const v0 = _triangle[ vertKeys[ j ] ];
  60. const v1 = _triangle[ vertKeys[ jNext ] ];
  61. const hash = `${ vecHash0 }_${ vecHash1 }`;
  62. const reverseHash = `${ vecHash1 }_${ vecHash0 }`;
  63. if ( reverseHash in edgeData && edgeData[ reverseHash ] ) {
  64. // if we found a sibling edge add it into the vertex array if
  65. // it meets the angle threshold and delete the edge from the map.
  66. if ( _normal.dot( edgeData[ reverseHash ].normal ) <= thresholdDot ) {
  67. vertices.push( v0.x, v0.y, v0.z );
  68. vertices.push( v1.x, v1.y, v1.z );
  69. }
  70. edgeData[ reverseHash ] = null;
  71. } else if ( ! ( hash in edgeData ) ) {
  72. // if we've already got an edge here then skip adding a new one
  73. edgeData[ hash ] = {
  74. index0: indexArr[ j ],
  75. index1: indexArr[ jNext ],
  76. normal: _normal.clone(),
  77. };
  78. }
  79. }
  80. }
  81. // iterate over all remaining, unmatched edges and add them to the vertex array
  82. for ( const key in edgeData ) {
  83. if ( edgeData[ key ] ) {
  84. const { index0, index1 } = edgeData[ key ];
  85. _v0.fromBufferAttribute( positionAttr, index0 );
  86. _v1.fromBufferAttribute( positionAttr, index1 );
  87. vertices.push( _v0.x, _v0.y, _v0.z );
  88. vertices.push( _v1.x, _v1.y, _v1.z );
  89. }
  90. }
  91. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  92. }
  93. }
  94. copy( source ) {
  95. super.copy( source );
  96. this.parameters = Object.assign( {}, source.parameters );
  97. return this;
  98. }
  99. }
  100. export { EdgesGeometry };
粤ICP备19079148号