@driftengine/ui2d 3.61.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 (52) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +9 -0
  3. package/README.md +228 -0
  4. package/dist/camera2d.d.ts +47 -0
  5. package/dist/camera2d.js +46 -0
  6. package/dist/index.d.ts +30 -0
  7. package/dist/index.js +21 -0
  8. package/dist/shaders/generated/sprite.wgsl.d.ts +54 -0
  9. package/dist/shaders/generated/sprite.wgsl.js +60 -0
  10. package/dist/shaders/sprite.d.ts +3 -0
  11. package/dist/shaders/sprite.js +98 -0
  12. package/dist/spriteBatch.d.ts +78 -0
  13. package/dist/spriteBatch.js +93 -0
  14. package/dist/spriteGl.d.ts +26 -0
  15. package/dist/spriteGl.js +207 -0
  16. package/dist/spriteGpu.d.ts +31 -0
  17. package/dist/spriteGpu.js +201 -0
  18. package/dist/spritePass.d.ts +54 -0
  19. package/dist/spritePass.js +142 -0
  20. package/dist/spriteSheet.d.ts +64 -0
  21. package/dist/spriteSheet.js +84 -0
  22. package/dist/spriteTexture.d.ts +36 -0
  23. package/dist/spriteTexture.js +5 -0
  24. package/dist/tilemap.d.ts +47 -0
  25. package/dist/tilemap.js +70 -0
  26. package/dist/uiDraw.d.ts +28 -0
  27. package/dist/uiDraw.js +41 -0
  28. package/dist/uiFocus.d.ts +36 -0
  29. package/dist/uiFocus.js +78 -0
  30. package/dist/uiInput.d.ts +51 -0
  31. package/dist/uiInput.js +85 -0
  32. package/dist/uiLayout.d.ts +23 -0
  33. package/dist/uiLayout.js +144 -0
  34. package/dist/uiNode.d.ts +105 -0
  35. package/dist/uiNode.js +79 -0
  36. package/package.json +57 -0
  37. package/src/camera2d.ts +88 -0
  38. package/src/index.ts +55 -0
  39. package/src/shaders/generated/sprite.wgsl.ts +63 -0
  40. package/src/shaders/sprite.ts +102 -0
  41. package/src/spriteBatch.ts +169 -0
  42. package/src/spriteGl.ts +270 -0
  43. package/src/spriteGpu.ts +271 -0
  44. package/src/spritePass.ts +231 -0
  45. package/src/spriteSheet.ts +147 -0
  46. package/src/spriteTexture.ts +42 -0
  47. package/src/tilemap.ts +114 -0
  48. package/src/uiDraw.ts +63 -0
  49. package/src/uiFocus.ts +80 -0
  50. package/src/uiInput.ts +115 -0
  51. package/src/uiLayout.ts +157 -0
  52. package/src/uiNode.ts +186 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Drift Technologies
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,9 @@
1
+ DriftEngine
2
+ Copyright 2026 Drift Technologies
3
+
4
+ This product includes software developed at Drift Technologies
5
+ (https://github.com/drftrun/driftengine).
6
+
7
+ Licensed under the Apache License, Version 2.0. Section 4(d) of that licence
8
+ requires this NOTICE to be reproduced in any derivative work you distribute,
9
+ in the source, the documentation, or a display generated by the work.
package/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # @driftengine/ui2d
2
+
3
+ The 2D layer: quads with a texture on them, batched, in the order you drew them.
4
+
5
+ **Cost: 8.4 KB gzipped on top of core.** Measured by `scripts/size-gate.test.mjs`, which fails if it
6
+ drifts more than 3% — the number is derived from the same floors that gate asserts, so a README
7
+ quoting a stale one is a red suite rather than a thing somebody notices.
8
+
9
+ ## What it is
10
+
11
+ ```ts
12
+ import { createAffine2D, createSpritePass, drawSprite, screenToNdc } from '@driftengine/ui2d';
13
+
14
+ const sprites = createSpritePass({ capacity: 4096 });
15
+ sprites.setTexture(0, await createImageBitmap(sheetImage));
16
+ const handle = renderer.registerPass(sprites);
17
+
18
+ const affine = createAffine2D();
19
+
20
+ function frame(): void {
21
+ sprites.reset();
22
+ sprites.setTransform(screenToNdc(canvas.clientWidth, canvas.clientHeight, affine));
23
+ drawSprite(sprites.batch, 0, { x: 24, y: 24, w: 64, h: 64 }, null, null);
24
+
25
+ renderer.beginFrame(clear);
26
+ renderer.bindMeshPass(camera, env);
27
+ renderer.drawMesh(ground, model);
28
+ renderer.drawPass(handle); // wherever in the frame you want the 2D layer
29
+ renderer.endFrame();
30
+ }
31
+ ```
32
+
33
+ **It draws through `registerPass`, not through a verb on the renderer.** That is what lets it be a
34
+ package at all: the renderer's verb surface is fixed and nothing outside `render/` can add to it, so
35
+ this owns its own program on both backends and the caller decides where in the frame it lands.
36
+ `pass.ts` in core names this package as one of the three that mechanism exists for.
37
+
38
+ ## Two coordinate systems, and they count y opposite ways
39
+
40
+ `screenToNdc(width, height, out)` is **CSS pixels from the top-left** — the convention `InsetRect`
41
+ and `fillPanel` already use, and the one a caller laying out an overlay already has.
42
+
43
+ `worldToNdc(camera, width, height, out)` is a **2D world seen through a camera**, and there y counts
44
+ **up**. A caller placing a platform above a floor should not have to subtract, and a 2D world is a
45
+ world. The two are different enough that sharing one convention would make every caller wrong half
46
+ the time, so they are named separately and each says which it is.
47
+
48
+ A `Camera2D` is where it is, how far in (`zoom`, in pixels per world unit) and which way up
49
+ (`rotation`, radians, anticlockwise). Turning the camera turns the world the other way.
50
+
51
+ ## Order is the layering
52
+
53
+ There is no depth here, on purpose: a sprite writes no depth and tests none, so **the order sprites
54
+ were submitted in is the order they are drawn**. That is the same decision the rest of the engine
55
+ takes about pass ordering — `ROADMAP.md` records gate 1.2 withdrawing dependency ordering because
56
+ the API is immediate-mode and the caller's draw order is already the semantic one.
57
+
58
+ ## One batch, many textures, runs between them
59
+
60
+ `drawSprite` names a texture slot. Consecutive sprites on the same slot are one **run** and one
61
+ draw; a slot change starts a new run, and going back to the first slot starts a third rather than
62
+ regrouping — regrouping would reorder the picture.
63
+
64
+ This is the shape `AGENTS.md` asks for: _the number to hold down is material changes, not draws_. A
65
+ tilemap over one sheet is one run however many thousand tiles it is.
66
+
67
+ A batch has a fixed capacity and does not grow. Past it, `drawSprite` counts into `batch.dropped`
68
+ rather than allocating under a frame — a caller drawing more than it planned for wants counting,
69
+ not absorbing.
70
+
71
+ ## Sheets
72
+
73
+ A sheet is the rectangles of one texture that each hold a picture. `gridSheet` cuts it into equal
74
+ cells, row-major; `namedSheet` takes whatever rectangles a packer put where. Both answer a frame by
75
+ index, and `frameOf` turns a name into one.
76
+
77
+ ```ts
78
+ const sheet = gridSheet(0, 256, 128, 32, 32); // slot 0, 8 x 4 cells
79
+ const frame = createSpriteFrame(); // reused; reading one allocates nothing
80
+ drawSprite(batch, sheet.texture, place, sheetFrame(sheet, frameOf(sheet, '5'), frame), null);
81
+ ```
82
+
83
+ **A cell that would run off the edge is not emitted.** A sheet 70 texels wide cut into sixteens has
84
+ four whole cells and six texels of margin, and a fifth cell reading into that margin is a frame with
85
+ a stripe of nothing down one side — which reads as a rendering bug rather than as a badly measured
86
+ atlas.
87
+
88
+ **A frame index the sheet does not have reads as the whole texture rather than throwing**, because
89
+ this is called per sprite per frame and nothing may throw in the frame loop. What a caller then sees
90
+ is the entire atlas drawn where one picture should be, which is unmistakable.
91
+
92
+ ## Tilemaps
93
+
94
+ ```ts
95
+ const map = createTilemap(1000, 1000, 32, 32);
96
+ setTile(map, 4, 7, frameOf(sheet, 'grass'));
97
+ const drawn = drawTilemap(batch, map, sheet, { x: camX, y: camY, w: 1280, h: 720 }, null);
98
+ ```
99
+
100
+ **The cost is the view rather than the map.** The visible span is arithmetic on four numbers, so a
101
+ million-cell map costs the few hundred cells on screen — measured at 25 drawn from a 1,000 by 1,000
102
+ map. A walk over every cell testing each against the view draws exactly the same picture and is
103
+ unusable at the size a tilemap exists for.
104
+
105
+ **Which way the rows run is the affine's business, not the map's.** A cell's corner is
106
+ `y + row * tileHeight`, so in screen space — where y counts down — row 0 is the top row, and in a 2D
107
+ world it is the bottom. That is the same rule `SpritePlacement` states about its own corner, and one
108
+ rule rather than a flag is what keeps the two agreeing.
109
+
110
+ ## Textures
111
+
112
+ `setTexture(slot, source, options)` takes an `ImageBitmap`, an `HTMLCanvasElement` or an
113
+ `OffscreenCanvas` — narrower than `TexImageSource` because that is the union both backends take
114
+ without a conversion. An `<img>` reaches it through one `createImageBitmap`, which is also where the
115
+ decode's own orientation and premultiply options live.
116
+
117
+ `colorSpace` defaults to `srgb`, which is right for anything painted to be looked at; the sampler
118
+ decodes and the pass's own output transform re-encodes, so the blend happens in linear light.
119
+ `filter` defaults to `nearest`, because a sheet is usually pixel art and linear filtering is the
120
+ thing that makes pixel art look wrong.
121
+
122
+ **Neither backend flips the image.** `surfaceTexture.ts` in core carries the whole argument and the
123
+ bug it came from: WebGL2 ignores `UNPACK_FLIP_Y_WEBGL` for an `ImageBitmap` and honours it for a
124
+ canvas, so a pipeline checked with one source type is half checked. `scripts/sprite-check.mjs`
125
+ checks both.
126
+
127
+ ## The retained interface tree
128
+
129
+ ```ts
130
+ const root = createUiNode({ direction: 'column', width: 'grow', height: 'grow', justify: 'end' });
131
+ const bar = addUiChild(root, createUiNode({ direction: 'row', padding: 10, gap: 10, name: 'bar' }));
132
+ addUiChild(bar, createUiNode({ width: 60, height: 40, interactive: true, name: 'start' }));
133
+
134
+ layoutUiTree(root, 0, 0, canvas.clientWidth, canvas.clientHeight);
135
+ routeUiPointer(input, root, pointerX, pointerY, pointerDown);
136
+ drawUiTree(sprites.batch, root, sprites.white, null);
137
+ ```
138
+
139
+ **`layoutUiTree` is two passes and no third**: measure what every node comes to on its own,
140
+ bottom-up, then place what got a size, top-down. A `fit` that depended on the space it was given
141
+ would need a rule for when to stop, which is where a layout engine stops being explicable.
142
+
143
+ Three size cases — a number, `fit`, `grow` — rather than percentages or flex weights. A percentage
144
+ of a parent that is itself `fit` is a cycle. A `grow` child measures as `fit`, which is what lets a
145
+ menu sized to its contents hold a row that fills it.
146
+
147
+ **It allocates nothing.** Every number lands in a `UiRect` the node already owns, which is why a
148
+ node is an object with mutable fields rather than a description that gets resolved into one: an
149
+ interface tree is tens of nodes built once and mutated, not thousands rebuilt per frame.
150
+
151
+ The box you give it is what is _available_ — the root resolves its own size against it, so the same
152
+ call lays out a full-screen HUD and a tooltip.
153
+
154
+ ## Hit testing, focus and routing
155
+
156
+ `uiHitTest` searches **last-drawn first**, so it agrees with the picture. **A node that is not
157
+ `interactive` does not block what is behind it**: a backdrop that swallowed clicks makes every
158
+ button under a plate dead, which no screenshot shows. A modal that wants to swallow them says so by
159
+ being interactive.
160
+
161
+ `routeUiPointer` activates on **a press and a release on the same node**, so somebody who pressed
162
+ the wrong button can slide off it and let go. It sets `hovered` and `pressed` on the nodes
163
+ themselves, so a caller paints a button from its own state.
164
+
165
+ `routeUiKey` handles three keys — Tab both ways, Enter and space — and reports everything else
166
+ unhandled, so a caller can have it. Focus order is tree order rather than a declared index, and a
167
+ node hidden since it was focused does not take the keyboard with it.
168
+
169
+ **Text is the caller's.** This package draws quads; core already draws two kinds of text. Pass a
170
+ `UiContentSink` to `drawUiTree` and it hands you every node with text at its resolved rect, in the
171
+ order it was drawn.
172
+
173
+ ## From DriftScript
174
+
175
+ **`drift/ui` is bound**: eighteen capabilities over the tree, addressed by the names its nodes were
176
+ built with. A host builds the tree and hands the root to a script through `uses`, the way a
177
+ `Terrain` arrives.
178
+
179
+ ```
180
+ import { layout, hovered, show } from "drift/ui"
181
+
182
+ fn menu(tree: UiTree) {
183
+ ui.layout(tree, 0, 0, 1280, 720)
184
+ ui.show(tree, "start", ui.hovered(tree, "start"))
185
+ }
186
+ ```
187
+
188
+ Reading a laid-out box is `scene.read` and inside the fixed step; `hovered` and `pressed` are
189
+ `input.read` and outside it, so a `@deterministic` system cannot ask where the pointer is.
190
+
191
+ **`drift/2d` is bound too**, and it is why DriftScript has import aliases. A module's namespace was
192
+ the last segment of its path, so a call would have been `2d.sprite(...)` — a number followed by an
193
+ identifier the lexer refuses — and the binding was written, wired, found uncallable and withdrawn
194
+ rather than shipped. DriftScript 1.11.0 answered it at the import, so the namespace is written out:
195
+
196
+ ```
197
+ import { sprite, tilemap } from "drift/2d" as sprites
198
+
199
+ fn hud(batch: SpriteBatch) {
200
+ sprites.sprite(batch, 0, 10, 10, 32, 32)
201
+ }
202
+ ```
203
+
204
+ Twelve capabilities over a batch, a sheet and a tilemap. A draw is `scene.write` and outside the
205
+ fixed step; reading a sheet or a cell is `scene.read` and inside it, so a `@deterministic` system may
206
+ ask what is in a cell and may not draw it. `dropped` is exposed for the same reason it exists: a
207
+ batch that overflows drops draws, and a script with no way to read the count finds out by noticing
208
+ something missing from a corner of the screen.
209
+
210
+ ## What it is checked by
211
+
212
+ `scripts/sprite-check.mjs`, on a real GPU, on both backends. Every sheet in it is two texels by two
213
+ with four different colours, so a sprite's four quadrants say which corner is which — an assertion a
214
+ mirror fails and a bounding box cannot, since a mirror preserves every count and every box. The
215
+ control is submission order: the same two sprites are drawn both ways round and the overlap changes
216
+ colour.
217
+
218
+ It found a real defect on its first run. A contributed pass inherits whatever cull state the last
219
+ scene draw left on, and a screen-space quad's winding is not the scene's — so WebGL2 drew **nothing
220
+ at all** while WebGPU, whose pipeline states its own cull mode, was pixel-perfect. No error on
221
+ either side.
222
+
223
+ `scripts/ui-check.mjs` does the same for the tree, and it asserts the one thing a unit test cannot:
224
+ the page publishes **the boxes the layout computed** and **the bounding boxes of the colours they
225
+ were drawn in**, and the two must agree. A bar sized by its own contents lands at 530, 660, 220 by
226
+ 60 on both backends, to **0.0 px**, with the frame byte-identical in all three pointer states. The
227
+ control is the pointer: the middle button paints itself from its own `hovered` and `pressed` flags,
228
+ so routing is a colour in the frame rather than the router's own return value read back.
@@ -0,0 +1,47 @@
1
+ /** The affine that takes a point in a 2D world, or a CSS pixel, to normalised device coordinates. */
2
+ /**
3
+ * Six numbers, laid out the way a 2D graphics API has laid them out for thirty years:
4
+ *
5
+ * ```
6
+ * ndc.x = m[0] * x + m[2] * y + m[4]
7
+ * ndc.y = m[1] * x + m[3] * y + m[5]
8
+ * ```
9
+ *
10
+ * A `Float32Array` rather than six fields because it is uploaded as two `vec4`s and read by the
11
+ * vertex stage every frame; an object would be repacked on the way.
12
+ */
13
+ export type Affine2D = Float32Array;
14
+ export declare function createAffine2D(): Affine2D;
15
+ /** A 2D camera: where it is, how far in, and which way up. */
16
+ export interface Camera2D {
17
+ /** World units. The point that lands on the middle of the viewport. */
18
+ readonly x: number;
19
+ readonly y: number;
20
+ /**
21
+ * Pixels per world unit, and it is the same on both axes on purpose.
22
+ *
23
+ * A separate x and y zoom is a non-uniform scale, which turns a circle into an ellipse and a
24
+ * square tile into a rectangle — the caller who wants that has asked for a stretched picture and
25
+ * can say so in the sprite's own size, where it is visible.
26
+ */
27
+ readonly zoom: number;
28
+ /** Radians, anticlockwise. The world turns the other way; see `worldToNdc`. */
29
+ readonly rotation?: number;
30
+ }
31
+ /**
32
+ * The affine for a 2D world seen through a camera.
33
+ *
34
+ * **y counts upward**, which is the opposite of `screenToNdc` below and is deliberate: a 2D world is
35
+ * a world, and a caller placing a platform above a floor should not have to subtract. A screen-space
36
+ * overlay is the other convention because that is the one every layout system already uses, and the
37
+ * two are different enough that sharing one would make each caller wrong half the time.
38
+ *
39
+ * Rotation turns the *camera*, so the world turns the other way: at `rotation = +π/2` the camera has
40
+ * tilted its head anticlockwise and what was to its right is now above it.
41
+ */
42
+ export declare function worldToNdc(camera: Camera2D, viewportWidth: number, viewportHeight: number, out: Affine2D): Affine2D;
43
+ /**
44
+ * The affine for CSS pixels with a top-left origin — the convention `InsetRect` and `fillPanel`
45
+ * already use, and the one a caller laying out an overlay already has.
46
+ */
47
+ export declare function screenToNdc(viewportWidth: number, viewportHeight: number, out: Affine2D): Affine2D;
@@ -0,0 +1,46 @@
1
+ /** The affine that takes a point in a 2D world, or a CSS pixel, to normalised device coordinates. */
2
+ export function createAffine2D() {
3
+ return new Float32Array(6);
4
+ }
5
+ /**
6
+ * The affine for a 2D world seen through a camera.
7
+ *
8
+ * **y counts upward**, which is the opposite of `screenToNdc` below and is deliberate: a 2D world is
9
+ * a world, and a caller placing a platform above a floor should not have to subtract. A screen-space
10
+ * overlay is the other convention because that is the one every layout system already uses, and the
11
+ * two are different enough that sharing one would make each caller wrong half the time.
12
+ *
13
+ * Rotation turns the *camera*, so the world turns the other way: at `rotation = +π/2` the camera has
14
+ * tilted its head anticlockwise and what was to its right is now above it.
15
+ */
16
+ export function worldToNdc(camera, viewportWidth, viewportHeight, out) {
17
+ const rotation = camera.rotation ?? 0;
18
+ const cos = Math.cos(rotation);
19
+ const sin = Math.sin(rotation);
20
+ const perPixelX = 2 / viewportWidth;
21
+ const perPixelY = 2 / viewportHeight;
22
+ const a = camera.zoom * cos * perPixelX;
23
+ const c = camera.zoom * sin * perPixelX;
24
+ const b = -camera.zoom * sin * perPixelY;
25
+ const d = camera.zoom * cos * perPixelY;
26
+ out[0] = a;
27
+ out[1] = b;
28
+ out[2] = c;
29
+ out[3] = d;
30
+ out[4] = -(a * camera.x + c * camera.y);
31
+ out[5] = -(b * camera.x + d * camera.y);
32
+ return out;
33
+ }
34
+ /**
35
+ * The affine for CSS pixels with a top-left origin — the convention `InsetRect` and `fillPanel`
36
+ * already use, and the one a caller laying out an overlay already has.
37
+ */
38
+ export function screenToNdc(viewportWidth, viewportHeight, out) {
39
+ out[0] = 2 / viewportWidth;
40
+ out[1] = 0;
41
+ out[2] = 0;
42
+ out[3] = -2 / viewportHeight;
43
+ out[4] = -1;
44
+ out[5] = 1;
45
+ return out;
46
+ }
@@ -0,0 +1,30 @@
1
+ /*! DriftEngine | Copyright 2026 Drift Technologies | Apache-2.0 | https://github.com/drftrun/driftengine */
2
+ /**
3
+ * `@driftengine/ui2d` — the 2D layer.
4
+ *
5
+ * A quad is the whole of 2D, so this package is one batch and one pass over it: sprites, the
6
+ * sheets they are cut from, the tilemaps that place thousands of them, and the interface tree that
7
+ * lays a screen out. It draws through `registerPass` rather than through a verb on the renderer,
8
+ * which is what lets it be a package at all — see `pass.ts` in core, which names this package as
9
+ * one of the three that mechanism exists for.
10
+ */
11
+ export { createAffine2D, screenToNdc, worldToNdc } from './camera2d.ts';
12
+ export type { Affine2D, Camera2D } from './camera2d.ts';
13
+ export { SPRITE_FLOATS, createSpriteBatch, drawSprite, resetSpriteBatch, spriteRun, } from './spriteBatch.ts';
14
+ export type { SpriteBatch, SpritePlacement, SpriteRun, UvRect } from './spriteBatch.ts';
15
+ export { DEFAULT_SPRITE_CAPACITY, DEFAULT_SPRITE_SLOTS, createSpritePass } from './spritePass.ts';
16
+ export type { SpritePass, SpritePassOptions } from './spritePass.ts';
17
+ export { DEFAULT_SPRITE_TEXTURE_OPTIONS } from './spriteTexture.ts';
18
+ export type { SpriteImage, SpriteTextureOptions } from './spriteTexture.ts';
19
+ export { createSpriteFrame, frameOf, gridSheet, namedSheet, sheetFrame, sheetFrameHeight, sheetFrameWidth, } from './spriteSheet.ts';
20
+ export type { SheetEntry, SpriteFrame, SpriteSheet } from './spriteSheet.ts';
21
+ export { TILE_EMPTY, createTilemap, drawTilemap, setTile, tileAt } from './tilemap.ts';
22
+ export type { Tilemap, ViewRect } from './tilemap.ts';
23
+ export { addUiChild, createUiNode, removeUiChild, uiNodeNamed, uiRectHolds } from './uiNode.ts';
24
+ export type { UiAlign, UiJustify, UiNode, UiNodeOptions, UiRect, UiSize } from './uiNode.ts';
25
+ export { layoutUiTree } from './uiLayout.ts';
26
+ export { drawUiTree } from './uiDraw.ts';
27
+ export type { UiContentSink } from './uiDraw.ts';
28
+ export { uiFocusNext, uiFocusOrder, uiFocusPrevious, uiHitTest } from './uiFocus.ts';
29
+ export { createUiInput, resetUiInput, routeUiKey, routeUiPointer, setUiFocus } from './uiInput.ts';
30
+ export type { UiInput } from './uiInput.ts';
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ /*! DriftEngine | Copyright 2026 Drift Technologies | Apache-2.0 | https://github.com/drftrun/driftengine */
2
+ /**
3
+ * `@driftengine/ui2d` — the 2D layer.
4
+ *
5
+ * A quad is the whole of 2D, so this package is one batch and one pass over it: sprites, the
6
+ * sheets they are cut from, the tilemaps that place thousands of them, and the interface tree that
7
+ * lays a screen out. It draws through `registerPass` rather than through a verb on the renderer,
8
+ * which is what lets it be a package at all — see `pass.ts` in core, which names this package as
9
+ * one of the three that mechanism exists for.
10
+ */
11
+ export { createAffine2D, screenToNdc, worldToNdc } from './camera2d.js';
12
+ export { SPRITE_FLOATS, createSpriteBatch, drawSprite, resetSpriteBatch, spriteRun, } from './spriteBatch.js';
13
+ export { DEFAULT_SPRITE_CAPACITY, DEFAULT_SPRITE_SLOTS, createSpritePass } from './spritePass.js';
14
+ export { DEFAULT_SPRITE_TEXTURE_OPTIONS } from './spriteTexture.js';
15
+ export { createSpriteFrame, frameOf, gridSheet, namedSheet, sheetFrame, sheetFrameHeight, sheetFrameWidth, } from './spriteSheet.js';
16
+ export { TILE_EMPTY, createTilemap, drawTilemap, setTile, tileAt } from './tilemap.js';
17
+ export { addUiChild, createUiNode, removeUiChild, uiNodeNamed, uiRectHolds } from './uiNode.js';
18
+ export { layoutUiTree } from './uiLayout.js';
19
+ export { drawUiTree } from './uiDraw.js';
20
+ export { uiFocusNext, uiFocusOrder, uiFocusPrevious, uiHitTest } from './uiFocus.js';
21
+ export { createUiInput, resetUiInput, routeUiKey, routeUiPointer, setUiFocus } from './uiInput.js';
@@ -0,0 +1,54 @@
1
+ export declare const SPRITE_FRAG_WGSL = "struct Uniforms {\n uOutputTransform: i32,\n uOutputExposure: f32,\n}\n\n@group(0) @binding(1) \nvar<uniform> unnamed: Uniforms;\n@group(0) @binding(32) \nvar uSpriteTexture_t: texture_2d<f32>;\n@group(0) @binding(33) \nvar uSpriteTexture_s: sampler;\nvar<private> vUv_1: vec2<f32>;\nvar<private> vTint_1: vec4<f32>;\nvar<private> fragColor: vec4<f32>;\n\nfn linearToSrgb_u0028_vf3_u003b(c: ptr<function, vec3<f32>>) -> vec3<f32> {\n var low: vec3<f32>;\n var high: vec3<f32>;\n\n let _e55 = (*c);\n low = (_e55 * 12.92f);\n let _e57 = (*c);\n high = ((pow(max(_e57, vec3<f32>(0f, 0f, 0f)), vec3<f32>(0.41666666f, 0.41666666f, 0.41666666f)) * 1.055f) - vec3(0.055f));\n let _e63 = high;\n let _e64 = low;\n let _e65 = (*c);\n return mix(_e63, _e64, step(_e65, vec3<f32>(0.0031308f, 0.0031308f, 0.0031308f)));\n}\n\nfn rrtAndOdtFit_u0028_vf3_u003b(v: ptr<function, vec3<f32>>) -> vec3<f32> {\n var a: vec3<f32>;\n var b: vec3<f32>;\n\n let _e55 = (*v);\n let _e56 = (*v);\n a = ((_e55 * (_e56 + vec3(0.0245786f))) - vec3(0.000090537f));\n let _e62 = (*v);\n let _e63 = (*v);\n b = ((_e62 * ((_e63 * 0.983729f) + vec3(0.432951f))) + vec3(0.238081f));\n let _e70 = a;\n let _e71 = b;\n return (_e70 / _e71);\n}\n\nfn acesFilmic_u0028_vf3_u003b(x: ptr<function, vec3<f32>>) -> vec3<f32> {\n var param: vec3<f32>;\n\n let _e55 = unnamed.uOutputExposure;\n let _e56 = (*x);\n (*x) = (_e56 * _e55);\n let _e58 = (*x);\n param = (mat3x3<f32>(vec3<f32>(0.59719f, 0.076f, 0.0284f), vec3<f32>(0.35458f, 0.90834f, 0.13383f), vec3<f32>(0.04823f, 0.01566f, 0.83777f)) * _e58);\n let _e60 = rrtAndOdtFit_u0028_vf3_u003b((&param));\n return clamp((mat3x3<f32>(vec3<f32>(1.60475f, -0.10208f, -0.00327f), vec3<f32>(-0.53108f, 1.10813f, -0.07276f), vec3<f32>(-0.07367f, -0.00605f, 1.07602f)) * _e60), vec3(0f), vec3(1f));\n}\n\nfn applyOutputTransform_u0028_vf3_u003b(c_1: ptr<function, vec3<f32>>) -> vec3<f32> {\n var param_1: vec3<f32>;\n var param_2: vec3<f32>;\n\n let _e56 = unnamed.uOutputTransform;\n if (_e56 == 0i) {\n let _e58 = (*c_1);\n return _e58;\n }\n let _e60 = unnamed.uOutputTransform;\n if (_e60 == 2i) {\n let _e62 = (*c_1);\n param_1 = _e62;\n let _e63 = acesFilmic_u0028_vf3_u003b((&param_1));\n (*c_1) = _e63;\n }\n let _e64 = (*c_1);\n param_2 = _e64;\n let _e65 = linearToSrgb_u0028_vf3_u003b((&param_2));\n return _e65;\n}\n\nfn main_1() {\n var texel: vec4<f32>;\n var colour: vec4<f32>;\n var param_3: vec3<f32>;\n\n let _e55 = vUv_1;\n let _e56 = textureSample(uSpriteTexture_t, uSpriteTexture_s, _e55);\n texel = _e56;\n let _e57 = texel;\n let _e58 = vTint_1;\n colour = (_e57 * _e58);\n let _e61 = colour[3u];\n if (_e61 < 0.00392157f) {\n discard;\n }\n let _e63 = colour;\n param_3 = _e63.xyz;\n let _e65 = applyOutputTransform_u0028_vf3_u003b((&param_3));\n let _e66 = colour;\n colour = vec4<f32>(_e65.x, _e65.y, _e65.z, _e66.w);\n let _e72 = colour;\n let _e75 = colour[3u];\n let _e76 = (_e72.xyz * _e75);\n let _e78 = colour[3u];\n fragColor = vec4<f32>(_e76.x, _e76.y, _e76.z, _e78);\n return;\n}\n\n@fragment \nfn main(@location(0) vUv: vec2<f32>, @location(1) vTint: vec4<f32>) -> @location(0) vec4<f32> {\n vUv_1 = vUv;\n vTint_1 = vTint;\n main_1();\n let _e5 = fragColor;\n return _e5;\n}\n";
2
+ export declare const SPRITE_VERT_WGSL = "struct Uniforms {\n uToNdc0_: vec4<f32>,\n uToNdc1_: vec4<f32>,\n uClipCorrection: mat4x4<f32>,\n}\n\nstruct gl_PerVertex {\n @builtin(position) gl_Position: vec4<f32>,\n gl_PointSize: f32,\n}\n\nstruct VertexOutput {\n @location(0) member: vec2<f32>,\n @location(1) member_1: vec4<f32>,\n @builtin(position) gl_Position: vec4<f32>,\n}\n\nvar<private> gl_VertexIndex_1: i32;\nvar<private> aOrigin_1: vec2<f32>;\nvar<private> aEdges_1: vec4<f32>;\n@group(0) @binding(0) \nvar<uniform> unnamed: Uniforms;\nvar<private> vUv: vec2<f32>;\nvar<private> aUv_1: vec4<f32>;\nvar<private> vTint: vec4<f32>;\nvar<private> aTint_1: vec4<f32>;\nvar<private> unnamed_1: gl_PerVertex = gl_PerVertex(vec4<f32>(0f, 0f, 0f, 1f), 1f);\n\nfn main_1() {\n var corner: vec2<f32>;\n var indexable: array<vec2<f32>, 6>;\n var p: vec2<f32>;\n var ndc: vec2<f32>;\n\n let _e28 = gl_VertexIndex_1;\n indexable = array<vec2<f32>, 6>(vec2<f32>(0f, 0f), vec2<f32>(1f, 0f), vec2<f32>(0f, 1f), vec2<f32>(0f, 1f), vec2<f32>(1f, 0f), vec2<f32>(1f, 1f));\n let _e30 = indexable[_e28];\n corner = _e30;\n let _e31 = aOrigin_1;\n let _e32 = aEdges_1;\n let _e35 = corner[0u];\n let _e38 = aEdges_1;\n let _e41 = corner[1u];\n p = ((_e31 + (_e32.xy * _e35)) + (_e38.zw * _e41));\n let _e46 = unnamed.uToNdc0_[0u];\n let _e48 = p[0u];\n let _e52 = unnamed.uToNdc0_[2u];\n let _e54 = p[1u];\n let _e59 = unnamed.uToNdc1_[0u];\n let _e63 = unnamed.uToNdc0_[1u];\n let _e65 = p[0u];\n let _e69 = unnamed.uToNdc0_[3u];\n let _e71 = p[1u];\n let _e76 = unnamed.uToNdc1_[1u];\n ndc = vec2<f32>((((_e46 * _e48) + (_e52 * _e54)) + _e59), (((_e63 * _e65) + (_e69 * _e71)) + _e76));\n let _e79 = aUv_1;\n let _e81 = aUv_1;\n let _e83 = corner;\n vUv = mix(_e79.xy, _e81.zw, _e83);\n let _e85 = aTint_1;\n vTint = _e85;\n let _e87 = unnamed.uClipCorrection;\n let _e88 = ndc;\n unnamed_1.gl_Position = (_e87 * vec4<f32>(_e88.x, _e88.y, 0f, 1f));\n return;\n}\n\n@vertex \nfn main(@builtin(vertex_index) gl_VertexIndex: u32, @location(3) aOrigin: vec2<f32>, @location(0) aEdges: vec4<f32>, @location(1) aUv: vec4<f32>, @location(2) aTint: vec4<f32>) -> VertexOutput {\n gl_VertexIndex_1 = i32(gl_VertexIndex);\n aOrigin_1 = aOrigin;\n aEdges_1 = aEdges;\n aUv_1 = aUv;\n aTint_1 = aTint;\n main_1();\n let _e16 = unnamed_1.gl_Position.y;\n unnamed_1.gl_Position.y = -(_e16);\n let _e18 = vUv;\n let _e19 = vTint;\n let _e20 = unnamed_1.gl_Position;\n return VertexOutput(_e18, _e19, _e20);\n}\n";
3
+ /**
4
+ * What the transform assigned, so the renderer binds the same numbers.
5
+ *
6
+ * A permuted shader has one entry per variant, keyed as `FLAT_FRAG_WGSL` is.
7
+ */
8
+ export declare const SPRITE_BINDINGS: {
9
+ readonly SPRITE_FRAG: {
10
+ readonly uniforms: 1;
11
+ readonly uniformSize: 16;
12
+ readonly fields: {
13
+ readonly uOutputTransform: {
14
+ readonly offset: 0;
15
+ readonly size: 4;
16
+ readonly type: "int";
17
+ };
18
+ readonly uOutputExposure: {
19
+ readonly offset: 4;
20
+ readonly size: 4;
21
+ readonly type: "float";
22
+ };
23
+ };
24
+ readonly textures: {
25
+ readonly uSpriteTexture: {
26
+ readonly texture: 32;
27
+ readonly sampler: 33;
28
+ readonly type: "sampler2D";
29
+ };
30
+ };
31
+ };
32
+ readonly SPRITE_VERT: {
33
+ readonly uniforms: 0;
34
+ readonly uniformSize: 96;
35
+ readonly fields: {
36
+ readonly uToNdc0: {
37
+ readonly offset: 0;
38
+ readonly size: 16;
39
+ readonly type: "vec4";
40
+ };
41
+ readonly uToNdc1: {
42
+ readonly offset: 16;
43
+ readonly size: 16;
44
+ readonly type: "vec4";
45
+ };
46
+ readonly uClipCorrection: {
47
+ readonly offset: 32;
48
+ readonly size: 64;
49
+ readonly type: "mat4";
50
+ };
51
+ };
52
+ readonly textures: {};
53
+ };
54
+ };