webgl_loader_ifc.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  31. import { IFCLoader } from './jsm/loaders/IFCLoader.js';
  32. let scene, camera, renderer;
  33. init();
  34. function init() {
  35. //Scene
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x8cc7de );
  38. //Camera
  39. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  40. camera.position.z = - 70;
  41. camera.position.y = 25;
  42. camera.position.x = 90;
  43. //Initial cube
  44. const geometry = new THREE.BoxGeometry();
  45. const material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  46. const cube = new THREE.Mesh( geometry, material );
  47. scene.add( cube );
  48. //Lights
  49. const directionalLight1 = new THREE.DirectionalLight( 0xffeeff, 0.8 );
  50. directionalLight1.position.set( 1, 1, 1 );
  51. scene.add( directionalLight1 );
  52. const directionalLight2 = new THREE.DirectionalLight( 0xffffff, 0.8 );
  53. directionalLight2.position.set( - 1, 0.5, - 1 );
  54. scene.add( directionalLight2 );
  55. const ambientLight = new THREE.AmbientLight( 0xffffee, 0.25 );
  56. scene.add( ambientLight );
  57. //Setup IFC Loader
  58. const ifcLoader = new IFCLoader();
  59. ifcLoader.ifcManager.setWasmPath( 'jsm/loaders/ifc/' );
  60. ifcLoader.load( 'models/ifc/rac_advanced_sample_project.ifc', function ( model ) {
  61. scene.add( model.mesh );
  62. render();
  63. } );
  64. const highlightMaterial = new THREE.MeshPhongMaterial( { color: 0xff00ff, depthTest: false, transparent: true, opacity: 0.3 } );
  65. function selectObject( event ) {
  66. if ( event.button != 0 ) return;
  67. const mouse = new THREE.Vector2();
  68. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  69. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  70. const raycaster = new THREE.Raycaster();
  71. raycaster.setFromCamera( mouse, camera );
  72. const intersected = raycaster.intersectObjects( scene.children, false );
  73. if ( intersected.length ) {
  74. const found = intersected[ 0 ];
  75. const faceIndex = found.faceIndex;
  76. const geometry = found.object.geometry;
  77. const id = ifcLoader.ifcManager.getExpressId( geometry, faceIndex );
  78. const modelID = found.object.modelID;
  79. ifcLoader.ifcManager.createSubset( { modelID, ids: [ id ], scene, removePrevious: true, material: highlightMaterial } );
  80. const props = ifcLoader.ifcManager.getItemProperties( modelID, id, true );
  81. console.log( props );
  82. renderer.render( scene, camera );
  83. }
  84. }
  85. window.onpointerdown = selectObject;
  86. //Renderer
  87. renderer = new THREE.WebGLRenderer( { antialias: true } );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. document.body.appendChild( renderer.domElement );
  91. //Controls
  92. const controls = new OrbitControls( camera, renderer.domElement );
  93. controls.addEventListener( 'change', render );
  94. window.addEventListener( 'resize', onWindowResize );
  95. render();
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. render();
  102. }
  103. function render() {
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>