Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(look): what clips to a clipped layer stays inside what that layer holds

yhh66ca39293f60
diff --git a/pipeline/src/image/imageRaster.ts b/pipeline/src/image/imageRaster.ts
index 4418e8802..597d73337 100644
--- a/pipeline/src/image/imageRaster.ts
+++ b/pipeline/src/image/imageRaster.ts
@@ -661,7 +661,7 @@ function drawLayer(c: Canvas, layer: ImageLayer, parent: Matrix, drawn: Drawn):
for (let i = 0; i < channel.length; i++) channel[i] = inner.px[i * 4 + k]!;
return blurMask(channel, w, h, blur * drawnScale(parent));
}) as [Float32Array, Float32Array, Float32Array, Float32Array];
- if (layer.id) drawn.masks.set(layer.id, a);
+ if (layer.id) drawn.masks.set(layer.id, clip ? a.map((v, i) => v * clip![i]!) : a);
for (let i = 0; i < a.length; i++) {
const alpha = a[i]!;
if (alpha <= 0) continue;
@@ -1236,8 +1236,10 @@ function drawPaint(c: Canvas, layer: ImageLayer, parent: Matrix, drawn: Drawn, o
if (layer.id) drawn.painted.set(layer.id, { entries: (layer.strokes ?? []).length, strokes: drawnStrokes });
const alpha = new Float32Array(w * h);
for (let i = 0; i < alpha.length; i++) alpha[i] = inner.px[i * 4 + 3]!;
- if (layer.id) drawn.masks.set(layer.id, alpha);
- noteBox(drawn, layer.id, alpha, w, h);
+ // What it holds is what its clip let through: what clips to it must not get out past that.
+ const held = clip ? alpha.map((v, i) => v * clip[i]!) : alpha;
+ if (layer.id) drawn.masks.set(layer.id, held);
+ noteBox(drawn, layer.id, held, w, h);
if (layer.from === 'below') {
// It already holds the picture, so it takes that picture's place and does not go over it.
for (let i = 0; i < alpha.length * 4; i++) {
@@ -1271,11 +1273,12 @@ function drawFigure(c: Canvas, layer: ImageLayer, parent: Matrix, drawn: Drawn,
}
if (layer.id) {
drawn.figures.set(layer.id, { ...figure, normals: extended });
- drawn.masks.set(layer.id, figure.mask);
- noteBox(drawn, layer.id, figure.mask, w, h);
+ const held = (mask: Float32Array): Float32Array => (clip ? mask.map((v, i) => v * clip[i]!) : mask);
+ drawn.masks.set(layer.id, held(figure.mask));
+ noteBox(drawn, layer.id, held(figure.mask), w, h);
for (const [tag, mask] of Object.entries(figure.parts)) {
- drawn.masks.set(`${layer.id}.${tag}`, mask);
- noteBox(drawn, `${layer.id}.${tag}`, mask, w, h);
+ drawn.masks.set(`${layer.id}.${tag}`, held(mask));
+ noteBox(drawn, `${layer.id}.${tag}`, held(mask), w, h);
}
}
const [lx, ly, lz] = figure.light;
@@ -1326,8 +1329,9 @@ function drawPicture(c: Canvas, layer: ImageLayer, parent: Matrix, drawn: Drawn,
}
if (layer.id) {
drawn.images.set(layer.id, out);
- drawn.masks.set(layer.id, mask);
- noteBox(drawn, layer.id, mask, w, h);
+ const held = clip ? mask.map((v, i) => v * clip[i]!) : mask;
+ drawn.masks.set(layer.id, held);
+ noteBox(drawn, layer.id, held, w, h);
}
for (let i = 0; i < mask.length; i++) {
if (mask[i]! > 0) put(c, i, out[i * 4]!, out[i * 4 + 1]!, out[i * 4 + 2]!, mask[i]! * opacity * (clip ? clip[i]! : 1), blend);
diff --git a/pipeline/tests/image-raster.test.ts b/pipeline/tests/image-raster.test.ts
index ce51ee113..686be1850 100644
--- a/pipeline/tests/image-raster.test.ts
+++ b/pipeline/tests/image-raster.test.ts
@@ -1117,6 +1117,30 @@ describe('a picture to draw over', () => {
});
});
+describe('clipping to what was itself clipped', () => {
+ const left: ImageLayer = { id: 'left', shape: 'rect', at: [0, 0], size: [32, 64], fill: BLUE };
+ const strokes = [{ points: [[0, 32], [64, 32]], color: RED, size: 20, hardness: 1 }];
+
+ it('a paint layer holds only what its clip let through', () => {
+ const img = draw([left, { id: 'p', shape: 'paint', clip: 'left', strokes } as never, { shape: 'rect', at: [0, 0], size: [64, 64], fill: '#00ff00', clip: 'p' }]);
+ expect(px(img, 10, 32)[1]).toBe(255);
+ expect(px(img, 50, 32)[3]).toBe(0);
+ });
+
+ it('so does a figure, a picture and a blurred layer', () => {
+ const blurred = draw([left, { id: 'p', shape: 'rect', at: [0, 22], size: [64, 20], fill: RED, clip: 'left', blur: 1 }, { shape: 'rect', at: [0, 0], size: [64, 64], fill: '#00ff00', clip: 'p' }]);
+ expect(px(blurred, 50, 32)[3]).toBe(0);
+ const fig = renderImage(validateImageSource({ size: 256, layers: [{ id: 'half', shape: 'rect', at: [0, 0], size: [128, 256], fill: BLUE }, { id: 'f', shape: 'figure', at: [128, 240], height: 220, clip: 'half' }, { shape: 'rect', at: [0, 0], size: [256, 256], fill: RED, clip: 'f' }] }));
+ const m = fig.figures.f!;
+ expect(px(fig as never, m.chest![0] + 12, m.chest![1])[0]).toBe(0);
+ });
+
+ it('and its reported box is what it holds', () => {
+ const img = draw([left, { id: 'p', shape: 'paint', clip: 'left', strokes } as never]);
+ expect(img.layerBounds.p![0] + img.layerBounds.p![2]).toBeLessThanOrEqual(33);
+ });
+});
+
describe('what the third trial tripped on', () => {
const person = { id: 'f', shape: 'figure', at: [128, 240], height: 220 } as ImageLayer;
const looking = (layers: object[]): ReturnType<typeof renderImage> => renderImage(validateImageSource({ size: 256, layers }), { guides: true });
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 05ee6254b..fba6a9367 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3888,6 +3888,8 @@ export const NOTED = {
{"note":"An AI can draw 2D pictures."},
"feat(look): layers moved and wrapped, one pixel changed, a set and a close look drawn":
{"note":"An AI can draw 2D pictures."},
+ "fix(look): what clips to a clipped layer stays inside what that layer holds":
+ {"internal":"the paint, figure and image layers it fixes have not been released"},
"fix(look): a hidden guide still works, opacity 0 hides its bones, a faint layer has a real box":
{"internal":"what it fixes has not been released"},
"feat(look): brushes: flat tips, paper grain, dry streaks, wet mixing, and named media":