Spaces:
Running
Running
move dynamic properties to state
Browse files- index.js +18 -12
- wgpu-state.js +15 -2
index.js
CHANGED
@@ -4,8 +4,8 @@ import { createState } from './wgpu-state.js';
|
|
4 |
import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
|
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 |
|
@@ -14,15 +14,21 @@ const canvas = document.querySelector('canvas');
|
|
14 |
|
15 |
// State initialization
|
16 |
const state = createState(config);
|
|
|
17 |
|
18 |
const FIXED_DELTA_TIME = 1 / 60; // Fixed time step of 60 FPS for physics and game logic
|
19 |
const MAX_FRAME_TIME = 0.25; // Maximum time to simulate per frame to prevent spiral of death
|
20 |
const TARGET_FPS = 60; // Target frames per second
|
21 |
const FRAME_DURATION = 1000 / TARGET_FPS; // Duration of a single frame in milliseconds
|
22 |
|
|
|
|
|
|
|
|
|
|
|
23 |
async function Main() {
|
24 |
const adapter = await navigator.gpu?.requestAdapter();
|
25 |
-
const { device, context, presentationFormat } = await initializeWebGPU(navigator, adapter, canvas);
|
26 |
if (!device) return;
|
27 |
|
28 |
state.device = device;
|
@@ -86,28 +92,28 @@ function GenerateIndices(maxGlyphs) {
|
|
86 |
// Game loop function
|
87 |
function GameLoop(context) {
|
88 |
let lastTime = performance.now();
|
89 |
-
|
90 |
|
91 |
function Tick() {
|
92 |
const currentTime = performance.now();
|
93 |
const frameTime = (currentTime - lastTime) / 1000;
|
94 |
lastTime = currentTime;
|
95 |
|
96 |
-
const deltaTime = Math.min(frameTime,
|
97 |
-
accumulator += deltaTime;
|
98 |
|
99 |
// Fixed time step updates for game logic
|
100 |
-
while (accumulator >=
|
101 |
-
FixedUpdate(
|
102 |
-
accumulator -=
|
103 |
}
|
104 |
|
105 |
// Variable time step update for rendering
|
106 |
-
const alpha = accumulator /
|
107 |
Render(alpha, context);
|
108 |
|
109 |
// Schedule the next frame update
|
110 |
-
setTimeout(Tick,
|
111 |
}
|
112 |
|
113 |
Tick();
|
@@ -123,7 +129,7 @@ function FixedUpdate(deltaTime) {
|
|
123 |
function Render(alpha, context) {
|
124 |
// Set up projection and view matrices
|
125 |
const fov = 60 * Math.PI / 180;
|
126 |
-
const aspect = canvas.clientWidth / canvas.clientHeight;
|
127 |
const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
|
128 |
const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
|
129 |
const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
|
|
|
4 |
import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
|
5 |
import { createPipeline } from './wgpu-pipeline.js';
|
6 |
import { fetchShaderCode } from './wgpu-shader.js';
|
7 |
+
import { GenerateVertexDataAndTexture } from './wgpu-texture.js'; // Import the function
|
8 |
+
import { generateGlyphVerticesForText } from './wgpu-text.js'; // Corrected import statement
|
9 |
import { config } from './wgpu-config.js';
|
10 |
import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
|
11 |
|
|
|
14 |
|
15 |
// State initialization
|
16 |
const state = createState(config);
|
17 |
+
state.canvas = canvas; // Move canvas to state
|
18 |
|
19 |
const FIXED_DELTA_TIME = 1 / 60; // Fixed time step of 60 FPS for physics and game logic
|
20 |
const MAX_FRAME_TIME = 0.25; // Maximum time to simulate per frame to prevent spiral of death
|
21 |
const TARGET_FPS = 60; // Target frames per second
|
22 |
const FRAME_DURATION = 1000 / TARGET_FPS; // Duration of a single frame in milliseconds
|
23 |
|
24 |
+
state.fixedDeltaTime = FIXED_DELTA_TIME;
|
25 |
+
state.maxFrameTime = MAX_FRAME_TIME;
|
26 |
+
state.targetFps = TARGET_FPS;
|
27 |
+
state.frameDuration = FRAME_DURATION;
|
28 |
+
|
29 |
async function Main() {
|
30 |
const adapter = await navigator.gpu?.requestAdapter();
|
31 |
+
const { device, context, presentationFormat } = await initializeWebGPU(navigator, adapter, state.canvas); // Use state.canvas
|
32 |
if (!device) return;
|
33 |
|
34 |
state.device = device;
|
|
|
92 |
// Game loop function
|
93 |
function GameLoop(context) {
|
94 |
let lastTime = performance.now();
|
95 |
+
state.accumulator = 0;
|
96 |
|
97 |
function Tick() {
|
98 |
const currentTime = performance.now();
|
99 |
const frameTime = (currentTime - lastTime) / 1000;
|
100 |
lastTime = currentTime;
|
101 |
|
102 |
+
const deltaTime = Math.min(frameTime, state.maxFrameTime);
|
103 |
+
state.accumulator += deltaTime;
|
104 |
|
105 |
// Fixed time step updates for game logic
|
106 |
+
while (state.accumulator >= state.fixedDeltaTime) {
|
107 |
+
FixedUpdate(state.fixedDeltaTime);
|
108 |
+
state.accumulator -= state.fixedDeltaTime;
|
109 |
}
|
110 |
|
111 |
// Variable time step update for rendering
|
112 |
+
const alpha = state.accumulator / state.fixedDeltaTime;
|
113 |
Render(alpha, context);
|
114 |
|
115 |
// Schedule the next frame update
|
116 |
+
setTimeout(Tick, state.frameDuration);
|
117 |
}
|
118 |
|
119 |
Tick();
|
|
|
129 |
function Render(alpha, context) {
|
130 |
// Set up projection and view matrices
|
131 |
const fov = 60 * Math.PI / 180;
|
132 |
+
const aspect = state.canvas.clientWidth / state.canvas.clientHeight; // Use state.canvas
|
133 |
const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
|
134 |
const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
|
135 |
const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
|
wgpu-state.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
-
// wgpu-state.js
|
2 |
-
|
3 |
export function createState(config) {
|
4 |
return {
|
|
|
5 |
device: null,
|
6 |
pipeline: null,
|
7 |
vertexBuffer: null,
|
@@ -10,11 +9,25 @@ export function createState(config) {
|
|
10 |
texture: null,
|
11 |
sampler: null,
|
12 |
bindGroup: null,
|
|
|
|
|
13 |
uniformValues: new Float32Array(config.uniformBufferSize / 4),
|
14 |
matrix: null,
|
|
|
|
|
15 |
numGlyphs: 0,
|
16 |
width: 0,
|
17 |
height: 0,
|
|
|
|
|
|
|
|
|
|
|
18 |
time: 0,
|
|
|
|
|
|
|
|
|
|
|
19 |
};
|
20 |
}
|
|
|
|
|
|
|
1 |
export function createState(config) {
|
2 |
return {
|
3 |
+
// WebGPU resources
|
4 |
device: null,
|
5 |
pipeline: null,
|
6 |
vertexBuffer: null,
|
|
|
9 |
texture: null,
|
10 |
sampler: null,
|
11 |
bindGroup: null,
|
12 |
+
|
13 |
+
// Uniform values and matrices
|
14 |
uniformValues: new Float32Array(config.uniformBufferSize / 4),
|
15 |
matrix: null,
|
16 |
+
|
17 |
+
// Glyph details
|
18 |
numGlyphs: 0,
|
19 |
width: 0,
|
20 |
height: 0,
|
21 |
+
|
22 |
+
// Canvas and context
|
23 |
+
canvas: null,
|
24 |
+
|
25 |
+
// Timing and animation
|
26 |
time: 0,
|
27 |
+
fixedDeltaTime: 1 / 60,
|
28 |
+
maxFrameTime: 0.25,
|
29 |
+
targetFps: 60,
|
30 |
+
frameDuration: 1000 / 60,
|
31 |
+
accumulator: 0,
|
32 |
};
|
33 |
}
|