webgl_loader_3ds.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  16. import { TDSLoader } from './jsm/loaders/TDSLoader.js';
  17. let container, controls;
  18. let camera, scene, renderer;
  19. init();
  20. animate();
  21. function init() {
  22. container = document.createElement( 'div' );
  23. document.body.appendChild( container );
  24. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10 );
  25. camera.position.z = 2;
  26. scene = new THREE.Scene();
  27. scene.add( new THREE.HemisphereLight() );
  28. const directionalLight = new THREE.DirectionalLight( 0xffeedd );
  29. directionalLight.position.set( 0, 0, 2 );
  30. scene.add( directionalLight );
  31. //3ds files dont store normal maps
  32. const normal = new THREE.TextureLoader().load( 'models/3ds/portalgun/textures/normal.jpg' );
  33. const loader = new TDSLoader( );
  34. loader.setResourcePath( 'models/3ds/portalgun/textures/' );
  35. loader.load( 'models/3ds/portalgun/portalgun.3ds', function ( object ) {
  36. object.traverse( function ( child ) {
  37. if ( child.isMesh ) {
  38. child.material.specular.setScalar( 0.1 );
  39. child.material.normalMap = normal;
  40. }
  41. } );
  42. scene.add( object );
  43. } );
  44. renderer = new THREE.WebGLRenderer();
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.outputEncoding = THREE.sRGBEncoding;
  48. container.appendChild( renderer.domElement );
  49. controls = new TrackballControls( camera, renderer.domElement );
  50. window.addEventListener( 'resize', resize );
  51. }
  52. function resize() {
  53. camera.aspect = window.innerWidth / window.innerHeight;
  54. camera.updateProjectionMatrix();
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. }
  57. function animate() {
  58. controls.update();
  59. renderer.render( scene, camera );
  60. requestAnimationFrame( animate );
  61. }
  62. </script>
  63. </body>
  64. </html>