WireframeGeometry.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { BufferGeometry } from '../core/BufferGeometry.js';
  2. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  3. import { Vector3 } from '../math/Vector3.js';
  4. class WireframeGeometry extends BufferGeometry {
  5. constructor( geometry = null ) {
  6. super();
  7. this.type = 'WireframeGeometry';
  8. this.parameters = {
  9. geometry: geometry
  10. };
  11. if ( geometry !== null ) {
  12. // buffer
  13. const vertices = [];
  14. const edges = new Set();
  15. // helper variables
  16. const start = new Vector3();
  17. const end = new Vector3();
  18. if ( geometry.index !== null ) {
  19. // indexed BufferGeometry
  20. const position = geometry.attributes.position;
  21. const indices = geometry.index;
  22. let groups = geometry.groups;
  23. if ( groups.length === 0 ) {
  24. groups = [ { start: 0, count: indices.count, materialIndex: 0 } ];
  25. }
  26. // create a data structure that contains all edges without duplicates
  27. for ( let o = 0, ol = groups.length; o < ol; ++ o ) {
  28. const group = groups[ o ];
  29. const groupStart = group.start;
  30. const groupCount = group.count;
  31. for ( let i = groupStart, l = ( groupStart + groupCount ); i < l; i += 3 ) {
  32. for ( let j = 0; j < 3; j ++ ) {
  33. const index1 = indices.getX( i + j );
  34. const index2 = indices.getX( i + ( j + 1 ) % 3 );
  35. start.fromBufferAttribute( position, index1 );
  36. end.fromBufferAttribute( position, index2 );
  37. if ( isUniqueEdge( start, end, edges ) === true ) {
  38. vertices.push( start.x, start.y, start.z );
  39. vertices.push( end.x, end.y, end.z );
  40. }
  41. }
  42. }
  43. }
  44. } else {
  45. // non-indexed BufferGeometry
  46. const position = geometry.attributes.position;
  47. for ( let i = 0, l = ( position.count / 3 ); i < l; i ++ ) {
  48. for ( let j = 0; j < 3; j ++ ) {
  49. // three edges per triangle, an edge is represented as (index1, index2)
  50. // e.g. the first triangle has the following edges: (0,1),(1,2),(2,0)
  51. const index1 = 3 * i + j;
  52. const index2 = 3 * i + ( ( j + 1 ) % 3 );
  53. start.fromBufferAttribute( position, index1 );
  54. end.fromBufferAttribute( position, index2 );
  55. if ( isUniqueEdge( start, end, edges ) === true ) {
  56. vertices.push( start.x, start.y, start.z );
  57. vertices.push( end.x, end.y, end.z );
  58. }
  59. }
  60. }
  61. }
  62. // build geometry
  63. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  64. }
  65. }
  66. copy( source ) {
  67. super.copy( source );
  68. this.parameters = Object.assign( {}, source.parameters );
  69. return this;
  70. }
  71. }
  72. function isUniqueEdge( start, end, edges ) {
  73. const hash1 = `${start.x},${start.y},${start.z}-${end.x},${end.y},${end.z}`;
  74. const hash2 = `${end.x},${end.y},${end.z}-${start.x},${start.y},${start.z}`; // coincident edge
  75. if ( edges.has( hash1 ) === true || edges.has( hash2 ) === true ) {
  76. return false;
  77. } else {
  78. edges.add( hash1 );
  79. edges.add( hash2 );
  80. return true;
  81. }
  82. }
  83. export { WireframeGeometry };