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,13 @@
|
|
|
1
|
+
import { PathState } from '../path-state.js';
|
|
2
|
+
/**
|
|
3
|
+
* @hidden
|
|
4
|
+
* Z and z: (from www.w3.org)
|
|
5
|
+
*
|
|
6
|
+
* params: (none)
|
|
7
|
+
*
|
|
8
|
+
* Close the current subpath by drawing a straight line from the current point
|
|
9
|
+
* to current subpath's initial point. Since the Z and z commands take no
|
|
10
|
+
* parameters, they have an identical effect.
|
|
11
|
+
*/
|
|
12
|
+
declare function z(s: PathState): number[][];
|
|
13
|
+
export { z };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** @hidden */
|
|
2
|
+
interface PathState {
|
|
3
|
+
initialPoint?: number[] | undefined;
|
|
4
|
+
p: number[];
|
|
5
|
+
vals?: number[] | undefined;
|
|
6
|
+
/** Used in conjunction with "S", "s" */
|
|
7
|
+
prev2ndCubicControlPoint?: number[] | undefined;
|
|
8
|
+
/** Used in conjunction with "T", "t" */
|
|
9
|
+
prev2ndQuadraticControlPoint?: number[] | undefined;
|
|
10
|
+
}
|
|
11
|
+
export { PathState };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Just a general texture width for our textures holding general data.
|
|
3
|
+
* * width cannot be too small otherwise width or height might exceed
|
|
4
|
+
* `gl.getParameter(gl.MAX_TEXTURE_SIZE)`
|
|
5
|
+
* * if width is too large we might need to pad too many zeros so `256` is a
|
|
6
|
+
* good balance since we are guaranteed at least 4096 for MAX_TEXTURE_SIZE
|
|
7
|
+
* so we can store 256 * 4096 = 1_048_576 values per texture, more than
|
|
8
|
+
* adequate for our purposes
|
|
9
|
+
*/
|
|
10
|
+
declare const TEX_WIDTH = 256;
|
|
11
|
+
export { TEX_WIDTH };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single cell in the grid
|
|
3
|
+
*/
|
|
4
|
+
interface Cell {
|
|
5
|
+
/** line segments contained within the cell */
|
|
6
|
+
readonly lineSegs: number[][][];
|
|
7
|
+
/**
|
|
8
|
+
* other cells (represented by their u and v indexes as ROW_COUNT*u + v)
|
|
9
|
+
* containing line segments and being close to this one in
|
|
10
|
+
* some (specific) sense
|
|
11
|
+
*/
|
|
12
|
+
readonly closeCells: number[];
|
|
13
|
+
/**
|
|
14
|
+
* represented by their u and v indexes as ROW_COUNT*u + v
|
|
15
|
+
*/
|
|
16
|
+
readonly crossingCells: number[];
|
|
17
|
+
}
|
|
18
|
+
export type { Cell };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Program } from "./program";
|
|
2
|
+
import type { Texture } from "./texture";
|
|
3
|
+
/**
|
|
4
|
+
* An object wrapping a `WebGL2RenderingContext` and including additional state
|
|
5
|
+
* which includes the following:
|
|
6
|
+
*
|
|
7
|
+
* * `textures`
|
|
8
|
+
* * `programs`
|
|
9
|
+
* * `framebufferStack`
|
|
10
|
+
*
|
|
11
|
+
* so they don't have to be recreated on each draw call.
|
|
12
|
+
*
|
|
13
|
+
* * an optional `width` and `height` that can be used for any purpose and that are not
|
|
14
|
+
* set or read by any function within the library
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
interface GlContext {
|
|
18
|
+
readonly gl: WebGL2RenderingContext;
|
|
19
|
+
readonly textures: {
|
|
20
|
+
[index: string]: Texture;
|
|
21
|
+
};
|
|
22
|
+
readonly programs: {
|
|
23
|
+
[index: string]: Program;
|
|
24
|
+
};
|
|
25
|
+
readonly framebufferStack: WebGLFramebuffer[];
|
|
26
|
+
readonly onContextLoss: () => void;
|
|
27
|
+
}
|
|
28
|
+
export type { GlContext };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
type GLattrType = typeof WebGL2RenderingContext.BYTE | typeof WebGL2RenderingContext.SHORT | typeof WebGL2RenderingContext.UNSIGNED_BYTE | typeof WebGL2RenderingContext.UNSIGNED_SHORT | typeof WebGL2RenderingContext.FLOAT | typeof WebGL2RenderingContext.HALF_FLOAT | typeof WebGL2RenderingContext.INT | typeof WebGL2RenderingContext.UNSIGNED_INT | typeof WebGL2RenderingContext.INT_2_10_10_10_REV | typeof WebGL2RenderingContext.UNSIGNED_INT_2_10_10_10_REV;
|
|
2
|
+
export type { GLattrType };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
type GLusage = typeof WebGL2RenderingContext.STATIC_DRAW | typeof WebGL2RenderingContext.DYNAMIC_DRAW | typeof WebGL2RenderingContext.STREAM_DRAW | typeof WebGL2RenderingContext.STATIC_READ | typeof WebGL2RenderingContext.DYNAMIC_READ | typeof WebGL2RenderingContext.STREAM_READ | typeof WebGL2RenderingContext.STATIC_COPY | typeof WebGL2RenderingContext.DYNAMIC_COPY | typeof WebGL2RenderingContext.STREAM_COPY;
|
|
2
|
+
export type { GLusage };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Attribute } from "./attribute";
|
|
2
|
+
import type { UniformBlock } from "../webgl-utils/uniform-block";
|
|
3
|
+
interface Program {
|
|
4
|
+
readonly gl: WebGL2RenderingContext;
|
|
5
|
+
readonly program: WebGLProgram;
|
|
6
|
+
readonly attributes: {
|
|
7
|
+
[index: string]: Attribute;
|
|
8
|
+
};
|
|
9
|
+
readonly uniforms: {
|
|
10
|
+
[index: string]: WebGLUniformLocation | null;
|
|
11
|
+
};
|
|
12
|
+
readonly uniformBlocks: {
|
|
13
|
+
[index: string]: UniformBlock;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export { Program };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the max distance (in cell side length units) from one corner of
|
|
3
|
+
* the viewbox to the opposing corner.
|
|
4
|
+
*/
|
|
5
|
+
declare const circsCache: Range[];
|
|
6
|
+
interface Range {
|
|
7
|
+
readonly from: number;
|
|
8
|
+
readonly u: number;
|
|
9
|
+
readonly v: number;
|
|
10
|
+
}
|
|
11
|
+
export { circsCache };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Cell } from "../types/cell";
|
|
2
|
+
/**
|
|
3
|
+
* Clips a line segment to grid boundaries and returns multiple segments.
|
|
4
|
+
*
|
|
5
|
+
* * modifies grid by adding line segments to cells
|
|
6
|
+
* * size/count *must* be a power of 2
|
|
7
|
+
*
|
|
8
|
+
* @param count the number of grid cells per dimension
|
|
9
|
+
* @param width
|
|
10
|
+
* @param height
|
|
11
|
+
* @param seg the line segment (array of 2 points)
|
|
12
|
+
*/
|
|
13
|
+
declare function clipLineSegmentToGrid(grid: Cell[][], width: number, height: number, colCount: number, cellSize: number, seg: number[][], padCount: number): void;
|
|
14
|
+
export { clipLineSegmentToGrid };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Strip } from "../types/strip";
|
|
2
|
+
/**
|
|
3
|
+
* Clips a line segment to strip boundaries and returns multiple segments.
|
|
4
|
+
*
|
|
5
|
+
* * modifies strips by adding line segments to each strip
|
|
6
|
+
* * size/count *must* be a power of 2
|
|
7
|
+
*
|
|
8
|
+
* @param count the number of strips
|
|
9
|
+
* @param height the height of a strip
|
|
10
|
+
* @param seg the line segment (array of 2 points)
|
|
11
|
+
*/
|
|
12
|
+
declare function clipLineSegmentToStrips(strips: Strip[], height: number, seg: number[][]): void;
|
|
13
|
+
export { clipLineSegmentToStrips };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a function that returns the signed distance to the given line from
|
|
3
|
+
* the given point.
|
|
4
|
+
*
|
|
5
|
+
* @param pS a point on the line
|
|
6
|
+
* @param pE a different point on the line; if `pS` is the same as `pE` then
|
|
7
|
+
* the distance to the point `pS` (or `pE`) will be returned.
|
|
8
|
+
*/
|
|
9
|
+
declare function getDistanceToLineFunction(pS: number[], pE: number[]): (p: number[]) => number;
|
|
10
|
+
export { getDistanceToLineFunction };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the ordered (by t-value) intersection points of the given line
|
|
3
|
+
* segment and square.
|
|
4
|
+
*
|
|
5
|
+
* @param seg a line segment given by its endpoints
|
|
6
|
+
* @param square a square given by its two opposing corner points, min-x, min-y
|
|
7
|
+
* first
|
|
8
|
+
*/
|
|
9
|
+
declare function segBoxX(seg: number[][], square: number[][]): number[][];
|
|
10
|
+
export { segBoxX };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the ordered (by t-value) intersection points of the given line
|
|
3
|
+
* segment and half-open strip.
|
|
4
|
+
*
|
|
5
|
+
* @param seg a line segment given by its endpoints
|
|
6
|
+
* @param strip a half-open strip given by max-x, min-y, max-y
|
|
7
|
+
*/
|
|
8
|
+
declare function segStripX(seg: number[][], strip: number[]): number[][];
|
|
9
|
+
export { segStripX };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { GlContext } from "../types/gl-context";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a `GlContext` by reference via a cache of `WebGL2RenderingContext`s.
|
|
4
|
+
* * if `gl` doesn't exist in the cache yet a new context is created.
|
|
5
|
+
*
|
|
6
|
+
* @param gl the `WebGL2RenderingContext` context to wrap
|
|
7
|
+
* @param callback
|
|
8
|
+
*/
|
|
9
|
+
declare function getWebGLContext<T>(gl: WebGL2RenderingContext): GlContext;
|
|
10
|
+
export { getWebGLContext };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Program } from "../types/program";
|
|
2
|
+
import { GLattrType } from "../types/gl-type";
|
|
3
|
+
import { GLusage } from "../types/gl-usage";
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param name name used in glsl shaders
|
|
7
|
+
* @param size the number of components per vertex attribute. Must be 1, 2, 3, or 4.
|
|
8
|
+
* @param usage one of `gl.STATIC_DRAW`, `gl.DYNAMIC_DRAW`, `gl.STREAM_DRAW`, etc.
|
|
9
|
+
* @param data
|
|
10
|
+
* @param instancingDivisor defaults to 0; 0, 1, ...
|
|
11
|
+
* @param stride defaults to 0;
|
|
12
|
+
* @param offset defaults to 0;
|
|
13
|
+
*/
|
|
14
|
+
declare function setAttribute(program: Program): (name: string, size: number, type: GLattrType, usage: GLusage, data: AllowSharedBufferSource | null, instancingDivisor?: number, stride?: number, offset?: number) => void;
|
|
15
|
+
export { setAttribute };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Program } from "../types/program";
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param program_
|
|
5
|
+
* @param blockName used both as reference within the `Program` and within
|
|
6
|
+
* the GLSL shaders
|
|
7
|
+
*/
|
|
8
|
+
declare function setUniformBlock(program_: Program): (blockName: string, bindingPoint: number, buffer: AllowSharedBufferSource) => void;
|
|
9
|
+
export { setUniformBlock };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
type UniformType = '1f' | '2f' | '3f' | '4f' | '1i' | '2i' | '3i' | '4i' | '1ui' | '2ui' | '3ui' | '4ui' | '1fv' | '2fv' | '3fv' | '4fv' | '1iv' | '2iv' | '3iv' | '4iv' | '1uiv' | '2uiv' | '3uiv' | '4uiv' | 'Matrix2fv' | 'Matrix3fv' | 'Matrix4fv' | 'Matrix2x3fv' | 'Matrix3x2fv' | 'Matrix2x4fv' | 'Matrix4x2fv' | 'Matrix3x4fv' | 'Matrix4x3fv';
|
|
2
|
+
export type { UniformType };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Program } from "../types/program";
|
|
2
|
+
import type { GlContext } from "../types/gl-context";
|
|
3
|
+
/**
|
|
4
|
+
* Initiates a WebGL2 program by:
|
|
5
|
+
*
|
|
6
|
+
* * keeping track of `attributes` and `uniforms` locally (within the returned `Program`)
|
|
7
|
+
* * GL: `createProgram`
|
|
8
|
+
* * GL: `compileShader` and `attachShader` (for both Vertex and Fragment shaders)
|
|
9
|
+
* * GL: `linkProgram`
|
|
10
|
+
*
|
|
11
|
+
* @param glContext a context encapsulating our WebGl2 context
|
|
12
|
+
* @param name a name for us to refer to this program
|
|
13
|
+
* @param vert the vertex shader for this program
|
|
14
|
+
* @param frag the fragment shader for this program
|
|
15
|
+
*/
|
|
16
|
+
declare function initProgram(glContext: GlContext, name: string, vert: string, frag: string): Program;
|
|
17
|
+
export { initProgram };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GlContext } from "../types/gl-context.js";
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param glContext
|
|
5
|
+
* @param textureIndex the texture index to use on the GPU
|
|
6
|
+
* @param name a custom name to give to the texture for caching
|
|
7
|
+
*/
|
|
8
|
+
declare function useTexture(glContext: GlContext, textureIndex: number, name: string): import("../types/texture.js").Texture;
|
|
9
|
+
export { useTexture };
|
package/node/webgl2.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
declare function generateIntoFramebuffer(gl: WebGL2RenderingContext, psss: (number[][])[][] | string, width: number, height: number, viewbox: [number, number, number, number], maxDistance: number, sdfExponent?: number, inclInside?: boolean, inclOutside?: boolean, x?: number, y?: number, channel?: number, resolution?: 0.5 | 1): void;
|
|
2
|
+
export { generateIntoFramebuffer };
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "webgl2-sdf",
|
|
3
|
+
"sideEffects": false,
|
|
4
|
+
"description": "Fast sdf generator from bezier curves or svg string.",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Floris Steenkamp"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"exports": "./dist/index.js",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
15
|
+
},
|
|
16
|
+
"private": false,
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/FlorisSteenkamp/webgl2-sdf.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/FlorisSteenkamp/webgl2-sdf/issues"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"signed distance transform",
|
|
26
|
+
"signed",
|
|
27
|
+
"distance",
|
|
28
|
+
"transform",
|
|
29
|
+
"webgl",
|
|
30
|
+
"webgl2"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^25.0.9",
|
|
36
|
+
"c8": "^10.1.3",
|
|
37
|
+
"rimraf": "^6.1.2",
|
|
38
|
+
"ts-loader": "^9.5.4",
|
|
39
|
+
"ts-node": "^10.9.2",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"webpack": "^5.104.1",
|
|
42
|
+
"webpack-bundle-analyzer": "^5.1.1",
|
|
43
|
+
"webpack-cli": "^6.0.1",
|
|
44
|
+
"source-map-support": "^0.5.21"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"node",
|
|
48
|
+
"browser",
|
|
49
|
+
"src"
|
|
50
|
+
],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "npm run build-browser && npm run build-node",
|
|
53
|
+
"build-node": "rimraf node && tsc",
|
|
54
|
+
"build-browser": "webpack"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isReallyPoint } from "./is-really-point.js";
|
|
2
|
+
import { splitIntoLineSegments } from "./split-into-line-segments.js";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns an array of line segments that is an approximation of the given
|
|
7
|
+
* bezier curves up to `PIXEL_ACCURACY` accuracy.
|
|
8
|
+
*
|
|
9
|
+
* @param bezierLoops
|
|
10
|
+
* @param resolution each bezier curve is split until its hausdorff distance
|
|
11
|
+
* from the line connecting it's endpoints are less than this value
|
|
12
|
+
*/
|
|
13
|
+
function bezierCurvesToLineSegs(
|
|
14
|
+
bezierLoops: (number[][])[][],
|
|
15
|
+
resolution: 0.5|1) {
|
|
16
|
+
|
|
17
|
+
let lineSegs: number[][][] = [];
|
|
18
|
+
for (let i=0; i<bezierLoops.length; i++) {
|
|
19
|
+
const pss = bezierLoops[i];
|
|
20
|
+
for (let j=0; j<pss.length; j++) {
|
|
21
|
+
const ps = pss[j];
|
|
22
|
+
if (isReallyPoint(ps)) { continue; }
|
|
23
|
+
|
|
24
|
+
if (ps.length === 2) {
|
|
25
|
+
lineSegs.push(ps);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const lineSegs_ = splitIntoLineSegments(ps, resolution);
|
|
30
|
+
|
|
31
|
+
lineSegs.push(...lineSegs_.filter(ps => !isReallyPoint(ps)));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return lineSegs;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
export { bezierCurvesToLineSegs }
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Returns the resulting point of evaluating the given bezier curve at the
|
|
4
|
+
* given parameter `t`.
|
|
5
|
+
*
|
|
6
|
+
* * uses [De Casteljau's algorithm](https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm)
|
|
7
|
+
* in double precision floating point arithmetic
|
|
8
|
+
*
|
|
9
|
+
* The resulting point `p` is returned as the pair `[x,y]`, where `x` and `y` are
|
|
10
|
+
* double precision floating point numbers.
|
|
11
|
+
*
|
|
12
|
+
* @param ps an order 1,2 or 3 bezier curve, e.g. `[[0,0],[1,1],[2,1],[2,0]]`
|
|
13
|
+
* @param t the parameter value where the bezier should be evaluated
|
|
14
|
+
*
|
|
15
|
+
* @doc mdx
|
|
16
|
+
**/
|
|
17
|
+
function evalDeCasteljau(
|
|
18
|
+
ps: number[][],
|
|
19
|
+
t: number): number[] {
|
|
20
|
+
|
|
21
|
+
if (t === 0) {
|
|
22
|
+
return ps[0];
|
|
23
|
+
} else if (t === 1) {
|
|
24
|
+
return ps[ps.length-1];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (ps.length === 4) {
|
|
28
|
+
const [[x0,y0], [x1,y1], [x2,y2], [x3,y3]] = ps;
|
|
29
|
+
|
|
30
|
+
const a01 = x0 + (x1 - x0)*t;
|
|
31
|
+
const a11 = x1 + (x2 - x1)*t;
|
|
32
|
+
const a21 = x2 + (x3 - x2)*t;
|
|
33
|
+
const a02 = a01 + (a11 - a01)*t;
|
|
34
|
+
const a12 = a11 + (a21 - a11)*t;
|
|
35
|
+
const x = a02 + (a12 - a02)*t;
|
|
36
|
+
|
|
37
|
+
const b01 = y0 + (y1 - y0)*t;
|
|
38
|
+
const b11 = y1 + (y2 - y1)*t;
|
|
39
|
+
const b21 = y2 + (y3 - y2)*t;
|
|
40
|
+
const b02 = b01 + (b11 - b01)*t;
|
|
41
|
+
const b12 = b11 + (b21 - b11)*t;
|
|
42
|
+
const y = b02 + (b12 - b02)*t;
|
|
43
|
+
|
|
44
|
+
return [x,y];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (ps.length === 3) {
|
|
48
|
+
const [[x0,y0], [x1,y1], [x2,y2]] = ps;
|
|
49
|
+
|
|
50
|
+
const a01 = x0 + (x1 - x0)*t;
|
|
51
|
+
const a11 = x1 + (x2 - x1)*t;
|
|
52
|
+
const x = a01 + (a11 - a01)*t;
|
|
53
|
+
|
|
54
|
+
const b01 = y0 + (y1 - y0)*t;
|
|
55
|
+
const b11 = y1 + (y2 - y1)*t;
|
|
56
|
+
const y = b01 + (b11 - b01)*t;
|
|
57
|
+
|
|
58
|
+
return [x,y];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (ps.length === 2) {
|
|
62
|
+
const [[x0,y0], [x1,y1]] = ps;
|
|
63
|
+
|
|
64
|
+
const x = x0 + (x1 - x0)*t;
|
|
65
|
+
const y = y0 + (y1 - y0)*t;
|
|
66
|
+
|
|
67
|
+
return [x,y];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (ps.length === 1) {
|
|
71
|
+
return ps[0];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
throw new Error('The given bezier curve must be of order <= 3.');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
export { evalDeCasteljau }
|