loader.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display:block;
  23. }
  24. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="http://threejs.org" target="_blank">three.js</a> - OBJLoader test
  30. </div>
  31. <script src="node_modules/three/three.min.js"></script>
  32. <script src="node_modules/three/OBJLoader.js"></script>
  33. <script>
  34. var container;
  35. var camera, scene, renderer;
  36. var mouseX = 0, mouseY = 0;
  37. var windowHalfX = window.innerWidth / 2;
  38. var windowHalfY = window.innerHeight / 2;
  39. init();
  40. animate();
  41. function init() {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  45. camera.position.z = 250;
  46. // scene
  47. scene = new THREE.Scene();
  48. var ambient = new THREE.AmbientLight( 0x101030 );
  49. scene.add( ambient );
  50. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  51. directionalLight.position.set( 0, 0, 1 );
  52. scene.add( directionalLight );
  53. // texture
  54. var manager = new THREE.LoadingManager();
  55. manager.onProgress = function ( item, loaded, total ) {
  56. console.log( item, loaded, total );
  57. };
  58. var texture = new THREE.Texture();
  59. var onProgress = function ( xhr ) {
  60. if ( xhr.lengthComputable ) {
  61. var percentComplete = xhr.loaded / xhr.total * 100;
  62. console.log( Math.round(percentComplete, 2) + '% downloaded' );
  63. }
  64. };
  65. var onError = function ( xhr ) {
  66. };
  67. var loader = new THREE.ImageLoader( manager );
  68. loader.load( 'ship_diffuse.jpg', function ( image ) {
  69. texture.image = image;
  70. texture.needsUpdate = true;
  71. } );
  72. // model
  73. var loader = new THREE.OBJLoader( manager );
  74. loader.load( 'ship_engine.obj', function ( object ) {
  75. object.traverse( function ( child ) {
  76. if ( child instanceof THREE.Mesh ) {
  77. child.material.map = texture;
  78. }
  79. } );
  80. object.position.y = - 95;
  81. scene.add( object );
  82. }, onProgress, onError );
  83. //
  84. renderer = new THREE.WebGLRenderer();
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. container.appendChild( renderer.domElement );
  88. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  89. //
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. }
  92. function onWindowResize() {
  93. windowHalfX = window.innerWidth / 2;
  94. windowHalfY = window.innerHeight / 2;
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. function onDocumentMouseMove( event ) {
  100. mouseX = ( event.clientX - windowHalfX ) / 2;
  101. mouseY = ( event.clientY - windowHalfY ) / 2;
  102. }
  103. //
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. }
  108. function render() {
  109. camera.position.x += ( mouseX - camera.position.x ) * .05;
  110. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  111. camera.lookAt( scene.position );
  112. renderer.render( scene, camera );
  113. }
  114. </script>
  115. </body>
  116. </html>