Spaces:
Running
Running
π
Browse files- index.js +11 -15
- wgpu-texture.js +0 -1
index.js
CHANGED
@@ -1,17 +1,18 @@
|
|
|
|
|
|
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 {
|
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);
|
16 |
|
17 |
async function Main() {
|
@@ -23,14 +24,11 @@ async function Main() {
|
|
23 |
state.webgpu.context = context;
|
24 |
state.webgpu.presentationFormat = presentationFormat;
|
25 |
|
26 |
-
// Initialize timing properties
|
27 |
initializeTiming(state);
|
28 |
|
29 |
-
// Initialize Shaders and Resources
|
30 |
await InitializeShaders(state);
|
31 |
await InitializeResources(state);
|
32 |
|
33 |
-
// Start the game loop
|
34 |
GameLoop(state);
|
35 |
}
|
36 |
|
@@ -38,7 +36,7 @@ async function InitializeResources(state) {
|
|
38 |
const vertexSize = config.floatsPerVertex * 4;
|
39 |
|
40 |
state.webgpu.pipeline = await createPipeline(state.webgpu.device, state.webgpu.presentationFormat, vertexSize, state.webgpu.shaderCode);
|
41 |
-
|
42 |
const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
|
43 |
document.body.appendChild(glyphCanvas);
|
44 |
glyphCanvas.style.backgroundColor = '#222';
|
@@ -49,19 +47,18 @@ async function InitializeResources(state) {
|
|
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;
|
55 |
state.timing.lastTime = state.timing.currentTime;
|
56 |
-
|
57 |
state.timing.deltaTime = Math.min(state.timing.frameTime, state.timing.maxFrameTime);
|
58 |
state.timing.accumulator += state.timing.deltaTime;
|
59 |
|
60 |
while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
|
61 |
-
FixedUpdate(state
|
62 |
state.timing.accumulator -= state.timing.fixedDeltaTime;
|
63 |
}
|
64 |
-
|
65 |
Render(state);
|
66 |
|
67 |
setTimeout(() => Tick(state), state.timing.frameDuration);
|
@@ -70,8 +67,8 @@ function GameLoop(state) {
|
|
70 |
Tick(state);
|
71 |
}
|
72 |
|
73 |
-
function FixedUpdate(
|
74 |
-
state.timing.time +=
|
75 |
}
|
76 |
|
77 |
function Render(state) {
|
@@ -81,7 +78,6 @@ function Render(state) {
|
|
81 |
const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
|
82 |
const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
|
83 |
RENDER_PASS_DESCRIPTOR.colorAttachments[0].view = state.webgpu.context.getCurrentTexture().createView();
|
84 |
-
|
85 |
const encoder = state.webgpu.device.createCommandEncoder();
|
86 |
const pass = encoder.beginRenderPass(RENDER_PASS_DESCRIPTOR);
|
87 |
pass.setPipeline(state.webgpu.pipeline);
|
|
|
1 |
+
// index.js
|
2 |
+
|
3 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
4 |
import { initializeWebGPU } from './wgpu-device.js';
|
5 |
import { createState } from './wgpu-state.js';
|
6 |
import { initializeTiming } from './wgpu-timing.js';
|
7 |
import { createPipeline } from './wgpu-pipeline.js';
|
8 |
import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
|
9 |
+
import { InitializeShaders } from './wgpu-shader.js';
|
10 |
+
import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
|
11 |
+
import { generateGlyphVerticesForText } from './wgpu-text.js';
|
12 |
import { config } from './wgpu-config.js';
|
13 |
import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
|
14 |
import { CreateBuffers } from './wgpu-buffer.js';
|
15 |
|
|
|
16 |
const state = createState(config);
|
17 |
|
18 |
async function Main() {
|
|
|
24 |
state.webgpu.context = context;
|
25 |
state.webgpu.presentationFormat = presentationFormat;
|
26 |
|
|
|
27 |
initializeTiming(state);
|
28 |
|
|
|
29 |
await InitializeShaders(state);
|
30 |
await InitializeResources(state);
|
31 |
|
|
|
32 |
GameLoop(state);
|
33 |
}
|
34 |
|
|
|
36 |
const vertexSize = config.floatsPerVertex * 4;
|
37 |
|
38 |
state.webgpu.pipeline = await createPipeline(state.webgpu.device, state.webgpu.presentationFormat, vertexSize, state.webgpu.shaderCode);
|
39 |
+
|
40 |
const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
|
41 |
document.body.appendChild(glyphCanvas);
|
42 |
glyphCanvas.style.backgroundColor = '#222';
|
|
|
47 |
}
|
48 |
|
49 |
function GameLoop(state) {
|
50 |
+
function Tick(state) {
|
51 |
state.timing.currentTime = performance.now();
|
52 |
state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
|
53 |
state.timing.lastTime = state.timing.currentTime;
|
|
|
54 |
state.timing.deltaTime = Math.min(state.timing.frameTime, state.timing.maxFrameTime);
|
55 |
state.timing.accumulator += state.timing.deltaTime;
|
56 |
|
57 |
while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
|
58 |
+
FixedUpdate(state);
|
59 |
state.timing.accumulator -= state.timing.fixedDeltaTime;
|
60 |
}
|
61 |
+
|
62 |
Render(state);
|
63 |
|
64 |
setTimeout(() => Tick(state), state.timing.frameDuration);
|
|
|
67 |
Tick(state);
|
68 |
}
|
69 |
|
70 |
+
function FixedUpdate(state) {
|
71 |
+
state.timing.time += state.timing.fixedDeltaTime;
|
72 |
}
|
73 |
|
74 |
function Render(state) {
|
|
|
78 |
const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
|
79 |
const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
|
80 |
RENDER_PASS_DESCRIPTOR.colorAttachments[0].view = state.webgpu.context.getCurrentTexture().createView();
|
|
|
81 |
const encoder = state.webgpu.device.createCommandEncoder();
|
82 |
const pass = encoder.beginRenderPass(RENDER_PASS_DESCRIPTOR);
|
83 |
pass.setPipeline(state.webgpu.pipeline);
|
wgpu-texture.js
CHANGED
@@ -25,7 +25,6 @@ export function GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVe
|
|
25 |
],
|
26 |
});
|
27 |
|
28 |
-
// Update state with glyph details
|
29 |
state.glyphs.numGlyphs = glyphData.numGlyphs;
|
30 |
state.glyphs.width = glyphData.width;
|
31 |
state.glyphs.height = glyphData.height;
|
|
|
25 |
],
|
26 |
});
|
27 |
|
|
|
28 |
state.glyphs.numGlyphs = glyphData.numGlyphs;
|
29 |
state.glyphs.width = glyphData.width;
|
30 |
state.glyphs.height = glyphData.height;
|