p3nGu1nZz commited on
Commit
922216d
1 Parent(s): 6af9568

🚀Organize and streamline imports

Browse files
Files changed (2) hide show
  1. LICENSE +1 -1
  2. index.js +47 -48
LICENSE CHANGED
@@ -1,6 +1,6 @@
1
  MIT License
2
 
3
- Copyright (c) 2024 NaXzyu
4
 
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
  of this software and associated documentation files (the "Software"), to deal
 
1
  MIT License
2
 
3
+ Copyright (c) 2024 p3nGu1nZz
4
 
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
  of this software and associated documentation files (the "Software"), to deal
index.js CHANGED
@@ -1,28 +1,29 @@
 
 
1
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
2
- import { initializeDevice } 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 { InitializeShaders } 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
  // Initialize Canvas function
15
- async function InitializeCanvas(config) {
16
- // Create and configure the canvas element
17
  const canvas = document.createElement('canvas');
18
  canvas.width = config.canvas.width;
19
  canvas.height = config.canvas.height;
20
  document.body.appendChild(canvas);
21
 
22
- // Request WebGPU adapter and device
23
  const adapter = await navigator.gpu.requestAdapter();
24
-
25
- // Use the existing initializeDevice function
26
  const { device, context, presentationFormat } = await initializeDevice(navigator, adapter, canvas);
27
 
28
  if (!device) {
@@ -30,26 +31,10 @@ async function InitializeCanvas(config) {
30
  return;
31
  }
32
 
33
- // Return essential components for further setup
34
- return { canvas, device, context, presentationFormat };
35
- }
36
-
37
- // Main function
38
- const state = createState(config);
39
-
40
- async function Main() {
41
- const { canvas, device, context, presentationFormat } = await InitializeCanvas(config);
42
  state.canvas = canvas;
43
  state.webgpu.device = device;
44
  state.webgpu.context = context;
45
  state.webgpu.presentationFormat = presentationFormat;
46
-
47
- initializeTiming(state);
48
-
49
- await InitializeShaders(state);
50
- await InitializeResources(state);
51
-
52
- GameLoop(state);
53
  }
54
 
55
  // Initialize Resources function
@@ -63,32 +48,9 @@ async function InitializeResources(state) {
63
  glyphCanvas.style.backgroundColor = '#222';
64
 
65
  CreateBuffers(state, config);
66
-
67
  GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
68
  }
69
 
70
- // Game Loop function
71
- function GameLoop(state) {
72
- function Tick(state) {
73
- state.timing.currentTime = performance.now();
74
- state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
75
- state.timing.lastTime = state.timing.currentTime;
76
- state.timing.deltaTime = Math.min(state.timing.frameTime, state.timing.maxFrameTime);
77
- state.timing.accumulator += state.timing.deltaTime;
78
-
79
- while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
80
- FixedUpdate(state);
81
- state.timing.accumulator -= state.timing.fixedDeltaTime;
82
- }
83
-
84
- Render(state);
85
-
86
- setTimeout(() => Tick(state), state.timing.frameDuration);
87
- }
88
-
89
- Tick(state);
90
- }
91
-
92
  // Fixed Update function
93
  function FixedUpdate(state) {
94
  state.timing.time += state.timing.fixedDeltaTime;
@@ -101,20 +63,57 @@ function Render(state) {
101
  const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
102
  const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
103
  const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
 
104
  RENDER_PASS_DESCRIPTOR.colorAttachments[0].view = state.webgpu.context.getCurrentTexture().createView();
105
  const encoder = state.webgpu.device.createCommandEncoder();
106
  const pass = encoder.beginRenderPass(RENDER_PASS_DESCRIPTOR);
 
107
  pass.setPipeline(state.webgpu.pipeline);
108
  mat4.rotateY(viewProjectionMatrix, state.timing.time, state.matrices.matrix);
109
  mat4.translate(state.matrices.matrix, [-state.glyphs.width / 2, -state.glyphs.height / 2, 0], state.matrices.matrix);
 
110
  state.webgpu.device.queue.writeBuffer(state.webgpu.uniformBuffer, 0, state.matrices.uniformValues);
 
111
  pass.setBindGroup(0, state.webgpu.bindGroup);
112
  pass.setVertexBuffer(0, state.webgpu.vertexBuffer);
113
  pass.setIndexBuffer(state.webgpu.indexBuffer, 'uint32');
114
  pass.drawIndexed(state.glyphs.numGlyphs * 6);
115
  pass.end();
 
116
  state.webgpu.device.queue.submit([encoder.finish()]);
117
  }
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  // Start the main function
120
  Main();
 
1
+ // index.js
2
+
3
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
4
+
5
+ import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
6
+ import { config } from './wgpu-config.js';
7
+
8
  import { createState } from './wgpu-state.js';
9
+ import { initializeDevice } from './wgpu-device.js';
10
+ import { CreateBuffers } from './wgpu-buffer.js';
11
  import { initializeTiming } from './wgpu-timing.js';
12
  import { createPipeline } from './wgpu-pipeline.js';
13
+
14
  import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
15
  import { InitializeShaders } from './wgpu-shader.js';
16
  import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
17
  import { generateGlyphVerticesForText } from './wgpu-text.js';
 
 
 
18
 
19
  // Initialize Canvas function
20
+ async function InitializeCanvas(state) {
 
21
  const canvas = document.createElement('canvas');
22
  canvas.width = config.canvas.width;
23
  canvas.height = config.canvas.height;
24
  document.body.appendChild(canvas);
25
 
 
26
  const adapter = await navigator.gpu.requestAdapter();
 
 
27
  const { device, context, presentationFormat } = await initializeDevice(navigator, adapter, canvas);
28
 
29
  if (!device) {
 
31
  return;
32
  }
33
 
 
 
 
 
 
 
 
 
 
34
  state.canvas = canvas;
35
  state.webgpu.device = device;
36
  state.webgpu.context = context;
37
  state.webgpu.presentationFormat = presentationFormat;
 
 
 
 
 
 
 
38
  }
39
 
40
  // Initialize Resources function
 
48
  glyphCanvas.style.backgroundColor = '#222';
49
 
50
  CreateBuffers(state, config);
 
51
  GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
52
  }
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  // Fixed Update function
55
  function FixedUpdate(state) {
56
  state.timing.time += state.timing.fixedDeltaTime;
 
63
  const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
64
  const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
65
  const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
66
+
67
  RENDER_PASS_DESCRIPTOR.colorAttachments[0].view = state.webgpu.context.getCurrentTexture().createView();
68
  const encoder = state.webgpu.device.createCommandEncoder();
69
  const pass = encoder.beginRenderPass(RENDER_PASS_DESCRIPTOR);
70
+
71
  pass.setPipeline(state.webgpu.pipeline);
72
  mat4.rotateY(viewProjectionMatrix, state.timing.time, state.matrices.matrix);
73
  mat4.translate(state.matrices.matrix, [-state.glyphs.width / 2, -state.glyphs.height / 2, 0], state.matrices.matrix);
74
+
75
  state.webgpu.device.queue.writeBuffer(state.webgpu.uniformBuffer, 0, state.matrices.uniformValues);
76
+
77
  pass.setBindGroup(0, state.webgpu.bindGroup);
78
  pass.setVertexBuffer(0, state.webgpu.vertexBuffer);
79
  pass.setIndexBuffer(state.webgpu.indexBuffer, 'uint32');
80
  pass.drawIndexed(state.glyphs.numGlyphs * 6);
81
  pass.end();
82
+
83
  state.webgpu.device.queue.submit([encoder.finish()]);
84
  }
85
 
86
+ // Game Loop function
87
+ function GameLoop(state) {
88
+ function Tick() {
89
+ state.timing.currentTime = performance.now();
90
+ state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
91
+ state.timing.lastTime = state.timing.currentTime;
92
+ state.timing.deltaTime = Math.min(state.timing.frameTime, state.timing.maxFrameTime);
93
+ state.timing.accumulator += state.timing.deltaTime;
94
+
95
+ while (state.timing.accumulator >= state.timing.fixedDeltaTime) {
96
+ FixedUpdate(state);
97
+ state.timing.accumulator -= state.timing.fixedDeltaTime;
98
+ }
99
+
100
+ Render(state);
101
+ setTimeout(Tick, state.timing.frameDuration);
102
+ }
103
+
104
+ Tick();
105
+ }
106
+
107
+ // Main function
108
+ const state = createState(config);
109
+
110
+ async function Main() {
111
+ await InitializeCanvas(state);
112
+ initializeTiming(state);
113
+ await InitializeShaders(state);
114
+ await InitializeResources(state);
115
+ GameLoop(state);
116
+ }
117
+
118
  // Start the main function
119
  Main();