game-check-animations.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 type="module">
  76. import * as THREE from '../../build/three.module.js';
  77. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  78. import {GLTFLoader} from '../../examples/jsm/loaders/GLTFLoader.js';
  79. import * as SkeletonUtils from '../../examples/jsm/utils/SkeletonUtils.js';
  80. function main() {
  81. const canvas = document.querySelector('#c');
  82. const renderer = new THREE.WebGLRenderer({canvas});
  83. const fov = 45;
  84. const aspect = 2; // the canvas default
  85. const near = 0.1;
  86. const far = 100;
  87. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  88. camera.position.set(0, 20, 40);
  89. const controls = new OrbitControls(camera, canvas);
  90. controls.target.set(0, 5, 0);
  91. controls.update();
  92. const scene = new THREE.Scene();
  93. scene.background = new THREE.Color('white');
  94. function addLight(...pos) {
  95. const color = 0xFFFFFF;
  96. const intensity = 1;
  97. const light = new THREE.DirectionalLight(color, intensity);
  98. light.position.set(...pos);
  99. scene.add(light);
  100. scene.add(light.target);
  101. }
  102. addLight(5, 5, 2);
  103. addLight(-5, 5, 5);
  104. const manager = new THREE.LoadingManager();
  105. manager.onLoad = init;
  106. const progressbarElem = document.querySelector('#progressbar');
  107. manager.onProgress = (url, itemsLoaded, itemsTotal) => {
  108. progressbarElem.style.width = `${itemsLoaded / itemsTotal * 100 | 0}%`;
  109. };
  110. const models = {
  111. pig: { url: 'resources/models/animals/Pig.gltf' },
  112. cow: { url: 'resources/models/animals/Cow.gltf' },
  113. llama: { url: 'resources/models/animals/Llama.gltf' },
  114. pug: { url: 'resources/models/animals/Pug.gltf' },
  115. sheep: { url: 'resources/models/animals/Sheep.gltf' },
  116. zebra: { url: 'resources/models/animals/Zebra.gltf' },
  117. horse: { url: 'resources/models/animals/Horse.gltf' },
  118. knight: { url: 'resources/models/knight/KnightCharacter.gltf' },
  119. };
  120. {
  121. const gltfLoader = new GLTFLoader(manager);
  122. for (const model of Object.values(models)) {
  123. gltfLoader.load(model.url, (gltf) => {
  124. model.gltf = gltf;
  125. });
  126. }
  127. }
  128. function prepModelsAndAnimations() {
  129. Object.values(models).forEach(model => {
  130. const animsByName = {};
  131. model.gltf.animations.forEach((clip) => {
  132. animsByName[clip.name] = clip;
  133. });
  134. model.animations = animsByName;
  135. });
  136. }
  137. const mixerInfos = [];
  138. function init() {
  139. // hide the loading bar
  140. const loadingElem = document.querySelector('#loading');
  141. loadingElem.style.display = 'none';
  142. prepModelsAndAnimations();
  143. Object.values(models).forEach((model, ndx) => {
  144. const clonedScene = SkeletonUtils.clone(model.gltf.scene);
  145. const root = new THREE.Object3D();
  146. root.add(clonedScene);
  147. scene.add(root);
  148. root.position.x = (ndx - 3) * 3;
  149. const mixer = new THREE.AnimationMixer(clonedScene);
  150. const actions = Object.values(model.animations).map((clip) => {
  151. return mixer.clipAction(clip);
  152. });
  153. const mixerInfo = {
  154. mixer,
  155. actions,
  156. actionNdx: -1,
  157. };
  158. mixerInfos.push(mixerInfo);
  159. playNextAction(mixerInfo);
  160. });
  161. }
  162. function playNextAction(mixerInfo) {
  163. const {actions, actionNdx} = mixerInfo;
  164. const nextActionNdx = (actionNdx + 1) % actions.length;
  165. mixerInfo.actionNdx = nextActionNdx;
  166. actions.forEach((action, ndx) => {
  167. const enabled = ndx === nextActionNdx;
  168. action.enabled = enabled;
  169. if (enabled) {
  170. action.play();
  171. }
  172. });
  173. }
  174. window.addEventListener('keydown', (e) => {
  175. const mixerInfo = mixerInfos[e.keyCode - 49];
  176. if (!mixerInfo) {
  177. return;
  178. }
  179. playNextAction(mixerInfo);
  180. });
  181. function resizeRendererToDisplaySize(renderer) {
  182. const canvas = renderer.domElement;
  183. const width = canvas.clientWidth;
  184. const height = canvas.clientHeight;
  185. const needResize = canvas.width !== width || canvas.height !== height;
  186. if (needResize) {
  187. renderer.setSize(width, height, false);
  188. }
  189. return needResize;
  190. }
  191. let then = 0;
  192. function render(now) {
  193. now *= 0.001; // convert to sections
  194. const deltaTime = now - then;
  195. then = now;
  196. if (resizeRendererToDisplaySize(renderer)) {
  197. const canvas = renderer.domElement;
  198. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  199. camera.updateProjectionMatrix();
  200. }
  201. for (const {mixer} of mixerInfos) {
  202. mixer.update(deltaTime);
  203. }
  204. renderer.render(scene, camera);
  205. requestAnimationFrame(render);
  206. }
  207. requestAnimationFrame(render);
  208. }
  209. main();
  210. </script>
  211. </html>