webgl_loader_obj.html 3.5 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. <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="../build/three.min.js"></script>
  32. <script src="js/loaders/OBJLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script>
  36. var container, stats;
  37. var camera, scene, renderer;
  38. var mouseX = 0, mouseY = 0;
  39. var windowHalfX = window.innerWidth / 2;
  40. var windowHalfY = window.innerHeight / 2;
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  47. camera.position.z = 100;
  48. // scene
  49. scene = new THREE.Scene();
  50. var ambient = new THREE.AmbientLight( 0x101030 );
  51. scene.add( ambient );
  52. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  53. directionalLight.position.set( 0, 0, 1 );
  54. scene.add( directionalLight );
  55. // texture
  56. var manager = new THREE.LoadingManager();
  57. manager.onProgress = function ( item, loaded, total ) {
  58. console.log( item, loaded, total );
  59. };
  60. var texture = new THREE.Texture();
  61. var loader = new THREE.ImageLoader( manager );
  62. loader.load( 'textures/UV_Grid_Sm.jpg', function ( image ) {
  63. texture.image = image;
  64. texture.needsUpdate = true;
  65. } );
  66. // model
  67. var loader = new THREE.OBJLoader( manager );
  68. loader.load( 'obj/male02/male02.obj', function ( object ) {
  69. object.traverse( function ( child ) {
  70. if ( child instanceof THREE.Mesh ) {
  71. child.material.map = texture;
  72. }
  73. } );
  74. object.position.y = - 80;
  75. scene.add( object );
  76. } );
  77. //
  78. renderer = new THREE.WebGLRenderer();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. container.appendChild( renderer.domElement );
  81. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  82. //
  83. window.addEventListener( 'resize', onWindowResize, false );
  84. }
  85. function onWindowResize() {
  86. windowHalfX = window.innerWidth / 2;
  87. windowHalfY = window.innerHeight / 2;
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function onDocumentMouseMove( event ) {
  93. mouseX = ( event.clientX - windowHalfX ) / 2;
  94. mouseY = ( event.clientY - windowHalfY ) / 2;
  95. }
  96. //
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. render();
  100. }
  101. function render() {
  102. camera.position.x += ( mouseX - camera.position.x ) * .05;
  103. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  104. camera.lookAt( scene.position );
  105. renderer.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>