IFCLoader.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //Example: https://github.com/tomvandig/web-ifc-three/tree/main/examples/jsm
  2. import { IfcAPI } from './ifc/web-ifc-api.js';
  3. import {
  4. FileLoader,
  5. Loader,
  6. Object3D,
  7. Mesh,
  8. Color,
  9. MeshPhongMaterial,
  10. DoubleSide,
  11. Matrix4,
  12. BufferGeometry,
  13. InterleavedBuffer,
  14. InterleavedBufferAttribute,
  15. BufferAttribute,
  16. } from '../../../build/three.module.js';
  17. const ifcAPI = new IfcAPI();
  18. class IFCLoader extends Loader {
  19. constructor( manager ) {
  20. super( manager );
  21. }
  22. load( url, onLoad, onProgress, onError ) {
  23. const scope = this;
  24. const loader = new FileLoader( scope.manager );
  25. loader.setPath( scope.path );
  26. loader.setResponseType( 'arraybuffer' );
  27. loader.setRequestHeader( scope.requestHeader );
  28. loader.setWithCredentials( scope.withCredentials );
  29. loader.load(
  30. url,
  31. async function ( buffer ) {
  32. try {
  33. onLoad( await scope.parse( buffer ) );
  34. } catch ( e ) {
  35. if ( onError ) {
  36. onError( e );
  37. } else {
  38. console.error( e );
  39. }
  40. scope.manager.itemError( url );
  41. }
  42. },
  43. onProgress,
  44. onError
  45. );
  46. }
  47. async parse( buffer ) {
  48. if ( ifcAPI.wasmModule === undefined ) {
  49. await ifcAPI.Init();
  50. }
  51. const data = new Uint8Array( buffer );
  52. const modelID = ifcAPI.OpenModel( 'example.ifc', data );
  53. return loadAllGeometry( modelID );
  54. function loadAllGeometry( modelID ) {
  55. const flatMeshes = getFlatMeshes( modelID );
  56. const mainObject = new Object3D();
  57. for ( let i = 0; i < flatMeshes.size(); i ++ ) {
  58. const placedGeometries = flatMeshes.get( i ).geometries;
  59. for ( let j = 0; j < placedGeometries.size(); j ++ )
  60. mainObject.add( getPlacedGeometry( modelID, placedGeometries.get( j ) ) );
  61. }
  62. return mainObject;
  63. }
  64. function getFlatMeshes( modelID ) {
  65. const flatMeshes = ifcAPI.LoadAllGeometry( modelID );
  66. return flatMeshes;
  67. }
  68. function getPlacedGeometry( modelID, placedGeometry ) {
  69. const geometry = getBufferGeometry( modelID, placedGeometry );
  70. const material = getMeshMaterial( placedGeometry.color );
  71. const mesh = new Mesh( geometry, material );
  72. mesh.matrix = getMeshMatrix( placedGeometry.flatTransformation );
  73. mesh.matrixAutoUpdate = false;
  74. return mesh;
  75. }
  76. function getBufferGeometry( modelID, placedGeometry ) {
  77. const geometry = ifcAPI.GetGeometry(
  78. modelID,
  79. placedGeometry.geometryExpressID
  80. );
  81. const verts = ifcAPI.GetVertexArray(
  82. geometry.GetVertexData(),
  83. geometry.GetVertexDataSize()
  84. );
  85. const indices = ifcAPI.GetIndexArray(
  86. geometry.GetIndexData(),
  87. geometry.GetIndexDataSize()
  88. );
  89. const bufferGeometry = ifcGeometryToBuffer( verts, indices );
  90. return bufferGeometry;
  91. }
  92. function getMeshMaterial( color ) {
  93. const col = new Color( color.x, color.y, color.z );
  94. const material = new MeshPhongMaterial( { color: col, side: DoubleSide } );
  95. material.transparent = color.w !== 1;
  96. if ( material.transparent ) material.opacity = color.w;
  97. return material;
  98. }
  99. function getMeshMatrix( matrix ) {
  100. const mat = new Matrix4();
  101. mat.fromArray( matrix );
  102. // mat.elements[15 - 3] *= 0.001;
  103. // mat.elements[15 - 2] *= 0.001;
  104. // mat.elements[15 - 1] *= 0.001;
  105. return mat;
  106. }
  107. function ifcGeometryToBuffer( vertexData, indexData ) {
  108. const geometry = new BufferGeometry();
  109. const buffer32 = new InterleavedBuffer( vertexData, 6 );
  110. geometry.setAttribute(
  111. 'position',
  112. new InterleavedBufferAttribute( buffer32, 3, 0 )
  113. );
  114. geometry.setAttribute(
  115. 'normal',
  116. new InterleavedBufferAttribute( buffer32, 3, 3 )
  117. );
  118. geometry.setIndex( new BufferAttribute( indexData, 1 ) );
  119. return geometry;
  120. }
  121. }
  122. setWasmPath(path){
  123. ifcAPI.SetWasmPath(path);
  124. }
  125. };
  126. export { IFCLoader };