p3nGu1nZz commited on
Commit
0741357
1 Parent(s): 0f567ca

created `wgpu-text`

Browse files
Files changed (2) hide show
  1. index.js +1 -39
  2. wgpu-text.js +40 -0
index.js CHANGED
@@ -6,6 +6,7 @@ import { config } from './wgpu-config.js';
6
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
7
  import { createPipeline } from './wgpu-pipeline.js';
8
  import { createState } from './wgpu-state.js';
 
9
 
10
  const canvas = document.querySelector('canvas');
11
  const context = canvas.getContext('webgpu');
@@ -109,43 +110,4 @@ function render(time, mat4, context, state, RENDER_PASS_DESCRIPTOR) {
109
  requestAnimationFrame((t) => render(t, mat4, context, state, RENDER_PASS_DESCRIPTOR));
110
  }
111
 
112
- function generateGlyphVerticesForText(text, colors, config, glyphCanvas) {
113
- const vertexData = new Float32Array(config.maxGlyphs * config.floatsPerVertex * config.vertsPerGlyph);
114
- const glyphUVWidth = config.glyphWidth / glyphCanvas.width;
115
- const glyphUVHeight = config.glyphHeight / glyphCanvas.height;
116
- let offset = 0, x0 = 0, y0 = 0, x1 = 1, y1 = 1, width = 0;
117
- let colorNdx = 0;
118
-
119
- const addVertex = (x, y, u, v, color) => {
120
- vertexData.set([x, y, u, v, ...color], offset);
121
- offset += 8;
122
- };
123
-
124
- for (let i = 0; i < text.length; ++i) {
125
- const c = text.charCodeAt(i);
126
- if (c >= 33) {
127
- const cIndex = c - 33;
128
- const glyphX = cIndex % config.glyphsAcrossTexture;
129
- const glyphY = Math.floor(cIndex / config.glyphsAcrossTexture);
130
- const u0 = glyphX * config.glyphWidth / glyphCanvas.width;
131
- const v1 = glyphY * config.glyphHeight / glyphCanvas.height;
132
- const u1 = u0 + glyphUVWidth;
133
- const v0 = v1 + glyphUVHeight;
134
- width = Math.max(x1, width);
135
- addVertex(x0, y0, u0, v0, colors[colorNdx]);
136
- addVertex(x1, y0, u1, v0, colors[colorNdx]);
137
- addVertex(x0, y1, u0, v1, colors[colorNdx]);
138
- addVertex(x1, y1, u1, v1, colors[colorNdx]);
139
- } else {
140
- colorNdx = (colorNdx + 1) % colors.length;
141
- if (c === 10) { // Newline
142
- x0 = 0; x1 = 1; y0--; y1 = y0 + 1;
143
- continue;
144
- }
145
- }
146
- x0 += 0.55; x1 = x0 + 1;
147
- }
148
- return { vertexData, numGlyphs: offset / config.floatsPerVertex, width, height: y1 };
149
- }
150
-
151
  main();
 
6
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
7
  import { createPipeline } from './wgpu-pipeline.js';
8
  import { createState } from './wgpu-state.js';
9
+ import { generateGlyphVerticesForText } from './wgpu-text.js';
10
 
11
  const canvas = document.querySelector('canvas');
12
  const context = canvas.getContext('webgpu');
 
110
  requestAnimationFrame((t) => render(t, mat4, context, state, RENDER_PASS_DESCRIPTOR));
111
  }
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  main();
wgpu-text.js ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // wgpu-text.js
2
+
3
+ export function generateGlyphVerticesForText(text, colors, config, glyphCanvas) {
4
+ const vertexData = new Float32Array(config.maxGlyphs * config.floatsPerVertex * config.vertsPerGlyph);
5
+ const glyphUVWidth = config.glyphWidth / glyphCanvas.width;
6
+ const glyphUVHeight = config.glyphHeight / glyphCanvas.height;
7
+ let offset = 0, x0 = 0, y0 = 0, x1 = 1, y1 = 1, width = 0;
8
+ let colorNdx = 0;
9
+
10
+ const addVertex = (x, y, u, v, color) => {
11
+ vertexData.set([x, y, u, v, ...color], offset);
12
+ offset += 8;
13
+ };
14
+
15
+ for (let i = 0; i < text.length; ++i) {
16
+ const c = text.charCodeAt(i);
17
+ if (c >= 33) {
18
+ const cIndex = c - 33;
19
+ const glyphX = cIndex % config.glyphsAcrossTexture;
20
+ const glyphY = Math.floor(cIndex / config.glyphsAcrossTexture);
21
+ const u0 = glyphX * config.glyphWidth / glyphCanvas.width;
22
+ const v1 = glyphY * config.glyphHeight / glyphCanvas.height;
23
+ const u1 = u0 + glyphUVWidth;
24
+ const v0 = v1 + glyphUVHeight;
25
+ width = Math.max(x1, width);
26
+ addVertex(x0, y0, u0, v0, colors[colorNdx]);
27
+ addVertex(x1, y0, u1, v0, colors[colorNdx]);
28
+ addVertex(x0, y1, u0, v1, colors[colorNdx]);
29
+ addVertex(x1, y1, u1, v1, colors[colorNdx]);
30
+ } else {
31
+ colorNdx = (colorNdx + 1) % colors.length;
32
+ if (c === 10) { // Newline
33
+ x0 = 0; x1 = 1; y0--; y1 = y0 + 1;
34
+ continue;
35
+ }
36
+ }
37
+ x0 += 0.55; x1 = x0 + 1;
38
+ }
39
+ return { vertexData, numGlyphs: offset / config.floatsPerVertex, width, height: y1 };
40
+ }