123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <!-- Licensed under a BSD license. See license.html for license -->
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
- <title>Three.js - Game - Check Animations</title>
- <style>
- html, body {
- margin: 0;
- height: 100%;
- }
- #c {
- width: 100%;
- height: 100%;
- display: block;
- }
- #loading {
- position: absolute;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- display: flex;
- align-items: center;
- justify-content: center;
- text-align: center;
- font-size: xx-large;
- font-family: sans-serif;
- }
- #loading>div>div {
- padding: 2px;
- }
- .progress {
- width: 50vw;
- border: 1px solid black;
- }
- #progressbar {
- width: 0%;
- transition: width ease-out .5s;
- height: 1em;
- background-color: #888;
- background-image: linear-gradient(
- -45deg,
- rgba(255, 255, 255, .5) 25%,
- transparent 25%,
- transparent 50%,
- rgba(255, 255, 255, .5) 50%,
- rgba(255, 255, 255, .5) 75%,
- transparent 75%,
- transparent
- );
- background-size: 50px 50px;
- animation: progressanim 2s linear infinite;
- }
- @keyframes progressanim {
- 0% {
- background-position: 50px 50px;
- }
- 100% {
- background-position: 0 0;
- }
- }
- </style>
- </head>
- <body>
- <canvas id="c" tabindex="1"></canvas>
- <div id="loading">
- <div>
- <div>...loading...</div>
- <div class="progress"><div id="progressbar"></div></div>
- </div>
- </div>
- </body>
- <!-- Import maps polyfill -->
- <!-- Remove this when import maps will be widely supported -->
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <script type="importmap">
- {
- "imports": {
- "three": "../../build/three.module.js",
- "three/addons/": "../../examples/jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
- import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js';
- import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
- function main() {
- const canvas = document.querySelector('#c');
- const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
- renderer.outputColorSpace = THREE.SRGBColorSpace;
- const fov = 45;
- const aspect = 2; // the canvas default
- const near = 0.1;
- const far = 100;
- const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
- camera.position.set(0, 20, 40);
- const controls = new OrbitControls(camera, canvas);
- controls.target.set(0, 5, 0);
- controls.update();
- const scene = new THREE.Scene();
- scene.background = new THREE.Color('white');
- function addLight(...pos) {
- const color = 0xFFFFFF;
- const intensity = 0.8;
- const light = new THREE.DirectionalLight(color, intensity);
- light.position.set(...pos);
- scene.add(light);
- scene.add(light.target);
- }
- addLight(5, 5, 2);
- addLight(-5, 5, 5);
- const manager = new THREE.LoadingManager();
- manager.onLoad = init;
- const progressbarElem = document.querySelector('#progressbar');
- manager.onProgress = (url, itemsLoaded, itemsTotal) => {
- progressbarElem.style.width = `${itemsLoaded / itemsTotal * 100 | 0}%`;
- };
- const models = {
- pig: { url: 'resources/models/animals/Pig.gltf' },
- cow: { url: 'resources/models/animals/Cow.gltf' },
- llama: { url: 'resources/models/animals/Llama.gltf' },
- pug: { url: 'resources/models/animals/Pug.gltf' },
- sheep: { url: 'resources/models/animals/Sheep.gltf' },
- zebra: { url: 'resources/models/animals/Zebra.gltf' },
- horse: { url: 'resources/models/animals/Horse.gltf' },
- knight: { url: 'resources/models/knight/KnightCharacter.gltf' },
- };
- {
- const gltfLoader = new GLTFLoader(manager);
- for (const model of Object.values(models)) {
- gltfLoader.load(model.url, (gltf) => {
- model.gltf = gltf;
- });
- }
- }
- function prepModelsAndAnimations() {
- Object.values(models).forEach(model => {
- const animsByName = {};
- model.gltf.animations.forEach((clip) => {
- animsByName[clip.name] = clip;
- });
- model.animations = animsByName;
- });
- }
- const mixerInfos = [];
- function init() {
- // hide the loading bar
- const loadingElem = document.querySelector('#loading');
- loadingElem.style.display = 'none';
- prepModelsAndAnimations();
- Object.values(models).forEach((model, ndx) => {
- const clonedScene = SkeletonUtils.clone(model.gltf.scene);
- const root = new THREE.Object3D();
- root.add(clonedScene);
- scene.add(root);
- root.position.x = (ndx - 3) * 3;
- const mixer = new THREE.AnimationMixer(clonedScene);
- const actions = Object.values(model.animations).map((clip) => {
- return mixer.clipAction(clip);
- });
- const mixerInfo = {
- mixer,
- actions,
- actionNdx: -1,
- };
- mixerInfos.push(mixerInfo);
- playNextAction(mixerInfo);
- });
- }
- function playNextAction(mixerInfo) {
- const {actions, actionNdx} = mixerInfo;
- const nextActionNdx = (actionNdx + 1) % actions.length;
- mixerInfo.actionNdx = nextActionNdx;
- actions.forEach((action, ndx) => {
- const enabled = ndx === nextActionNdx;
- action.enabled = enabled;
- if (enabled) {
- action.play();
- }
- });
- }
- window.addEventListener('keydown', (e) => {
- const mixerInfo = mixerInfos[e.keyCode - 49];
- if (!mixerInfo) {
- return;
- }
- playNextAction(mixerInfo);
- });
- function resizeRendererToDisplaySize(renderer) {
- const canvas = renderer.domElement;
- const width = canvas.clientWidth;
- const height = canvas.clientHeight;
- const needResize = canvas.width !== width || canvas.height !== height;
- if (needResize) {
- renderer.setSize(width, height, false);
- }
- return needResize;
- }
- let then = 0;
- function render(now) {
- now *= 0.001; // convert to sections
- const deltaTime = now - then;
- then = now;
- if (resizeRendererToDisplaySize(renderer)) {
- const canvas = renderer.domElement;
- camera.aspect = canvas.clientWidth / canvas.clientHeight;
- camera.updateProjectionMatrix();
- }
- for (const {mixer} of mixerInfos) {
- mixer.update(deltaTime);
- }
- renderer.render(scene, camera);
- requestAnimationFrame(render);
- }
- requestAnimationFrame(render);
- }
- main();
- </script>
- </html>
|