webgl_loader_obj.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - OBJ 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> - OBJLoader test
  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. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  27. let container;
  28. let camera, scene, renderer;
  29. let mouseX = 0, mouseY = 0;
  30. let windowHalfX = window.innerWidth / 2;
  31. let windowHalfY = window.innerHeight / 2;
  32. let object;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  39. camera.position.z = 250;
  40. // scene
  41. scene = new THREE.Scene();
  42. const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  43. scene.add( ambientLight );
  44. const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  45. camera.add( pointLight );
  46. scene.add( camera );
  47. // manager
  48. function loadModel() {
  49. object.traverse( function ( child ) {
  50. if ( child.isMesh ) child.material.map = texture;
  51. } );
  52. object.position.y = - 95;
  53. scene.add( object );
  54. }
  55. const manager = new THREE.LoadingManager( loadModel );
  56. // texture
  57. const textureLoader = new THREE.TextureLoader( manager );
  58. const texture = textureLoader.load( 'textures/uv_grid_opengl.jpg' );
  59. // model
  60. function onProgress( xhr ) {
  61. if ( xhr.lengthComputable ) {
  62. const percentComplete = xhr.loaded / xhr.total * 100;
  63. console.log( 'model ' + Math.round( percentComplete, 2 ) + '% downloaded' );
  64. }
  65. }
  66. function onError() {}
  67. const loader = new OBJLoader( manager );
  68. loader.load( 'models/obj/male02/male02.obj', function ( obj ) {
  69. object = obj;
  70. }, onProgress, onError );
  71. //
  72. renderer = new THREE.WebGLRenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. container.appendChild( renderer.domElement );
  76. document.addEventListener( 'mousemove', onDocumentMouseMove );
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. windowHalfX = window.innerWidth / 2;
  82. windowHalfY = window.innerHeight / 2;
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function onDocumentMouseMove( event ) {
  88. mouseX = ( event.clientX - windowHalfX ) / 2;
  89. mouseY = ( event.clientY - windowHalfY ) / 2;
  90. }
  91. //
  92. function animate() {
  93. requestAnimationFrame( animate );
  94. render();
  95. }
  96. function render() {
  97. camera.position.x += ( mouseX - camera.position.x ) * .05;
  98. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  99. camera.lookAt( scene.position );
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>