game-just-player.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 - Just Player</title>
  8. <style>
  9. html {
  10. box-sizing: border-box;
  11. }
  12. *, *:before, *:after {
  13. box-sizing: inherit;
  14. }
  15. html, body {
  16. margin: 0;
  17. height: 100%;
  18. }
  19. #c {
  20. width: 100%;
  21. height: 100%;
  22. display: block;
  23. }
  24. #loading {
  25. position: absolute;
  26. left: 0;
  27. top: 0;
  28. width: 100%;
  29. height: 100%;
  30. display: flex;
  31. align-items: center;
  32. justify-content: center;
  33. text-align: center;
  34. font-size: xx-large;
  35. font-family: sans-serif;
  36. }
  37. #loading>div>div {
  38. padding: 2px;
  39. }
  40. .progress {
  41. width: 50vw;
  42. border: 1px solid black;
  43. }
  44. #progressbar {
  45. width: 0%;
  46. transition: width ease-out .5s;
  47. height: 1em;
  48. background-color: #888;
  49. background-image: linear-gradient(
  50. -45deg,
  51. rgba(255, 255, 255, .5) 25%,
  52. transparent 25%,
  53. transparent 50%,
  54. rgba(255, 255, 255, .5) 50%,
  55. rgba(255, 255, 255, .5) 75%,
  56. transparent 75%,
  57. transparent
  58. );
  59. background-size: 50px 50px;
  60. animation: progressanim 2s linear infinite;
  61. }
  62. @keyframes progressanim {
  63. 0% {
  64. background-position: 50px 50px;
  65. }
  66. 100% {
  67. background-position: 0 0;
  68. }
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <canvas id="c"></canvas>
  74. <div id="loading">
  75. <div>
  76. <div>...loading...</div>
  77. <div class="progress"><div id="progressbar"></div></div>
  78. </div>
  79. </div>
  80. </body>
  81. <script type="importmap">
  82. {
  83. "imports": {
  84. "three": "../../build/three.module.js",
  85. "three/addons/": "../../examples/jsm/"
  86. }
  87. }
  88. </script>
  89. <script type="module">
  90. import * as THREE from 'three';
  91. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  92. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  93. import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  94. function main() {
  95. const canvas = document.querySelector( '#c' );
  96. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  97. const fov = 45;
  98. const aspect = 2; // the canvas default
  99. const near = 0.1;
  100. const far = 1000;
  101. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  102. camera.position.set( 0, 40, 80 );
  103. const controls = new OrbitControls( camera, canvas );
  104. controls.target.set( 0, 5, 0 );
  105. controls.update();
  106. const scene = new THREE.Scene();
  107. scene.background = new THREE.Color( 'white' );
  108. function addLight( ...pos ) {
  109. const color = 0xFFFFFF;
  110. const intensity = 2.5;
  111. const light = new THREE.DirectionalLight( color, intensity );
  112. light.position.set( ...pos );
  113. scene.add( light );
  114. scene.add( light.target );
  115. }
  116. addLight( 5, 5, 2 );
  117. addLight( - 5, 5, 5 );
  118. const manager = new THREE.LoadingManager();
  119. manager.onLoad = init;
  120. const progressbarElem = document.querySelector( '#progressbar' );
  121. manager.onProgress = ( url, itemsLoaded, itemsTotal ) => {
  122. progressbarElem.style.width = `${itemsLoaded / itemsTotal * 100 | 0}%`;
  123. };
  124. const models = {
  125. pig: { url: 'resources/models/animals/Pig.gltf' },
  126. cow: { url: 'resources/models/animals/Cow.gltf' },
  127. llama: { url: 'resources/models/animals/Llama.gltf' },
  128. pug: { url: 'resources/models/animals/Pug.gltf' },
  129. sheep: { url: 'resources/models/animals/Sheep.gltf' },
  130. zebra: { url: 'resources/models/animals/Zebra.gltf' },
  131. horse: { url: 'resources/models/animals/Horse.gltf' },
  132. knight: { url: 'resources/models/knight/KnightCharacter.gltf' },
  133. };
  134. {
  135. const gltfLoader = new GLTFLoader( manager );
  136. for ( const model of Object.values( models ) ) {
  137. gltfLoader.load( model.url, ( gltf ) => {
  138. model.gltf = gltf;
  139. } );
  140. }
  141. }
  142. function prepModelsAndAnimations() {
  143. Object.values( models ).forEach( model => {
  144. const animsByName = {};
  145. model.gltf.animations.forEach( ( clip ) => {
  146. animsByName[ clip.name ] = clip;
  147. // Should really fix this in .blend file
  148. if ( clip.name === 'Walk' ) {
  149. clip.duration /= 2;
  150. }
  151. } );
  152. model.animations = animsByName;
  153. } );
  154. }
  155. function removeArrayElement( array, element ) {
  156. const ndx = array.indexOf( element );
  157. if ( ndx >= 0 ) {
  158. array.splice( ndx, 1 );
  159. }
  160. }
  161. class SafeArray {
  162. constructor() {
  163. this.array = [];
  164. this.addQueue = [];
  165. this.removeQueue = new Set();
  166. }
  167. get isEmpty() {
  168. return this.addQueue.length + this.array.length > 0;
  169. }
  170. add( element ) {
  171. this.addQueue.push( element );
  172. }
  173. remove( element ) {
  174. this.removeQueue.add( element );
  175. }
  176. forEach( fn ) {
  177. this._addQueued();
  178. this._removeQueued();
  179. for ( const element of this.array ) {
  180. if ( this.removeQueue.has( element ) ) {
  181. continue;
  182. }
  183. fn( element );
  184. }
  185. this._removeQueued();
  186. }
  187. _addQueued() {
  188. if ( this.addQueue.length ) {
  189. this.array.splice( this.array.length, 0, ...this.addQueue );
  190. this.addQueue = [];
  191. }
  192. }
  193. _removeQueued() {
  194. if ( this.removeQueue.size ) {
  195. this.array = this.array.filter( element => ! this.removeQueue.has( element ) );
  196. this.removeQueue.clear();
  197. }
  198. }
  199. }
  200. class GameObjectManager {
  201. constructor() {
  202. this.gameObjects = new SafeArray();
  203. }
  204. createGameObject( parent, name ) {
  205. const gameObject = new GameObject( parent, name );
  206. this.gameObjects.add( gameObject );
  207. return gameObject;
  208. }
  209. removeGameObject( gameObject ) {
  210. this.gameObjects.remove( gameObject );
  211. }
  212. update() {
  213. this.gameObjects.forEach( gameObject => gameObject.update() );
  214. }
  215. }
  216. const globals = {
  217. time: 0,
  218. deltaTime: 0,
  219. };
  220. const gameObjectManager = new GameObjectManager();
  221. class GameObject {
  222. constructor( parent, name ) {
  223. this.name = name;
  224. this.components = [];
  225. this.transform = new THREE.Object3D();
  226. parent.add( this.transform );
  227. }
  228. addComponent( ComponentType, ...args ) {
  229. const component = new ComponentType( this, ...args );
  230. this.components.push( component );
  231. return component;
  232. }
  233. removeComponent( component ) {
  234. removeArrayElement( this.components, component );
  235. }
  236. getComponent( ComponentType ) {
  237. return this.components.find( c => c instanceof ComponentType );
  238. }
  239. update() {
  240. for ( const component of this.components ) {
  241. component.update();
  242. }
  243. }
  244. }
  245. // Base for all components
  246. class Component {
  247. constructor( gameObject ) {
  248. this.gameObject = gameObject;
  249. }
  250. update() {
  251. }
  252. }
  253. class SkinInstance extends Component {
  254. constructor( gameObject, model ) {
  255. super( gameObject );
  256. this.model = model;
  257. this.animRoot = SkeletonUtils.clone( this.model.gltf.scene );
  258. this.mixer = new THREE.AnimationMixer( this.animRoot );
  259. gameObject.transform.add( this.animRoot );
  260. this.actions = {};
  261. }
  262. setAnimation( animName ) {
  263. const clip = this.model.animations[ animName ];
  264. // turn off all current actions
  265. for ( const action of Object.values( this.actions ) ) {
  266. action.enabled = false;
  267. }
  268. // get or create existing action for clip
  269. const action = this.mixer.clipAction( clip );
  270. action.enabled = true;
  271. action.reset();
  272. action.play();
  273. this.actions[ animName ] = action;
  274. }
  275. update() {
  276. this.mixer.update( globals.deltaTime );
  277. }
  278. }
  279. class Player extends Component {
  280. constructor( gameObject ) {
  281. super( gameObject );
  282. const model = models.knight;
  283. this.skinInstance = gameObject.addComponent( SkinInstance, model );
  284. this.skinInstance.setAnimation( 'Run' );
  285. }
  286. }
  287. function init() {
  288. // hide the loading bar
  289. const loadingElem = document.querySelector( '#loading' );
  290. loadingElem.style.display = 'none';
  291. prepModelsAndAnimations();
  292. {
  293. const gameObject = gameObjectManager.createGameObject( scene, 'player' );
  294. gameObject.addComponent( Player );
  295. }
  296. }
  297. function resizeRendererToDisplaySize( renderer ) {
  298. const canvas = renderer.domElement;
  299. const width = canvas.clientWidth;
  300. const height = canvas.clientHeight;
  301. const needResize = canvas.width !== width || canvas.height !== height;
  302. if ( needResize ) {
  303. renderer.setSize( width, height, false );
  304. }
  305. return needResize;
  306. }
  307. let then = 0;
  308. function render( now ) {
  309. // convert to seconds
  310. globals.time = now * 0.001;
  311. // make sure delta time isn't too big.
  312. globals.deltaTime = Math.min( globals.time - then, 1 / 20 );
  313. then = globals.time;
  314. if ( resizeRendererToDisplaySize( renderer ) ) {
  315. const canvas = renderer.domElement;
  316. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  317. camera.updateProjectionMatrix();
  318. }
  319. gameObjectManager.update();
  320. renderer.render( scene, camera );
  321. requestAnimationFrame( render );
  322. }
  323. requestAnimationFrame( render );
  324. }
  325. main();
  326. </script>
  327. </html>