webgl_loader_obj_mtl.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - OBJLoader + MTLLoader</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> - OBJLoader + MTLLoader
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { DDSLoader } from './jsm/loaders/DDSLoader.js';
  16. import { MTLLoader } from './jsm/loaders/MTLLoader.js';
  17. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  18. var container;
  19. var camera, scene, renderer;
  20. var mouseX = 0, mouseY = 0;
  21. var windowHalfX = window.innerWidth / 2;
  22. var windowHalfY = window.innerHeight / 2;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  29. camera.position.z = 250;
  30. // scene
  31. scene = new THREE.Scene();
  32. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  33. scene.add( ambientLight );
  34. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  35. camera.add( pointLight );
  36. scene.add( camera );
  37. // model
  38. var onProgress = function ( xhr ) {
  39. if ( xhr.lengthComputable ) {
  40. var percentComplete = xhr.loaded / xhr.total * 100;
  41. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  42. }
  43. };
  44. var onError = function () { };
  45. var manager = new THREE.LoadingManager();
  46. manager.addHandler( /\.dds$/i, new DDSLoader() );
  47. // comment in the following line and import TGALoader if your asset uses TGA textures
  48. // manager.addHandler( /\.tga$/i, new TGALoader() );
  49. new MTLLoader( manager )
  50. .setPath( 'models/obj/male02/' )
  51. .load( 'male02_dds.mtl', function ( materials ) {
  52. materials.preload();
  53. new OBJLoader( manager )
  54. .setMaterials( materials )
  55. .setPath( 'models/obj/male02/' )
  56. .load( 'male02.obj', function ( object ) {
  57. object.position.y = - 95;
  58. scene.add( object );
  59. }, onProgress, onError );
  60. } );
  61. //
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  67. //
  68. window.addEventListener( 'resize', onWindowResize, false );
  69. }
  70. function onWindowResize() {
  71. windowHalfX = window.innerWidth / 2;
  72. windowHalfY = window.innerHeight / 2;
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. function onDocumentMouseMove( event ) {
  78. mouseX = ( event.clientX - windowHalfX ) / 2;
  79. mouseY = ( event.clientY - windowHalfY ) / 2;
  80. }
  81. //
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. render();
  85. }
  86. function render() {
  87. camera.position.x += ( mouseX - camera.position.x ) * .05;
  88. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  89. camera.lookAt( scene.position );
  90. renderer.render( scene, camera );
  91. }
  92. </script>
  93. </body>
  94. </html>