webgl_loader_ifc.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. <link type="text/css" rel="stylesheet" href="main.css" />
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  15. -
  16. <a href="https://www.buildingsmart.org/standards/bsi-standards/industry-foundation-classes/" target="_blank" rel="noopener">IFCLoader</a>
  17. </div>
  18. <!-- Import maps polyfill -->
  19. <!-- Remove this when import maps will be widely supported -->
  20. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { IFCLoader } from 'three/addons/loaders/IFCLoader.js';
  33. let scene, camera, renderer;
  34. init();
  35. function init() {
  36. //Scene
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0x8cc7de );
  39. //Camera
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  41. camera.position.z = - 70;
  42. camera.position.y = 25;
  43. camera.position.x = 90;
  44. //Initial cube
  45. const geometry = new THREE.BoxGeometry();
  46. const material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  47. const cube = new THREE.Mesh( geometry, material );
  48. scene.add( cube );
  49. //Lights
  50. const directionalLight1 = new THREE.DirectionalLight( 0xffeeff, 0.8 );
  51. directionalLight1.position.set( 1, 1, 1 );
  52. scene.add( directionalLight1 );
  53. const directionalLight2 = new THREE.DirectionalLight( 0xffffff, 0.8 );
  54. directionalLight2.position.set( - 1, 0.5, - 1 );
  55. scene.add( directionalLight2 );
  56. const ambientLight = new THREE.AmbientLight( 0xffffee, 0.25 );
  57. scene.add( ambientLight );
  58. //Setup IFC Loader
  59. const ifcLoader = new IFCLoader();
  60. ifcLoader.ifcManager.setWasmPath( 'jsm/loaders/ifc/' );
  61. ifcLoader.load( 'models/ifc/rac_advanced_sample_project.ifc', function ( model ) {
  62. scene.add( model.mesh );
  63. render();
  64. } );
  65. const highlightMaterial = new THREE.MeshPhongMaterial( { color: 0xff00ff, depthTest: false, transparent: true, opacity: 0.3 } );
  66. function selectObject( event ) {
  67. if ( event.button != 0 ) return;
  68. const mouse = new THREE.Vector2();
  69. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  70. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  71. const raycaster = new THREE.Raycaster();
  72. raycaster.setFromCamera( mouse, camera );
  73. const intersected = raycaster.intersectObjects( scene.children, false );
  74. if ( intersected.length ) {
  75. const found = intersected[ 0 ];
  76. const faceIndex = found.faceIndex;
  77. const geometry = found.object.geometry;
  78. const id = ifcLoader.ifcManager.getExpressId( geometry, faceIndex );
  79. const modelID = found.object.modelID;
  80. ifcLoader.ifcManager.createSubset( { modelID, ids: [ id ], scene, removePrevious: true, material: highlightMaterial } );
  81. const props = ifcLoader.ifcManager.getItemProperties( modelID, id, true );
  82. console.log( props );
  83. renderer.render( scene, camera );
  84. }
  85. }
  86. window.onpointerdown = selectObject;
  87. //Renderer
  88. renderer = new THREE.WebGLRenderer( { antialias: true } );
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. document.body.appendChild( renderer.domElement );
  92. //Controls
  93. const controls = new OrbitControls( camera, renderer.domElement );
  94. controls.addEventListener( 'change', render );
  95. window.addEventListener( 'resize', onWindowResize );
  96. render();
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. render();
  103. }
  104. function render() {
  105. renderer.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>