Spaces:
Running
Running
utility.js
Browse files- index.html +25 -26
- utility.js +1 -0
index.html
CHANGED
@@ -11,25 +11,10 @@
|
|
11 |
<canvas></canvas>
|
12 |
<script type="module">
|
13 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
|
|
14 |
const glyphWidth = 32;
|
15 |
const glyphHeight = 40;
|
16 |
const glyphsAcrossTexture = 16;
|
17 |
-
function genreateGlyphTextureAtlas() {
|
18 |
-
const canvas = document.createElement('canvas');
|
19 |
-
canvas.width = 512;
|
20 |
-
canvas.height = 256;
|
21 |
-
const ctx = canvas.getContext('2d');
|
22 |
-
ctx.font = '32px monospace';
|
23 |
-
ctx.textBaseline = 'middle';
|
24 |
-
ctx.textAlign = 'center';
|
25 |
-
ctx.fillStyle = 'white';
|
26 |
-
for (let c = 33, x = 0, y = 0; c < 128; ++c) {
|
27 |
-
ctx.fillText(String.fromCodePoint(c), x + glyphWidth / 2, y + glyphHeight / 2);
|
28 |
-
x = (x + glyphWidth) % canvas.width;
|
29 |
-
if (x === 0) y += glyphHeight;
|
30 |
-
}
|
31 |
-
return canvas;
|
32 |
-
}
|
33 |
async function main() {
|
34 |
const adapter = await navigator.gpu?.requestAdapter();
|
35 |
const device = await adapter?.requestDevice();
|
@@ -58,7 +43,7 @@
|
|
58 |
code: shaderCode,
|
59 |
});
|
60 |
|
61 |
-
const glyphCanvas =
|
62 |
document.body.appendChild(glyphCanvas);
|
63 |
glyphCanvas.style.backgroundColor = '#222';
|
64 |
|
@@ -164,21 +149,19 @@
|
|
164 |
},
|
165 |
});
|
166 |
|
167 |
-
function copySourceToTexture(device, texture, source, { flipY } = {}) {
|
168 |
-
device.queue.copyExternalImageToTexture(
|
169 |
-
{ source, flipY },
|
170 |
-
{ texture, premultipliedAlpha: true },
|
171 |
-
{ width: source.width, height: source.height }
|
172 |
-
);
|
173 |
-
}
|
174 |
-
|
175 |
function createTextureFromSource(device, source, options = {}) {
|
176 |
const texture = device.createTexture({
|
177 |
format: 'rgba8unorm',
|
178 |
size: [source.width, source.height],
|
179 |
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
|
180 |
});
|
181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
return texture;
|
183 |
}
|
184 |
|
@@ -242,6 +225,22 @@
|
|
242 |
|
243 |
requestAnimationFrame(render);
|
244 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
function fail(msg) {
|
246 |
alert(msg);
|
247 |
}
|
|
|
11 |
<canvas></canvas>
|
12 |
<script type="module">
|
13 |
import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
|
14 |
+
import * as utils from './utility.js';
|
15 |
const glyphWidth = 32;
|
16 |
const glyphHeight = 40;
|
17 |
const glyphsAcrossTexture = 16;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
async function main() {
|
19 |
const adapter = await navigator.gpu?.requestAdapter();
|
20 |
const device = await adapter?.requestDevice();
|
|
|
43 |
code: shaderCode,
|
44 |
});
|
45 |
|
46 |
+
const glyphCanvas = generateGlyphTextureAtlas();
|
47 |
document.body.appendChild(glyphCanvas);
|
48 |
glyphCanvas.style.backgroundColor = '#222';
|
49 |
|
|
|
149 |
},
|
150 |
});
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
function createTextureFromSource(device, source, options = {}) {
|
153 |
const texture = device.createTexture({
|
154 |
format: 'rgba8unorm',
|
155 |
size: [source.width, source.height],
|
156 |
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
|
157 |
});
|
158 |
+
|
159 |
+
device.queue.copyExternalImageToTexture(
|
160 |
+
{ source, flipY: options.flipY },
|
161 |
+
{ texture, premultipliedAlpha: true },
|
162 |
+
{ width: source.width, height: source.height }
|
163 |
+
);
|
164 |
+
|
165 |
return texture;
|
166 |
}
|
167 |
|
|
|
225 |
|
226 |
requestAnimationFrame(render);
|
227 |
}
|
228 |
+
function generateGlyphTextureAtlas() {
|
229 |
+
const canvas = document.createElement('canvas');
|
230 |
+
canvas.width = 512;
|
231 |
+
canvas.height = 256;
|
232 |
+
const ctx = canvas.getContext('2d');
|
233 |
+
ctx.font = '32px monospace';
|
234 |
+
ctx.textBaseline = 'middle';
|
235 |
+
ctx.textAlign = 'center';
|
236 |
+
ctx.fillStyle = 'white';
|
237 |
+
for (let c = 33, x = 0, y = 0; c < 128; ++c) {
|
238 |
+
ctx.fillText(String.fromCodePoint(c), x + glyphWidth / 2, y + glyphHeight / 2);
|
239 |
+
x = (x + glyphWidth) % canvas.width;
|
240 |
+
if (x === 0) y += glyphHeight;
|
241 |
+
}
|
242 |
+
return canvas;
|
243 |
+
}
|
244 |
function fail(msg) {
|
245 |
alert(msg);
|
246 |
}
|
utility.js
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
// utility.js
|