threejs-game-check-animations.html 6.2 KB

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