webgl2-sdf 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -0
- package/node/bezier/bezier-curves-to-line-segs.d.ts +10 -0
- package/node/bezier/eval-de-casteljau.d.ts +17 -0
- package/node/bezier/from-to/from-to-2.d.ts +12 -0
- package/node/bezier/from-to/from-to-3.d.ts +12 -0
- package/node/bezier/from-to/from-to.d.ts +10 -0
- package/node/bezier/is-cubic-obtuse.d.ts +11 -0
- package/node/bezier/is-quad-obtuse.d.ts +11 -0
- package/node/bezier/is-really-point.d.ts +9 -0
- package/node/bezier/split-by-deviation-from-straight-line-cubic.d.ts +20 -0
- package/node/bezier/split-by-deviation-from-straight-line-quad.d.ts +11 -0
- package/node/bezier/split-into-line-segments.d.ts +10 -0
- package/node/debug-shaders.d.ts +9 -0
- package/node/generate-sdf.d.ts +18 -0
- package/node/helpers/calc-circs.d.ts +11 -0
- package/node/helpers/clip-line-segment-to-grid.d.ts +14 -0
- package/node/helpers/clip-line-segment-to-strips.d.ts +13 -0
- package/node/helpers/create-empty-grid.d.ts +8 -0
- package/node/helpers/create-empty-strips.d.ts +7 -0
- package/node/helpers/distance-seg-to-p.d.ts +5 -0
- package/node/helpers/find-close-cells.d.ts +3 -0
- package/node/helpers/find-crossing-cells.d.ts +3 -0
- package/node/helpers/get-distance-to-line-function.d.ts +10 -0
- package/node/helpers/jump-idx.d.ts +2 -0
- package/node/helpers/map-to-viewbox.d.ts +2 -0
- package/node/helpers/seg-box-x.d.ts +10 -0
- package/node/helpers/seg-strip-x.d.ts +9 -0
- package/node/index.d.ts +1 -0
- package/node/main-program.d.ts +4 -0
- package/node/max-aspect-ratio-before-stretch.d.ts +2 -0
- package/node/prepare-buffers.d.ts +10 -0
- package/node/row-count.d.ts +2 -0
- package/node/shaders/main.fragment.d.ts +2 -0
- package/node/shaders/main.vertex.d.ts +2 -0
- package/node/svg/get-beziers-from-raw-paths.d.ts +12 -0
- package/node/svg/get-paths-from-str.d.ts +9 -0
- package/node/svg/path-data-polyfill/parse-number.d.ts +10 -0
- package/node/svg/path-data-polyfill/parse-path-data-string.d.ts +9 -0
- package/node/svg/path-data-polyfill/source.d.ts +19 -0
- package/node/svg/path-segment/c.d.ts +19 -0
- package/node/svg/path-segment/h.d.ts +15 -0
- package/node/svg/path-segment/l.d.ts +16 -0
- package/node/svg/path-segment/q.d.ts +16 -0
- package/node/svg/path-segment/s.d.ts +20 -0
- package/node/svg/path-segment/t.d.ts +18 -0
- package/node/svg/path-segment/v.d.ts +15 -0
- package/node/svg/path-segment/z.d.ts +13 -0
- package/node/svg/path-state.d.ts +11 -0
- package/node/tex-width.d.ts +11 -0
- package/node/types/attribute.d.ts +6 -0
- package/node/types/cell.d.ts +18 -0
- package/node/types/gl-context.d.ts +28 -0
- package/node/types/gl-type.d.ts +2 -0
- package/node/types/gl-usage.d.ts +2 -0
- package/node/types/program.d.ts +16 -0
- package/node/types/strip.d.ts +8 -0
- package/node/types/texture.d.ts +4 -0
- package/node/utils/calc-circs.d.ts +11 -0
- package/node/utils/clip-line-segment-to-grid.d.ts +14 -0
- package/node/utils/clip-line-segment-to-strips.d.ts +13 -0
- package/node/utils/create-empty-grid.d.ts +8 -0
- package/node/utils/create-empty-strips.d.ts +7 -0
- package/node/utils/distance-seg-to-p.d.ts +5 -0
- package/node/utils/find-close-cells.d.ts +3 -0
- package/node/utils/find-crossing-cells.d.ts +3 -0
- package/node/utils/get-distance-to-line-function.d.ts +10 -0
- package/node/utils/jump-idx.d.ts +2 -0
- package/node/utils/map-to-viewbox.d.ts +2 -0
- package/node/utils/seg-box-x.d.ts +10 -0
- package/node/utils/seg-strip-x.d.ts +9 -0
- package/node/vector/dot.d.ts +8 -0
- package/node/vector/from-to-vec.d.ts +8 -0
- package/node/vector/len.d.ts +6 -0
- package/node/webgl-utils/compile-shader.d.ts +2 -0
- package/node/webgl-utils/get-gl-context.d.ts +10 -0
- package/node/webgl-utils/set-attribute.d.ts +15 -0
- package/node/webgl-utils/set-uniform-block.d.ts +9 -0
- package/node/webgl-utils/set-uniform.d.ts +4 -0
- package/node/webgl-utils/uniform-block.d.ts +6 -0
- package/node/webgl-utils/uniform-type.d.ts +2 -0
- package/node/webgl-utils/use-program.d.ts +17 -0
- package/node/webgl-utils/use-texture.d.ts +9 -0
- package/node/webgl2.d.ts +2 -0
- package/package.json +56 -0
- package/src/bezier/bezier-curves-to-line-segs.ts +39 -0
- package/src/bezier/eval-de-casteljau.ts +78 -0
- package/src/bezier/from-to/from-to-2.ts +159 -0
- package/src/bezier/from-to/from-to-3.ts +176 -0
- package/src/bezier/from-to/from-to.ts +30 -0
- package/src/bezier/is-cubic-obtuse.ts +31 -0
- package/src/bezier/is-quad-obtuse.ts +26 -0
- package/src/bezier/is-really-point.ts +25 -0
- package/src/bezier/split-by-deviation-from-straight-line-cubic.ts +109 -0
- package/src/bezier/split-by-deviation-from-straight-line-quad.ts +66 -0
- package/src/bezier/split-into-line-segments.ts +39 -0
- package/src/debug-shaders.ts +38 -0
- package/src/generate-sdf.ts +91 -0
- package/src/index.ts +2 -0
- package/src/main-program.ts +160 -0
- package/src/max-aspect-ratio-before-stretch.ts +5 -0
- package/src/prepare-buffers.ts +149 -0
- package/src/row-count.ts +6 -0
- package/src/shaders/main.fragment.ts +157 -0
- package/src/shaders/main.vertex.ts +55 -0
- package/src/svg/get-beziers-from-raw-paths.ts +112 -0
- package/src/svg/get-paths-from-str.ts +19 -0
- package/src/svg/path-data-polyfill/parse-number.ts +138 -0
- package/src/svg/path-data-polyfill/parse-path-data-string.ts +26 -0
- package/src/svg/path-data-polyfill/source.ts +176 -0
- package/src/svg/path-segment/c.ts +34 -0
- package/src/svg/path-segment/h.ts +28 -0
- package/src/svg/path-segment/l.ts +30 -0
- package/src/svg/path-segment/q.ts +30 -0
- package/src/svg/path-segment/s.ts +40 -0
- package/src/svg/path-segment/t.ts +35 -0
- package/src/svg/path-segment/v.ts +28 -0
- package/src/svg/path-segment/z.ts +27 -0
- package/src/svg/path-state.ts +15 -0
- package/src/tex-width.ts +14 -0
- package/src/types/attribute.ts +9 -0
- package/src/types/cell.ts +21 -0
- package/src/types/gl-context.ts +28 -0
- package/src/types/gl-type.ts +16 -0
- package/src/types/gl-usage.ts +14 -0
- package/src/types/program.ts +14 -0
- package/src/types/strip.ts +11 -0
- package/src/types/texture.ts +7 -0
- package/src/types/typed-array.ts +16 -0
- package/src/utils/calc-circs.ts +129 -0
- package/src/utils/clip-line-segment-to-grid.ts +133 -0
- package/src/utils/clip-line-segment-to-strips.ts +196 -0
- package/src/utils/create-empty-grid.ts +32 -0
- package/src/utils/create-empty-strips.ts +21 -0
- package/src/utils/distance-seg-to-p.ts +50 -0
- package/src/utils/find-close-cells.ts +171 -0
- package/src/utils/find-crossing-cells.ts +40 -0
- package/src/utils/get-distance-to-line-function.ts +59 -0
- package/src/utils/is-point-in-box.ts +16 -0
- package/src/utils/jump-idx.ts +107 -0
- package/src/utils/map-to-viewbox.ts +41 -0
- package/src/utils/path.ts +137 -0
- package/src/utils/seg-box-x.ts +84 -0
- package/src/utils/seg-strip-x.ts +72 -0
- package/src/utils/sum.ts +13 -0
- package/src/vector/dot.ts +13 -0
- package/src/vector/from-to-vec.ts +13 -0
- package/src/vector/len.ts +11 -0
- package/src/webgl-utils/compile-shader.ts +15 -0
- package/src/webgl-utils/get-gl-context.ts +61 -0
- package/src/webgl-utils/set-attribute.ts +74 -0
- package/src/webgl-utils/set-uniform-block.ts +45 -0
- package/src/webgl-utils/set-uniform.ts +24 -0
- package/src/webgl-utils/uniform-block.ts +9 -0
- package/src/webgl-utils/uniform-type.ts +10 -0
- package/src/webgl-utils/use-program.ts +48 -0
- package/src/webgl-utils/use-texture.ts +34 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Returns the ordered (by t-value) intersection points of the given line
|
|
4
|
+
* segment and square.
|
|
5
|
+
*
|
|
6
|
+
* @param seg a line segment given by its endpoints
|
|
7
|
+
* @param square a square given by its two opposing corner points, min-x, min-y
|
|
8
|
+
* first
|
|
9
|
+
*/
|
|
10
|
+
function segBoxX(
|
|
11
|
+
seg: number[][],
|
|
12
|
+
square: number[][]) {
|
|
13
|
+
|
|
14
|
+
const [p0, p1] = seg;
|
|
15
|
+
const [x0, y0] = p0;
|
|
16
|
+
const [x1, y1] = p1;
|
|
17
|
+
|
|
18
|
+
const [[minX, minY], [maxX, maxY]] = square;
|
|
19
|
+
|
|
20
|
+
// Check if line segment intersects any of the four square edges
|
|
21
|
+
const dx = x1 - x0;
|
|
22
|
+
const dy = y1 - y0;
|
|
23
|
+
|
|
24
|
+
// Parametric line equation: P(t) = P1 + t*(P2-P1), where 0 <= t <= 1
|
|
25
|
+
|
|
26
|
+
const ts: number[] = [];
|
|
27
|
+
const ps: number[][] = [];
|
|
28
|
+
|
|
29
|
+
if (dx !== 0) {
|
|
30
|
+
// Check intersection with left edge (x = minX)
|
|
31
|
+
const tL = (minX - x0)/dx;
|
|
32
|
+
if (tL >= 0 && tL <= 1) {
|
|
33
|
+
const y = y0 + tL*dy;
|
|
34
|
+
if (y >= minY && y <= maxY) {
|
|
35
|
+
ts.push(tL);
|
|
36
|
+
ps.push([minX, y]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check intersection with right edge (x = maxX)
|
|
41
|
+
const tR = (maxX - x0)/dx;
|
|
42
|
+
if (tR >= 0 && tR <= 1) {
|
|
43
|
+
const y = y0 + tR*dy;
|
|
44
|
+
if (y >= minY && y <= maxY) {
|
|
45
|
+
ts.push(tR);
|
|
46
|
+
ps.push([maxX, y]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (dy !== 0) {
|
|
52
|
+
// Check intersection with bottom edge (y = minY)
|
|
53
|
+
const tT = (minY - y0)/dy;
|
|
54
|
+
if (tT >= 0 && tT <= 1) {
|
|
55
|
+
const x = x0 + tT*dx;
|
|
56
|
+
if (x >= minX && x <= maxX) {
|
|
57
|
+
ts.push(tT);
|
|
58
|
+
ps.push([x, minY]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Check intersection with top edge (y = maxY)
|
|
63
|
+
const tB = (maxY - y0)/dy;
|
|
64
|
+
if (tB >= 0 && tB <= 1) {
|
|
65
|
+
const x = x0 + tB*dx;
|
|
66
|
+
if (x >= minX && x <= maxX) {
|
|
67
|
+
ts.push(tB);
|
|
68
|
+
ps.push([x, maxY]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// TODO might be more than 2!!
|
|
74
|
+
if (ts.length === 2) {
|
|
75
|
+
return ts[0] < ts[1]
|
|
76
|
+
? ps
|
|
77
|
+
: [ps[1],ps[0]];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return ps;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
export { segBoxX }
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Returns the ordered (by t-value) intersection points of the given line
|
|
4
|
+
* segment and half-open strip.
|
|
5
|
+
*
|
|
6
|
+
* @param seg a line segment given by its endpoints
|
|
7
|
+
* @param strip a half-open strip given by max-x, min-y, max-y
|
|
8
|
+
*/
|
|
9
|
+
function segStripX(
|
|
10
|
+
seg: number[][],
|
|
11
|
+
strip: number[]) {
|
|
12
|
+
|
|
13
|
+
const [p0, p1] = seg;
|
|
14
|
+
const [x0, y0] = p0;
|
|
15
|
+
const [x1, y1] = p1;
|
|
16
|
+
|
|
17
|
+
const [maxX, minY, maxY] = strip;
|
|
18
|
+
|
|
19
|
+
// Check if line segment intersects any of the four square edges
|
|
20
|
+
const dx = x1 - x0;
|
|
21
|
+
const dy = y1 - y0;
|
|
22
|
+
|
|
23
|
+
// Parametric line equation: P(t) = P1 + t*(P2-P1), where 0 <= t <= 1
|
|
24
|
+
|
|
25
|
+
const ts: number[] = [];
|
|
26
|
+
const ps: number[][] = [];
|
|
27
|
+
|
|
28
|
+
if (dx !== 0) {
|
|
29
|
+
// Check intersection with right edge (x = maxX)
|
|
30
|
+
const tL = (maxX - x0)/dx;
|
|
31
|
+
if (tL >= 0 && tL <= 1) {
|
|
32
|
+
const y = y0 + tL*dy;
|
|
33
|
+
if (y >= minY && y <= maxY) {
|
|
34
|
+
ts.push(tL);
|
|
35
|
+
ps.push([maxX, y]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (dy !== 0) {
|
|
41
|
+
// Check intersection with bottom edge (y = minY)
|
|
42
|
+
const tT = (minY - y0)/dy;
|
|
43
|
+
if (tT >= 0 && tT <= 1) {
|
|
44
|
+
const x = x0 + tT*dx;
|
|
45
|
+
if (x <= maxX) {
|
|
46
|
+
ts.push(tT);
|
|
47
|
+
ps.push([x, minY]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Check intersection with top edge (y = maxY)
|
|
52
|
+
const tB = (maxY - y0)/dy;
|
|
53
|
+
if (tB >= 0 && tB <= 1) {
|
|
54
|
+
const x = x0 + tB*dx;
|
|
55
|
+
if (x <= maxX) {
|
|
56
|
+
ts.push(tB);
|
|
57
|
+
ps.push([x, maxY]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (ts.length === 2) {
|
|
63
|
+
return ts[0] < ts[1]
|
|
64
|
+
? ps
|
|
65
|
+
: [ps[1],ps[0]];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return ps;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
export { segStripX }
|
package/src/utils/sum.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Returns the second 2-vector minus the first.
|
|
4
|
+
*
|
|
5
|
+
* @param p the first vector
|
|
6
|
+
* @param q the second vector
|
|
7
|
+
*/
|
|
8
|
+
function fromToVec(p: number[], q: number[]): number[] {
|
|
9
|
+
return [q[0] - p[0], q[1] - p[1]];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export { fromToVec }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
function compileShader(
|
|
3
|
+
gl: WebGL2RenderingContext,
|
|
4
|
+
src: string,
|
|
5
|
+
type: typeof gl.VERTEX_SHADER | typeof gl.FRAGMENT_SHADER) {
|
|
6
|
+
|
|
7
|
+
const shader = gl.createShader(type)!;
|
|
8
|
+
gl.shaderSource(shader, src);
|
|
9
|
+
gl.compileShader(shader);
|
|
10
|
+
|
|
11
|
+
return shader;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export { compileShader }
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Texture } from "../types/texture";
|
|
2
|
+
import type { Program } from "../types/program";
|
|
3
|
+
import type { GlContext } from "../types/gl-context";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const cache = new WeakMap<WebGL2RenderingContext, GlContext>();
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns a `GlContext` by reference via a cache of `WebGL2RenderingContext`s.
|
|
11
|
+
* * if `gl` doesn't exist in the cache yet a new context is created.
|
|
12
|
+
*
|
|
13
|
+
* @param gl the `WebGL2RenderingContext` context to wrap
|
|
14
|
+
* @param callback
|
|
15
|
+
*/
|
|
16
|
+
function getWebGLContext<T>(
|
|
17
|
+
gl: WebGL2RenderingContext): GlContext {
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
const glContext = cache.get(gl);
|
|
21
|
+
if (glContext) { return glContext; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const programs: { [index:string]: Program } = {};
|
|
25
|
+
const textures: { [index:string]: Texture } = {};
|
|
26
|
+
const framebufferStack: WebGLFramebuffer[] = [];
|
|
27
|
+
|
|
28
|
+
gl.canvas.addEventListener('webglcontextlost', e => {
|
|
29
|
+
handleContextLoss();
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
}, false);
|
|
32
|
+
|
|
33
|
+
const glContext: GlContext = {
|
|
34
|
+
gl,
|
|
35
|
+
onContextLoss: handleContextLoss,
|
|
36
|
+
textures,
|
|
37
|
+
programs,
|
|
38
|
+
framebufferStack
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
cache.set(gl, glContext);
|
|
42
|
+
|
|
43
|
+
return glContext;
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
////////////////////////
|
|
47
|
+
|
|
48
|
+
function handleContextLoss() {
|
|
49
|
+
deleteAllProps(programs);
|
|
50
|
+
deleteAllProps(textures);
|
|
51
|
+
framebufferStack.length = 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
function deleteAllProps(o: { [key:string] : unknown }) {
|
|
57
|
+
Object.keys(o).forEach(key => { delete o[key]; });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
export { getWebGLContext }
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Program } from "../types/program";
|
|
2
|
+
import { GLattrType } from "../types/gl-type";
|
|
3
|
+
import { GLusage } from "../types/gl-usage";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
// Integer types
|
|
8
|
+
BYTE, // 5120
|
|
9
|
+
UNSIGNED_BYTE, // 5121
|
|
10
|
+
SHORT, // 5122
|
|
11
|
+
UNSIGNED_SHORT, // 5123
|
|
12
|
+
INT, // 5124
|
|
13
|
+
UNSIGNED_INT, // 5125
|
|
14
|
+
// Float types
|
|
15
|
+
FLOAT, // 5126
|
|
16
|
+
HALF_FLOAT, // 5131
|
|
17
|
+
|
|
18
|
+
INT_2_10_10_10_REV, // 36255
|
|
19
|
+
UNSIGNED_INT_2_10_10_10_REV // 33640
|
|
20
|
+
} = WebGL2RenderingContext;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param name name used in glsl shaders
|
|
26
|
+
* @param size the number of components per vertex attribute. Must be 1, 2, 3, or 4.
|
|
27
|
+
* @param usage one of `gl.STATIC_DRAW`, `gl.DYNAMIC_DRAW`, `gl.STREAM_DRAW`, etc.
|
|
28
|
+
* @param data
|
|
29
|
+
* @param instancingDivisor defaults to 0; 0, 1, ...
|
|
30
|
+
* @param stride defaults to 0;
|
|
31
|
+
* @param offset defaults to 0;
|
|
32
|
+
*/
|
|
33
|
+
function setAttribute(
|
|
34
|
+
program: Program) {
|
|
35
|
+
|
|
36
|
+
return (name: string,
|
|
37
|
+
size: number,
|
|
38
|
+
type: GLattrType,
|
|
39
|
+
usage: GLusage,
|
|
40
|
+
data: AllowSharedBufferSource | null,
|
|
41
|
+
instancingDivisor = 0,
|
|
42
|
+
stride = 0,
|
|
43
|
+
offset = 0) => {
|
|
44
|
+
|
|
45
|
+
const { gl, attributes } = program;
|
|
46
|
+
|
|
47
|
+
const attr = attributes[name] = attributes[name] ?? {
|
|
48
|
+
buf: gl.createBuffer(), // TODO should we destroy our buffers?
|
|
49
|
+
loc: gl.getAttribLocation(program.program, name),
|
|
50
|
+
data: null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const { loc, buf } = attr;
|
|
54
|
+
|
|
55
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
|
|
56
|
+
if (type === FLOAT || type === HALF_FLOAT) {
|
|
57
|
+
gl.vertexAttribPointer(loc, size, type, false, stride, offset);
|
|
58
|
+
} else {
|
|
59
|
+
gl.vertexAttribIPointer(loc, size, type, stride, offset);
|
|
60
|
+
}
|
|
61
|
+
gl.enableVertexAttribArray(loc);
|
|
62
|
+
if (instancingDivisor !== 0) {
|
|
63
|
+
gl.vertexAttribDivisor(loc, instancingDivisor);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (data !== attr.data) {
|
|
67
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, usage);
|
|
68
|
+
attr.data = data;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
export { setAttribute }
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Program } from "../types/program";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param program_
|
|
7
|
+
* @param blockName used both as reference within the `Program` and within
|
|
8
|
+
* the GLSL shaders
|
|
9
|
+
*/
|
|
10
|
+
function setUniformBlock(
|
|
11
|
+
program_: Program) {
|
|
12
|
+
|
|
13
|
+
return (blockName: string,
|
|
14
|
+
bindingPoint: number,
|
|
15
|
+
buffer: AllowSharedBufferSource) => {
|
|
16
|
+
|
|
17
|
+
const { gl, uniformBlocks, program } = program_;
|
|
18
|
+
|
|
19
|
+
// const sizeInBytes = buffer.byteLength;
|
|
20
|
+
|
|
21
|
+
let uniformBlock = uniformBlocks[blockName];
|
|
22
|
+
if (uniformBlocks[blockName] === undefined) {
|
|
23
|
+
const blockIndex = gl.getUniformBlockIndex(program, blockName);
|
|
24
|
+
gl.uniformBlockBinding(program, blockIndex, bindingPoint);
|
|
25
|
+
const buf = gl.createBuffer();
|
|
26
|
+
|
|
27
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, buf);
|
|
28
|
+
|
|
29
|
+
uniformBlock = { blockName, blockIndex, buf };
|
|
30
|
+
uniformBlocks[blockName] = uniformBlock;
|
|
31
|
+
} else {
|
|
32
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, uniformBlock.buf);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { buf } = uniformBlock;
|
|
36
|
+
|
|
37
|
+
gl.bufferData(gl.UNIFORM_BUFFER, buffer, gl.DYNAMIC_DRAW);
|
|
38
|
+
// gl.bindBuffer(gl.UNIFORM_BUFFER, null);
|
|
39
|
+
gl.bindBufferBase(gl.UNIFORM_BUFFER, bindingPoint, buf);
|
|
40
|
+
// gl.bindBufferRange(gl.UNIFORM_BUFFER, bindingPoint, ubo, 0, sizeInBytes);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export { setUniformBlock }
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Program } from "../types/program";
|
|
2
|
+
import { UniformType } from "./uniform-type";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
function setUniform(
|
|
6
|
+
program: Program) {
|
|
7
|
+
|
|
8
|
+
return (type: UniformType,
|
|
9
|
+
name: string,
|
|
10
|
+
...values: any[]): void => {
|
|
11
|
+
|
|
12
|
+
const { gl, uniforms } = program;
|
|
13
|
+
|
|
14
|
+
const uniformLoc =
|
|
15
|
+
uniforms[name] ||
|
|
16
|
+
(uniforms[name] = gl.getUniformLocation(program.program, name));
|
|
17
|
+
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
gl[`uniform${type}`](uniformLoc, ...values);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export { setUniform }
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
type UniformType =
|
|
3
|
+
| '1f' | '2f' | '3f' | '4f' | '1i' | '2i' | '3i' | '4i' | '1ui' | '2ui'
|
|
4
|
+
| '3ui' | '4ui' | '1fv' | '2fv' | '3fv' | '4fv' | '1iv' | '2iv'
|
|
5
|
+
| '3iv' | '4iv' | '1uiv' | '2uiv' | '3uiv' | '4uiv'
|
|
6
|
+
| 'Matrix2fv' | 'Matrix3fv' | 'Matrix4fv' | 'Matrix2x3fv' | 'Matrix3x2fv'
|
|
7
|
+
| 'Matrix2x4fv' | 'Matrix4x2fv' | 'Matrix3x4fv' | 'Matrix4x3fv';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export type { UniformType }
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Program } from "../types/program";
|
|
2
|
+
import type { Attribute } from "../types/attribute";
|
|
3
|
+
import type { UniformBlock } from "./uniform-block";
|
|
4
|
+
import type { GlContext } from "../types/gl-context";
|
|
5
|
+
import { compileShader } from "./compile-shader";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Initiates a WebGL2 program by:
|
|
10
|
+
*
|
|
11
|
+
* * keeping track of `attributes` and `uniforms` locally (within the returned `Program`)
|
|
12
|
+
* * GL: `createProgram`
|
|
13
|
+
* * GL: `compileShader` and `attachShader` (for both Vertex and Fragment shaders)
|
|
14
|
+
* * GL: `linkProgram`
|
|
15
|
+
*
|
|
16
|
+
* @param glContext a context encapsulating our WebGl2 context
|
|
17
|
+
* @param name a name for us to refer to this program
|
|
18
|
+
* @param vert the vertex shader for this program
|
|
19
|
+
* @param frag the fragment shader for this program
|
|
20
|
+
*/
|
|
21
|
+
function initProgram(
|
|
22
|
+
glContext: GlContext,
|
|
23
|
+
name: string,
|
|
24
|
+
vert: string,
|
|
25
|
+
frag: string): Program {
|
|
26
|
+
|
|
27
|
+
const { gl, programs } = glContext;
|
|
28
|
+
|
|
29
|
+
if (programs[name]) {
|
|
30
|
+
return programs[name];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const attributes: { [index:string]: Attribute } = {};
|
|
34
|
+
const uniforms: { [index:string]: WebGLUniformLocation | null } = {};
|
|
35
|
+
const uniformBlocks: { [index:string]: UniformBlock } = {};
|
|
36
|
+
|
|
37
|
+
const program = gl.createProgram();
|
|
38
|
+
gl.attachShader(program, compileShader(gl, vert, gl.VERTEX_SHADER));
|
|
39
|
+
gl.attachShader(program, compileShader(gl, frag, gl.FRAGMENT_SHADER));
|
|
40
|
+
gl.linkProgram(program);
|
|
41
|
+
|
|
42
|
+
programs[name] = { gl, program, attributes, uniforms, uniformBlocks };
|
|
43
|
+
|
|
44
|
+
return programs[name];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
export { initProgram }
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { GlContext } from "../types/gl-context.js";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param glContext
|
|
7
|
+
* @param textureIndex the texture index to use on the GPU
|
|
8
|
+
* @param name a custom name to give to the texture for caching
|
|
9
|
+
*/
|
|
10
|
+
function useTexture(
|
|
11
|
+
glContext: GlContext,
|
|
12
|
+
textureIndex: number,
|
|
13
|
+
name: string) {
|
|
14
|
+
|
|
15
|
+
const { gl, textures } = glContext;
|
|
16
|
+
gl.activeTexture(gl.TEXTURE0 + textureIndex);
|
|
17
|
+
let texture = textures[name];
|
|
18
|
+
|
|
19
|
+
if (!texture) {
|
|
20
|
+
texture = textures[name] = { tex: gl.createTexture() };
|
|
21
|
+
gl.bindTexture(gl.TEXTURE_2D, texture.tex);
|
|
22
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
23
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
24
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
25
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
26
|
+
} else {
|
|
27
|
+
gl.bindTexture(gl.TEXTURE_2D, texture.tex);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return texture;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export { useTexture }
|