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://www.davidoreilly.com/2009/01/walt-disneys-head-on-a-plate" 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. camera = new THREE.PerspectiveCamera( 20, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 2000 );
  58. camera.position.z = 800;
  59. scene = new THREE.Scene();
  60. scene.fog = new THREE.Fog( 0x050505, 800, camera.far );
  61. var path = "textures/cube/SwedishRoyalCastle/";
  62. var format = '.jpg';
  63. var urls = [
  64. path + 'px' + format, path + 'nx' + format,
  65. path + 'py' + format, path + 'ny' + format,
  66. path + 'pz' + format, path + 'nz' + format
  67. ];
  68. reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
  69. // LIGHTS
  70. var ambient = new THREE.AmbientLight( 0xaaaaaa );
  71. scene.add( ambient );
  72. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  73. directionalLight.position.set( 0, 0, 1 ).normalize();
  74. scene.add( directionalLight );
  75. // RENDERER
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  78. renderer.setClearColor( scene.fog.color, 1 );
  79. renderer.domElement.style.position = "relative";
  80. container.appendChild( renderer.domElement );
  81. //
  82. renderer.gammaInput = true;
  83. renderer.gammaOutput = true;
  84. // STATS
  85. stats = new Stats();
  86. stats.domElement.style.position = 'absolute';
  87. stats.domElement.style.top = '0px';
  88. stats.domElement.style.zIndex = 100;
  89. container.appendChild( stats.domElement );
  90. // EVENTS
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. // LOADER
  93. var c = 0, s = Date.now();
  94. function checkTime() {
  95. c ++;
  96. if ( c === 3 ) {
  97. var e = Date.now();
  98. console.log( "Total parse time: " + (e-s) + " ms" );
  99. }
  100. }
  101. var useWorker = true
  102. useBuffers = true;
  103. var loader = new THREE.CTMLoader( renderer.context );
  104. loader.load( "ctm/ben.ctm", function( geometry ) {
  105. var material = new THREE.MeshLambertMaterial( { color: 0xffaa00, map: THREE.ImageUtils.loadTexture( "textures/ash_uvgrid01.jpg" ), envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  106. callbackModel( geometry, 450, material, 0, -200, 0, 0, 0 );
  107. checkTime();
  108. }, useWorker, useBuffers );
  109. loader.load( "ctm/WaltHead.ctm", function( geometry ) {
  110. //geometry.computeVertexNormals();
  111. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, envMaps: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  112. callbackModel( geometry, 5, material, -200, 0, 0, 0, 0 );
  113. checkTime();
  114. }, useWorker, useBuffers );
  115. loader.load( "ctm/LeePerry.ctm", function( geometry ) {
  116. 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 } );
  117. callbackModel( geometry, 1300, material, 200, 50, 0, 0, 0 );
  118. checkTime();
  119. }, useWorker, useBuffers );
  120. }
  121. function callbackModel( geometry, s, material, x, y, z, rx, ry ) {
  122. var mesh = new THREE.Mesh( geometry, material );
  123. mesh.position.set( x, y, z );
  124. mesh.scale.set( s, s, s );
  125. mesh.rotation.x = rx;
  126. mesh.rotation.z = ry;
  127. //mesh.flipSided = true;
  128. scene.add( mesh );
  129. }
  130. //
  131. function onWindowResize( event ) {
  132. SCREEN_WIDTH = window.innerWidth;
  133. SCREEN_HEIGHT = window.innerHeight;
  134. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  135. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  136. camera.updateProjectionMatrix();
  137. }
  138. function onDocumentMouseMove( event ) {
  139. mouseX = ( event.clientX - windowHalfX );
  140. mouseY = ( event.clientY - windowHalfY );
  141. }
  142. //
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. render();
  146. stats.update();
  147. }
  148. function render() {
  149. camera.position.x += ( mouseX - camera.position.x ) * .05;
  150. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  151. camera.lookAt( scene.position );
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>
粤ICP备19079148号