webgl_loader_obj.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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( function ( item, loaded, total ) {
  57. console.log( item, loaded, total );
  58. } );
  59. var texture = new THREE.Texture();
  60. var loader = new THREE.ImageLoader( manager );
  61. loader.load( 'textures/ash_uvgrid01.jpg', function ( image ) {
  62. texture.image = image;
  63. texture.needsUpdate = true;
  64. } );
  65. // model
  66. var loader = new THREE.OBJLoader( manager );
  67. loader.load( 'obj/male02/male02.obj', function ( object ) {
  68. object.traverse( function ( child ) {
  69. if ( child instanceof THREE.Mesh ) {
  70. child.material.map = texture;
  71. }
  72. } );
  73. object.position.y = - 80;
  74. scene.add( object );
  75. } );
  76. //
  77. renderer = new THREE.WebGLRenderer();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  81. //
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. }
  84. function onWindowResize() {
  85. windowHalfX = window.innerWidth / 2;
  86. windowHalfY = window.innerHeight / 2;
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function onDocumentMouseMove( event ) {
  92. mouseX = ( event.clientX - windowHalfX ) / 2;
  93. mouseY = ( event.clientY - windowHalfY ) / 2;
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. }
  100. function render() {
  101. camera.position.x += ( mouseX - camera.position.x ) * .05;
  102. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  103. camera.lookAt( scene.position );
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>