Spaces:
Running
Running
extrapolate index.render
Browse files
index.js
CHANGED
@@ -53,7 +53,7 @@ async function main() {
|
|
53 |
});
|
54 |
state.device.queue.writeBuffer(state.indexBuffer, 0, new Uint32Array(indices));
|
55 |
|
56 |
-
const { vertexData, numGlyphs, width, height } = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS, glyphCanvas);
|
57 |
state.device.queue.writeBuffer(state.vertexBuffer, 0, vertexData);
|
58 |
|
59 |
state.texture = createTextureFromSource(state.device, glyphCanvas, { mips: true });
|
@@ -83,10 +83,10 @@ async function main() {
|
|
83 |
state.width = width;
|
84 |
state.height = height;
|
85 |
|
86 |
-
requestAnimationFrame((time) => render(time, context, state, RENDER_PASS_DESCRIPTOR));
|
87 |
}
|
88 |
|
89 |
-
function render(time, context, state, RENDER_PASS_DESCRIPTOR) {
|
90 |
time *= config.time.phase;
|
91 |
const fov = 60 * Math.PI / 180;
|
92 |
const aspect = canvas.clientWidth / canvas.clientHeight;
|
@@ -106,10 +106,10 @@ function render(time, context, state, RENDER_PASS_DESCRIPTOR) {
|
|
106 |
pass.drawIndexed(state.numGlyphs * 6);
|
107 |
pass.end();
|
108 |
state.device.queue.submit([encoder.finish()]);
|
109 |
-
requestAnimationFrame((t) => render(t, context, state, RENDER_PASS_DESCRIPTOR));
|
110 |
}
|
111 |
|
112 |
-
function generateGlyphVerticesForText(
|
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;
|
@@ -121,8 +121,8 @@ function generateGlyphVerticesForText(s, COLORS, glyphCanvas) {
|
|
121 |
offset += 8;
|
122 |
};
|
123 |
|
124 |
-
for (let i = 0; i <
|
125 |
-
const c =
|
126 |
if (c >= 33) {
|
127 |
const cIndex = c - 33;
|
128 |
const glyphX = cIndex % config.glyphsAcrossTexture;
|
@@ -132,12 +132,12 @@ function generateGlyphVerticesForText(s, COLORS, glyphCanvas) {
|
|
132 |
const u1 = u0 + glyphUVWidth;
|
133 |
const v0 = v1 + glyphUVHeight;
|
134 |
width = Math.max(x1, width);
|
135 |
-
addVertex(x0, y0, u0, v0,
|
136 |
-
addVertex(x1, y0, u1, v0,
|
137 |
-
addVertex(x0, y1, u0, v1,
|
138 |
-
addVertex(x1, y1, u1, v1,
|
139 |
} else {
|
140 |
-
colorNdx = (colorNdx + 1) %
|
141 |
if (c === 10) { // Newline
|
142 |
x0 = 0; x1 = 1; y0--; y1 = y0 + 1;
|
143 |
continue;
|
|
|
53 |
});
|
54 |
state.device.queue.writeBuffer(state.indexBuffer, 0, new Uint32Array(indices));
|
55 |
|
56 |
+
const { vertexData, numGlyphs, width, height } = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS, config, glyphCanvas);
|
57 |
state.device.queue.writeBuffer(state.vertexBuffer, 0, vertexData);
|
58 |
|
59 |
state.texture = createTextureFromSource(state.device, glyphCanvas, { mips: true });
|
|
|
83 |
state.width = width;
|
84 |
state.height = height;
|
85 |
|
86 |
+
requestAnimationFrame((time) => render(time, mat4, context, state, RENDER_PASS_DESCRIPTOR));
|
87 |
}
|
88 |
|
89 |
+
function render(time, mat4, context, state, RENDER_PASS_DESCRIPTOR) {
|
90 |
time *= config.time.phase;
|
91 |
const fov = 60 * Math.PI / 180;
|
92 |
const aspect = canvas.clientWidth / canvas.clientHeight;
|
|
|
106 |
pass.drawIndexed(state.numGlyphs * 6);
|
107 |
pass.end();
|
108 |
state.device.queue.submit([encoder.finish()]);
|
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;
|
|
|
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;
|
|
|
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;
|