JSWAI commited on
Commit
0ee62dd
·
verified ·
1 Parent(s): 7412247

Create js/main.js

Browse files
Files changed (1) hide show
  1. js/main.js +703 -214
js/main.js CHANGED
@@ -2,13 +2,55 @@
2
  RAIN WALKER
3
  main.js
4
 
5
- Main Three.js scene / render loop.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  ============================================================ */
7
 
8
  import * as THREE from
9
  "https://cdn.jsdelivr.net/npm/three@0.180.0/build/three.module.js";
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  /* ============================================================
13
  DOM
14
  ============================================================ */
@@ -26,6 +68,58 @@ const screenOverlay =
26
  document.getElementById("screen-overlay");
27
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  /* ============================================================
30
  SCENE
31
  ============================================================ */
@@ -33,22 +127,22 @@ const screenOverlay =
33
  const scene =
34
  new THREE.Scene();
35
 
 
36
  scene.background =
37
- new THREE.Color(0x000000);
 
 
38
 
39
 
40
  /*
41
- Fog is going to be extremely useful.
42
-
43
- Buildings disappear into darkness instead of the world
44
- having an obvious rendering boundary.
45
  */
46
 
47
  scene.fog =
48
- new THREE.Fog(
49
  0x000000,
50
- 12,
51
- 42
52
  );
53
 
54
 
@@ -58,29 +152,29 @@ scene.fog =
58
 
59
  const camera =
60
  new THREE.PerspectiveCamera(
 
61
  38,
 
62
  window.innerWidth /
63
  window.innerHeight,
 
64
  0.1,
65
- 100
66
- );
67
 
 
68
 
69
- /*
70
- Side-view composition.
71
 
72
- Character will eventually walk along X.
73
- */
74
 
75
  camera.position.set(
76
  0,
77
- 3.2,
78
- 10
79
  );
80
 
 
81
  camera.lookAt(
82
  0,
83
- 2.1,
84
  0
85
  );
86
 
@@ -91,47 +185,70 @@ camera.lookAt(
91
 
92
  const renderer =
93
  new THREE.WebGLRenderer({
 
94
  antialias: false,
 
95
  alpha: false,
96
- powerPreference: "high-performance"
 
 
 
97
  });
98
 
99
 
100
- /*
101
- Important:
102
 
103
- Instead of rendering at full screen resolution,
104
- render at a reduced internal resolution.
105
 
106
- The browser then enlarges the canvas.
 
107
 
108
- This gives us genuine chunky pixels rather than simply
109
- putting a CSS filter over smooth 3D.
110
- */
111
 
112
- const PIXEL_SCALE = 0.55;
 
113
 
114
 
115
- function setRendererSize() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  const width =
118
  Math.max(
119
  1,
120
  Math.floor(
121
  window.innerWidth *
122
- PIXEL_SCALE
123
  )
124
  );
125
 
 
126
  const height =
127
  Math.max(
128
  1,
129
  Math.floor(
130
  window.innerHeight *
131
- PIXEL_SCALE
132
  )
133
  );
134
 
 
 
 
 
135
  renderer.setSize(
136
  width,
137
  height,
@@ -140,50 +257,22 @@ function setRendererSize() {
140
 
141
 
142
  /*
143
- CSS displays the low-resolution render at the
144
- full viewport dimensions.
 
 
145
  */
146
 
147
  renderer.domElement.style.width =
148
  "100vw";
149
 
 
150
  renderer.domElement.style.height =
151
  "100vh";
152
  }
153
 
154
 
155
- setRendererSize();
156
-
157
-
158
- renderer.setPixelRatio(1);
159
-
160
- renderer.outputColorSpace =
161
- THREE.SRGBColorSpace;
162
-
163
-
164
- /*
165
- PBR tone mapping.
166
-
167
- This will matter once character.glb is loaded.
168
- */
169
-
170
- renderer.toneMapping =
171
- THREE.ACESFilmicToneMapping;
172
-
173
- renderer.toneMappingExposure =
174
- 0.85;
175
-
176
-
177
- /*
178
- Shadows.
179
-
180
- We'll keep their resolution fairly low intentionally.
181
- */
182
-
183
- renderer.shadowMap.enabled = true;
184
-
185
- renderer.shadowMap.type =
186
- THREE.PCFShadowMap;
187
 
188
 
189
  game.appendChild(
@@ -196,41 +285,50 @@ game.appendChild(
196
  ============================================================ */
197
 
198
 
199
- /*
200
- Extremely weak ambient illumination.
201
-
202
- We want darkness to dominate.
203
- */
204
 
205
- const ambientLight =
206
  new THREE.HemisphereLight(
207
- 0x647080,
 
 
208
  0x071008,
209
- 0.55
 
 
210
  );
211
 
 
212
  scene.add(
213
- ambientLight
214
  );
215
 
216
 
217
- /*
218
- Cold moon / city light.
219
- */
220
 
221
  const moonLight =
222
  new THREE.DirectionalLight(
223
- 0x9eb8cf,
224
- 1.5
 
 
 
225
  );
226
 
 
227
  moonLight.position.set(
228
  -5,
229
  9,
230
- 6
231
  );
232
 
233
- moonLight.castShadow = true;
 
 
234
 
235
 
236
  moonLight.shadow.mapSize.set(
@@ -239,10 +337,24 @@ moonLight.shadow.mapSize.set(
239
  );
240
 
241
 
242
- moonLight.shadow.camera.left = -10;
243
- moonLight.shadow.camera.right = 10;
244
- moonLight.shadow.camera.top = 10;
245
- moonLight.shadow.camera.bottom = -5;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
 
247
 
248
  scene.add(
@@ -250,120 +362,408 @@ scene.add(
250
  );
251
 
252
 
253
- /*
254
- Small warm city light.
 
255
 
256
- This is what will occasionally reveal the character's
257
- green clothing / hair / leather against the darkness.
 
258
  */
259
 
260
  const cityLight =
261
  new THREE.PointLight(
262
- 0xd59b55,
263
- 3.0,
 
 
 
264
  10,
 
265
  2
 
266
  );
267
 
 
268
  cityLight.position.set(
269
- -4,
270
- 3,
271
  3
272
  );
273
 
 
274
  scene.add(
275
  cityLight
276
  );
277
 
278
 
279
- /* ============================================================
280
- TEMPORARY FLOOR
281
- ============================================================ */
282
 
283
- /*
284
- world.js will eventually replace this with the tile-based
285
- grass floor.
286
 
287
- For now this gives the character something to stand on.
288
- */
289
 
290
- const temporaryFloor =
291
- new THREE.Mesh(
292
 
293
- new THREE.PlaneGeometry(
294
- 100,
295
- 30
296
- ),
297
 
298
- new THREE.MeshStandardMaterial({
299
- color: 0x071008,
300
- roughness: 1,
301
- metalness: 0
302
- })
303
 
304
  );
305
 
306
 
307
- temporaryFloor.rotation.x =
308
- -Math.PI / 2;
309
-
310
- temporaryFloor.position.y = 0;
 
311
 
312
- temporaryFloor.receiveShadow = true;
313
 
314
  scene.add(
315
- temporaryFloor
316
  );
317
 
318
 
319
  /* ============================================================
320
- CLOCK
321
  ============================================================ */
322
 
323
- const clock =
324
- new THREE.Clock();
 
325
 
326
- let elapsedTime = 0;
 
 
 
 
 
 
 
327
 
328
 
329
  /* ============================================================
330
- TEMPORARY VISUAL MARKER
331
  ============================================================ */
332
 
 
 
 
 
 
 
333
  /*
334
- This cube only confirms that Three.js is working.
335
 
336
- character.js will remove the need for it.
 
337
  */
338
 
339
- const testObject =
340
- new THREE.Mesh(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
 
342
- new THREE.BoxGeometry(
343
- 0.5,
344
- 1.5,
345
- 0.5
346
- ),
347
 
348
- new THREE.MeshStandardMaterial({
349
- color: 0x235f2c,
350
- roughness: 0.9
351
- })
352
 
353
  );
 
354
 
355
 
356
- testObject.position.set(
357
- 0,
358
- 0.75,
359
- 0
360
- );
361
 
362
- testObject.castShadow = true;
 
 
363
 
364
- scene.add(
365
- testObject
366
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
 
369
  /* ============================================================
@@ -373,12 +773,16 @@ scene.add(
373
  function resize() {
374
 
375
  camera.aspect =
 
376
  window.innerWidth /
377
  window.innerHeight;
378
 
 
379
  camera.updateProjectionMatrix();
380
 
381
- setRendererSize();
 
 
382
  }
383
 
384
 
@@ -389,87 +793,131 @@ window.addEventListener(
389
 
390
 
391
  /* ============================================================
392
- STARTUP
393
  ============================================================ */
394
 
395
- function finishLoading() {
396
-
397
- loadingText.textContent =
398
- "THE RAIN BEGINS";
399
 
 
 
 
400
 
401
- /*
402
- Short delay lets us actually see the message.
403
- */
 
404
 
405
- setTimeout(() => {
406
 
407
- loadingScreen.classList.add(
408
- "hidden"
409
- );
410
 
411
- screenOverlay.classList.add(
412
- "hidden"
413
- );
414
 
415
- }, 500);
416
- }
417
 
 
 
 
418
 
419
- /*
420
- We don't actually have asynchronous assets yet,
421
- so this is temporary.
422
 
423
- character.js will eventually call this only after
424
- character.glb has loaded.
425
- */
426
 
427
- window.addEventListener(
428
- "load",
429
- finishLoading
430
- );
431
 
432
 
433
  /* ============================================================
434
- ANIMATION
435
  ============================================================ */
436
 
437
- function update(delta) {
 
 
438
 
439
- elapsedTime += delta;
 
440
 
441
 
442
- /*
443
- Temporary mathematical motion.
 
444
 
445
- This demonstrates the same basic principle we'll
446
- eventually use for the character skeleton.
447
- */
 
448
 
449
- testObject.position.y =
450
- 0.75 +
451
- Math.abs(
452
- Math.sin(
453
- elapsedTime * 4
454
- )
455
- ) * 0.03;
456
 
 
 
 
457
 
458
- /*
459
- Tiny variation in the warm light makes the city
460
- illumination feel imperfect rather than electronic.
461
- */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
462
 
463
- cityLight.intensity =
464
- 2.8 +
465
- Math.sin(
466
- elapsedTime * 2.1
467
- ) * 0.15;
468
  }
469
 
470
 
471
  /* ============================================================
472
- RENDER LOOP
473
  ============================================================ */
474
 
475
  function animate() {
@@ -480,43 +928,84 @@ function animate() {
480
 
481
 
482
  /*
483
- Clamp delta.
484
-
485
- Prevents giant movement jumps after returning to a
486
- browser tab that has been inactive.
487
  */
488
 
489
  const delta =
 
490
  Math.min(
491
  clock.getDelta(),
492
  0.05
493
  );
494
 
495
 
496
- update(delta);
 
 
497
 
498
 
499
- renderer.render(
500
- scene,
501
- camera
502
- );
503
  }
504
 
505
 
506
- animate();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507
 
508
 
509
  /* ============================================================
510
- EXPORTS
 
 
511
 
512
- Other modules will import these rather than creating their
513
- own duplicate scene / camera / renderer.
 
 
 
514
  ============================================================ */
515
 
516
- export {
517
- THREE,
518
  scene,
 
519
  camera,
 
520
  renderer,
521
- clock
 
 
 
 
 
 
 
 
 
 
 
 
522
  };
 
2
  RAIN WALKER
3
  main.js
4
 
5
+ Main application entry point.
6
+
7
+ Responsibilities:
8
+ - Three.js renderer
9
+ - Scene
10
+ - Camera
11
+ - Lighting
12
+ - Character loading
13
+ - World creation
14
+ - Rain
15
+ - Character traversal
16
+ - Camera composition
17
+ - Loading screen
18
+ - Main animation loop
19
+ ============================================================ */
20
+
21
+
22
+ /* ============================================================
23
+ THREE.JS
24
  ============================================================ */
25
 
26
  import * as THREE from
27
  "https://cdn.jsdelivr.net/npm/three@0.180.0/build/three.module.js";
28
 
29
 
30
+ /* ============================================================
31
+ PROJECT MODULES
32
+ ============================================================ */
33
+
34
+ import {
35
+ loadCharacter,
36
+ updateCharacter,
37
+ getCharacter,
38
+ isCharacterReady
39
+ } from "./character.js";
40
+
41
+
42
+ import {
43
+ createWorld,
44
+ updateWorld
45
+ } from "./world.js";
46
+
47
+
48
+ import {
49
+ createRain,
50
+ updateRain
51
+ } from "./rain.js";
52
+
53
+
54
  /* ============================================================
55
  DOM
56
  ============================================================ */
 
68
  document.getElementById("screen-overlay");
69
 
70
 
71
+ /* ============================================================
72
+ CONFIGURATION
73
+ ============================================================ */
74
+
75
+ const CONFIG = {
76
+
77
+ /*
78
+ Render below native resolution and enlarge with CSS.
79
+
80
+ This creates the chunky/pixel presentation.
81
+ */
82
+
83
+ pixelScale: 0.55,
84
+
85
+
86
+ /*
87
+ Character world movement.
88
+ */
89
+
90
+ characterSpeed: 0.72,
91
+
92
+
93
+ /*
94
+ Character starts off-screen left and exits right.
95
+ */
96
+
97
+ walkStart: -7.5,
98
+
99
+ walkEnd: 7.5,
100
+
101
+
102
+ /*
103
+ Camera.
104
+ */
105
+
106
+ cameraHeight: 3.0,
107
+
108
+ cameraDistance: 10,
109
+
110
+
111
+ /*
112
+ Keep camera mostly stationary.
113
+
114
+ This gives us the original concept:
115
+ character crosses the actual screen.
116
+ */
117
+
118
+ cameraFollowAmount: 0.06
119
+
120
+ };
121
+
122
+
123
  /* ============================================================
124
  SCENE
125
  ============================================================ */
 
127
  const scene =
128
  new THREE.Scene();
129
 
130
+
131
  scene.background =
132
+ new THREE.Color(
133
+ 0x000000
134
+ );
135
 
136
 
137
  /*
138
+ Heavy black fog makes the city disappear naturally
139
+ into the background.
 
 
140
  */
141
 
142
  scene.fog =
143
+ new THREE.FogExp2(
144
  0x000000,
145
+ 0.055
 
146
  );
147
 
148
 
 
152
 
153
  const camera =
154
  new THREE.PerspectiveCamera(
155
+
156
  38,
157
+
158
  window.innerWidth /
159
  window.innerHeight,
160
+
161
  0.1,
 
 
162
 
163
+ 100
164
 
165
+ );
 
166
 
 
 
167
 
168
  camera.position.set(
169
  0,
170
+ CONFIG.cameraHeight,
171
+ CONFIG.cameraDistance
172
  );
173
 
174
+
175
  camera.lookAt(
176
  0,
177
+ 1.65,
178
  0
179
  );
180
 
 
185
 
186
  const renderer =
187
  new THREE.WebGLRenderer({
188
+
189
  antialias: false,
190
+
191
  alpha: false,
192
+
193
+ powerPreference:
194
+ "high-performance"
195
+
196
  });
197
 
198
 
199
+ renderer.outputColorSpace =
200
+ THREE.SRGBColorSpace;
201
 
 
 
202
 
203
+ renderer.toneMapping =
204
+ THREE.ACESFilmicToneMapping;
205
 
 
 
 
206
 
207
+ renderer.toneMappingExposure =
208
+ 0.82;
209
 
210
 
211
+ /* ============================================================
212
+ SHADOWS
213
+ ============================================================ */
214
+
215
+ renderer.shadowMap.enabled =
216
+ true;
217
+
218
+
219
+ renderer.shadowMap.type =
220
+ THREE.PCFShadowMap;
221
+
222
+
223
+ /* ============================================================
224
+ PIXEL RESOLUTION
225
+ ============================================================ */
226
+
227
+ function updateRendererSize() {
228
 
229
  const width =
230
  Math.max(
231
  1,
232
  Math.floor(
233
  window.innerWidth *
234
+ CONFIG.pixelScale
235
  )
236
  );
237
 
238
+
239
  const height =
240
  Math.max(
241
  1,
242
  Math.floor(
243
  window.innerHeight *
244
+ CONFIG.pixelScale
245
  )
246
  );
247
 
248
+
249
+ renderer.setPixelRatio(1);
250
+
251
+
252
  renderer.setSize(
253
  width,
254
  height,
 
257
 
258
 
259
  /*
260
+ Stretch low-resolution WebGL canvas back to the
261
+ full browser viewport.
262
+
263
+ style.css uses image-rendering: pixelated.
264
  */
265
 
266
  renderer.domElement.style.width =
267
  "100vw";
268
 
269
+
270
  renderer.domElement.style.height =
271
  "100vh";
272
  }
273
 
274
 
275
+ updateRendererSize();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
 
278
  game.appendChild(
 
285
  ============================================================ */
286
 
287
 
288
+ /* ------------------------------------------------------------
289
+ VERY DARK SKY / GROUND LIGHT
290
+ ------------------------------------------------------------ */
 
 
291
 
292
+ const hemisphereLight =
293
  new THREE.HemisphereLight(
294
+
295
+ 0x566675,
296
+
297
  0x071008,
298
+
299
+ 0.48
300
+
301
  );
302
 
303
+
304
  scene.add(
305
+ hemisphereLight
306
  );
307
 
308
 
309
+ /* ------------------------------------------------------------
310
+ COLD NIGHT LIGHT
311
+ ------------------------------------------------------------ */
312
 
313
  const moonLight =
314
  new THREE.DirectionalLight(
315
+
316
+ 0x9fb8cc,
317
+
318
+ 1.45
319
+
320
  );
321
 
322
+
323
  moonLight.position.set(
324
  -5,
325
  9,
326
+ 7
327
  );
328
 
329
+
330
+ moonLight.castShadow =
331
+ true;
332
 
333
 
334
  moonLight.shadow.mapSize.set(
 
337
  );
338
 
339
 
340
+ moonLight.shadow.camera.left =
341
+ -12;
342
+
343
+
344
+ moonLight.shadow.camera.right =
345
+ 12;
346
+
347
+
348
+ moonLight.shadow.camera.top =
349
+ 12;
350
+
351
+
352
+ moonLight.shadow.camera.bottom =
353
+ -4;
354
+
355
+
356
+ moonLight.shadow.bias =
357
+ -0.0005;
358
 
359
 
360
  scene.add(
 
362
  );
363
 
364
 
365
+ /* ------------------------------------------------------------
366
+ WARM CITY LIGHT
367
+ ------------------------------------------------------------ */
368
 
369
+ /*
370
+ This lets the character's green tunic, hair and leather
371
+ briefly stand out from the blue-black environment.
372
  */
373
 
374
  const cityLight =
375
  new THREE.PointLight(
376
+
377
+ 0xd49a55,
378
+
379
+ 2.7,
380
+
381
  10,
382
+
383
  2
384
+
385
  );
386
 
387
+
388
  cityLight.position.set(
389
+ -2.5,
390
+ 3.5,
391
  3
392
  );
393
 
394
+
395
  scene.add(
396
  cityLight
397
  );
398
 
399
 
400
+ /* ------------------------------------------------------------
401
+ SECOND COLD STREET LIGHT
402
+ ------------------------------------------------------------ */
403
 
404
+ const coldLight =
405
+ new THREE.PointLight(
 
406
 
407
+ 0x628aa2,
 
408
 
409
+ 1.5,
 
410
 
411
+ 9,
 
 
 
412
 
413
+ 2
 
 
 
 
414
 
415
  );
416
 
417
 
418
+ coldLight.position.set(
419
+ 5,
420
+ 4,
421
+ 2
422
+ );
423
 
 
424
 
425
  scene.add(
426
+ coldLight
427
  );
428
 
429
 
430
  /* ============================================================
431
+ WORLD
432
  ============================================================ */
433
 
434
+ createWorld(
435
+ scene
436
+ );
437
 
438
+
439
+ /* ============================================================
440
+ RAIN
441
+ ============================================================ */
442
+
443
+ createRain(
444
+ scene
445
+ );
446
 
447
 
448
  /* ============================================================
449
+ CHARACTER STATE
450
  ============================================================ */
451
 
452
+ let character = null;
453
+
454
+ let characterLoaded =
455
+ false;
456
+
457
+
458
  /*
459
+ Separate locomotion clock.
460
 
461
+ This lets us reset the character cleanly when it reaches
462
+ the other side of the screen.
463
  */
464
 
465
+ let walkTime = 0;
466
+
467
+
468
+ /* ============================================================
469
+ LOAD CHARACTER
470
+ ============================================================ */
471
+
472
+ async function initializeCharacter() {
473
+
474
+ try {
475
+
476
+ setLoadingMessage(
477
+ "LOADING WANDERER..."
478
+ );
479
+
480
+
481
+ const result =
482
+ await loadCharacter(
483
+ scene
484
+ );
485
+
486
+
487
+ character =
488
+ result.character;
489
+
490
+
491
+ characterLoaded =
492
+ true;
493
+
494
+
495
+ /*
496
+ Start outside the left edge.
497
+ */
498
+
499
+ character.position.x =
500
+ CONFIG.walkStart;
501
+
502
+
503
+ /*
504
+ Keep character on the gameplay plane.
505
+ */
506
+
507
+ character.position.z =
508
+ 0;
509
+
510
+
511
+ /*
512
+ IMPORTANT:
513
+
514
+ Depending on how your GLB was exported, its forward
515
+ direction may need changing.
516
+
517
+ Try:
518
+ 0
519
+ Math.PI
520
+ Math.PI / 2
521
+ -Math.PI / 2
522
+
523
+ We begin with a side-facing assumption.
524
+ */
525
+
526
+ character.rotation.y =
527
+ Math.PI / 2;
528
+
529
+
530
+ setLoadingMessage(
531
+ "THE RAIN BEGINS"
532
+ );
533
+
534
+
535
+ /*
536
+ Give textures one render frame to upload.
537
+ */
538
+
539
+ requestAnimationFrame(
540
+ finishLoading
541
+ );
542
+
543
+ }
544
+
545
+ catch (error) {
546
+
547
+ console.error(
548
+ "Character initialization failed:",
549
+ error
550
+ );
551
+
552
+
553
+ setLoadingMessage(
554
+ "CHARACTER FAILED TO LOAD"
555
+ );
556
+
557
+
558
+ /*
559
+ World still runs even if model fails.
560
+ */
561
+
562
+ setTimeout(
563
+ finishLoading,
564
+ 1200
565
+ );
566
+
567
+ }
568
+ }
569
+
570
+
571
+ /* ============================================================
572
+ LOADING SCREEN
573
+ ============================================================ */
574
+
575
+ function setLoadingMessage(
576
+ message
577
+ ) {
578
+
579
+ if (!loadingText) {
580
+ return;
581
+ }
582
+
583
+
584
+ loadingText.textContent =
585
+ message;
586
+ }
587
+
588
+
589
+ function finishLoading() {
590
+
591
+ setTimeout(
592
+ () => {
593
+
594
+ if (loadingScreen) {
595
+
596
+ loadingScreen.classList.add(
597
+ "hidden"
598
+ );
599
+
600
+ }
601
+
602
+
603
+ if (screenOverlay) {
604
+
605
+ screenOverlay.classList.add(
606
+ "hidden"
607
+ );
608
 
609
+ }
 
 
 
 
610
 
611
+ },
612
+
613
+ 450
 
614
 
615
  );
616
+ }
617
 
618
 
619
+ /* ============================================================
620
+ CHARACTER MOVEMENT
621
+ ============================================================ */
 
 
622
 
623
+ function updateCharacterMovement(
624
+ delta
625
+ ) {
626
 
627
+ if (
628
+ !characterLoaded ||
629
+ !character
630
+ ) {
631
+
632
+ return;
633
+ }
634
+
635
+
636
+ /*
637
+ Actual horizontal traversal.
638
+ */
639
+
640
+ character.position.x +=
641
+
642
+ CONFIG.characterSpeed *
643
+ delta;
644
+
645
+
646
+ walkTime += delta;
647
+
648
+
649
+ /*
650
+ Character exits the right edge and reappears from
651
+ the left.
652
+
653
+ Reset is deliberately outside the visible frame.
654
+ */
655
+
656
+ if (
657
+ character.position.x >
658
+ CONFIG.walkEnd
659
+ ) {
660
+
661
+ character.position.x =
662
+ CONFIG.walkStart;
663
+
664
+
665
+ walkTime = 0;
666
+
667
+ }
668
+ }
669
+
670
+
671
+ /* ============================================================
672
+ CAMERA
673
+ ============================================================ */
674
+
675
+ function updateCamera(
676
+ delta
677
+ ) {
678
+
679
+ /*
680
+ The camera stays mostly stationary.
681
+
682
+ A tiny amount of follow gives the shot some life
683
+ without preventing the character from visibly crossing
684
+ the screen.
685
+ */
686
+
687
+ if (
688
+ characterLoaded &&
689
+ character
690
+ ) {
691
+
692
+ const desiredX =
693
+
694
+ character.position.x *
695
+ CONFIG.cameraFollowAmount;
696
+
697
+
698
+ camera.position.x =
699
+ THREE.MathUtils.lerp(
700
+
701
+ camera.position.x,
702
+
703
+ desiredX,
704
+
705
+ 1 -
706
+ Math.exp(
707
+ -2 * delta
708
+ )
709
+
710
+ );
711
+
712
+ }
713
+
714
+
715
+ camera.lookAt(
716
+ camera.position.x * 0.15,
717
+ 1.65,
718
+ 0
719
+ );
720
+ }
721
+
722
+
723
+ /* ============================================================
724
+ LIGHT ANIMATION
725
+ ============================================================ */
726
+
727
+ function updateLights(
728
+ elapsedTime
729
+ ) {
730
+
731
+ /*
732
+ Imperfect city electricity.
733
+
734
+ Deliberately subtle.
735
+ */
736
+
737
+ cityLight.intensity =
738
+
739
+ 2.7 +
740
+
741
+ Math.sin(
742
+ elapsedTime * 2.15
743
+ ) *
744
+ 0.12 +
745
+
746
+ Math.sin(
747
+ elapsedTime * 7.7
748
+ ) *
749
+ 0.035;
750
+
751
+
752
+ /*
753
+ Tiny movement makes reflected illumination feel less
754
+ completely static in the rain.
755
+ */
756
+
757
+ cityLight.position.x =
758
+
759
+ -2.5 +
760
+
761
+ Math.sin(
762
+ elapsedTime * 0.18
763
+ ) *
764
+ 0.15;
765
+
766
+ }
767
 
768
 
769
  /* ============================================================
 
773
  function resize() {
774
 
775
  camera.aspect =
776
+
777
  window.innerWidth /
778
  window.innerHeight;
779
 
780
+
781
  camera.updateProjectionMatrix();
782
 
783
+
784
+ updateRendererSize();
785
+
786
  }
787
 
788
 
 
793
 
794
 
795
  /* ============================================================
796
+ VISIBILITY
797
  ============================================================ */
798
 
799
+ /*
800
+ Prevent huge time jumps when returning to a background tab.
801
+ */
 
802
 
803
+ document.addEventListener(
804
+ "visibilitychange",
805
+ () => {
806
 
807
+ if (
808
+ document.visibilityState ===
809
+ "visible"
810
+ ) {
811
 
812
+ clock.getDelta();
813
 
814
+ }
 
 
815
 
816
+ }
817
+ );
 
818
 
 
 
819
 
820
+ /* ============================================================
821
+ CLOCK
822
+ ============================================================ */
823
 
824
+ const clock =
825
+ new THREE.Clock();
 
826
 
 
 
 
827
 
828
+ let elapsedTime = 0;
 
 
 
829
 
830
 
831
  /* ============================================================
832
+ UPDATE
833
  ============================================================ */
834
 
835
+ function update(
836
+ delta
837
+ ) {
838
 
839
+ elapsedTime +=
840
+ delta;
841
 
842
 
843
+ /* --------------------------------------------------------
844
+ WORLD
845
+ -------------------------------------------------------- */
846
 
847
+ updateWorld(
848
+ delta,
849
+ elapsedTime
850
+ );
851
 
 
 
 
 
 
 
 
852
 
853
+ /* --------------------------------------------------------
854
+ RAIN
855
+ -------------------------------------------------------- */
856
 
857
+ updateRain(
858
+ delta,
859
+ elapsedTime
860
+ );
861
+
862
+
863
+ /* --------------------------------------------------------
864
+ CHARACTER TRAVERSAL
865
+ -------------------------------------------------------- */
866
+
867
+ updateCharacterMovement(
868
+ delta
869
+ );
870
+
871
+
872
+ /* --------------------------------------------------------
873
+ CHARACTER SKELETON
874
+
875
+ character.js performs the mathematical walk if bones
876
+ exist in the GLB.
877
+ -------------------------------------------------------- */
878
+
879
+ updateCharacter(
880
+ delta,
881
+ walkTime
882
+ );
883
+
884
+
885
+ /* --------------------------------------------------------
886
+ CAMERA
887
+ -------------------------------------------------------- */
888
+
889
+ updateCamera(
890
+ delta
891
+ );
892
+
893
+
894
+ /* --------------------------------------------------------
895
+ LIGHTING
896
+ -------------------------------------------------------- */
897
+
898
+ updateLights(
899
+ elapsedTime
900
+ );
901
+
902
+ }
903
+
904
+
905
+ /* ============================================================
906
+ RENDER
907
+ ============================================================ */
908
+
909
+ function render() {
910
+
911
+ renderer.render(
912
+ scene,
913
+ camera
914
+ );
915
 
 
 
 
 
 
916
  }
917
 
918
 
919
  /* ============================================================
920
+ GAME LOOP
921
  ============================================================ */
922
 
923
  function animate() {
 
928
 
929
 
930
  /*
931
+ Clamp delta to prevent giant simulation jumps.
 
 
 
932
  */
933
 
934
  const delta =
935
+
936
  Math.min(
937
  clock.getDelta(),
938
  0.05
939
  );
940
 
941
 
942
+ update(
943
+ delta
944
+ );
945
 
946
 
947
+ render();
948
+
 
 
949
  }
950
 
951
 
952
+ /* ============================================================
953
+ START
954
+ ============================================================ */
955
+
956
+ async function start() {
957
+
958
+ /*
959
+ Begin rendering immediately.
960
+
961
+ This means the city/rain can exist while the GLB loads.
962
+ */
963
+
964
+ animate();
965
+
966
+
967
+ /*
968
+ Load character asynchronously.
969
+ */
970
+
971
+ await initializeCharacter();
972
+
973
+ }
974
+
975
+
976
+ start();
977
 
978
 
979
  /* ============================================================
980
+ DEVELOPMENT ACCESS
981
+
982
+ Handy while we're tuning the model in DevTools.
983
 
984
+ You can type:
985
+
986
+ RainWalker.character
987
+
988
+ into the console.
989
  ============================================================ */
990
 
991
+ window.RainWalker = {
992
+
993
  scene,
994
+
995
  camera,
996
+
997
  renderer,
998
+
999
+ get character() {
1000
+
1001
+ return getCharacter();
1002
+
1003
+ },
1004
+
1005
+ get characterReady() {
1006
+
1007
+ return isCharacterReady();
1008
+
1009
+ }
1010
+
1011
  };