webgl_loader_collada_keyframe.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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://threejs.org" 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/collada/Animation.js"></script>
  33. <script src="js/loaders/collada/AnimationHandler.js"></script>
  34. <script src="js/loaders/collada/KeyFrameAnimation.js"></script>
  35. <script src="js/loaders/ColladaLoader.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var stats;
  41. var scene;
  42. var pointLight;
  43. var camera;
  44. var renderer;
  45. var model;
  46. var animations;
  47. var kfAnimations = [ ];
  48. var kfAnimationsLength = 0;
  49. var loader = new THREE.ColladaLoader();
  50. var lastTimestamp = 0;
  51. var progress = 0;
  52. loader.load( './models/collada/pump/pump.dae', function ( collada ) {
  53. model = collada.scene;
  54. animations = collada.animations;
  55. kfAnimationsLength = animations.length;
  56. model.scale.x = model.scale.y = model.scale.z = 0.125; // 1/8 scale, modeled in cm
  57. init();
  58. start();
  59. animate( lastTimestamp );
  60. } );
  61. function init() {
  62. var container = document.createElement( 'div' );
  63. document.body.appendChild( container );
  64. // Camera
  65. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.01, 1000 );
  66. camera.position.set( -5.00181875, 3.42631375, 11.3102925 );
  67. camera.lookAt( new THREE.Vector3( -1.224774125, 2.18410625, 4.57969125 ) );
  68. // Scene
  69. scene = new THREE.Scene();
  70. // KeyFrame Animations
  71. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  72. var animation = animations[ i ];
  73. var kfAnimation = new THREE.KeyFrameAnimation( animation );
  74. kfAnimation.timeScale = 1;
  75. kfAnimations.push( kfAnimation );
  76. }
  77. // Grid
  78. var material = new THREE.LineBasicMaterial( { color: 0x303030 } );
  79. var geometry = new THREE.Geometry();
  80. var floor = -0.04, step = 1, size = 14;
  81. for ( var i = 0; i <= size / step * 2; i ++ ) {
  82. geometry.vertices.push( new THREE.Vector3( - size, floor, i * step - size ) );
  83. geometry.vertices.push( new THREE.Vector3( size, floor, i * step - size ) );
  84. geometry.vertices.push( new THREE.Vector3( i * step - size, floor, -size ) );
  85. geometry.vertices.push( new THREE.Vector3( i * step - size, floor, size ) );
  86. }
  87. var line = new THREE.LineSegments( geometry, material );
  88. scene.add( line );
  89. // Add the COLLADA
  90. model.getObjectByName( 'camEye_camera', true ).visible = false;
  91. model.getObjectByName( 'camTarget_camera', true ).visible = false;
  92. scene.add( model );
  93. // Lights
  94. pointLight = new THREE.PointLight( 0xffffff, 1.75 );
  95. scene.add( pointLight );
  96. // Renderer
  97. renderer = new THREE.WebGLRenderer( { antialias: true } );
  98. renderer.setPixelRatio( window.devicePixelRatio );
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. container.appendChild( renderer.domElement );
  101. // Stats
  102. stats = new Stats();
  103. container.appendChild( stats.dom );
  104. //
  105. window.addEventListener( 'resize', onWindowResize, false );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function start() {
  113. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  114. var animation = kfAnimations[i];
  115. for ( var h = 0, hl = animation.hierarchy.length; h < hl; h++ ) {
  116. var keys = animation.data.hierarchy[ h ].keys;
  117. var sids = animation.data.hierarchy[ h ].sids;
  118. var obj = animation.hierarchy[ h ];
  119. if ( keys.length && sids ) {
  120. for ( var s = 0; s < sids.length; s++ ) {
  121. var sid = sids[ s ];
  122. var next = animation.getNextKeyWith( sid, h, 0 );
  123. if ( next ) next.apply( sid );
  124. }
  125. obj.matrixAutoUpdate = false;
  126. animation.data.hierarchy[ h ].node.updateMatrix();
  127. obj.matrixWorldNeedsUpdate = true;
  128. }
  129. }
  130. animation.loop = false;
  131. animation.play();
  132. }
  133. }
  134. function animate( timestamp ) {
  135. var frameTime = ( timestamp - lastTimestamp ) * 0.001;
  136. if ( progress >= 0 && progress < 48 ) {
  137. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  138. kfAnimations[ i ].update( frameTime );
  139. }
  140. } else if ( progress >= 48 ) {
  141. for ( var i = 0; i < kfAnimationsLength; ++i ) {
  142. kfAnimations[ i ].stop();
  143. }
  144. progress = 0;
  145. start();
  146. }
  147. pointLight.position.copy( camera.position );
  148. progress += frameTime;
  149. lastTimestamp = timestamp;
  150. renderer.render( scene, camera );
  151. stats.update();
  152. requestAnimationFrame( animate );
  153. }
  154. </script>
  155. </body>
  156. </html>