webgl_loader_obj.html 3.6 KB

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