@babylonjs/lite-gl 1.18.0 → 1.20.0

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.
Files changed (50) hide show
  1. package/README.md +31 -0
  2. package/index.d.ts +13 -0
  3. package/index.js +7 -0
  4. package/index.js.map +1 -1
  5. package/package.json +6 -3
  6. package/text/_gpu/text-effect.d.ts +1 -0
  7. package/text/_gpu/text-effect.js +70 -0
  8. package/text/_gpu/text-effect.js.map +1 -0
  9. package/text/_gpu/text-instances.d.ts +1 -0
  10. package/text/_gpu/text-instances.js +57 -0
  11. package/text/_gpu/text-instances.js.map +1 -0
  12. package/text/_gpu/text-textures.d.ts +1 -0
  13. package/text/_gpu/text-textures.js +82 -0
  14. package/text/_gpu/text-textures.js.map +1 -0
  15. package/text/_gpu/text-vao.d.ts +1 -0
  16. package/text/_gpu/text-vao.js +49 -0
  17. package/text/_gpu/text-vao.js.map +1 -0
  18. package/text/default-text-data.d.ts +28 -0
  19. package/text/default-text-data.js +103 -0
  20. package/text/default-text-data.js.map +1 -0
  21. package/text/font.d.ts +16 -0
  22. package/text/font.js +24 -0
  23. package/text/font.js.map +1 -0
  24. package/text/glyph-extraction.d.ts +19 -0
  25. package/text/glyph-extraction.js +115 -0
  26. package/text/glyph-extraction.js.map +1 -0
  27. package/text/glyph-storage.d.ts +68 -0
  28. package/text/glyph-storage.js +298 -0
  29. package/text/glyph-storage.js.map +1 -0
  30. package/text/layout.d.ts +18 -0
  31. package/text/layout.js +219 -0
  32. package/text/layout.js.map +1 -0
  33. package/text/set-font-weight-offset.d.ts +5 -0
  34. package/text/set-font-weight-offset.js +67 -0
  35. package/text/set-font-weight-offset.js.map +1 -0
  36. package/text/shaders/slug-shader.d.ts +1 -0
  37. package/text/shaders/slug-shader.js +181 -0
  38. package/text/shaders/slug-shader.js.map +1 -0
  39. package/text/shaders/text-shader-fragment.d.ts +1 -0
  40. package/text/shaders/text-shader-fragment.js +2 -0
  41. package/text/shaders/text-shader-fragment.js.map +1 -0
  42. package/text/shaders/weight-shader-fragment.d.ts +1 -0
  43. package/text/shaders/weight-shader-fragment.js +75 -0
  44. package/text/shaders/weight-shader-fragment.js.map +1 -0
  45. package/text/text-data.d.ts +109 -0
  46. package/text/text-data.js +723 -0
  47. package/text/text-data.js.map +1 -0
  48. package/text/text-renderer.d.ts +68 -0
  49. package/text/text-renderer.js +238 -0
  50. package/text/text-renderer.js.map +1 -0
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=text-shader-fragment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-shader-fragment.js","sourceRoot":"","sources":["../../../src/text/shaders/text-shader-fragment.ts"],"names":[],"mappings":"","sourcesContent":["/** @internal Typed composition slots; feature modules own all optional shader logic. */\nexport interface TextShaderFragment {\n /** @internal */\n readonly _id: string;\n /** @internal */\n readonly _vertexSlots?: Partial<Record<\"VO\" | \"VD\" | \"VB\" | \"VA\", string>>;\n /** @internal */\n readonly _fragmentSlots?: Partial<Record<\"FI\" | \"FH\" | \"CO\", string>>;\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,75 @@
1
+ /** @internal GLSL counterpart of Lite's opt-in, radius-bounded contour-distance scan. */
2
+ export const WEIGHT_SHADER_FRAGMENT = {
3
+ _id: "w",
4
+ _vertexSlots: {
5
+ VO: "flat out float weightOffset;",
6
+ VD: "weightOffset = 0.0;",
7
+ VB: "float wo = params.y;\nvec4 sb = vec4(bounds.xy - vec2(wo), bounds.zw + vec2(wo));",
8
+ VA: "weightOffset = wo;",
9
+ },
10
+ _fragmentSlots: {
11
+ FI: "flat in float weightOffset;",
12
+ FH: `float dot2(vec2 v) { return dot(v, v); }
13
+ float quadraticDistance(vec2 p, vec2 A, vec2 B, vec2 C) {
14
+ vec2 a = B - A;
15
+ vec2 b = A - 2.0 * B + C;
16
+ vec2 c = a * 2.0;
17
+ vec2 d = A - p;
18
+ float bb = dot(b, b);
19
+ if (bb < 1.0e-7) {
20
+ vec2 ba = C - A;
21
+ float t = clamp(dot(p - A, ba) / max(dot(ba, ba), 1.0e-9), 0.0, 1.0);
22
+ return length(A + ba * t - p);
23
+ }
24
+ float kk = 1.0 / bb;
25
+ float kx = kk * dot(a, b);
26
+ float ky = kk * (2.0 * dot(a, a) + dot(d, b)) / 3.0;
27
+ float kz = kk * dot(d, a);
28
+ float pp = ky - kx * kx;
29
+ float q = kx * (2.0 * kx * kx - 3.0 * ky) + kz;
30
+ float h = q * q + 4.0 * pp * pp * pp;
31
+ float result;
32
+ if (h >= 0.0) {
33
+ float hh = sqrt(h);
34
+ vec2 x = (vec2(hh, -hh) - q) / 2.0;
35
+ vec2 uv = sign(x) * pow(abs(x), vec2(1.0 / 3.0));
36
+ float t = clamp(uv.x + uv.y - kx, 0.0, 1.0);
37
+ result = dot2(d + (c + b * t) * t);
38
+ } else {
39
+ float z = sqrt(-pp);
40
+ float v = acos(q / (pp * z * 2.0)) / 3.0;
41
+ float m = cos(v);
42
+ float n = sin(v) * 1.732050808;
43
+ vec3 t = clamp(vec3(m + m, -n - m, n - m) * z - kx, vec3(0.0), vec3(1.0));
44
+ result = min(dot2(d + (c + b * t.x) * t.x), dot2(d + (c + b * t.y) * t.y));
45
+ }
46
+ return sqrt(result);
47
+ }
48
+ // The coverage walk cannot be reused: nearby contours can lie in adjacent bands or behind its early exit.
49
+ float weightDistance(vec2 rc, ivec2 gp, ivec2 bm, vec4 bn, float radius) {
50
+ int y0 = clamp(int((rc.y - radius) * bn.y + bn.w), 0, bm.y);
51
+ int y1 = clamp(int((rc.y + radius) * bn.y + bn.w), 0, bm.y);
52
+ float left = rc.x - radius;
53
+ float nearest = 1.0e9;
54
+ for (int band = y0; band <= y1; band++) {
55
+ uvec4 header = texelFetch(bandTex, ivec2(gp.x + band, gp.y), 0);
56
+ int count = int(header.x);
57
+ ivec2 list = bandLocation(gp, int(header.y));
58
+ for (int i = 0; i < count; i++) {
59
+ ivec2 cv = ivec2(texelFetch(bandTex, bandLocation(list, i), 0).xy);
60
+ vec4 q12 = texelFetch(curveTex, cv, 0);
61
+ vec2 q3 = texelFetch(curveTex, ivec2(cv.x + 1, cv.y), 0).xy;
62
+ if (max(max(q12.x, q12.z), q3.x) < left) { break; }
63
+ nearest = min(nearest, quadraticDistance(rc, q12.xy, q12.zw, q3));
64
+ }
65
+ }
66
+ return nearest;
67
+ }`,
68
+ CO: `if (weightOffset != 0.0) {
69
+ float aaScale = max(max(pe.x, pe.y), 1.0e-8);
70
+ float distance = weightDistance(rc, gp, bm, banding, weightOffset + 1.0 / aaScale);
71
+ coverage = max(coverage, clamp((weightOffset - distance) * aaScale + 0.5, 0.0, 1.0));
72
+ }`,
73
+ },
74
+ };
75
+ //# sourceMappingURL=weight-shader-fragment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"weight-shader-fragment.js","sourceRoot":"","sources":["../../../src/text/shaders/weight-shader-fragment.ts"],"names":[],"mappings":"AAEA,yFAAyF;AACzF,MAAM,CAAC,MAAM,sBAAsB,GAAuB;IACtD,GAAG,EAAE,GAAG;IACR,YAAY,EAAE;QACV,EAAE,EAAE,8BAA8B;QAClC,EAAE,EAAE,qBAAqB;QACzB,EAAE,EAAE,mFAAmF;QACvF,EAAE,EAAE,oBAAoB;KAC3B;IACD,cAAc,EAAE;QACZ,EAAE,EAAE,6BAA6B;QACjC,EAAE,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuDV;QACM,EAAE,EAAE;;;;EAIV;KACG;CACJ,CAAC","sourcesContent":["import type { TextShaderFragment } from \"./text-shader-fragment.js\";\n\n/** @internal GLSL counterpart of Lite's opt-in, radius-bounded contour-distance scan. */\nexport const WEIGHT_SHADER_FRAGMENT: TextShaderFragment = {\n _id: \"w\",\n _vertexSlots: {\n VO: \"flat out float weightOffset;\",\n VD: \"weightOffset = 0.0;\",\n VB: \"float wo = params.y;\\nvec4 sb = vec4(bounds.xy - vec2(wo), bounds.zw + vec2(wo));\",\n VA: \"weightOffset = wo;\",\n },\n _fragmentSlots: {\n FI: \"flat in float weightOffset;\",\n FH: `float dot2(vec2 v) { return dot(v, v); }\nfloat quadraticDistance(vec2 p, vec2 A, vec2 B, vec2 C) {\n vec2 a = B - A;\n vec2 b = A - 2.0 * B + C;\n vec2 c = a * 2.0;\n vec2 d = A - p;\n float bb = dot(b, b);\n if (bb < 1.0e-7) {\n vec2 ba = C - A;\n float t = clamp(dot(p - A, ba) / max(dot(ba, ba), 1.0e-9), 0.0, 1.0);\n return length(A + ba * t - p);\n }\n float kk = 1.0 / bb;\n float kx = kk * dot(a, b);\n float ky = kk * (2.0 * dot(a, a) + dot(d, b)) / 3.0;\n float kz = kk * dot(d, a);\n float pp = ky - kx * kx;\n float q = kx * (2.0 * kx * kx - 3.0 * ky) + kz;\n float h = q * q + 4.0 * pp * pp * pp;\n float result;\n if (h >= 0.0) {\n float hh = sqrt(h);\n vec2 x = (vec2(hh, -hh) - q) / 2.0;\n vec2 uv = sign(x) * pow(abs(x), vec2(1.0 / 3.0));\n float t = clamp(uv.x + uv.y - kx, 0.0, 1.0);\n result = dot2(d + (c + b * t) * t);\n } else {\n float z = sqrt(-pp);\n float v = acos(q / (pp * z * 2.0)) / 3.0;\n float m = cos(v);\n float n = sin(v) * 1.732050808;\n vec3 t = clamp(vec3(m + m, -n - m, n - m) * z - kx, vec3(0.0), vec3(1.0));\n result = min(dot2(d + (c + b * t.x) * t.x), dot2(d + (c + b * t.y) * t.y));\n }\n return sqrt(result);\n}\n// The coverage walk cannot be reused: nearby contours can lie in adjacent bands or behind its early exit.\nfloat weightDistance(vec2 rc, ivec2 gp, ivec2 bm, vec4 bn, float radius) {\n int y0 = clamp(int((rc.y - radius) * bn.y + bn.w), 0, bm.y);\n int y1 = clamp(int((rc.y + radius) * bn.y + bn.w), 0, bm.y);\n float left = rc.x - radius;\n float nearest = 1.0e9;\n for (int band = y0; band <= y1; band++) {\n uvec4 header = texelFetch(bandTex, ivec2(gp.x + band, gp.y), 0);\n int count = int(header.x);\n ivec2 list = bandLocation(gp, int(header.y));\n for (int i = 0; i < count; i++) {\n ivec2 cv = ivec2(texelFetch(bandTex, bandLocation(list, i), 0).xy);\n vec4 q12 = texelFetch(curveTex, cv, 0);\n vec2 q3 = texelFetch(curveTex, ivec2(cv.x + 1, cv.y), 0).xy;\n if (max(max(q12.x, q12.z), q3.x) < left) { break; }\n nearest = min(nearest, quadraticDistance(rc, q12.xy, q12.zw, q3));\n }\n }\n return nearest;\n}`,\n CO: `if (weightOffset != 0.0) {\n float aaScale = max(max(pe.x, pe.y), 1.0e-8);\n float distance = weightDistance(rc, gp, bm, banding, weightOffset + 1.0 / aaScale);\n coverage = max(coverage, clamp((weightOffset - distance) * aaScale + 0.5, 0.0, 1.0));\n}`,\n },\n};\n"]}
@@ -0,0 +1,109 @@
1
+ /** GLTextData — slot-allocator-backed per-instance vertex buffer for a text block.
2
+ *
3
+ * Each draw group owns a contiguous slot range `[slotStart, slotStart + slotCount)`
4
+ * in the shared instance buffer. Live and dead slots intermix within that range;
5
+ * dead slots carry an all-ones packed word that the vertex shader detects and turns
6
+ * into a degenerate off-screen triangle. `addRun` / `replaceRun` reuse
7
+ * from the group's `freeSlots` LIFO when possible; otherwise they extend the
8
+ * group's range (shifting later *groups* — never other runs in the same group).
9
+ * `removeRun` writes the sentinel into its slots and returns them to the free-list.
10
+ *
11
+ * Each GLTextData is bound to one `GLGlyphStorage` for its glyph catalog. The storage is
12
+ * borrowed — caller owns its lifetime. `disposeTextData` releases the per-block
13
+ * instance buffer + style texture only.
14
+ *
15
+ * Cost per edit: O(touched glyphs) in the common single-font case, with an extra
16
+ * O(later-group slot count) shift only when the touched group must grow.
17
+ */
18
+ import type { GLCurveSetId, GLGlyphStorage } from "./glyph-storage.js";
19
+ declare const textDataBrand: unique symbol;
20
+ /** Positioned glyph instance in pixel space, usually produced by layout before being packed into a `GLGlyphRun`. */
21
+ export type GLPlacedGlyph = {
22
+ readonly glyphId: number;
23
+ /** Pixel position of glyph baseline origin. */
24
+ readonly x: number;
25
+ readonly y: number;
26
+ /** Optional per-glyph color as linear RGBA in [0,1]. When present this overrides the
27
+ * run's `defaultColor` for this glyph. When omitted, the glyph falls back to the run's
28
+ * `defaultColor`, and if that is also omitted, to opaque white. The rendered alpha is
29
+ * additionally scaled by the whole-block opacity (e.g. `TextRenderable.opacity`). */
30
+ readonly color?: readonly [number, number, number, number];
31
+ };
32
+ /** Batch of placed glyphs that all use the same curve set and font-unit-to-pixel scale. */
33
+ export type GLGlyphRun = {
34
+ /** Which curve set this run's glyph ids index into. */
35
+ readonly curveSet: GLCurveSetId;
36
+ readonly glyphs: readonly GLPlacedGlyph[];
37
+ /** GLFont-units → pixels scale used by the layout. */
38
+ readonly pixelsPerFontUnit: number;
39
+ /** Optional default color for every glyph in this run, as linear RGBA in [0,1]. A glyph's
40
+ * own `GLPlacedGlyph.color` takes precedence over this. When omitted, glyphs default to
41
+ * opaque white. The rendered alpha is additionally scaled by the whole-block opacity. */
42
+ readonly defaultColor?: readonly [number, number, number, number];
43
+ };
44
+ /** Discriminated union driving `updateTextData`. Each variant's `update` field is the
45
+ * discriminator. Arrays/maps passed inside any variant are *adopted* by the `GLTextData`
46
+ * and must not be read or mutated by the caller afterward. */
47
+ export type GLTextDataUpdate = {
48
+ /** Rebuild runs and/or swap to a different storage. Both `runs` and `storage`
49
+ * are optional; missing fields default to the GLTextData's current value, so
50
+ * `{ update: "reset" }` with neither performs a pure compaction pass that
51
+ * re-lays-out the slot allocator without dead slots or gaps.
52
+ * Invalidates any previously-passed `GLGlyphRun` references when `runs` is set. */
53
+ update: "reset";
54
+ runs?: GLGlyphRun[];
55
+ storage?: GLGlyphStorage;
56
+ } | {
57
+ /** Append a new run to the live runs list, or insert it before the run currently at
58
+ * `insertBefore`. The run's `curveSet` must already exist in the bound storage. */
59
+ update: "addRun";
60
+ run: GLGlyphRun;
61
+ /** Index in `data.runs` to insert before. Default = append at end. */
62
+ insertBefore?: number;
63
+ } | {
64
+ /** Remove a previously-added run. Accepts either the `GLGlyphRun` reference or its
65
+ * current index in `data.runs`. */
66
+ update: "removeRun";
67
+ run: GLGlyphRun | number;
68
+ } | {
69
+ /** Replace one run's contents in place. The new run takes the slot in `data.runs`
70
+ * that the previous run occupied. Cheapest when the new run has the same glyph
71
+ * count and the same `curveSet` as the previous one. */
72
+ update: "replaceRun";
73
+ previous: GLGlyphRun | number;
74
+ run: GLGlyphRun;
75
+ };
76
+ /** Mutable text block data containing glyph runs and the packed per-glyph instance buffer consumed by text renderers. */
77
+ export interface GLTextData {
78
+ readonly [textDataBrand]: true;
79
+ /** Live, in-insertion-order view of the runs currently rendered. Mutated by
80
+ * `updateTextData`. Do not mutate from outside. */
81
+ readonly runs: readonly GLGlyphRun[];
82
+ }
83
+ /** Floats per instance: the anchor (x, y) plus one packed u32 holding the glyph index in its
84
+ * low 16 bits and the style index in its high 16. Everything else the shader needs is either
85
+ * glyph-invariant — the atlas's `GlyphMetadata` table, indexed by the glyph half — or shared
86
+ * by every glyph drawn at the same color and scale — this GLTextData's style palette, indexed
87
+ * by the style half. Neither is copied per instance. */
88
+ export declare const TEXT_INSTANCE_FLOATS = 3;
89
+ export declare const TEXT_INSTANCE_BYTES: number;
90
+ /** Floats per style-palette entry, matching the WGSL `TextStyle` struct: a `vec4<f32>` color
91
+ * followed by a `vec4<f32>` of params whose `.x` carries the run's `invScale` and whose `.y`
92
+ * carries the optional per-run style parameter (see {@link TextStyleSeam}; 0 when no styling
93
+ * feature is installed). The rest is reserved padding that keeps the struct naturally
94
+ * 16-byte aligned. */
95
+ export declare const TEXT_STYLE_FLOATS = 8;
96
+ export declare const TEXT_STYLE_BYTES: number;
97
+ /** Create a GLTextData bound to `storage`. If `runs` is omitted the GLTextData starts empty;
98
+ * runs can be appended later via `updateTextData({ update: "addRun", … })`. */
99
+ export declare function createTextData(storage: GLGlyphStorage, runs?: readonly GLGlyphRun[]): GLTextData;
100
+ /** Apply an incremental edit to a `GLTextData`, such as adding, removing, replacing, or compacting runs.
101
+ *
102
+ * @param data - Text data block to update.
103
+ * @param update - Discriminated update operation to apply. */
104
+ export declare function updateTextData(data: GLTextData, update: GLTextDataUpdate): void;
105
+ /** Release per-block GPU resources owned by `data`. Does NOT dispose the bound
106
+ * `GLGlyphStorage` — caller owns its lifetime and must dispose it separately via
107
+ * `disposeGlyphStorage` once no `GLTextData` references it. */
108
+ export declare function disposeTextData(data: GLTextData): void;
109
+ export {};