LDrawUtils.js 5.8 KB

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