Spaces:
Running
Running
initialization of resources
Browse files
index.js
CHANGED
@@ -26,6 +26,15 @@ async function Main() {
|
|
26 |
|
27 |
state.device = device;
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
// Load shader code
|
30 |
const shaderCode = await fetchShaderCode('shaders.wgsl');
|
31 |
const vertexSize = config.floatsPerVertex * 4;
|
@@ -43,11 +52,9 @@ async function Main() {
|
|
43 |
|
44 |
// Generate vertex buffer data and texture
|
45 |
GenerateVertexDataAndTexture(glyphCanvas);
|
46 |
-
|
47 |
-
// Start the game loop
|
48 |
-
GameLoop(context);
|
49 |
}
|
50 |
|
|
|
51 |
function CreateBuffers() {
|
52 |
const vertexBufferSize = config.maxGlyphs * config.vertsPerGlyph * config.floatsPerVertex * 4;
|
53 |
state.vertexBuffer = state.device.createBuffer({
|
@@ -66,6 +73,7 @@ function CreateBuffers() {
|
|
66 |
state.device.queue.writeBuffer(state.indexBuffer, 0, new Uint32Array(indices));
|
67 |
}
|
68 |
|
|
|
69 |
function GenerateIndices(maxGlyphs) {
|
70 |
// Generate index array for glyphs
|
71 |
return Array.from({ length: maxGlyphs * 6 }, (_, i) => {
|
@@ -74,6 +82,7 @@ function GenerateIndices(maxGlyphs) {
|
|
74 |
});
|
75 |
}
|
76 |
|
|
|
77 |
function GenerateVertexDataAndTexture(glyphCanvas) {
|
78 |
const glyphData = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS, config, glyphCanvas);
|
79 |
state.device.queue.writeBuffer(state.vertexBuffer, 0, glyphData.vertexData);
|
@@ -107,6 +116,7 @@ function GenerateVertexDataAndTexture(glyphCanvas) {
|
|
107 |
state.height = glyphData.height;
|
108 |
}
|
109 |
|
|
|
110 |
function GameLoop(context) {
|
111 |
let lastTime = performance.now();
|
112 |
let accumulator = 0;
|
@@ -136,11 +146,13 @@ function GameLoop(context) {
|
|
136 |
Tick();
|
137 |
}
|
138 |
|
|
|
139 |
function FixedUpdate(deltaTime) {
|
140 |
state.time += deltaTime;
|
141 |
// Perform game logic updates here, such as physics and AI
|
142 |
}
|
143 |
|
|
|
144 |
function Render(alpha, context) {
|
145 |
// Set up projection and view matrices
|
146 |
const fov = 60 * Math.PI / 180;
|
@@ -164,4 +176,6 @@ function Render(alpha, context) {
|
|
164 |
state.device.queue.submit([encoder.finish()]);
|
165 |
}
|
166 |
|
|
|
167 |
Main();
|
|
|
|
26 |
|
27 |
state.device = device;
|
28 |
|
29 |
+
// Initialize Resources
|
30 |
+
await InitializeResources(presentationFormat);
|
31 |
+
|
32 |
+
// Start the game loop
|
33 |
+
GameLoop(context);
|
34 |
+
}
|
35 |
+
|
36 |
+
// Initialize shaders, pipeline, textures, and buffers
|
37 |
+
async function InitializeResources(presentationFormat) {
|
38 |
// Load shader code
|
39 |
const shaderCode = await fetchShaderCode('shaders.wgsl');
|
40 |
const vertexSize = config.floatsPerVertex * 4;
|
|
|
52 |
|
53 |
// Generate vertex buffer data and texture
|
54 |
GenerateVertexDataAndTexture(glyphCanvas);
|
|
|
|
|
|
|
55 |
}
|
56 |
|
57 |
+
// Function to create vertex and index buffers
|
58 |
function CreateBuffers() {
|
59 |
const vertexBufferSize = config.maxGlyphs * config.vertsPerGlyph * config.floatsPerVertex * 4;
|
60 |
state.vertexBuffer = state.device.createBuffer({
|
|
|
73 |
state.device.queue.writeBuffer(state.indexBuffer, 0, new Uint32Array(indices));
|
74 |
}
|
75 |
|
76 |
+
// Function to generate indices for glyphs
|
77 |
function GenerateIndices(maxGlyphs) {
|
78 |
// Generate index array for glyphs
|
79 |
return Array.from({ length: maxGlyphs * 6 }, (_, i) => {
|
|
|
82 |
});
|
83 |
}
|
84 |
|
85 |
+
// Function to generate vertex data and texture
|
86 |
function GenerateVertexDataAndTexture(glyphCanvas) {
|
87 |
const glyphData = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS, config, glyphCanvas);
|
88 |
state.device.queue.writeBuffer(state.vertexBuffer, 0, glyphData.vertexData);
|
|
|
116 |
state.height = glyphData.height;
|
117 |
}
|
118 |
|
119 |
+
// Game loop function
|
120 |
function GameLoop(context) {
|
121 |
let lastTime = performance.now();
|
122 |
let accumulator = 0;
|
|
|
146 |
Tick();
|
147 |
}
|
148 |
|
149 |
+
// Fixed update function for game logic
|
150 |
function FixedUpdate(deltaTime) {
|
151 |
state.time += deltaTime;
|
152 |
// Perform game logic updates here, such as physics and AI
|
153 |
}
|
154 |
|
155 |
+
// Render function
|
156 |
function Render(alpha, context) {
|
157 |
// Set up projection and view matrices
|
158 |
const fov = 60 * Math.PI / 180;
|
|
|
176 |
state.device.queue.submit([encoder.finish()]);
|
177 |
}
|
178 |
|
179 |
+
// Initialize application
|
180 |
Main();
|
181 |
+
|