webgl_animation_skinning_blending.html 6.2 KB

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