webgl_loader_3dm.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - Rhino 3DM loader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #loader {
  10. border: 5px solid #f3f3f3; /* Light grey */
  11. border-top: 5px solid #3d3d3d; /* Grey */
  12. border-radius: 50%;
  13. width: 40px;
  14. height: 40px;
  15. animation: spin 1s linear infinite;
  16. position: absolute;
  17. top: 50%;
  18. left: 50%;
  19. z-index: 2;
  20. }
  21. @keyframes spin {
  22. 0% { transform: rotate(0deg); }
  23. 100% { transform: rotate(360deg); }
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Rhino 3DM loader
  30. </div>
  31. <div id="loader"></div>
  32. <script type="module">
  33. import * as THREE from '../build/three.module.js';
  34. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  35. import { Rhino3dmLoader } from './jsm/loaders/3DMLoader.js';
  36. import { GUI } from './jsm/libs/dat.gui.module.js';
  37. let container, controls;
  38. let camera, scene, renderer;
  39. let gui;
  40. init();
  41. animate();
  42. function init() {
  43. THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 0, 1 );
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( 26, - 40, 5 );
  48. scene = new THREE.Scene();
  49. const directionalLight = new THREE.DirectionalLight( 0xffffff );
  50. directionalLight.position.set( 0, 0, 2 );
  51. directionalLight.castShadow = true;
  52. directionalLight.intensity = 2;
  53. scene.add( directionalLight );
  54. const loader = new Rhino3dmLoader();
  55. loader.setLibraryPath( 'https://cdn.jsdelivr.net/npm/[email protected]/' );
  56. loader.load( 'models/3dm/Rhino_Logo.3dm', function ( object ) {
  57. scene.add( object );
  58. initGUI( object.userData.layers );
  59. // hide spinner
  60. document.getElementById( 'loader' ).style.display = 'none';
  61. } );
  62. const width = window.innerWidth;
  63. const height = window.innerHeight;
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( width, height );
  67. container.appendChild( renderer.domElement );
  68. controls = new OrbitControls( camera, container );
  69. window.addEventListener( 'resize', resize );
  70. }
  71. function resize() {
  72. const width = window.innerWidth;
  73. const height = window.innerHeight;
  74. camera.aspect = width / height;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( width, height );
  77. }
  78. function animate() {
  79. controls.update();
  80. renderer.render( scene, camera );
  81. requestAnimationFrame( animate );
  82. }
  83. function initGUI( layers ) {
  84. gui = new GUI( { width: 300 } );
  85. const layersControl = gui.addFolder( 'layers' );
  86. layersControl.open();
  87. for ( let i = 0; i < layers.length; i ++ ) {
  88. const layer = layers[ i ];
  89. layersControl.add( layer, 'visible' ).name( layer.name ).onChange( function ( val ) {
  90. const name = this.object.name;
  91. scene.traverse( function ( child ) {
  92. if ( child.userData.hasOwnProperty( 'attributes' ) ) {
  93. if ( 'layerIndex' in child.userData.attributes ) {
  94. const layerName = layers[ child.userData.attributes.layerIndex ].name;
  95. if ( layerName === name ) {
  96. child.visible = val;
  97. layer.visible = val;
  98. }
  99. }
  100. }
  101. } );
  102. } );
  103. }
  104. }
  105. </script>
  106. </body>
  107. </html>