webgl_loader_obj.html 3.3 KB

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