webgl_animation_skinning_blending.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - skinning</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. color: #fff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info">
  27. <a href="http://threejs.org" target="_blank">three.js</a> - Skeletal Animation Blending
  28. <br><br> Adjust blend weights to affect the animations that are currently playing.
  29. <br> Cross fades (and warping) blend between 2 animations and end with a single animation.
  30. </div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script src="js/controls/OrbitControls.js"></script>
  35. <script src="js/BlendCharacter.js"></script>
  36. <script src="js/BlendCharacterGui.js"></script>
  37. <script src="js/libs/dat.gui.min.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var container, stats;
  41. var blendMesh, helper, camera, scene, renderer, controls;
  42. var clock = new THREE.Clock();
  43. var gui = null;
  44. var isFrameStepping = false;
  45. var timeToStep = 0;
  46. init();
  47. function init() {
  48. container = document.getElementById( 'container' );
  49. scene = new THREE.Scene();
  50. scene.add ( new THREE.AmbientLight( 0xffffff ) );
  51. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false } );
  52. renderer.setClearColor( 0x777777 );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.autoClear = true;
  56. container.appendChild( renderer.domElement );
  57. //
  58. stats = new Stats();
  59. stats.domElement.style.position = 'absolute';
  60. stats.domElement.style.top = '0px';
  61. container.appendChild( stats.domElement );
  62. //
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. // listen for messages from the gui
  65. window.addEventListener( 'start-animation', onStartAnimation );
  66. window.addEventListener( 'stop-animation', onStopAnimation );
  67. window.addEventListener( 'pause-animation', onPauseAnimation );
  68. window.addEventListener( 'step-animation', onStepAnimation );
  69. window.addEventListener( 'weight-animation', onWeightAnimation );
  70. window.addEventListener( 'crossfade', onCrossfade );
  71. window.addEventListener( 'warp', onWarp );
  72. window.addEventListener( 'toggle-show-skeleton', onShowSkeleton );
  73. window.addEventListener( 'toggle-show-model', onShowModel );
  74. blendMesh = new THREE.BlendCharacter();
  75. blendMesh.load( "models/skinned/marine/marine_anims_core.json", start );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function onStartAnimation( event ) {
  83. var data = event.detail;
  84. blendMesh.stopAll();
  85. blendMesh.unPauseAll();
  86. // the blend mesh will combine 1 or more animations
  87. for ( var i = 0; i < data.anims.length; ++i ) {
  88. blendMesh.play(data.anims[i], data.weights[i]);
  89. }
  90. isFrameStepping = false;
  91. }
  92. function onStopAnimation( event ) {
  93. blendMesh.stopAll();
  94. isFrameStepping = false;
  95. }
  96. function onPauseAnimation( event ) {
  97. ( isFrameStepping ) ? blendMesh.unPauseAll(): blendMesh.pauseAll();
  98. isFrameStepping = false;
  99. }
  100. function onStepAnimation( event ) {
  101. blendMesh.unPauseAll();
  102. isFrameStepping = true;
  103. timeToStep = event.detail.stepSize;
  104. }
  105. function onWeightAnimation(event) {
  106. var data = event.detail;
  107. for ( var i = 0; i < data.anims.length; ++i ) {
  108. blendMesh.applyWeight( data.anims[ i ], data.weights[ i ] );
  109. }
  110. }
  111. function onCrossfade(event) {
  112. var data = event.detail;
  113. blendMesh.stopAll();
  114. blendMesh.crossfade( data.from, data.to, data.time );
  115. isFrameStepping = false;
  116. }
  117. function onWarp( event ) {
  118. var data = event.detail;
  119. blendMesh.stopAll();
  120. blendMesh.warp( data.from, data.to, data.time );
  121. isFrameStepping = false;
  122. }
  123. function onShowSkeleton( event ) {
  124. var shouldShow = event.detail.shouldShow;
  125. helper.visible = shouldShow;
  126. }
  127. function onShowModel( event ) {
  128. var shouldShow = event.detail.shouldShow;
  129. blendMesh.showModel( shouldShow );
  130. }
  131. function start() {
  132. blendMesh.rotation.y = Math.PI * -135 / 180;
  133. scene.add( blendMesh );
  134. var aspect = window.innerWidth / window.innerHeight;
  135. var radius = blendMesh.geometry.boundingSphere.radius;
  136. camera = new THREE.PerspectiveCamera( 45, aspect, 1, 10000 );
  137. camera.position.set( 0.0, radius, radius * 3.5 );
  138. controls = new THREE.OrbitControls( camera );
  139. controls.target.set( 0, radius, 0 );
  140. controls.update();
  141. // Set default weights
  142. blendMesh.applyWeight( 'idle', 1 / 3 );
  143. blendMesh.applyWeight( 'walk', 1 / 3 );
  144. blendMesh.applyWeight( 'run', 1 / 3 );
  145. gui = new BlendCharacterGui(blendMesh);
  146. // Create the debug visualization
  147. helper = new THREE.SkeletonHelper( blendMesh );
  148. helper.material.linewidth = 3;
  149. scene.add( helper );
  150. helper.visible = false;
  151. animate();
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate, renderer.domElement );
  155. stats.begin();
  156. // step forward in time based on whether we're stepping and scale
  157. var scale = gui.getTimeScale();
  158. var delta = clock.getDelta();
  159. var stepSize = (!isFrameStepping) ? delta * scale: timeToStep;
  160. // modify blend weights
  161. blendMesh.update( stepSize );
  162. helper.update();
  163. gui.update( blendMesh.mixer.time );
  164. renderer.render( scene, camera );
  165. stats.end();
  166. // if we are stepping, consume time
  167. // ( will equal step size next time a single step is desired )
  168. timeToStep = 0;
  169. }
  170. </script>
  171. </body>
  172. </html>