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. // the blend mesh will combine 1 or more animations
  89. for ( var i = 0; i < data.anims.length; ++i ) {
  90. blendMesh.play(data.anims[i], data.weights[i]);
  91. }
  92. isFrameStepping = false;
  93. }
  94. function onStopAnimation( event ) {
  95. blendMesh.stopAll();
  96. isFrameStepping = false;
  97. }
  98. function onPauseAnimation( event ) {
  99. ( isFrameStepping ) ? blendMesh.unPauseAll(): blendMesh.pauseAll();
  100. isFrameStepping = false;
  101. }
  102. function onStepAnimation( event ) {
  103. blendMesh.unPauseAll();
  104. isFrameStepping = true;
  105. timeToStep = event.detail.stepSize;
  106. }
  107. function onWeightAnimation(event) {
  108. var data = event.detail;
  109. for ( var i = 0; i < data.anims.length; ++i ) {
  110. blendMesh.applyWeight(data.anims[i], data.weights[i]);
  111. }
  112. }
  113. function onCrossfade(event) {
  114. var data = event.detail;
  115. blendMesh.stopAll();
  116. blendMesh.crossfade( data.from, data.to, data.time );
  117. isFrameStepping = false;
  118. }
  119. function onWarp( event ) {
  120. var data = event.detail;
  121. blendMesh.stopAll();
  122. blendMesh.warp( data.from, data.to, data.time );
  123. isFrameStepping = false;
  124. }
  125. function onShowSkeleton( event ) {
  126. var shouldShow = event.detail.shouldShow;
  127. helper.visible = shouldShow;
  128. }
  129. function onShowModel( event ) {
  130. var shouldShow = event.detail.shouldShow;
  131. blendMesh.showModel( shouldShow );
  132. }
  133. function start() {
  134. blendMesh.rotation.y = Math.PI * -135 / 180;
  135. scene.add( blendMesh );
  136. var aspect = window.innerWidth / window.innerHeight;
  137. var radius = blendMesh.geometry.boundingSphere.radius;
  138. camera = new THREE.PerspectiveCamera( 45, aspect, 1, 10000 );
  139. camera.position.set( 0.0, radius, radius * 3.5 );
  140. controls = new THREE.OrbitControls( camera );
  141. controls.target.set( 0, radius, 0 );
  142. controls.update();
  143. // Set default weights
  144. blendMesh.animations[ 'idle' ].weight = 1 / 3;
  145. blendMesh.animations[ 'walk' ].weight = 1 / 3;
  146. blendMesh.animations[ 'run' ].weight = 1 / 3;
  147. gui = new BlendCharacterGui(blendMesh.animations);
  148. // Create the debug visualization
  149. helper = new THREE.SkeletonHelper( blendMesh );
  150. helper.material.linewidth = 3;
  151. scene.add( helper );
  152. helper.visible = false;
  153. animate();
  154. }
  155. function animate() {
  156. requestAnimationFrame( animate, renderer.domElement );
  157. // step forward in time based on whether we're stepping and scale
  158. var scale = gui.getTimeScale();
  159. var delta = clock.getDelta();
  160. var stepSize = (!isFrameStepping) ? delta * scale: timeToStep;
  161. // modify blend weights
  162. blendMesh.update( stepSize );
  163. helper.update();
  164. gui.update();
  165. THREE.AnimationHandler.update( stepSize );
  166. renderer.render( scene, camera );
  167. stats.update();
  168. // if we are stepping, consume time
  169. // ( will equal step size next time a single step is desired )
  170. timeToStep = 0;
  171. }
  172. </script>
  173. </body>
  174. </html>