Refactoring round 9 covers everything since round 8 (14972d0): 62 commits with about 29 000 new lines in src, api, tools and tests. Behaviour is unchanged. The one fix is in a check: e2e 04 counted 25 asset types, and the shader function from #303 makes 26, so the run ofa526e1bfailed there. Duplicate code (jscpd over src, 50 tokens, without the two licence texts: 35 clones / 289 lines before, 29 / 232 after): - ShaderPicture (new, viewport/shaderPicture.ts) is the picture a camera's shader and a particle field's shader are laid over. It keeps the render texture while its size holds and frees the replaced one once the shader holds the new one. CameraShade and ParticleField.shadeField drew it the same way, line for line. - CameraShade builds its mesh on the first draw. Before, every redraw of a view without a camera shader built a mesh and threw it away. - emptyHost in inspector/host.ts builds the empty host the Inspector, the Variables tab and the Tilemap panel start from. - paddedQuad(null) is the plain unit quad. symbols.ts and shaderPasses.ts each wrote it out by hand. - In graph/nested.ts, calls of shader functions and On behavior nodes are followed by one walk, `reaches`, over ids `namedBy` a node type. Dead code: - ShaderEditor.colorFieldOf and the getter CurveCanvas.snapping had no reader. - 22 values used only in their own file lose their `export`. Types and the node catalogues of shaders and particles stay exported. - Kept on purpose: PhysicsSystem.holdsJoint is read by tests only, but it is their one view of whether a joint holds. - A pass with stricter lint rules over every file changed since round 8 found no bug. PixelArtEditor.started is now readonly. Comments and docs: - Ten comments told history ("since #190", "used to", "any more"). They now describe what is. The bundle is md5-identical before and after, and the four e2e files minify to the same bytes. - README: joint and shader function in the asset list, joints on the Physics row, what the Shaders page covers, two CI runners. - e2e 04 keeps the number of asset types once, as ASSET_TYPE_COUNT. - screenshot.png in the README is a new picture of the editor (3840x2160 instead of 1825x1140), taken by the user. Tests: - Gate on melektaus-atx: 229 files, 3514 tests, CHECK EXIT 0 (+3 for emptyHost, +1 for the unit quad). - e2e 1-4, 31, 63 on melektaus-atx: ALL PASSED, 195 checks. That is the + menu of 04, the field shader of 31 and the camera shader of 63, both through ShaderPicture. - No new e2e check, so the sum stays 1728. - Counter-checks, each put back and compared with cmp: reaches that never goes on fails 1 test, listensTo reading the wrong parameter fails 1, emptyHost dropping what a panel hands in fails 1, folding without a relayout fails 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
4.3 KiB
TypeScript
101 lines
4.3 KiB
TypeScript
/**
|
|
* A shader's Padding as geometry (#69): the quad grows by so many pixels of the picture and its
|
|
* UVs grow with it, so the picture stays where it was and the margin reads past its border.
|
|
*/
|
|
import { describe, expect, it } from 'vitest';
|
|
import type { NineSlice } from '../../src/core/model/texture';
|
|
import { paddedSpans, sliceGeometry, sliceKey, sliceSpans } from '../../src/viewport/nineSlice';
|
|
import { paddedQuad, paddingKey, quadPadding } from '../../src/viewport/quadPadding';
|
|
|
|
const slice = (scale = 1): NineSlice => ({
|
|
border: { left: 8, top: 4, right: 8, bottom: 4 },
|
|
fill: 'stretch',
|
|
sourceW: 24,
|
|
sourceH: 16,
|
|
scale,
|
|
});
|
|
|
|
describe('quadPadding', () => {
|
|
it('counts in pixels of the picture, per axis', () => {
|
|
expect(quadPadding(2, 16, 32)).toEqual({ u: 0.125, v: 0.0625 });
|
|
});
|
|
|
|
it('is nothing without padding, and without a picture to count in', () => {
|
|
expect(quadPadding(0, 16, 16)).toBeNull();
|
|
expect(quadPadding(-1, 16, 16)).toBeNull();
|
|
expect(quadPadding(2, 0, 16)).toBeNull();
|
|
expect(quadPadding(2, 16, 0)).toBeNull();
|
|
expect(quadPadding(Number.NaN, 16, 16)).toBeNull();
|
|
});
|
|
|
|
it('keys two margins apart, and no margin as the shared quad', () => {
|
|
expect(paddingKey(null)).toBe('');
|
|
expect(paddingKey(quadPadding(2, 16, 16))).not.toBe(paddingKey(quadPadding(2, 16, 32)));
|
|
expect(paddingKey(quadPadding(2, 16, 16))).toBe(paddingKey(quadPadding(4, 32, 32)));
|
|
});
|
|
});
|
|
|
|
describe('paddedQuad', () => {
|
|
it('grows on every side and reads past the border by the same amount', () => {
|
|
const { positions, uvs, indices } = paddedQuad({ u: 0.125, v: 0.25 });
|
|
expect([...positions]).toEqual([-0.125, -0.25, 1.125, -0.25, 1.125, 1.25, -0.125, 1.25]);
|
|
// Same numbers: 0..1 of the box is still 0..1 of the picture, so the picture does not move.
|
|
expect([...uvs]).toEqual([...positions]);
|
|
expect([...indices]).toEqual([0, 1, 2, 0, 2, 3]);
|
|
});
|
|
|
|
it('is the plain unit quad without a margin', () => {
|
|
const { positions, uvs } = paddedQuad(null);
|
|
expect([...positions]).toEqual([0, 0, 1, 0, 1, 1, 0, 1]);
|
|
expect([...uvs]).toEqual([...positions]);
|
|
});
|
|
});
|
|
|
|
describe('a sliced sprite with padding', () => {
|
|
it('gets a ring as wide as that many border pixels, reading that far past the frame', () => {
|
|
const spans = paddedSpans(sliceSpans(100, 24, 8, 8, 'stretch'), 100, 24, 3);
|
|
expect(spans.map((s) => [s.p0, s.p1])).toEqual([
|
|
[-3, 0],
|
|
[0, 8],
|
|
[8, 92],
|
|
[92, 100],
|
|
[100, 103],
|
|
]);
|
|
expect(spans[0]).toMatchObject({ t0: -3 / 24, t1: 0 });
|
|
expect(spans.at(-1)).toMatchObject({ t0: 1, t1: 1 + 3 / 24 });
|
|
});
|
|
|
|
it('draws the ring at the scale the borders are drawn at', () => {
|
|
const spans = paddedSpans(sliceSpans(100, 24, 8, 8, 'stretch', 2), 100, 24, 3, 2);
|
|
expect([spans[0]!.p0, spans.at(-1)!.p1]).toEqual([-6, 106]);
|
|
// Twice as wide on screen, the same three pixels of the picture.
|
|
expect(spans[0]!.t0).toBe(-3 / 24);
|
|
});
|
|
|
|
it('leaves the parts alone without padding, and adds nothing to a sprite of no size', () => {
|
|
const plain = sliceSpans(100, 24, 8, 8, 'stretch');
|
|
expect(paddedSpans(plain, 100, 24, 0)).toBe(plain);
|
|
expect(paddedSpans([], 0, 24, 3)).toEqual([]);
|
|
});
|
|
|
|
it('builds the ring into the geometry: sixteen more quads around the nine', () => {
|
|
const plain = sliceGeometry(slice(), 100, 50);
|
|
const padded = sliceGeometry(slice(), 100, 50, 2);
|
|
expect(plain.indices.length / 6).toBe(9);
|
|
expect(padded.indices.length / 6).toBe(25);
|
|
const xs = [...padded.positions].filter((_, i) => i % 2 === 0);
|
|
const ys = [...padded.positions].filter((_, i) => i % 2 === 1);
|
|
expect([Math.min(...xs), Math.max(...xs)]).toEqual([-2 / 100, 1 + 2 / 100].map(Math.fround));
|
|
expect([Math.min(...ys), Math.max(...ys)]).toEqual([-2 / 50, 1 + 2 / 50].map(Math.fround));
|
|
const us = [...padded.uvs].filter((_, i) => i % 2 === 0);
|
|
const vs = [...padded.uvs].filter((_, i) => i % 2 === 1);
|
|
expect([Math.min(...us), Math.max(...us)]).toEqual([-2 / 24, 1 + 2 / 24].map(Math.fround));
|
|
expect([Math.min(...vs), Math.max(...vs)]).toEqual([-2 / 16, 1 + 2 / 16].map(Math.fround));
|
|
});
|
|
|
|
it('keeps the key of an unpadded slice as it was', () => {
|
|
expect(sliceKey(slice(), 100, 50)).toBe(sliceKey(slice(), 100, 50));
|
|
expect(sliceGeometry(slice(), 100, 50, 0).positions).toEqual(sliceGeometry(slice(), 100, 50).positions);
|
|
});
|
|
});
|