LDrawUtils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Group,
  5. LineSegments,
  6. Matrix3,
  7. Mesh
  8. } from 'three';
  9. import { mergeBufferGeometries } from './BufferGeometryUtils.js';
  10. class LDrawUtils {
  11. static mergeObject( object ) {
  12. // Merges geometries in object by materials and returns new object. Use on not indexed geometries.
  13. // The object buffers reference the old object ones.
  14. // Special treatment is done to the conditional lines generated by LDrawLoader.
  15. function extractGroup( geometry, group, elementSize, isConditionalLine ) {
  16. // Extracts a group from a geometry as a new geometry (with attribute buffers referencing original buffers)
  17. const newGeometry = new BufferGeometry();
  18. const originalPositions = geometry.getAttribute( 'position' ).array;
  19. const originalNormals = elementSize === 3 ? geometry.getAttribute( 'normal' ).array : null;
  20. const numVertsGroup = Math.min( group.count, Math.floor( originalPositions.length / 3 ) - group.start );
  21. const vertStart = group.start * 3;
  22. const vertEnd = ( group.start + numVertsGroup ) * 3;
  23. const positions = originalPositions.subarray( vertStart, vertEnd );
  24. const normals = originalNormals !== null ? originalNormals.subarray( vertStart, vertEnd ) : null;
  25. newGeometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
  26. if ( normals !== null ) newGeometry.setAttribute( 'normal', new BufferAttribute( normals, 3 ) );
  27. if ( isConditionalLine ) {
  28. const controlArray0 = geometry.getAttribute( 'control0' ).array.subarray( vertStart, vertEnd );
  29. const controlArray1 = geometry.getAttribute( 'control1' ).array.subarray( vertStart, vertEnd );
  30. const directionArray = geometry.getAttribute( 'direction' ).array.subarray( vertStart, vertEnd );
  31. newGeometry.setAttribute( 'control0', new BufferAttribute( controlArray0, 3, false ) );
  32. newGeometry.setAttribute( 'control1', new BufferAttribute( controlArray1, 3, false ) );
  33. newGeometry.setAttribute( 'direction', new BufferAttribute( directionArray, 3, false ) );
  34. }
  35. return newGeometry;
  36. }
  37. function addGeometry( mat, geometry, geometries ) {
  38. const geoms = geometries[ mat.uuid ];
  39. if ( ! geoms ) {
  40. geometries[ mat.uuid ] = {
  41. mat: mat,
  42. arr: [ geometry ]
  43. };
  44. } else {
  45. geoms.arr.push( geometry );
  46. }
  47. }
  48. function permuteAttribute( attribute, elemSize ) {
  49. // Permutes first two vertices of each attribute element
  50. if ( ! attribute ) return;
  51. const verts = attribute.array;
  52. const numVerts = Math.floor( verts.length / 3 );
  53. let offset = 0;
  54. for ( let i = 0; i < numVerts; i ++ ) {
  55. const x = verts[ offset ];
  56. const y = verts[ offset + 1 ];
  57. const z = verts[ offset + 2 ];
  58. verts[ offset ] = verts[ offset + 3 ];
  59. verts[ offset + 1 ] = verts[ offset + 4 ];
  60. verts[ offset + 2 ] = verts[ offset + 5 ];
  61. verts[ offset + 3 ] = x;
  62. verts[ offset + 4 ] = y;
  63. verts[ offset + 5 ] = z;
  64. offset += elemSize * 3;
  65. }
  66. }
  67. // Traverse the object hierarchy collecting geometries and transforming them to world space
  68. const meshGeometries = {};
  69. const linesGeometries = {};
  70. const condLinesGeometries = {};
  71. object.updateMatrixWorld( true );
  72. const normalMatrix = new Matrix3();
  73. object.traverse( c => {
  74. if ( c.isMesh | c.isLineSegments ) {
  75. const elemSize = c.isMesh ? 3 : 2;
  76. const geometry = c.geometry.clone();
  77. const matrixIsInverted = c.matrixWorld.determinant() < 0;
  78. if ( matrixIsInverted ) {
  79. permuteAttribute( geometry.attributes.position, elemSize );
  80. permuteAttribute( geometry.attributes.normal, elemSize );
  81. }
  82. geometry.applyMatrix4( c.matrixWorld );
  83. if ( c.isConditionalLine ) {
  84. geometry.attributes.control0.applyMatrix4( c.matrixWorld );
  85. geometry.attributes.control1.applyMatrix4( c.matrixWorld );
  86. normalMatrix.getNormalMatrix( c.matrixWorld );
  87. geometry.attributes.direction.applyNormalMatrix( normalMatrix );
  88. }
  89. const geometries = c.isMesh ? meshGeometries : ( c.isConditionalLine ? condLinesGeometries : linesGeometries );
  90. if ( Array.isArray( c.material ) ) {
  91. for ( const groupIndex in geometry.groups ) {
  92. const group = geometry.groups[ groupIndex ];
  93. const mat = c.material[ group.materialIndex ];
  94. const newGeometry = extractGroup( geometry, group, elemSize, c.isConditionalLine );
  95. addGeometry( mat, newGeometry, geometries );
  96. }
  97. } else {
  98. addGeometry( c.material, geometry, geometries );
  99. }
  100. }
  101. } );
  102. // Create object with merged geometries
  103. const mergedObject = new Group();
  104. const meshMaterialsIds = Object.keys( meshGeometries );
  105. for ( const meshMaterialsId of meshMaterialsIds ) {
  106. const meshGeometry = meshGeometries[ meshMaterialsId ];
  107. const mergedGeometry = mergeBufferGeometries( meshGeometry.arr );
  108. mergedObject.add( new Mesh( mergedGeometry, meshGeometry.mat ) );
  109. }
  110. const linesMaterialsIds = Object.keys( linesGeometries );
  111. for ( const linesMaterialsId of linesMaterialsIds ) {
  112. const lineGeometry = linesGeometries[ linesMaterialsId ];
  113. const mergedGeometry = mergeBufferGeometries( lineGeometry.arr );
  114. mergedObject.add( new LineSegments( mergedGeometry, lineGeometry.mat ) );
  115. }
  116. const condLinesMaterialsIds = Object.keys( condLinesGeometries );
  117. for ( const condLinesMaterialsId of condLinesMaterialsIds ) {
  118. const condLineGeometry = condLinesGeometries[ condLinesMaterialsId ];
  119. const mergedGeometry = mergeBufferGeometries( condLineGeometry.arr );
  120. const condLines = new LineSegments( mergedGeometry, condLineGeometry.mat );
  121. condLines.isConditionalLine = true;
  122. mergedObject.add( condLines );
  123. }
  124. mergedObject.userData.constructionStep = 0;
  125. mergedObject.userData.numConstructionSteps = 1;
  126. return mergedObject;
  127. }
  128. }
  129. export { LDrawUtils };