Vector shapes can fill with gradients and textures (#186)
A vector shape could only be filled and outlined with one flat colour. Shading, glows and patterned surfaces had to be faked with stacks of shapes. Every shape now has a paint type for its fill and its outline. - Fill type: solid colour, linear gradient, radial gradient or texture. Line type offers the same without texture. - Colour stops are edited on a bar in the inspector, as agreed in the issue. A click on the bar adds a stop in the colour the gradient already has there, so adding never changes the picture. At least two stops remain. - The new Paint tool in the tab shows two handles for the selected shape: axis start and end, centre and radius, or the corners of one texture tile. "Handles: Fill / Outline" picks which paint is edited. - Moving a whole shape moves its gradients and tile with it. Every gesture is one undo step. - A texture fill always tiles, because Pixi turns a non-repeating pattern into a repeating one anyway. When switched on, one tile covers the whole shape. - Only uploaded textures and frames can be a fill, never a drawing. A drawing filled with itself would rasterise itself mid-raster. - The fingerprint of a drawing includes the texture and frame a fill points to, so the picture redraws when the image is swapped. Findings: - graphics.destroy(true) also destroyed the textures of the fills. With a texture fill it deleted the tile slice and the next redraw crashed on reading 'scaleMode' of null. It is now destroy(). - The gradient cache destroyed old gradients itself, which gave 20 Pixi warnings "destroyed while still bound". They are now handed to Pixi's texture GC with autoGarbageCollect switched on. The console of module 47 is empty again, the last CI log had 0 of them. Tests: - ./check.sh: 135 files, 2011 tests, exit 0 (before 134 / 1986) - npm run build: exit 0 - GM_STEPS=47, freshly seeded: ALL PASSED, 54 checks (before 44), 0 console warnings - 25 new unit tests, 10 new e2e checks in block 47i - Four counter-checks bite, originals restored and verified with cmp - Check sum 1232 in README and wiki, calculated (1222 + 10) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1116818011
commit
592ea942d9
22 files changed
+2090
-98
No files matched your search
@@ -25,7 +25,10 @@ import type {
|
||||
Asset,
|
||||
StateMachineAsset,
|
||||
TextureFrame,
|
||||
GradientStop,
|
||||
TilemapLayer,
|
||||
VectorGradient,
|
||||
VectorPaint,
|
||||
VectorPoint,
|
||||
VectorRing,
|
||||
VectorShape,
|
||||
@@ -603,9 +606,15 @@ function normalizeShapes(raw: unknown): VectorShape[] {
|
||||
fill: normalizeColor(s.fill, SHAPE_FILL_DEFAULT),
|
||||
opacity: normalizeOpacity(s.opacity),
|
||||
};
|
||||
const paint = normalizePaint(s.paint);
|
||||
if (paint) shape.paint = paint;
|
||||
const stroke = asRecord(s.stroke);
|
||||
const width = asFinite(stroke.width);
|
||||
if (width !== null && width > 0) shape.stroke = { color: normalizeColor(stroke.color, 0), width };
|
||||
if (width !== null && width > 0) {
|
||||
shape.stroke = { color: normalizeColor(stroke.color, 0), width };
|
||||
const line = normalizePaint(stroke.paint);
|
||||
if (line && line.type !== 'texture') shape.stroke.paint = line;
|
||||
}
|
||||
shapes.push(shape);
|
||||
}
|
||||
return shapes;
|
||||
@@ -619,6 +628,35 @@ function asFinite(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A gradient or picture fill (#186). One that cannot be drawn as written – no ends, an unknown type,
|
||||
* fewer than two stops – is dropped and the shape reads as the solid colour it kept all along. A
|
||||
* texture id is kept even when nothing answers to it: the drawing falls back to its colour and
|
||||
* shows it, as a sprite shows a texture that has gone.
|
||||
*/
|
||||
function normalizePaint(raw: unknown): VectorPaint | null {
|
||||
const p = asRecord(raw);
|
||||
const from = normalizeHandle(p.from);
|
||||
const to = normalizeHandle(p.to);
|
||||
if (!from || !to) return null;
|
||||
if (p.type === 'texture')
|
||||
return typeof p.texture === 'string' ? { type: 'texture', from, to, texture: p.texture } : null;
|
||||
if (p.type !== 'linear' && p.type !== 'radial') return null;
|
||||
const stops: GradientStop[] = [];
|
||||
for (const item of Array.isArray(p.stops) ? p.stops : []) {
|
||||
const stop = asRecord(item);
|
||||
const offset = asFinite(stop.offset);
|
||||
if (offset === null) continue;
|
||||
stops.push({
|
||||
offset: Math.max(0, Math.min(1, offset)),
|
||||
color: normalizeColor(stop.color, 0),
|
||||
alpha: normalizeOpacity(stop.alpha),
|
||||
});
|
||||
}
|
||||
const gradient: VectorGradient = { type: p.type, from, to, stops };
|
||||
return stops.length >= 2 ? gradient : null;
|
||||
}
|
||||
|
||||
function normalizeHandle(raw: unknown): Vec2 | undefined {
|
||||
const handle = asRecord(raw);
|
||||
const x = asFinite(handle.x);
|
||||
|
||||
Reference in new issue
Block a user