Skip to content

Commit ac2200e

Browse files
committed
🚤 optimizations
1 parent 39c9877 commit ac2200e

File tree

10 files changed

+330
-274
lines changed

10 files changed

+330
-274
lines changed

‎src/index.ts‎

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ import {
55
stepPointerOnGround,
66
} from "./logic/camera";
77
import { computeFinalPlacement } from "./logic/computeFinalPlacement";
8-
import { stepCrowd, stepRunnerLogic } from "./logic/crowd";
8+
import { createPhysicStepper, stepRunnerLogic } from "./logic/crowd";
99
import { stepProgress } from "./logic/progress";
1010
import { deriveSprites } from "./logic/sprites";
11-
import { createState, setInitialState } from "./logic/state";
11+
import { createState } from "./logic/state";
1212
import { attachUserEvent } from "./logic/userEvent";
1313
import { createSpriteRenderer } from "./renderer/spriteRenderer";
1414
import { createSpriteAtlas } from "./sprites";
1515

16+
const spritePromise = createSpriteAtlas();
17+
18+
const searchParams = new URL(location.href).searchParams;
19+
const message = searchParams.get("message") || "Hello";
20+
const runnerCount = parseInt(searchParams.get("n")) || 2000;
21+
1622
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
1723
const gl = canvas.getContext("webgl2")!;
18-
const dpr = window.devicePixelRatio ?? 1;
1924

2025
gl.disable(gl.CULL_FACE);
2126

@@ -27,50 +32,41 @@ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
2732

2833
const renderer = createSpriteRenderer(gl);
2934

30-
const state = createState();
31-
const resize = () => {
32-
canvas.width = canvas.clientWidth * dpr;
33-
canvas.height = canvas.clientHeight * dpr;
34-
35-
gl.viewport(0, 0, canvas.width, canvas.height);
35+
const set = renderer.createSet();
3636

37-
const aspect = canvas.width / canvas.height;
38-
mat4.perspective(
39-
state.projectionMatrix,
40-
Math.PI / 4 / aspect,
41-
aspect,
42-
0.1,
43-
2000,
44-
);
45-
};
46-
resize();
37+
//
38+
//
4739

48-
window.addEventListener("resize", resize);
49-
attachUserEvent(state);
40+
const state = createState(runnerCount);
41+
attachUserEvent(state, canvas, gl);
42+
window.dispatchEvent(new Event("resize"));
5043

51-
const set = renderer.createSet();
52-
const sets = [set];
44+
computeFinalPlacement(state, message);
5345

54-
createSpriteAtlas().then((res) => {
55-
const searchParams = new URL(location.href).searchParams;
56-
const message = searchParams.get("message") || "Hello";
57-
const entityCount = parseInt(searchParams.get("n")) || 2000;
46+
const stepPhysic = createPhysicStepper(runnerCount);
5847

48+
spritePromise.then((res) => {
5949
renderer.updateSet(set, { colorTexture: res.texture });
60-
setInitialState(state, res.animationIndex, entityCount);
61-
computeFinalPlacement(state, res.animationIndex, message);
50+
51+
gl.useProgram(renderer._internal.program);
52+
53+
gl.uniform1i(renderer._internal.u_colorTexture, 0);
54+
gl.bindVertexArray(set.vao);
55+
56+
gl.activeTexture(gl.TEXTURE0);
57+
gl.bindTexture(gl.TEXTURE_2D, set.colorTexture);
6258

6359
const loop = () => {
6460
state.time++;
6561

6662
stepPointerOnGround(state);
6763

68-
stepCrowd(state);
69-
stepRunnerLogic(state, res.animationIndex);
64+
stepPhysic(state);
65+
stepRunnerLogic(state);
7066
stepCameraWobble(state);
7167
stepProgress(state);
7268

73-
deriveSprites(state, res.animationIndex, res.coords);
69+
deriveSprites(state, res.coords);
7470
deriveViewMatrix(state);
7571

7672
//
@@ -81,7 +77,21 @@ createSpriteAtlas().then((res) => {
8177

8278
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
8379

84-
renderer.draw(state.projectionMatrix, state.viewMatrix, sets);
80+
//
81+
// usually the API is meant to call renderer.draw(state.projectionMatrix,state.viewMatrix,[set])
82+
// but since there is only oone program let's save some gl instructions
83+
gl.uniformMatrix4fv(
84+
renderer._internal.u_viewMatrix,
85+
false,
86+
state.viewMatrix,
87+
);
88+
gl.uniformMatrix4fv(
89+
renderer._internal.u_projectionMatrix,
90+
false,
91+
state.projectionMatrix,
92+
);
93+
94+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, set.numInstances);
8595

8696
//
8797

‎src/logic/computeFinalPlacement.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import type { AnimationIndex } from "../sprites";
1+
import { sprite } from "../sprites/type";
22
import type { State } from "./state";
33

4-
export const computeFinalPlacement = (
5-
game: State,
6-
animationIndex: AnimationIndex,
7-
text: string,
8-
) => {
4+
export const computeFinalPlacement = (game: State, text: string) => {
95
const canvas = new OffscreenCanvas(1024, 256);
106
// const canvas = document.createElement("canvas");
117
// document.body.appendChild(canvas);
@@ -60,7 +56,7 @@ export const computeFinalPlacement = (
6056
runner.finalTarget[1] = (y + Math.random()) * s;
6157
runner.randomTargetCount = 1;
6258

63-
runner.animationIndex = animationIndex.walk;
59+
runner.animationIndex = sprite.walk;
6460
runner.animationFrameDuration = 3;
6561
}
6662

0 commit comments

Comments
 (0)