Spaces:
Running
Running
pass state into Tick
Browse files
index.js
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
2 |
import { initializeWebGPU } from './wgpu-device.js';
|
3 |
import { createState } from './wgpu-state.js';
|
4 |
-
import {
|
5 |
import { createPipeline } from './wgpu-pipeline.js';
|
|
|
6 |
import { fetchShaderCode } from './wgpu-shader.js';
|
7 |
import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
|
8 |
import { generateGlyphVerticesForText } from './wgpu-text.js';
|
9 |
import { config } from './wgpu-config.js';
|
10 |
import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
|
11 |
import { CreateBuffers } from './wgpu-buffer.js';
|
12 |
-
import { initializeTiming } from './wgpu-timing.js';
|
13 |
|
14 |
// State initialization
|
15 |
const state = createState(config);
|
@@ -27,13 +27,13 @@ async function Main() {
|
|
27 |
initializeTiming(state);
|
28 |
|
29 |
// Initialize Resources
|
30 |
-
await InitializeResources();
|
31 |
|
32 |
// Start the game loop
|
33 |
-
GameLoop();
|
34 |
}
|
35 |
|
36 |
-
async function InitializeResources() {
|
37 |
const shaderCode = await fetchShaderCode('shaders.wgsl');
|
38 |
const vertexSize = config.floatsPerVertex * 4;
|
39 |
|
@@ -48,7 +48,7 @@ async function InitializeResources() {
|
|
48 |
GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
|
49 |
}
|
50 |
|
51 |
-
function GameLoop() {
|
52 |
function Tick() {
|
53 |
state.timing.currentTime = performance.now();
|
54 |
state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
|
@@ -58,11 +58,11 @@ function GameLoop() {
|
|
58 |
state.timing.accumulator += state.timing.deltaTime;
|
59 |
|
60 |
while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
|
61 |
-
FixedUpdate(state.timing.fixedDeltaTime);
|
62 |
state.timing.accumulator -= state.timing.fixedDeltaTime;
|
63 |
}
|
64 |
|
65 |
-
Render();
|
66 |
|
67 |
setTimeout(Tick, state.timing.frameDuration);
|
68 |
}
|
@@ -70,11 +70,11 @@ function GameLoop() {
|
|
70 |
Tick();
|
71 |
}
|
72 |
|
73 |
-
function FixedUpdate(deltaTime) {
|
74 |
state.timing.time += deltaTime;
|
75 |
}
|
76 |
|
77 |
-
function Render() {
|
78 |
const fov = 60 * Math.PI / 180;
|
79 |
const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
|
80 |
const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
|
|
|
1 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
2 |
import { initializeWebGPU } from './wgpu-device.js';
|
3 |
import { createState } from './wgpu-state.js';
|
4 |
+
import { initializeTiming } from './wgpu-timing.js';
|
5 |
import { createPipeline } from './wgpu-pipeline.js';
|
6 |
+
import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
|
7 |
import { fetchShaderCode } from './wgpu-shader.js';
|
8 |
import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
|
9 |
import { generateGlyphVerticesForText } from './wgpu-text.js';
|
10 |
import { config } from './wgpu-config.js';
|
11 |
import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
|
12 |
import { CreateBuffers } from './wgpu-buffer.js';
|
|
|
13 |
|
14 |
// State initialization
|
15 |
const state = createState(config);
|
|
|
27 |
initializeTiming(state);
|
28 |
|
29 |
// Initialize Resources
|
30 |
+
await InitializeResources(state);
|
31 |
|
32 |
// Start the game loop
|
33 |
+
GameLoop(state);
|
34 |
}
|
35 |
|
36 |
+
async function InitializeResources(state) {
|
37 |
const shaderCode = await fetchShaderCode('shaders.wgsl');
|
38 |
const vertexSize = config.floatsPerVertex * 4;
|
39 |
|
|
|
48 |
GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
|
49 |
}
|
50 |
|
51 |
+
function GameLoop(state) {
|
52 |
function Tick() {
|
53 |
state.timing.currentTime = performance.now();
|
54 |
state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
|
|
|
58 |
state.timing.accumulator += state.timing.deltaTime;
|
59 |
|
60 |
while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
|
61 |
+
FixedUpdate(state.timing.fixedDeltaTime, state);
|
62 |
state.timing.accumulator -= state.timing.fixedDeltaTime;
|
63 |
}
|
64 |
|
65 |
+
Render(state);
|
66 |
|
67 |
setTimeout(Tick, state.timing.frameDuration);
|
68 |
}
|
|
|
70 |
Tick();
|
71 |
}
|
72 |
|
73 |
+
function FixedUpdate(deltaTime, state) {
|
74 |
state.timing.time += deltaTime;
|
75 |
}
|
76 |
|
77 |
+
function Render(state) {
|
78 |
const fov = 60 * Math.PI / 180;
|
79 |
const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
|
80 |
const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
|