webgl_loader_ctm.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - io - CTM 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://openctm.sourceforge.net/" target="_blank">CTM format</a> loader test -
  31. using <a href="http://code.google.com/p/js-openctm/">js-openctm</a> -
  32. models from <a href="http://www.sci.utah.edu/~wald/animrep/" target="_blank">The Utah 3D Animation Repository</a>,
  33. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a>,
  34. <a href="http://davidoreilly.com/post/18087489343/disneyhead" target="_blank">David OReilly</a>
  35. </div>
  36. <script src="../build/Three.js"></script>
  37. <script src="js/ctm/lzma.js"></script>
  38. <script src="js/ctm/ctm.js"></script>
  39. <script src="js/ctm/CTMLoader.js"></script>
  40. <script src="js/Detector.js"></script>
  41. <script src="js/Stats.js"></script>
  42. <script>
  43. var SCREEN_WIDTH = window.innerWidth;
  44. var SCREEN_HEIGHT = window.innerHeight;
  45. var container, stats;
  46. var camera, scene, renderer;
  47. var mesh, zmesh, geometry;
  48. var mouseX = 0, mouseY = 0;
  49. var windowHalfX = window.innerWidth / 2;
  50. var windowHalfY = window.innerHeight / 2;
  51. document.addEventListener('mousemove', onDocumentMouseMove, false);
  52. init();
  53. animate();
  54. function init() {
  55. container = document.createElement( 'div' );
  56. document.body.appendChild( container );
  57. scene = new THREE.Scene();
  58. scene.fog = new THREE.Fog( 0x050505, 800, 2000 );
  59. camera = new THREE.PerspectiveCamera( 20, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 2000 );
  60. camera.position.z = 800;
  61. scene.add( camera );
  62. var path = "textures/cube/SwedishRoyalCastle/";
  63. var format = '.jpg';
  64. var urls = [
  65. path + 'px' + format, path + 'nx' + format,
  66. path + 'py' + format, path + 'ny' + format,
  67. path + 'pz' + format, path + 'nz' + format
  68. ];
  69. reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
  70. // LIGHTS
  71. var ambient = new THREE.AmbientLight( 0x050505 );
  72. scene.add( ambient );
  73. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  74. directionalLight.position.set( 0, 0, 1 ).normalize();
  75. scene.add( directionalLight );
  76. // RENDERER
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  79. renderer.setClearColor( scene.fog.color, 1 );
  80. renderer.domElement.style.position = "relative";
  81. container.appendChild( renderer.domElement );
  82. //
  83. renderer.gammaInput = true;
  84. renderer.gammaOutput = true;
  85. // STATS
  86. stats = new Stats();
  87. stats.domElement.style.position = 'absolute';
  88. stats.domElement.style.top = '0px';
  89. stats.domElement.style.zIndex = 100;
  90. container.appendChild( stats.domElement );
  91. // EVENTS
  92. window.addEventListener( 'resize', onWindowResize, false );
  93. // LOADER
  94. var c = 0, s = Date.now();
  95. function checkTime() {
  96. c ++;
  97. if ( c === 3 ) {
  98. var e = Date.now();
  99. console.log( "Total parse time: " + (e-s) + " ms" );
  100. }
  101. }
  102. var useWorker = true;
  103. var useBuffers = true;
  104. var loader = new THREE.CTMLoader( renderer.context );
  105. loader.load( "ctm/ben.ctm", function( geometry ) {
  106. var material = new THREE.MeshLambertMaterial( { color: 0xffaa00, map: THREE.ImageUtils.loadTexture( "textures/ash_uvgrid01.jpg" ), envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  107. callbackModel( geometry, 450, material, 0, -200, 0, 0, 0 );
  108. checkTime();
  109. }, useWorker, useBuffers );
  110. loader.load( "ctm/WaltHead.ctm", function( geometry ) {
  111. //geometry.computeVertexNormals();
  112. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, envMaps: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  113. callbackModel( geometry, 5, material, -200, 0, 0, 0, 0 );
  114. checkTime();
  115. }, useWorker, useBuffers );
  116. loader.load( "ctm/LeePerry.ctm", function( geometry ) {
  117. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x444444, shininess: 30, map: THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" ), envMaps: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  118. callbackModel( geometry, 1300, material, 200, 50, 0, 0, 0 );
  119. checkTime();
  120. }, useWorker, useBuffers );
  121. }
  122. function callbackModel( geometry, s, material, x, y, z, rx, ry ) {
  123. var mesh = new THREE.Mesh( geometry, material );
  124. mesh.position.set( x, y, z );
  125. mesh.scale.set( s, s, s );
  126. mesh.rotation.x = rx;
  127. mesh.rotation.z = ry;
  128. //mesh.flipSided = true;
  129. scene.add( mesh );
  130. }
  131. //
  132. function onWindowResize( event ) {
  133. SCREEN_WIDTH = window.innerWidth;
  134. SCREEN_HEIGHT = window.innerHeight;
  135. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  136. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  137. camera.updateProjectionMatrix();
  138. }
  139. function onDocumentMouseMove( event ) {
  140. mouseX = ( event.clientX - windowHalfX );
  141. mouseY = ( event.clientY - windowHalfY );
  142. }
  143. //
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. render();
  147. stats.update();
  148. }
  149. function render() {
  150. camera.position.x += ( mouseX - camera.position.x ) * .05;
  151. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  152. camera.lookAt( scene.position );
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>