webgl_animation_skinning_blending.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. container.appendChild( stats.dom );
  60. //
  61. window.addEventListener( 'resize', onWindowResize, false );
  62. // listen for messages from the gui
  63. window.addEventListener( 'start-animation', onStartAnimation );
  64. window.addEventListener( 'stop-animation', onStopAnimation );
  65. window.addEventListener( 'pause-animation', onPauseAnimation );
  66. window.addEventListener( 'step-animation', onStepAnimation );
  67. window.addEventListener( 'weight-animation', onWeightAnimation );
  68. window.addEventListener( 'crossfade', onCrossfade );
  69. window.addEventListener( 'warp', onWarp );
  70. window.addEventListener( 'toggle-show-skeleton', onShowSkeleton );
  71. window.addEventListener( 'toggle-show-model', onShowModel );
  72. blendMesh = new THREE.BlendCharacter();
  73. blendMesh.load( "models/skinned/marine/marine_anims_core.json", start );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. function onStartAnimation( event ) {
  81. var data = event.detail;
  82. blendMesh.stopAll();
  83. blendMesh.unPauseAll();
  84. // the blend mesh will combine 1 or more animations
  85. for ( var i = 0; i < data.anims.length; ++i ) {
  86. blendMesh.play(data.anims[i], data.weights[i]);
  87. }
  88. isFrameStepping = false;
  89. }
  90. function onStopAnimation( event ) {
  91. blendMesh.stopAll();
  92. isFrameStepping = false;
  93. }
  94. function onPauseAnimation( event ) {
  95. ( isFrameStepping ) ? blendMesh.unPauseAll(): blendMesh.pauseAll();
  96. isFrameStepping = false;
  97. }
  98. function onStepAnimation( event ) {
  99. blendMesh.unPauseAll();
  100. isFrameStepping = true;
  101. timeToStep = event.detail.stepSize;
  102. }
  103. function onWeightAnimation(event) {
  104. var data = event.detail;
  105. for ( var i = 0; i < data.anims.length; ++i ) {
  106. blendMesh.applyWeight( data.anims[ i ], data.weights[ i ] );
  107. }
  108. }
  109. function onCrossfade(event) {
  110. var data = event.detail;
  111. blendMesh.stopAll();
  112. blendMesh.crossfade( data.from, data.to, data.time );
  113. isFrameStepping = false;
  114. }
  115. function onWarp( event ) {
  116. var data = event.detail;
  117. blendMesh.stopAll();
  118. blendMesh.warp( data.from, data.to, data.time );
  119. isFrameStepping = false;
  120. }
  121. function onShowSkeleton( event ) {
  122. var shouldShow = event.detail.shouldShow;
  123. helper.visible = shouldShow;
  124. }
  125. function onShowModel( event ) {
  126. var shouldShow = event.detail.shouldShow;
  127. blendMesh.showModel( shouldShow );
  128. }
  129. function start() {
  130. blendMesh.rotation.y = Math.PI * -135 / 180;
  131. scene.add( blendMesh );
  132. var aspect = window.innerWidth / window.innerHeight;
  133. var radius = blendMesh.geometry.boundingSphere.radius;
  134. camera = new THREE.PerspectiveCamera( 45, aspect, 1, 10000 );
  135. camera.position.set( 0.0, radius, radius * 3.5 );
  136. controls = new THREE.OrbitControls( camera );
  137. controls.target.set( 0, radius, 0 );
  138. controls.update();
  139. // Set default weights
  140. blendMesh.applyWeight( 'idle', 1 / 3 );
  141. blendMesh.applyWeight( 'walk', 1 / 3 );
  142. blendMesh.applyWeight( 'run', 1 / 3 );
  143. gui = new BlendCharacterGui(blendMesh);
  144. // Create the debug visualization
  145. helper = new THREE.SkeletonHelper( blendMesh );
  146. helper.material.linewidth = 3;
  147. scene.add( helper );
  148. helper.visible = false;
  149. animate();
  150. }
  151. function animate() {
  152. requestAnimationFrame( animate, renderer.domElement );
  153. stats.begin();
  154. // step forward in time based on whether we're stepping and scale
  155. var scale = gui.getTimeScale();
  156. var delta = clock.getDelta();
  157. var stepSize = (!isFrameStepping) ? delta * scale: timeToStep;
  158. // modify blend weights
  159. blendMesh.update( stepSize );
  160. helper.update();
  161. gui.update( blendMesh.mixer.time );
  162. renderer.render( scene, camera );
  163. stats.end();
  164. // if we are stepping, consume time
  165. // ( will equal step size next time a single step is desired )
  166. timeToStep = 0;
  167. }
  168. </script>
  169. </body>
  170. </html>