LDrawUtils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Group,
  5. LineSegments,
  6. Matrix3,
  7. Mesh
  8. } from '../../../build/three.module.js';
  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. }
  45. else geoms.arr.push( geometry );
  46. }
  47. function permuteAttribute( attribute, elemSize ) {
  48. // Permutes first two vertices of each attribute element
  49. if ( ! attribute ) return;
  50. const verts = attribute.array;
  51. const numVerts = Math.floor( verts.length / 3 );
  52. let offset = 0;
  53. for ( let i = 0; i < numVerts; i ++ ) {
  54. const x = verts[ offset ];
  55. const y = verts[ offset + 1 ];
  56. const z = verts[ offset + 2 ];
  57. verts[ offset ] = verts[ offset + 3 ];
  58. verts[ offset + 1 ] = verts[ offset + 4 ];
  59. verts[ offset + 2 ] = verts[ offset + 5 ];
  60. verts[ offset + 3 ] = x;
  61. verts[ offset + 4 ] = y;
  62. verts[ offset + 5 ] = z;
  63. offset += elemSize * 3;
  64. }
  65. }
  66. // Traverse the object hierarchy collecting geometries and transforming them to world space
  67. const meshGeometries = {};
  68. const linesGeometries = {};
  69. const condLinesGeometries = {};
  70. object.updateMatrixWorld( true );
  71. const normalMatrix = new Matrix3();
  72. object.traverse( c => {
  73. if ( c.isMesh | c.isLineSegments ) {
  74. const elemSize = c.isMesh ? 3 : 2;
  75. const matrixIsInverted = c.matrixWorld.determinant() < 0;
  76. if ( matrixIsInverted ) {
  77. permuteAttribute( c.geometry.attributes.position, elemSize );
  78. permuteAttribute( c.geometry.attributes.normal, elemSize );
  79. }
  80. c.geometry.applyMatrix4( c.matrixWorld );
  81. if ( c.isConditionalLine ) {
  82. c.geometry.attributes.control0.applyMatrix4( c.matrixWorld );
  83. c.geometry.attributes.control1.applyMatrix4( c.matrixWorld );
  84. normalMatrix.getNormalMatrix( c.matrixWorld );
  85. c.geometry.attributes.direction.applyNormalMatrix( normalMatrix );
  86. }
  87. const geometries = c.isMesh ? meshGeometries : ( c.isConditionalLine ? condLinesGeometries : linesGeometries );
  88. if ( Array.isArray( c.material ) ) {
  89. for ( const groupIndex in c.geometry.groups ) {
  90. const group = c.geometry.groups[ groupIndex ];
  91. const mat = c.material[ group.materialIndex ];
  92. const newGeometry = extractGroup( c.geometry, group, elemSize, c.isConditionalLine );
  93. addGeometry( mat, newGeometry, geometries );
  94. }
  95. }
  96. else addGeometry( c.material, c.geometry, geometries );
  97. }
  98. } );
  99. // Create object with merged geometries
  100. const mergedObject = new Group();
  101. const meshMaterialsIds = Object.keys( meshGeometries );
  102. for ( const i in meshMaterialsIds ) {
  103. const meshGeometry = meshGeometries[ meshMaterialsIds[ i ] ];
  104. const mergedGeometry = mergeBufferGeometries( meshGeometry.arr );
  105. mergedObject.add( new Mesh( mergedGeometry, meshGeometry.mat ) );
  106. }
  107. const linesMaterialsIds = Object.keys( linesGeometries );
  108. for ( const i in linesMaterialsIds ) {
  109. const lineGeometry = linesGeometries[ linesMaterialsIds[ i ] ];
  110. const mergedGeometry = mergeBufferGeometries( lineGeometry.arr );
  111. mergedObject.add( new LineSegments( mergedGeometry, lineGeometry.mat ) );
  112. }
  113. const condLinesMaterialsIds = Object.keys( condLinesGeometries );
  114. for ( const i in condLinesMaterialsIds ) {
  115. const condLineGeometry = condLinesGeometries[ condLinesMaterialsIds[ i ] ];
  116. const mergedGeometry = mergeBufferGeometries( condLineGeometry.arr );
  117. const condLines = new LineSegments( mergedGeometry, condLineGeometry.mat );
  118. condLines.isConditionalLine = true;
  119. mergedObject.add( condLines );
  120. }
  121. mergedObject.userData.constructionStep = 0;
  122. mergedObject.userData.numConstructionSteps = 1;
  123. return mergedObject;
  124. }
  125. }
  126. export { LDrawUtils };
粤ICP备19079148号