webgl_loader_collada_keyframe.html 5.3 KB

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