webgl_loader_collada_keyframe.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada</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: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a { color: skyblue }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> -
  29. pump by <a href="http://code.google.com/p/kuda" target="_blank">Kuda</a>
  30. </div>
  31. <script src="../build/Three.js"></script>
  32. <script src="js/loaders/ColladaLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/Stats.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var stats;
  38. var scene;
  39. var camera;
  40. var renderer;
  41. var model;
  42. var animations;
  43. var kfAnimations = [ ];
  44. var kfAnimationsLength = 0;
  45. var loader = new THREE.ColladaLoader();
  46. var lastTimestamp;
  47. var progress = 0;
  48. loader.load( './models/collada/pump/pump.dae', function ( collada ) {
  49. model = collada.scene;
  50. animations = collada.animations;
  51. kfAnimationsLength = animations.length;
  52. model.scale.x = model.scale.y = model.scale.z = 0.125; // 1/8 scale, modeled in cm
  53. init();
  54. start();
  55. animate( lastTimestamp );
  56. } );
  57. function init() {
  58. var container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. // Scene
  61. scene = new THREE.Scene();
  62. // Camera
  63. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.01, 1000 );
  64. camera.position.set( -5.00181875, 3.42631375, 11.3102925 );
  65. camera.lookAt( new THREE.Vector3( -1.224774125, 2.18410625, 4.57969125 ) );
  66. scene.add( camera );
  67. // KeyFrame Animations
  68. var animHandler = THREE.AnimationHandler;
  69. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  70. var animation = animations[ i ];
  71. animHandler.add( animation );
  72. var kfAnimation = new THREE.KeyFrameAnimation( animation.node, animation.name );
  73. kfAnimation.timeScale = 1;
  74. kfAnimations.push( kfAnimation );
  75. }
  76. // Grid
  77. var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
  78. var geometry = new THREE.Geometry();
  79. var floor = -0.04, step = 1, size = 14;
  80. for ( var i = 0; i <= size / step * 2; i ++ ) {
  81. geometry.vertices.push( new THREE.Vector3( - size, floor, i * step - size ) );
  82. geometry.vertices.push( new THREE.Vector3( size, floor, i * step - size ) );
  83. geometry.vertices.push( new THREE.Vector3( i * step - size, floor, -size ) );
  84. geometry.vertices.push( new THREE.Vector3( i * step - size, floor, size ) );
  85. }
  86. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  87. scene.add( line );
  88. // Add the COLLADA
  89. model.getChildByName( 'camEye_camera', true ).visible = false;
  90. model.getChildByName( 'camTarget_camera', true ).visible = false;
  91. scene.add( model );
  92. // Lights
  93. pointLight = new THREE.PointLight( 0xffffff, 1.75 );
  94. pointLight.position = camera.position;
  95. pointLight.rotation = camera.rotation;
  96. pointLight.scale = camera.scale;
  97. scene.add( pointLight );
  98. // Renderer
  99. renderer = new THREE.WebGLRenderer( { antialias: true } );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. container.appendChild( renderer.domElement );
  102. // Stats
  103. stats = new Stats();
  104. stats.domElement.style.position = 'absolute';
  105. stats.domElement.style.top = '0px';
  106. container.appendChild( stats.domElement );
  107. //
  108. window.addEventListener( 'resize', onWindowResize, false );
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function start() {
  116. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  117. var animation = kfAnimations[i];
  118. for ( var h = 0, hl = animation.hierarchy.length; h < hl; h++ ) {
  119. var keys = animation.data.hierarchy[ h ].keys;
  120. var sids = animation.data.hierarchy[ h ].sids;
  121. var obj = animation.hierarchy[ h ];
  122. if ( keys.length && sids ) {
  123. for ( var s = 0; s < sids.length; s++ ) {
  124. var sid = sids[ s ];
  125. var next = animation.getNextKeyWith( sid, h, 0 );
  126. if ( next ) next.apply( sid );
  127. }
  128. obj.matrixAutoUpdate = false;
  129. animation.data.hierarchy[ h ].node.updateMatrix();
  130. obj.matrixWorldNeedsUpdate = true;
  131. }
  132. }
  133. animation.play( false, 0 );
  134. lastTimestamp = Date.now();
  135. }
  136. }
  137. function animate( timestamp ) {
  138. var frameTime = ( timestamp - lastTimestamp ) * 0.001; // seconds
  139. if ( progress >= 0 && progress < 48 ) {
  140. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  141. kfAnimations[ i ].update( frameTime );
  142. }
  143. } else if ( progress >= 48 ) {
  144. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  145. kfAnimations[ i ].stop();
  146. }
  147. progress = 0;
  148. start();
  149. }
  150. progress += frameTime;
  151. lastTimestamp = timestamp;
  152. renderer.render( scene, camera );
  153. stats.update();
  154. requestAnimationFrame( animate );
  155. }
  156. </script>
  157. </body>
  158. </html>