Spaces:
Running
Running
init frame timing in main
Browse files- index.js +13 -20
- wgpu-state.js +8 -5
index.js
CHANGED
@@ -16,18 +16,6 @@ const canvas = document.querySelector('canvas');
|
|
16 |
const state = createState(config);
|
17 |
state.canvas = canvas;
|
18 |
|
19 |
-
const FIXED_DELTA_TIME = 1 / 60;
|
20 |
-
const MAX_FRAME_TIME = 0.25;
|
21 |
-
const TARGET_FPS = 60;
|
22 |
-
const FRAME_DURATION = 1000 / TARGET_FPS;
|
23 |
-
|
24 |
-
// Ensure timing object exists within state
|
25 |
-
state.timing = state.timing || {};
|
26 |
-
state.timing.fixedDeltaTime = FIXED_DELTA_TIME;
|
27 |
-
state.timing.maxFrameTime = MAX_FRAME_TIME;
|
28 |
-
state.timing.targetFps = TARGET_FPS;
|
29 |
-
state.timing.frameDuration = FRAME_DURATION;
|
30 |
-
|
31 |
async function Main() {
|
32 |
const adapter = await navigator.gpu?.requestAdapter();
|
33 |
const { device, context, presentationFormat } = await initializeWebGPU(navigator, adapter, state.canvas);
|
@@ -37,6 +25,12 @@ async function Main() {
|
|
37 |
state.webgpu.context = context;
|
38 |
state.webgpu.presentationFormat = presentationFormat;
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
// Initialize Resources
|
41 |
await InitializeResources();
|
42 |
|
@@ -89,20 +83,19 @@ function GameLoop() {
|
|
89 |
state.timing.accumulator = 0;
|
90 |
|
91 |
function Tick() {
|
92 |
-
|
93 |
-
|
94 |
-
lastTime = currentTime;
|
95 |
|
96 |
-
|
97 |
-
state.timing.accumulator += deltaTime;
|
98 |
|
99 |
while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
|
100 |
FixedUpdate(state.timing.fixedDeltaTime);
|
101 |
state.timing.accumulator -= state.timing.fixedDeltaTime;
|
102 |
}
|
103 |
|
104 |
-
|
105 |
-
Render(alpha);
|
106 |
|
107 |
setTimeout(Tick, state.timing.frameDuration);
|
108 |
}
|
@@ -114,7 +107,7 @@ function FixedUpdate(deltaTime) {
|
|
114 |
state.timing.time += deltaTime;
|
115 |
}
|
116 |
|
117 |
-
function Render(
|
118 |
const fov = 60 * Math.PI / 180;
|
119 |
const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
|
120 |
const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
|
|
|
16 |
const state = createState(config);
|
17 |
state.canvas = canvas;
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
async function Main() {
|
20 |
const adapter = await navigator.gpu?.requestAdapter();
|
21 |
const { device, context, presentationFormat } = await initializeWebGPU(navigator, adapter, state.canvas);
|
|
|
25 |
state.webgpu.context = context;
|
26 |
state.webgpu.presentationFormat = presentationFormat;
|
27 |
|
28 |
+
// Initialize timing properties
|
29 |
+
state.timing.fixedDeltaTime = 1 / 60;
|
30 |
+
state.timing.maxFrameTime = 0.25;
|
31 |
+
state.timing.targetFps = 60;
|
32 |
+
state.timing.frameDuration = 1000 / 60;
|
33 |
+
|
34 |
// Initialize Resources
|
35 |
await InitializeResources();
|
36 |
|
|
|
83 |
state.timing.accumulator = 0;
|
84 |
|
85 |
function Tick() {
|
86 |
+
state.timing.currentTime = performance.now();
|
87 |
+
state.timing.frameTime = (state.timing.currentTime - lastTime) / 1000;
|
88 |
+
lastTime = state.timing.currentTime;
|
89 |
|
90 |
+
state.timing.deltaTime = Math.min(state.timing.frameTime, state.timing.maxFrameTime);
|
91 |
+
state.timing.accumulator += state.timing.deltaTime;
|
92 |
|
93 |
while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
|
94 |
FixedUpdate(state.timing.fixedDeltaTime);
|
95 |
state.timing.accumulator -= state.timing.fixedDeltaTime;
|
96 |
}
|
97 |
|
98 |
+
Render();
|
|
|
99 |
|
100 |
setTimeout(Tick, state.timing.frameDuration);
|
101 |
}
|
|
|
107 |
state.timing.time += deltaTime;
|
108 |
}
|
109 |
|
110 |
+
function Render() {
|
111 |
const fov = 60 * Math.PI / 180;
|
112 |
const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
|
113 |
const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
|
wgpu-state.js
CHANGED
@@ -14,7 +14,7 @@ export function createState(config) {
|
|
14 |
},
|
15 |
matrices: {
|
16 |
uniformValues: new Float32Array(config.uniformBufferSize / 4),
|
17 |
-
matrix:
|
18 |
},
|
19 |
glyphs: {
|
20 |
numGlyphs: 0,
|
@@ -24,11 +24,14 @@ export function createState(config) {
|
|
24 |
canvas: null,
|
25 |
timing: {
|
26 |
time: 0,
|
27 |
-
fixedDeltaTime:
|
28 |
-
maxFrameTime: 0
|
29 |
-
targetFps:
|
30 |
-
frameDuration:
|
31 |
accumulator: 0,
|
|
|
|
|
|
|
32 |
}
|
33 |
};
|
34 |
}
|
|
|
14 |
},
|
15 |
matrices: {
|
16 |
uniformValues: new Float32Array(config.uniformBufferSize / 4),
|
17 |
+
matrix: new Float32Array(16),
|
18 |
},
|
19 |
glyphs: {
|
20 |
numGlyphs: 0,
|
|
|
24 |
canvas: null,
|
25 |
timing: {
|
26 |
time: 0,
|
27 |
+
fixedDeltaTime: 0,
|
28 |
+
maxFrameTime: 0,
|
29 |
+
targetFps: 0,
|
30 |
+
frameDuration: 0,
|
31 |
accumulator: 0,
|
32 |
+
deltaTime: 0,
|
33 |
+
currentTime: 0,
|
34 |
+
frameTime: 0,
|
35 |
}
|
36 |
};
|
37 |
}
|