LDrawUtils.js 5.7 KB

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