webgl_loader_obj.html 3.6 KB

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