webgl_loader_3ds.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - 3DS 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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 3DS loader
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  26. import { TDSLoader } from './jsm/loaders/TDSLoader.js';
  27. let container, controls;
  28. let camera, scene, renderer;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10 );
  35. camera.position.z = 2;
  36. scene = new THREE.Scene();
  37. scene.add( new THREE.HemisphereLight() );
  38. const directionalLight = new THREE.DirectionalLight( 0xffeedd );
  39. directionalLight.position.set( 0, 0, 2 );
  40. scene.add( directionalLight );
  41. //3ds files dont store normal maps
  42. const normal = new THREE.TextureLoader().load( 'models/3ds/portalgun/textures/normal.jpg' );
  43. const loader = new TDSLoader( );
  44. loader.setResourcePath( 'models/3ds/portalgun/textures/' );
  45. loader.load( 'models/3ds/portalgun/portalgun.3ds', function ( object ) {
  46. object.traverse( function ( child ) {
  47. if ( child.isMesh ) {
  48. child.material.specular.setScalar( 0.1 );
  49. child.material.normalMap = normal;
  50. }
  51. } );
  52. scene.add( object );
  53. } );
  54. renderer = new THREE.WebGLRenderer();
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.outputEncoding = THREE.sRGBEncoding;
  58. container.appendChild( renderer.domElement );
  59. controls = new TrackballControls( camera, renderer.domElement );
  60. window.addEventListener( 'resize', resize );
  61. }
  62. function resize() {
  63. camera.aspect = window.innerWidth / window.innerHeight;
  64. camera.updateProjectionMatrix();
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. }
  67. function animate() {
  68. controls.update();
  69. renderer.render( scene, camera );
  70. requestAnimationFrame( animate );
  71. }
  72. </script>
  73. </body>
  74. </html>