webgl_loader_utf8.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.js"></script>
  34. <script src="js/Detector.js"></script>
  35. <script src="js/Stats.js"></script>
  36. <script>
  37. var SCREEN_WIDTH = window.innerWidth;
  38. var SCREEN_HEIGHT = window.innerHeight;
  39. var FLOOR = -150;
  40. var container, stats;
  41. var camera, scene, renderer;
  42. var mesh, zmesh, geometry;
  43. var mouseX = 0, mouseY = 0;
  44. var windowHalfX = window.innerWidth / 2;
  45. var windowHalfY = window.innerHeight / 2;
  46. document.addEventListener('mousemove', onDocumentMouseMove, false);
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. scene = new THREE.Scene();
  53. scene.fog = new THREE.Fog( 0x000000, 800, 2000 );
  54. camera = new THREE.PerspectiveCamera( 20, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 2000 );
  55. camera.position.z = 800;
  56. scene.add( camera );
  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( "utf8/hand.utf8", function( geometry ) { callbackModel( geometry, 400, 0xffffff, 125, FLOOR, 0 ); },
  85. { scale: 0.815141, offsetX: -0.371823, offsetY: -0.011920, offsetZ: -0.416061 } );
  86. loader.load( "utf8/ben.utf8", function( geometry ) { callbackModel( geometry, 400, 0xffaa00, -125, FLOOR, 0 ); },
  87. { scale: 0.707192, offsetX: -0.109362, offsetY: -0.006435, offsetZ: -0.268751 } );
  88. }
  89. function callbackModel( geometry, s, color, x, y, z ) {
  90. var material = new THREE.MeshLambertMaterial( { color: color, map: THREE.ImageUtils.loadTexture( "textures/ash_uvgrid01.jpg" ), envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  91. //material.shading = THREE.FlatShading;
  92. var mesh = new THREE.Mesh( geometry, material );
  93. mesh.position.set( x, y, z );
  94. mesh.scale.set( s, s, s );
  95. scene.add( mesh );
  96. }
  97. function onDocumentMouseMove( event ) {
  98. mouseX = ( event.clientX - windowHalfX );
  99. mouseY = ( event.clientY - windowHalfY );
  100. }
  101. //
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. render();
  105. stats.update();
  106. }
  107. function render() {
  108. camera.position.x += ( mouseX - camera.position.x ) * .05;
  109. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  110. camera.lookAt( scene.position );
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>