p3nGu1nZz commited on
Commit
551d60d
1 Parent(s): e31d2a8

refactor config

Browse files
Files changed (2) hide show
  1. config.js +1 -1
  2. index.js +18 -18
config.js CHANGED
@@ -1,6 +1,6 @@
1
  // config.js
2
 
3
- export const CONFIG = {
4
  glyphsAcrossTexture: 16,
5
  glyphWidth: 32,
6
  glyphHeight: 40,
 
1
  // config.js
2
 
3
+ export const config = {
4
  glyphsAcrossTexture: 16,
5
  glyphWidth: 32,
6
  glyphHeight: 40,
index.js CHANGED
@@ -1,6 +1,6 @@
1
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
2
  import { fetchShaderCode, generateGlyphTextureAtlas } from './utility.js';
3
- import { CONFIG } from './config.js';
4
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './constants.js';
5
 
6
  const canvas = document.querySelector('canvas');
@@ -34,12 +34,12 @@ async function main() {
34
  code: shaderCode,
35
  });
36
 
37
- const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, CONFIG);
38
  document.body.appendChild(glyphCanvas);
39
  glyphCanvas.style.backgroundColor = '#222';
40
 
41
- const vertexSize = CONFIG.floatsPerVertex * 4;
42
- const vertexBufferSize = CONFIG.maxGlyphs * CONFIG.vertsPerGlyph * vertexSize;
43
  vertexBuffer = device.createBuffer({
44
  label: 'vertices',
45
  size: vertexBufferSize,
@@ -48,11 +48,11 @@ async function main() {
48
 
49
  indexBuffer = device.createBuffer({
50
  label: 'indices',
51
- size: CONFIG.maxGlyphs * CONFIG.vertsPerGlyph * 4,
52
  usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
53
  });
54
 
55
- const indices = Array.from({ length: CONFIG.maxGlyphs * 6 }, (_, i) => {
56
  const ndx = Math.floor(i / 6) * 4;
57
  return (i % 6 < 3 ? [ndx, ndx + 1, ndx + 2] : [ndx + 2, ndx + 1, ndx + 3])[i % 3];
58
  });
@@ -99,11 +99,11 @@ async function main() {
99
 
100
  uniformBuffer = device.createBuffer({
101
  label: 'uniforms for quad',
102
- size: CONFIG.uniformBufferSize,
103
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
104
  });
105
 
106
- const uniformValues = new Float32Array(CONFIG.uniformBufferSize / 4);
107
  const matrix = uniformValues.subarray(0, 16);
108
 
109
  bindGroup = device.createBindGroup({
@@ -116,10 +116,10 @@ async function main() {
116
  });
117
 
118
  function render(time, context, pipeline, uniformBuffer, uniformValues, bindGroup, vertexBuffer, indexBuffer, RENDER_PASS_DESCRIPTOR) {
119
- time *= CONFIG.time.phase;
120
  const fov = 60 * Math.PI / 180;
121
  const aspect = canvas.clientWidth / canvas.clientHeight;
122
- const projectionMatrix = mat4.perspective(fov, aspect, CONFIG.render.zNear, CONFIG.render.zFar);
123
  const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
124
  const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
125
  RENDER_PASS_DESCRIPTOR.colorAttachments[0].view = context.getCurrentTexture().createView();
@@ -142,9 +142,9 @@ async function main() {
142
  }
143
 
144
  function generateGlyphVerticesForText(s, COLORS, glyphCanvas) {
145
- const vertexData = new Float32Array(CONFIG.maxGlyphs * CONFIG.floatsPerVertex * CONFIG.vertsPerGlyph);
146
- const glyphUVWidth = CONFIG.glyphWidth / glyphCanvas.width;
147
- const glyphUVHeight = CONFIG.glyphHeight / glyphCanvas.height;
148
  let offset = 0, x0 = 0, y0 = 0, x1 = 1, y1 = 1, width = 0;
149
  let colorNdx = 0;
150
 
@@ -157,10 +157,10 @@ function generateGlyphVerticesForText(s, COLORS, glyphCanvas) {
157
  const c = s.charCodeAt(i);
158
  if (c >= 33) {
159
  const cIndex = c - 33;
160
- const glyphX = cIndex % CONFIG.glyphsAcrossTexture;
161
- const glyphY = Math.floor(cIndex / CONFIG.glyphsAcrossTexture);
162
- const u0 = glyphX * CONFIG.glyphWidth / glyphCanvas.width;
163
- const v1 = glyphY * CONFIG.glyphHeight / glyphCanvas.height;
164
  const u1 = u0 + glyphUVWidth;
165
  const v0 = v1 + glyphUVHeight;
166
  width = Math.max(x1, width);
@@ -177,7 +177,7 @@ function generateGlyphVerticesForText(s, COLORS, glyphCanvas) {
177
  }
178
  x0 += 0.55; x1 = x0 + 1;
179
  }
180
- return { vertexData, numGlyphs: offset / CONFIG.floatsPerVertex, width, height: y1 };
181
  }
182
 
183
  function createTextureFromSource(device, source, options = {}) {
 
1
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
2
  import { fetchShaderCode, generateGlyphTextureAtlas } from './utility.js';
3
+ import { config } from './config.js';
4
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './constants.js';
5
 
6
  const canvas = document.querySelector('canvas');
 
34
  code: shaderCode,
35
  });
36
 
37
+ const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
38
  document.body.appendChild(glyphCanvas);
39
  glyphCanvas.style.backgroundColor = '#222';
40
 
41
+ const vertexSize = config.floatsPerVertex * 4;
42
+ const vertexBufferSize = config.maxGlyphs * config.vertsPerGlyph * vertexSize;
43
  vertexBuffer = device.createBuffer({
44
  label: 'vertices',
45
  size: vertexBufferSize,
 
48
 
49
  indexBuffer = device.createBuffer({
50
  label: 'indices',
51
+ size: config.maxGlyphs * config.vertsPerGlyph * 4,
52
  usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
53
  });
54
 
55
+ const indices = Array.from({ length: config.maxGlyphs * 6 }, (_, i) => {
56
  const ndx = Math.floor(i / 6) * 4;
57
  return (i % 6 < 3 ? [ndx, ndx + 1, ndx + 2] : [ndx + 2, ndx + 1, ndx + 3])[i % 3];
58
  });
 
99
 
100
  uniformBuffer = device.createBuffer({
101
  label: 'uniforms for quad',
102
+ size: config.uniformBufferSize,
103
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
104
  });
105
 
106
+ const uniformValues = new Float32Array(config.uniformBufferSize / 4);
107
  const matrix = uniformValues.subarray(0, 16);
108
 
109
  bindGroup = device.createBindGroup({
 
116
  });
117
 
118
  function render(time, context, pipeline, uniformBuffer, uniformValues, bindGroup, vertexBuffer, indexBuffer, RENDER_PASS_DESCRIPTOR) {
119
+ time *= config.time.phase;
120
  const fov = 60 * Math.PI / 180;
121
  const aspect = canvas.clientWidth / canvas.clientHeight;
122
+ const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
123
  const viewMatrix = mat4.lookAt([0, 0, 5], [0, 0, 0], [0, 1, 0]);
124
  const viewProjectionMatrix = mat4.multiply(projectionMatrix, viewMatrix);
125
  RENDER_PASS_DESCRIPTOR.colorAttachments[0].view = context.getCurrentTexture().createView();
 
142
  }
143
 
144
  function generateGlyphVerticesForText(s, COLORS, glyphCanvas) {
145
+ const vertexData = new Float32Array(config.maxGlyphs * config.floatsPerVertex * config.vertsPerGlyph);
146
+ const glyphUVWidth = config.glyphWidth / glyphCanvas.width;
147
+ const glyphUVHeight = config.glyphHeight / glyphCanvas.height;
148
  let offset = 0, x0 = 0, y0 = 0, x1 = 1, y1 = 1, width = 0;
149
  let colorNdx = 0;
150
 
 
157
  const c = s.charCodeAt(i);
158
  if (c >= 33) {
159
  const cIndex = c - 33;
160
+ const glyphX = cIndex % config.glyphsAcrossTexture;
161
+ const glyphY = Math.floor(cIndex / config.glyphsAcrossTexture);
162
+ const u0 = glyphX * config.glyphWidth / glyphCanvas.width;
163
+ const v1 = glyphY * config.glyphHeight / glyphCanvas.height;
164
  const u1 = u0 + glyphUVWidth;
165
  const v0 = v1 + glyphUVHeight;
166
  width = Math.max(x1, width);
 
177
  }
178
  x0 += 0.55; x1 = x0 + 1;
179
  }
180
+ return { vertexData, numGlyphs: offset / config.floatsPerVertex, width, height: y1 };
181
  }
182
 
183
  function createTextureFromSource(device, source, options = {}) {