webgl_animation_skinning_blending.html 6.0 KB

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