Spaces:
Running
Running
add to constants
Browse files- constants.js +9 -0
- index.html +9 -14
constants.js
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// constants.js
|
2 |
+
|
3 |
+
export const COLORS = [
|
4 |
+
[1, 1, 0, 1],
|
5 |
+
[0, 1, 1, 1],
|
6 |
+
[1, 0, 1, 1],
|
7 |
+
[1, 0, 0, 1],
|
8 |
+
[0, 0.5, 1, 1]
|
9 |
+
];
|
index.html
CHANGED
@@ -13,6 +13,8 @@
|
|
13 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
14 |
import { fetchShaderCode } from './utility.js';
|
15 |
import { config } from './config.js';
|
|
|
|
|
16 |
function generateGlyphTextureAtlas() {
|
17 |
const canvas = document.createElement('canvas');
|
18 |
canvas.width = 512;
|
@@ -73,7 +75,7 @@
|
|
73 |
});
|
74 |
device.queue.writeBuffer(indexBuffer, 0, new Uint32Array(indices));
|
75 |
|
76 |
-
function generateGlyphVerticesForText(s,
|
77 |
const vertexData = new Float32Array(config.maxGlyphs * config.floatsPerVertex * config.vertsPerGlyph);
|
78 |
const glyphUVWidth = config.glyphWidth / glyphCanvas.width;
|
79 |
const glyphUVHeight = config.glyphHeight / glyphCanvas.height;
|
@@ -97,12 +99,12 @@
|
|
97 |
const v0 = v1 + glyphUVHeight;
|
98 |
width = Math.max(x1, width);
|
99 |
|
100 |
-
addVertex(x0, y0, u0, v0,
|
101 |
-
addVertex(x1, y0, u1, v0,
|
102 |
-
addVertex(x0, y1, u0, v1,
|
103 |
-
addVertex(x1, y1, u1, v1,
|
104 |
} else {
|
105 |
-
colorNdx = (colorNdx + 1) %
|
106 |
if (c === 10) { // Newline character
|
107 |
x0 = 0; x1 = 1; y0--; y1 = y0 + 1;
|
108 |
continue;
|
@@ -113,14 +115,7 @@
|
|
113 |
return { vertexData, numGlyphs: offset / config.floatsPerVertex, width, height: y1 };
|
114 |
}
|
115 |
|
116 |
-
const
|
117 |
-
[1, 1, 0, 1],
|
118 |
-
[0, 1, 1, 1],
|
119 |
-
[1, 0, 1, 1],
|
120 |
-
[1, 0, 0, 1],
|
121 |
-
[0, .5, 1, 1]
|
122 |
-
];
|
123 |
-
const { vertexData, numGlyphs, width, height } = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', colors);
|
124 |
device.queue.writeBuffer(vertexBuffer, 0, vertexData);
|
125 |
|
126 |
const pipeline = device.createRenderPipeline({
|
|
|
13 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
14 |
import { fetchShaderCode } from './utility.js';
|
15 |
import { config } from './config.js';
|
16 |
+
import { COLORS } from './constants.js';
|
17 |
+
|
18 |
function generateGlyphTextureAtlas() {
|
19 |
const canvas = document.createElement('canvas');
|
20 |
canvas.width = 512;
|
|
|
75 |
});
|
76 |
device.queue.writeBuffer(indexBuffer, 0, new Uint32Array(indices));
|
77 |
|
78 |
+
function generateGlyphVerticesForText(s, COLORS = [[1, 1, 1, 1]]) {
|
79 |
const vertexData = new Float32Array(config.maxGlyphs * config.floatsPerVertex * config.vertsPerGlyph);
|
80 |
const glyphUVWidth = config.glyphWidth / glyphCanvas.width;
|
81 |
const glyphUVHeight = config.glyphHeight / glyphCanvas.height;
|
|
|
99 |
const v0 = v1 + glyphUVHeight;
|
100 |
width = Math.max(x1, width);
|
101 |
|
102 |
+
addVertex(x0, y0, u0, v0, COLORS[colorNdx]);
|
103 |
+
addVertex(x1, y0, u1, v0, COLORS[colorNdx]);
|
104 |
+
addVertex(x0, y1, u0, v1, COLORS[colorNdx]);
|
105 |
+
addVertex(x1, y1, u1, v1, COLORS[colorNdx]);
|
106 |
} else {
|
107 |
+
colorNdx = (colorNdx + 1) % COLORS.length;
|
108 |
if (c === 10) { // Newline character
|
109 |
x0 = 0; x1 = 1; y0--; y1 = y0 + 1;
|
110 |
continue;
|
|
|
115 |
return { vertexData, numGlyphs: offset / config.floatsPerVertex, width, height: y1 };
|
116 |
}
|
117 |
|
118 |
+
const { vertexData, numGlyphs, width, height } = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
device.queue.writeBuffer(vertexBuffer, 0, vertexData);
|
120 |
|
121 |
const pipeline = device.createRenderPipeline({
|