webgl_loader_ifc.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - IFCLoader</title>
  5. <meta charset="utf-8" />
  6. <meta
  7. name="viewport"
  8. content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
  9. />
  10. <meta property="og:title" content="three.js webgl - IFCLoader" />
  11. <meta property="og:type" content="website" />
  12. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_ifc.html" />
  13. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_ifc.jpg" />
  14. <link type="text/css" rel="stylesheet" href="main.css" />
  15. </head>
  16. <body>
  17. <div id="info">
  18. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  19. -
  20. IFC loader using
  21. <a href="https://github.com/ThatOpen/engine_web-ifc" target="_blank" rel="noopener">web-ifc</a>.
  22. See <a href="https://github.com/ThatOpen" target="_blank" rel="noopener">main project repository</a> for more information and BIM tools.
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/",
  29. "web-ifc": "https://cdn.jsdelivr.net/npm/web-ifc@0.0.77/web-ifc-api.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
  37. import { IfcAPI } from 'web-ifc';
  38. const WEB_IFC_VERSION = '0.0.77';
  39. const WEB_IFC_WASM_PATH = `https://cdn.jsdelivr.net/npm/web-ifc@${ WEB_IFC_VERSION }/`;
  40. let scene, camera, renderer;
  41. init();
  42. async function init() {
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0x8cc7de );
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  46. camera.position.set( 82.48, 22.09, - 45.24 );
  47. const directionalLight1 = new THREE.DirectionalLight( 0xffeeff, 2.5 );
  48. directionalLight1.position.set( 1, 1, 1 );
  49. scene.add( directionalLight1 );
  50. const directionalLight2 = new THREE.DirectionalLight( 0xffffff, 2.5 );
  51. directionalLight2.position.set( - 1, 0.5, - 1 );
  52. scene.add( directionalLight2 );
  53. const ambientLight = new THREE.AmbientLight( 0xffffee, 0.75 );
  54. scene.add( ambientLight );
  55. renderer = new THREE.WebGLRenderer( { antialias: true } );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. document.body.appendChild( renderer.domElement );
  59. const controls = new OrbitControls( camera, renderer.domElement );
  60. controls.target.set( 30.86, 7.73, 0.15 );
  61. controls.update();
  62. controls.addEventListener( 'change', render );
  63. window.addEventListener( 'resize', onWindowResize );
  64. const ifcAPI = new IfcAPI();
  65. ifcAPI.SetWasmPath( WEB_IFC_WASM_PATH );
  66. await ifcAPI.Init();
  67. const response = await fetch( 'models/ifc/rac_advanced_sample_project.ifc' );
  68. const data = new Uint8Array( await response.arrayBuffer() );
  69. const modelID = ifcAPI.OpenModel( data, { COORDINATE_TO_ORIGIN: true } );
  70. loadAllGeometry( ifcAPI, modelID );
  71. ifcAPI.CloseModel( modelID );
  72. render();
  73. }
  74. function loadAllGeometry( ifcAPI, modelID ) {
  75. const opaqueGeometries = [];
  76. const transparentGeometries = [];
  77. const materialCache = {};
  78. ifcAPI.StreamAllMeshes( modelID, ( flatMesh ) => {
  79. const placedGeometries = flatMesh.geometries;
  80. for ( let i = 0; i < placedGeometries.size(); i ++ ) {
  81. const placedGeometry = placedGeometries.get( i );
  82. const mesh = getPlacedGeometry( ifcAPI, modelID, placedGeometry, materialCache );
  83. const geometry = mesh.geometry.applyMatrix4( mesh.matrix );
  84. if ( placedGeometry.color.w !== 1 ) {
  85. transparentGeometries.push( geometry );
  86. } else {
  87. opaqueGeometries.push( geometry );
  88. }
  89. }
  90. } );
  91. if ( opaqueGeometries.length > 0 ) {
  92. const merged = BufferGeometryUtils.mergeGeometries( opaqueGeometries );
  93. const material = new THREE.MeshPhongMaterial( { side: THREE.DoubleSide, vertexColors: true } );
  94. scene.add( new THREE.Mesh( merged, material ) );
  95. }
  96. if ( transparentGeometries.length > 0 ) {
  97. const merged = BufferGeometryUtils.mergeGeometries( transparentGeometries );
  98. const material = new THREE.MeshPhongMaterial( {
  99. side: THREE.DoubleSide,
  100. vertexColors: true,
  101. transparent: true,
  102. } );
  103. scene.add( new THREE.Mesh( merged, material ) );
  104. }
  105. }
  106. function getPlacedGeometry( ifcAPI, modelID, placedGeometry, materialCache ) {
  107. const geometry = getBufferGeometry( ifcAPI, modelID, placedGeometry );
  108. const material = getMeshMaterial( placedGeometry.color, materialCache );
  109. const mesh = new THREE.Mesh( geometry, material );
  110. mesh.matrix = new THREE.Matrix4().fromArray( placedGeometry.flatTransformation );
  111. mesh.matrixAutoUpdate = false;
  112. return mesh;
  113. }
  114. function getBufferGeometry( ifcAPI, modelID, placedGeometry ) {
  115. const geometry = ifcAPI.GetGeometry( modelID, placedGeometry.geometryExpressID );
  116. const vertexData = ifcAPI.GetVertexArray( geometry.GetVertexData(), geometry.GetVertexDataSize() );
  117. const indexData = ifcAPI.GetIndexArray( geometry.GetIndexData(), geometry.GetIndexDataSize() );
  118. const bufferGeometry = ifcGeometryToBuffer( placedGeometry.color, vertexData, indexData );
  119. // Geometry is owned by the WASM heap and must be released.
  120. geometry.delete();
  121. return bufferGeometry;
  122. }
  123. function getMeshMaterial( color, materialCache ) {
  124. const id = `${ color.x }-${ color.y }-${ color.z }-${ color.w }`;
  125. const cached = materialCache[ id ];
  126. if ( cached ) return cached;
  127. const material = new THREE.MeshPhongMaterial( {
  128. color: new THREE.Color( color.x, color.y, color.z ),
  129. side: THREE.DoubleSide,
  130. } );
  131. if ( color.w !== 1 ) {
  132. material.transparent = true;
  133. material.opacity = color.w;
  134. }
  135. materialCache[ id ] = material;
  136. return material;
  137. }
  138. const _tmpColor = new THREE.Color();
  139. function ifcGeometryToBuffer( color, vertexData, indexData ) {
  140. // web-ifc returns interleaved [px, py, pz, nx, ny, nz] per vertex.
  141. const vertexCount = vertexData.length / 6;
  142. const positions = new Float32Array( vertexCount * 3 );
  143. const normals = new Float32Array( vertexCount * 3 );
  144. const colors = new Float32Array( vertexCount * 4 );
  145. // IFC stores colors in sRGB display space; convert to linear once per geometry.
  146. _tmpColor.setRGB( color.x, color.y, color.z, THREE.SRGBColorSpace );
  147. for ( let v = 0; v < vertexCount; v ++ ) {
  148. const src = v * 6;
  149. const dst3 = v * 3;
  150. const dst4 = v * 4;
  151. positions[ dst3 + 0 ] = vertexData[ src + 0 ];
  152. positions[ dst3 + 1 ] = vertexData[ src + 1 ];
  153. positions[ dst3 + 2 ] = vertexData[ src + 2 ];
  154. normals[ dst3 + 0 ] = vertexData[ src + 3 ];
  155. normals[ dst3 + 1 ] = vertexData[ src + 4 ];
  156. normals[ dst3 + 2 ] = vertexData[ src + 5 ];
  157. colors[ dst4 + 0 ] = _tmpColor.r;
  158. colors[ dst4 + 1 ] = _tmpColor.g;
  159. colors[ dst4 + 2 ] = _tmpColor.b;
  160. colors[ dst4 + 3 ] = color.w;
  161. }
  162. const geometry = new THREE.BufferGeometry();
  163. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  164. geometry.setAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  165. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 4 ) );
  166. geometry.setIndex( new THREE.BufferAttribute( indexData, 1 ) );
  167. return geometry;
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. render();
  174. }
  175. function render() {
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>
粤ICP备19079148号