webgl_loader_collada_keyframe.html 5.2 KB

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