webgl_loader_utf8.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - io - UTF8 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://github.com/mrdoob/three.js" target="_blank">three.js</a> -
  30. <a href="http://code.google.com/p/webgl-loader/" target="_blank">UTF8 format</a> loader test -
  31. models from <a href="http://www.sci.utah.edu/~wald/animrep/" target="_blank">The Utah 3D Animation Repository</a>
  32. </div>
  33. <script src="../build/three.min.js"></script>
  34. <script src="js/loaders/UTF8Loader.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/Stats.js"></script>
  37. <script>
  38. var SCREEN_WIDTH = window.innerWidth;
  39. var SCREEN_HEIGHT = window.innerHeight;
  40. var FLOOR = -150;
  41. var container, stats;
  42. var camera, scene, renderer;
  43. var mesh, zmesh, geometry;
  44. var mouseX = 0, mouseY = 0;
  45. var windowHalfX = window.innerWidth / 2;
  46. var windowHalfY = window.innerHeight / 2;
  47. document.addEventListener('mousemove', onDocumentMouseMove, false);
  48. init();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. camera = new THREE.PerspectiveCamera( 20, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 2000 );
  54. camera.position.z = 800;
  55. scene = new THREE.Scene();
  56. scene.fog = new THREE.Fog( 0x000000, 800, 2000 );
  57. var path = "textures/cube/SwedishRoyalCastle/";
  58. var format = '.jpg';
  59. var urls = [
  60. path + 'px' + format, path + 'nx' + format,
  61. path + 'py' + format, path + 'ny' + format,
  62. path + 'pz' + format, path + 'nz' + format
  63. ];
  64. reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
  65. // LIGHTS
  66. var ambient = new THREE.AmbientLight( 0x221100 );
  67. scene.add( ambient );
  68. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  69. directionalLight.position.set( 0, 0, 1 ).normalize();
  70. scene.add( directionalLight );
  71. // RENDERER
  72. renderer = new THREE.WebGLRenderer();
  73. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  74. renderer.setClearColor( scene.fog.color, 1 );
  75. renderer.domElement.style.position = "relative";
  76. container.appendChild( renderer.domElement );
  77. // STATS
  78. stats = new Stats();
  79. stats.domElement.style.position = 'absolute';
  80. stats.domElement.style.top = '0px';
  81. stats.domElement.style.zIndex = 100;
  82. container.appendChild( stats.domElement );
  83. var loader = new THREE.UTF8Loader();
  84. loader.load( "models/utf8/hand.utf8", function ( geometry ) {
  85. callbackModel( geometry, 400, 0xffffff, 125, FLOOR, 0 );
  86. }, { scale: 0.815141, offsetX: -0.371823, offsetY: -0.011920, offsetZ: -0.416061 } );
  87. loader.load( "models/utf8/ben.utf8", function ( geometry ) {
  88. callbackModel( geometry, 400, 0xffaa00, -125, FLOOR, 0 );
  89. }, { scale: 0.707192, offsetX: -0.109362, offsetY: -0.006435, offsetZ: -0.268751 } );
  90. //
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. }
  93. function onWindowResize() {
  94. windowHalfX = window.innerWidth / 2;
  95. windowHalfY = window.innerHeight / 2;
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function callbackModel( geometry, s, color, x, y, z ) {
  101. var material = new THREE.MeshLambertMaterial( {
  102. color: color,
  103. map: THREE.ImageUtils.loadTexture( "textures/ash_uvgrid01.jpg" ),
  104. envMap: reflectionCube,
  105. combine: THREE.MixOperation,
  106. reflectivity: 0.3
  107. } );
  108. //material.shading = THREE.FlatShading;
  109. var mesh = new THREE.Mesh( geometry, material );
  110. mesh.position.set( x, y, z );
  111. mesh.scale.set( s, s, s );
  112. scene.add( mesh );
  113. }
  114. function onDocumentMouseMove( event ) {
  115. mouseX = ( event.clientX - windowHalfX );
  116. mouseY = ( event.clientY - windowHalfY );
  117. }
  118. //
  119. function animate() {
  120. requestAnimationFrame( animate );
  121. render();
  122. stats.update();
  123. }
  124. function render() {
  125. camera.position.x += ( mouseX - camera.position.x ) * .05;
  126. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  127. camera.lookAt( scene.position );
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>