@margoengine/pixel 0.1.0 → 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 -201
- package/NOTICE +4 -4
- package/README.md +335 -335
- package/dist/blit-DyxX_nQq.js +2 -2
- package/dist/crt.js +3 -3
- package/dist/follow.js +4 -4
- package/dist/index.js +4 -4
- package/dist/palette.js +4 -4
- package/dist/render3d.js +4 -4
- package/dist/scale.js +3 -3
- package/package.json +71 -74
package/README.md
CHANGED
|
@@ -1,335 +1,335 @@
|
|
|
1
|
-
# @margoengine/pixel
|
|
2
|
-
|
|
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.
|
|
1
|
+
# @margoengine/pixel
|
|
2
|
+
|
|
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.
|