threejs-game-check-animations.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Game - Check Animations</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. #loading {
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. width: 100%;
  23. height: 100%;
  24. display: flex;
  25. align-items: center;
  26. justify-content: center;
  27. text-align: center;
  28. font-size: xx-large;
  29. font-family: sans-serif;
  30. }
  31. #loading>div>div {
  32. padding: 2px;
  33. }
  34. .progress {
  35. width: 50vw;
  36. border: 1px solid black;
  37. }
  38. #progressbar {
  39. width: 0%;
  40. transition: width ease-out .5s;
  41. height: 1em;
  42. background-color: #888;
  43. background-image: linear-gradient(
  44. -45deg,
  45. rgba(255, 255, 255, .5) 25%,
  46. transparent 25%,
  47. transparent 50%,
  48. rgba(255, 255, 255, .5) 50%,
  49. rgba(255, 255, 255, .5) 75%,
  50. transparent 75%,
  51. transparent
  52. );
  53. background-size: 50px 50px;
  54. animation: progressanim 2s linear infinite;
  55. }
  56. @keyframes progressanim {
  57. 0% {
  58. background-position: 50px 50px;
  59. }
  60. 100% {
  61. background-position: 0 0;
  62. }
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <canvas id="c" tabindex="1"></canvas>
  68. <div id="loading">
  69. <div>
  70. <div>...loading...</div>
  71. <div class="progress"><div id="progressbar"></div></div>
  72. </div>
  73. </div>
  74. </body>
  75. <script src="resources/threejs/r108/three.min.js"></script>
  76. <script src="resources/threejs/r108/js/controls/OrbitControls.js"></script>
  77. <script src="resources/threejs/r108/js/loaders/GLTFLoader.js"></script>
  78. <script src="resources/threejs/r108/js/utils/SkeletonUtils.js"></script>
  79. <script>
  80. 'use strict';
  81. /* global THREE */
  82. function main() {
  83. const canvas = document.querySelector('#c');
  84. const renderer = new THREE.WebGLRenderer({canvas});
  85. const fov = 45;
  86. const aspect = 2; // the canvas default
  87. const near = 0.1;
  88. const far = 100;
  89. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  90. camera.position.set(0, 20, 40);
  91. const controls = new THREE.OrbitControls(camera, canvas);
  92. controls.target.set(0, 5, 0);
  93. controls.update();
  94. const scene = new THREE.Scene();
  95. scene.background = new THREE.Color('white');
  96. function addLight(...pos) {
  97. const color = 0xFFFFFF;
  98. const intensity = 1;
  99. const light = new THREE.DirectionalLight(color, intensity);
  100. light.position.set(...pos);
  101. scene.add(light);
  102. scene.add(light.target);
  103. }
  104. addLight(5, 5, 2);
  105. addLight(-5, 5, 5);
  106. const manager = new THREE.LoadingManager();
  107. manager.onLoad = init;
  108. const progressbarElem = document.querySelector('#progressbar');
  109. manager.onProgress = (url, itemsLoaded, itemsTotal) => {
  110. progressbarElem.style.width = `${itemsLoaded / itemsTotal * 100 | 0}%`;
  111. };
  112. const models = {
  113. pig: { url: 'resources/models/animals/Pig.gltf' },
  114. cow: { url: 'resources/models/animals/Cow.gltf' },
  115. llama: { url: 'resources/models/animals/Llama.gltf' },
  116. pug: { url: 'resources/models/animals/Pug.gltf' },
  117. sheep: { url: 'resources/models/animals/Sheep.gltf' },
  118. zebra: { url: 'resources/models/animals/Zebra.gltf' },
  119. horse: { url: 'resources/models/animals/Horse.gltf' },
  120. knight: { url: 'resources/models/knight/KnightCharacter.gltf' },
  121. };
  122. {
  123. const gltfLoader = new THREE.GLTFLoader(manager);
  124. for (const model of Object.values(models)) {
  125. gltfLoader.load(model.url, (gltf) => {
  126. model.gltf = gltf;
  127. });
  128. }
  129. }
  130. function prepModelsAndAnimations() {
  131. Object.values(models).forEach(model => {
  132. const animsByName = {};
  133. model.gltf.animations.forEach((clip) => {
  134. animsByName[clip.name] = clip;
  135. });
  136. model.animations = animsByName;
  137. });
  138. }
  139. const mixerInfos = [];
  140. function init() {
  141. // hide the loading bar
  142. const loadingElem = document.querySelector('#loading');
  143. loadingElem.style.display = 'none';
  144. prepModelsAndAnimations();
  145. Object.values(models).forEach((model, ndx) => {
  146. const clonedScene = THREE.SkeletonUtils.clone(model.gltf.scene);
  147. const root = new THREE.Object3D();
  148. root.add(clonedScene);
  149. scene.add(root);
  150. root.position.x = (ndx - 3) * 3;
  151. const mixer = new THREE.AnimationMixer(clonedScene);
  152. const actions = Object.values(model.animations).map((clip) => {
  153. return mixer.clipAction(clip);
  154. });
  155. const mixerInfo = {
  156. mixer,
  157. actions,
  158. actionNdx: -1,
  159. };
  160. mixerInfos.push(mixerInfo);
  161. playNextAction(mixerInfo);
  162. });
  163. }
  164. function playNextAction(mixerInfo) {
  165. const {actions, actionNdx} = mixerInfo;
  166. const nextActionNdx = (actionNdx + 1) % actions.length;
  167. mixerInfo.actionNdx = nextActionNdx;
  168. actions.forEach((action, ndx) => {
  169. const enabled = ndx === nextActionNdx;
  170. action.enabled = enabled;
  171. if (enabled) {
  172. action.play();
  173. }
  174. });
  175. }
  176. window.addEventListener('keydown', (e) => {
  177. const mixerInfo = mixerInfos[e.keyCode - 49];
  178. if (!mixerInfo) {
  179. return;
  180. }
  181. playNextAction(mixerInfo);
  182. });
  183. function resizeRendererToDisplaySize(renderer) {
  184. const canvas = renderer.domElement;
  185. const width = canvas.clientWidth;
  186. const height = canvas.clientHeight;
  187. const needResize = canvas.width !== width || canvas.height !== height;
  188. if (needResize) {
  189. renderer.setSize(width, height, false);
  190. }
  191. return needResize;
  192. }
  193. let then = 0;
  194. function render(now) {
  195. now *= 0.001; // convert to sections
  196. const deltaTime = now - then;
  197. then = now;
  198. if (resizeRendererToDisplaySize(renderer)) {
  199. const canvas = renderer.domElement;
  200. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  201. camera.updateProjectionMatrix();
  202. }
  203. for (const {mixer} of mixerInfos) {
  204. mixer.update(deltaTime);
  205. }
  206. renderer.render(scene, camera);
  207. requestAnimationFrame(render);
  208. }
  209. requestAnimationFrame(render);
  210. }
  211. main();
  212. </script>
  213. </html>