@margoengine/pixel 0.0.0-stage → 1.0.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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +334 -2
- package/dist/blit-DyxX_nQq.js +165 -0
- package/dist/blit.d.ts +49 -0
- package/dist/camera.d.ts +81 -0
- package/dist/crt.d.ts +63 -0
- package/dist/crt.js +117 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/follow.d.ts +90 -0
- package/dist/follow.js +172 -0
- package/dist/index.d.ts +160 -0
- package/dist/index.js +345 -0
- package/dist/palette.d.ts +124 -0
- package/dist/palette.js +344 -0
- package/dist/render3d.d.ts +139 -0
- package/dist/render3d.js +367 -0
- package/dist/scale.d.ts +38 -0
- package/dist/scale.js +123 -0
- package/dist/texture-option-3VFmNske.js +39 -0
- package/dist/view-rect-sVPGRhlO.js +39 -0
- package/package.json +69 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Scott Campbell
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,335 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @margoengine/pixel
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Import `margoengine/pixel` from the `margoengine` package, or `@margoengine/pixel` from this package alone; the two are the same module. Neither is on npm yet: the [Install](../../README.md#install) section lists the two routes that work today (run in a clone, or install local tarballs).
|
|
4
|
+
|
|
5
|
+
Pixel art that stays crisp. A `PixelCamera` scrolls by whole low-res pixels with parallax per layer and a whole-number
|
|
6
|
+
zoom. A `LayerCompositor` gives each layer its own low-res target and puts them on the screen with no filtering. Each
|
|
7
|
+
output pixel works out which source pixel it shows, so even a layer turned 30 degrees is made only of source colours.
|
|
8
|
+
Core stays the same size: an app that does not import this package ships none of it.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { EngineKernel } from 'margoengine/core';
|
|
12
|
+
import { LayerCompositor, PixelCamera } from 'margoengine/pixel';
|
|
13
|
+
|
|
14
|
+
const canvas = document.querySelector<HTMLCanvasElement>('#viewport')!;
|
|
15
|
+
const engine = await EngineKernel.create(canvas, { exactSize: [640, 360] });
|
|
16
|
+
|
|
17
|
+
// A 160 x 90 view. With no zoom set it takes the largest whole number that fits: 4 here.
|
|
18
|
+
const camera = new PixelCamera({ width: 160, height: 90, smooth: true });
|
|
19
|
+
const compositor = LayerCompositor.attach(engine, { camera });
|
|
20
|
+
|
|
21
|
+
// A picture fixed in the world, at half the camera's speed. An ImageBitmap works too.
|
|
22
|
+
const data = new Uint8Array(160 * 90 * 4).fill(255);
|
|
23
|
+
const hills = compositor.addLayer({ anchor: 'world', parallax: 0.5 });
|
|
24
|
+
hills.write({ width: 160, height: 90, data });
|
|
25
|
+
|
|
26
|
+
// A layer on top of it, turned 30 degrees about the middle of the view and half see-through.
|
|
27
|
+
const glow = compositor.addLayer({ anchor: 'world', blend: 'add', opacity: 0.5, rotation: Math.PI / 6 });
|
|
28
|
+
glow.write({ width: 160, height: 90, data });
|
|
29
|
+
|
|
30
|
+
engine.start();
|
|
31
|
+
|
|
32
|
+
// With smooth: true, half a low-res pixel moves two output pixels at zoom 4.
|
|
33
|
+
compositor.camera.x += 0.5;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`smooth: true` carries fractional low-res scroll into whole output pixels, preserving crisp texels. The default is
|
|
37
|
+
`false`: scrolling then waits for a whole low-res pixel. View layers include the extra border needed by smooth scroll.
|
|
38
|
+
|
|
39
|
+
Draw into a layer with `@margoengine/sprites`, which reads the layer's target and camera each frame, or with a core view.
|
|
40
|
+
A sprite layer's order must be below the compositor's, and its default of 10 is. Views into layers come before the canvas:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { EngineKernel } from 'margoengine/core';
|
|
44
|
+
import { LayerCompositor } from 'margoengine/pixel';
|
|
45
|
+
|
|
46
|
+
const canvas = document.querySelector<HTMLCanvasElement>('#viewport')!;
|
|
47
|
+
const engine = await EngineKernel.create(canvas, { exactSize: [320, 180] });
|
|
48
|
+
const compositor = LayerCompositor.attach(engine, { camera: { width: 160, height: 90 } });
|
|
49
|
+
const actors = compositor.addLayer();
|
|
50
|
+
|
|
51
|
+
// With sprites: SpriteLayer.attach(engine, { atlas, target: actors.target, camera: actors.camera });
|
|
52
|
+
engine.render({
|
|
53
|
+
views: [
|
|
54
|
+
{ camera: engine.camera, viewport: [0, 0, 1, 1], target: { color: actors.texture } },
|
|
55
|
+
{ camera: engine.camera, viewport: [0, 0, 1, 1] },
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## PixelCamera
|
|
61
|
+
|
|
62
|
+
| Option | Default | What it does |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| `width`, `height` | required | Low-res view size, whole numbers |
|
|
65
|
+
| `x`, `y` | 0 | World position at the top-left, y down, read each frame |
|
|
66
|
+
| `zoom` | fits the canvas | Output pixels per low-res pixel. Anything but a whole number throws `RangeError` |
|
|
67
|
+
| `minZoom`, `maxZoom` | 1, 64 | Bounds of `zoom` and of `fit` |
|
|
68
|
+
| `unitsPerPixel` | 1 | World units in one low-res pixel |
|
|
69
|
+
| `smooth` | false | Carries scroll below a low-res pixel into the composite, in whole output pixels |
|
|
70
|
+
|
|
71
|
+
Methods: `fit(width, height)` sets the largest zoom that fits and returns it; `pixelX(parallax)` and `pixelY(parallax)`
|
|
72
|
+
are the whole pixels scrolled; `scroll(parallax)` also gives the rest. `projection(parallax)` gives orthographic bounds
|
|
73
|
+
for `CameraProjection` in `@margoengine/cameras`, so 3D views line up with the same pixels.
|
|
74
|
+
|
|
75
|
+
The scroll goes down to the whole pixel below it, in both directions: `x = 0.9` shows pixel 0 and `x = -0.1` shows
|
|
76
|
+
pixel -1, so a step of one world pixel always moves the picture by one.
|
|
77
|
+
|
|
78
|
+
## LayerCompositor
|
|
79
|
+
|
|
80
|
+
| Option | Default | What it does |
|
|
81
|
+
| --- | --- | --- |
|
|
82
|
+
| `camera` | required | A `PixelCamera`, or its options |
|
|
83
|
+
| `order` | 30 | Frame-graph order. A `Tonemap` at 31 grades what is drawn before it, so set 40 to keep bytes exact |
|
|
84
|
+
| `layers` | 1 | Render-layer bits; a canvas view with `layers` that shares none gets no composite |
|
|
85
|
+
| `name` | `pixel-1`, ... | Plugin name, unique per scene |
|
|
86
|
+
|
|
87
|
+
Methods: `addLayer(options)`, `removeLayer(layer)`, `dispose()` (or `using`). Read `layers` for the list in draw order.
|
|
88
|
+
The picture sits in the middle of the canvas or view. Views into a texture get no composite, since they may be drawing a
|
|
89
|
+
layer.
|
|
90
|
+
|
|
91
|
+
## PixelLayer
|
|
92
|
+
|
|
93
|
+
| Option | Default | What it does |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| `width`, `height` | the camera's | Target size in low-res pixels |
|
|
96
|
+
| `anchor` | `'view'` | `'view'`: whatever draws it follows `layer.camera`. `'world'`: a fixed picture the compositor moves |
|
|
97
|
+
| `parallax` | 1 | Scroll factor, or `[x, y]`. 0 stays put |
|
|
98
|
+
| `x`, `y` | 0 | Offset in low-res pixels |
|
|
99
|
+
| `opacity` | 1 | Multiplies the colours, 0 to 1 |
|
|
100
|
+
| `blend` | `'normal'` | `'normal'`, `'add'`, `'multiply'` or `'screen'` |
|
|
101
|
+
| `rotation` | 0 | Clockwise turn, radians |
|
|
102
|
+
| `pivot` | middle of the view | Point it turns about, in view pixels |
|
|
103
|
+
| `z` | 0 | Higher draws over lower; equal `z` keeps the order added |
|
|
104
|
+
| `visible` | true | False leaves it out |
|
|
105
|
+
|
|
106
|
+
All but size and anchor can change whenever you like. Read `texture` for the `rgba8unorm` target and `target` for the
|
|
107
|
+
same thing as a function; `camera` is the live camera for whatever draws the layer; `write(image)` puts a picture in it.
|
|
108
|
+
Colours in a target are premultiplied by alpha, which is what a sprite layer leaves over a clear target; `write` takes
|
|
109
|
+
straight alpha and converts. A `smooth` camera's `'view'` layers have one extra pixel each way to show when they shift.
|
|
110
|
+
|
|
111
|
+
Exact bytes need one sample per pixel, which `exactSize` gives; an engine with MSAA draws the same picture. Targets come
|
|
112
|
+
back by themselves after a device loss, with the last `write` in them, so read `layer.texture` each frame rather than
|
|
113
|
+
keeping it.
|
|
114
|
+
|
|
115
|
+
## Scaling to any screen
|
|
116
|
+
|
|
117
|
+
`margoengine/pixel/scale` draws a low-res picture as large as the canvas allows when the canvas is not a whole multiple
|
|
118
|
+
of it, such as 320 x 180 art on a 1366 x 768 laptop (4.27 times). Nearest sampling there makes some art pixels one
|
|
119
|
+
screen pixel wider than others; `'sharp-bilinear'` keeps every art pixel flat and mixes only the one screen pixel that
|
|
120
|
+
straddles two of them, by how much of it each covers. `'edge'` first doubles the art by the Scale2x rule, which rounds
|
|
121
|
+
diagonal steps, then scales the same way.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import {EngineKernel} from 'margoengine/core';
|
|
125
|
+
import {LayerCompositor} from 'margoengine/pixel';
|
|
126
|
+
import {PixelScale} from 'margoengine/pixel/scale';
|
|
127
|
+
const canvas = document.querySelector('canvas')!;
|
|
128
|
+
const engine = await EngineKernel.create(canvas, {backend: 'webgpu', exactSize: [1366, 768]});
|
|
129
|
+
const compositor = LayerCompositor.attach(engine, {camera: {width: 320, height: 180}});
|
|
130
|
+
const art = compositor.addLayer({visible: false}); // sprites and maps draw here
|
|
131
|
+
const scale = PixelScale.attach(engine, {source: art.target, filter: 'sharp-bilinear'});
|
|
132
|
+
engine.render({delta: 1 / 60});
|
|
133
|
+
scale.filter = 'edge'; // read each frame
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
| Option | Default | What it does |
|
|
137
|
+
| --- | --- | --- |
|
|
138
|
+
| `source` | required | The low-res picture, premultiplied as in a pixel layer: a texture or a function returning one |
|
|
139
|
+
| `target` | each canvas view | A texture to draw into instead, cleared first; on the canvas the picture goes over what is there |
|
|
140
|
+
| `filter` | `'sharp-bilinear'` | `'nearest'`, `'sharp-bilinear'` or `'edge'`, read each frame |
|
|
141
|
+
| `fit` | `'contain'` | `'contain'`: as large as fits keeping its shape, in the middle; `'stretch'`: the whole view. Read each frame |
|
|
142
|
+
| `layers` | 1 | Render-layer bits; a canvas view whose `layers` shares none is left alone |
|
|
143
|
+
| `order` | `35` | Frame-graph order, after a compositor (30) |
|
|
144
|
+
| `name` | `pixel-scale-1`, ... | Plugin name, unique per scene |
|
|
145
|
+
|
|
146
|
+
The picture covers whole screen pixels: `fitRect(width, height, viewWidth, viewHeight, fit)` gives that rectangle.
|
|
147
|
+
`'edge'` needs room for the doubled picture: below 2 times it draws as `'sharp-bilinear'`. Colours mix as stored, not
|
|
148
|
+
in linear light. `scale2x(image)` is the CPU form of the doubling, to export sprites at twice the size.
|
|
149
|
+
Canvas draws intersect the fitted picture with the view's scissor and canvas bounds, without rescaling the clipped
|
|
150
|
+
picture. PixelScale and CRT may switch output formats and return to a cached format. Direct `dispose()` unregisters
|
|
151
|
+
the pass from its owning scene, even when another scene has a plugin of the same name, and frees its uniform buffers.
|
|
152
|
+
|
|
153
|
+
## CRT look
|
|
154
|
+
|
|
155
|
+
`margoengine/pixel/crt` draws a low-res picture as an old tube screen would show it: the glass bows out, each art row
|
|
156
|
+
is a beam bright in its middle with darker gaps between rows, an aperture grille passes red, green and blue in turn
|
|
157
|
+
across the screen columns, and glow adds a soft blur of the picture on top. It is worked out from the art's own rows,
|
|
158
|
+
so the lines stay one per art pixel at any screen size.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import {EngineKernel} from 'margoengine/core';
|
|
162
|
+
import {LayerCompositor} from 'margoengine/pixel';
|
|
163
|
+
import {CRT} from 'margoengine/pixel/crt';
|
|
164
|
+
const canvas = document.querySelector('canvas')!;
|
|
165
|
+
const engine = await EngineKernel.create(canvas, {backend: 'webgpu', exactSize: [960, 720]});
|
|
166
|
+
const compositor = LayerCompositor.attach(engine, {camera: {width: 256, height: 192}});
|
|
167
|
+
const art = compositor.addLayer({visible: false}); // sprites and maps draw here
|
|
168
|
+
const crt = CRT.attach(engine, {source: art.target, curvature: 0.1, scanlines: 0.6});
|
|
169
|
+
engine.render({delta: 1 / 60});
|
|
170
|
+
crt.glow = 0.4; // every look setting is read each frame
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
| Option | Default | What it does |
|
|
174
|
+
| --- | --- | --- |
|
|
175
|
+
| `source`, `target`, `fit`, `layers` | as `PixelScale` | The picture, where it goes and how it fills the view |
|
|
176
|
+
| `curvature` | `0.08` | How much the glass bends, 0 (flat) to 0.5; outside the glass is black |
|
|
177
|
+
| `scanlines` | `0.5` | How dark the gaps between art rows get, 0 to 1 |
|
|
178
|
+
| `mask` | `0.2` | How much the grille dims the two colours a screen column does not pass, 0 to 1 |
|
|
179
|
+
| `glow` | `0.25` | How much of a 3 x 3 blur of the art is added, 0 to 1 |
|
|
180
|
+
| `brightness` | `1.2` | Multiplies the beam to win back what the lines and grille take, 0 to 4 |
|
|
181
|
+
| `order` | `35` | Frame-graph order, after a compositor (30) |
|
|
182
|
+
| `name` | `crt-1`, ... | Plugin name, unique per scene |
|
|
183
|
+
|
|
184
|
+
With every effect at 0 and `brightness` 1 the CRT is plain nearest sampling, including exact source boundaries:
|
|
185
|
+
output coordinate `o` inside a fitted side of length `w` selects source texel `floor((2 * o + 1) * n / (2 * w))`.
|
|
186
|
+
Flat glass uses this integer quotient, so a boundary selects the texel on its right or below. The grille needs about 3 screen pixels a
|
|
187
|
+
column to read as one; below that it shows as a faint pattern.
|
|
188
|
+
|
|
189
|
+
## Palettes and dithering
|
|
190
|
+
|
|
191
|
+
`margoengine/pixel/palette` reduces a low-res picture to a palette: one pass from a layer into another, with each pixel becoming exactly one palette colour. Matching is in Oklab or plain sRGB. Ordered Bayer 2, 4 and 8 and a 64 x 64 blue-noise tile dither before matching. `swap` draws other colours for the same matches.
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
import {EngineKernel} from 'margoengine/core';
|
|
195
|
+
import {LayerCompositor} from 'margoengine/pixel';
|
|
196
|
+
import {PaletteQuantize, paletteRamp} from 'margoengine/pixel/palette';
|
|
197
|
+
const canvas = document.querySelector('canvas')!;
|
|
198
|
+
const engine = await EngineKernel.create(canvas, {backend: 'webgpu', exactSize: [320, 180]});
|
|
199
|
+
const compositor = LayerCompositor.attach(engine, {camera: {width: 320, height: 180}});
|
|
200
|
+
const drawn = compositor.addLayer({visible: false}); // sprites and maps draw here
|
|
201
|
+
const shown = compositor.addLayer();
|
|
202
|
+
PaletteQuantize.attach(engine, {
|
|
203
|
+
palette: paletteRamp('#101820', '#f4e9d8', 4),
|
|
204
|
+
source: drawn.target,
|
|
205
|
+
target: shown.target,
|
|
206
|
+
dither: 'bayer4',
|
|
207
|
+
});
|
|
208
|
+
engine.render({delta: 1 / 60});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
| Option | Default | What it does |
|
|
212
|
+
| --- | --- | --- |
|
|
213
|
+
| `palette` | required | 1 to 256 colours, `#rrggbb` or `[r, g, b]` bytes. `setPalette` swaps it |
|
|
214
|
+
| `swap` | the palette | Colours to draw for each match, same length |
|
|
215
|
+
| `source`, `target` | required | Different textures, or functions returning them; `target` is cleared first |
|
|
216
|
+
| `distance` | `'oklab'` | `'oklab'` or `'srgb'` |
|
|
217
|
+
| `dither` | `'none'` | `'none'`, `'bayer2'`, `'bayer4'`, `'bayer8'`, `'blue-noise'`, read each frame |
|
|
218
|
+
| `spread` | `0.125` | Finite, non-negative pattern strength in sRGB values, read each frame |
|
|
219
|
+
| `premultiplied` | `true` | Colours are multiplied by alpha, as in a pixel layer |
|
|
220
|
+
| `order` | `20` | Frame-graph order, after sprites and maps and before the compositor |
|
|
221
|
+
| `name` | `palette-1`, ... | Plugin name, unique per scene |
|
|
222
|
+
|
|
223
|
+
The pattern is fixed to the target's pixels. `shiftPaletteHue(colors, degrees)` and `paletteRamp(from, to, steps)` make palettes in Oklab; `bayerMatrix`, `blueNoise` and `nearestPaletteIndex` are the CPU forms the pass uses.
|
|
224
|
+
|
|
225
|
+
Palette validation names the failing field, its allowed range or enum choices
|
|
226
|
+
and a help link. Hue-shift degrees and pass order must be finite, ramp steps an
|
|
227
|
+
integer of at least 2, Bayer size 2/4/8, and blue-noise size an integer in
|
|
228
|
+
`[4, 256]`. An unreadable colour identifies its palette index. Mutable `spread`
|
|
229
|
+
is checked before GPU work, including rejection of infinity. The shader retains
|
|
230
|
+
descriptive settings, texture and entry-point names, with readable layout;
|
|
231
|
+
matching, dithering, alpha and palette-swap calculations are unchanged.
|
|
232
|
+
|
|
233
|
+
### Palette cycling and ramps
|
|
234
|
+
|
|
235
|
+
`cyclePalette(colors, cycles, seconds)` rotates runs of palette entries over time, the old way to animate water, fire
|
|
236
|
+
and lights by repainting no pixel; `mixPalettes(a, b, t)` mixes two palettes entry by entry in Oklab, for day and
|
|
237
|
+
night. Hand the result to `setSwap` each frame: it rewrites the drawn colours in place and keeps the matching.
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import {EngineKernel} from 'margoengine/core';
|
|
241
|
+
import {LayerCompositor} from 'margoengine/pixel';
|
|
242
|
+
import {cyclePalette, mixPalettes, PaletteQuantize} from 'margoengine/pixel/palette';
|
|
243
|
+
const canvas = document.querySelector('canvas')!;
|
|
244
|
+
const engine = await EngineKernel.create(canvas, {backend: 'webgpu', exactSize: [320, 180]});
|
|
245
|
+
const compositor = LayerCompositor.attach(engine, {camera: {width: 320, height: 180}});
|
|
246
|
+
const drawn = compositor.addLayer({visible: false});
|
|
247
|
+
const shown = compositor.addLayer();
|
|
248
|
+
// Entries 1 to 3 are water; dusk is the same palette in the evening.
|
|
249
|
+
const day = ['#101820', '#1d4e89', '#3a7fc1', '#7fb8e6', '#f4e9d8'];
|
|
250
|
+
const dusk = ['#100818', '#2a2a6a', '#4a4a9a', '#8a78b8', '#e8b890'];
|
|
251
|
+
const pass = PaletteQuantize.attach(engine, {palette: day, source: drawn.target, target: shown.target});
|
|
252
|
+
const water = {from: 1, to: 3, rate: 4, blend: true}; // 4 entries a second, mixed between steps
|
|
253
|
+
// Three frames a quarter of a second apart; a game does this in its frame loop.
|
|
254
|
+
for (const seconds of [0, 0.25, 0.5]) {
|
|
255
|
+
pass.setSwap(cyclePalette(mixPalettes(day, dusk, seconds), [water], seconds));
|
|
256
|
+
engine.render({delta: 0.25});
|
|
257
|
+
}
|
|
258
|
+
console.log(cyclePalette(['#000000', '#ff0000', '#00ff00', '#0000ff'], [{from: 1, to: 3, rate: 1}], 1)); // ['#000000', '#0000ff', '#ff0000', '#00ff00']
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
| Field of a cycle | What it does |
|
|
262
|
+
| --- | --- |
|
|
263
|
+
| `from`, `to` | The run, both ends included, `from < to`; runs must not share entries |
|
|
264
|
+
| `rate` | Entries a second the colours move toward `to`; below 0 toward `from` |
|
|
265
|
+
| `blend` | Mixes in Oklab between steps instead of jumping a whole entry. Default false |
|
|
266
|
+
|
|
267
|
+
The finite product `rate * seconds` is reduced modulo the run length before subtracting entry indices, so large
|
|
268
|
+
finite shifts preserve the rotation. A product that overflows throws `MargoError` with code `'state'` and a help link;
|
|
269
|
+
reduce the rate or elapsed time. `PaletteQuantize.dispose()` unregisters only its owning scene and destroys its
|
|
270
|
+
palette textures, dither textures and uniform buffer. `LayerCompositor.dispose()` likewise removes only its owner.
|
|
271
|
+
|
|
272
|
+
## 3D to pixels
|
|
273
|
+
|
|
274
|
+
`margoengine/pixel/render3d` draws the 3D scene small. `Render3D` holds a low-res view with depth, and outlines what it shows from depth and surface direction. Pass its `view` to `render` with your canvas view.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import {Camera, EngineKernel, PbrMaterial, box} from 'margoengine/core';
|
|
278
|
+
import {LayerCompositor} from 'margoengine/pixel';
|
|
279
|
+
import {Render3D, renderSpriteFrames} from 'margoengine/pixel/render3d';
|
|
280
|
+
const canvas = document.querySelector('canvas')!;
|
|
281
|
+
const engine = await EngineKernel.create(canvas, {backend: 'webgpu', exactSize: [640, 360]});
|
|
282
|
+
const camera = new Camera({position: [3, 2, 4], target: [0, 0, 0]});
|
|
283
|
+
const mesh = engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial({color: '#84d6bd'}));
|
|
284
|
+
const compositor = LayerCompositor.attach(engine, {camera: {width: 160, height: 90}});
|
|
285
|
+
const layer = compositor.addLayer();
|
|
286
|
+
const small = Render3D.attach(engine, {camera, width: 160, height: 90, target: layer.target});
|
|
287
|
+
engine.render({delta: 1 / 60, views: [small.view, {camera, viewport: [0, 0, 1, 1]}]});
|
|
288
|
+
|
|
289
|
+
const sheet = renderSpriteFrames(engine, mesh, {camera, angles: [0, Math.PI / 4], width: 32, height: 32});
|
|
290
|
+
console.log(sheet.frames.length); // 2; sheet.texture works as a SpriteLayer atlas
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
| Option | Default | What it does |
|
|
294
|
+
| --- | --- | --- |
|
|
295
|
+
| `camera`, `width`, `height` | required | The view and its low-res size |
|
|
296
|
+
| `target` | a texture of its own, `output` | Where the picture ends up, the same size |
|
|
297
|
+
| `outline` | on | `{color: '#000000', depthEdge: 0.1, crease: 0.6}` (world units, radians), or false; read each frame |
|
|
298
|
+
| `background` | `transparent` | What the view clears to |
|
|
299
|
+
| `scene`, `layers` | the engine's, every layer | What the view draws |
|
|
300
|
+
| `samples` | `1` | The engine's samples per pixel: 1 with `exactSize` |
|
|
301
|
+
| `order` | `20` | Frame-graph order of the outline pass |
|
|
302
|
+
| `name` | `render3d-1`, ... | Plugin name, unique per scene |
|
|
303
|
+
|
|
304
|
+
Every pixel is one colour of the view or the outline colour. For a palette, chain `PaletteQuantize` of `margoengine/pixel/palette`. `outlineMask` is the CPU form of the outline. `renderSpriteFrames` shoots a mesh at each turn about Y into one sheet and puts its rotation back.
|
|
305
|
+
|
|
306
|
+
## Textures after a device restore
|
|
307
|
+
|
|
308
|
+
`source` and `target` of `PixelScale`, `CRT` and `PaletteQuantize`, and `target` of `Render3D`, take a texture or a function that returns one. A
|
|
309
|
+
function is read every frame, as a pixel layer's `target` getter is, so a restore needs nothing from you. A plain texture belongs to the device you
|
|
310
|
+
made it on: after the device is lost and restored the pass draws nothing, binds nothing of the old device, and reports one `'state'` error through
|
|
311
|
+
`engine.onError` (help `borrowed-lost`) for each lost texture. `Render3D` puts its picture in its own `output` texture until it has a new target.
|
|
312
|
+
Hand in new textures with `setSource` and `setTarget` (all four passes have `setTarget`), usually from your own `onDeviceRestored`; the setter takes the
|
|
313
|
+
engine's device as it is now.
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
import {EngineKernel} from 'margoengine/core';
|
|
317
|
+
import {PixelScale} from 'margoengine/pixel/scale';
|
|
318
|
+
const canvas = document.querySelector('canvas')!;
|
|
319
|
+
const engine = await EngineKernel.create(canvas, {backend: 'webgpu', exactSize: [64, 64]});
|
|
320
|
+
const make = (size: number) => engine.gpu!.createTexture({size: [size, size], format: 'rgba8unorm', usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING});
|
|
321
|
+
const scale = PixelScale.attach(engine, {source: make(16), target: make(32)});
|
|
322
|
+
engine.render({delta: 1 / 60});
|
|
323
|
+
await engine.rebootDevice(); // both textures are gone with the old device
|
|
324
|
+
scale.setSource(make(16));
|
|
325
|
+
scale.setTarget(make(32));
|
|
326
|
+
engine.render({delta: 1 / 60});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## Rendering tiers
|
|
330
|
+
|
|
331
|
+
GPU attachments refuse the small `@margoengine/webgl2-compat/preview` engine during setup with linked `MargoError` code `unsupported`. Use native WebGPU, the full WebGL2 compatibility layer, or awaited [automatic escalation](../webgl2-compat/README.md#automatic-escalation). The package-tier browser regression checks refusal and escalation; the package GPU suite checks drawing on both complete tiers.
|
|
332
|
+
|
|
333
|
+
## Film time
|
|
334
|
+
|
|
335
|
+
`PixelFollow.advanceTime(delta)` is one frame of the rig with no draw (follow, shake, flash, fade; `rig.time` counts the seconds), so `seek` from `@margoengine/cutscene/seek` replays it while film code moves the target on the way.
|