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>
88 lines
3.2 KiB
TypeScript
88 lines
3.2 KiB
TypeScript
/**
|
||
* The room a shader's Padding adds around what wears it (#69): no Pixi and no project, so it stays
|
||
* unit-testable. The quad grows and its UVs grow with it, so the picture keeps its place and size
|
||
* and the margin reads past its border – what is there is the Output's Edge.
|
||
*/
|
||
|
||
/** The margin per side as a fraction of the picture: what both the positions and the UVs grow by. */
|
||
export interface QuadPadding {
|
||
u: number;
|
||
v: number;
|
||
}
|
||
|
||
/**
|
||
* `padding` pixels of a picture `w`×`h` pixels big. A wearer without a picture hands in its own
|
||
* size, so one pixel is one of its units.
|
||
*/
|
||
export function quadPadding(padding: number, w: number, h: number): QuadPadding | null {
|
||
if (!(padding > 0) || !(w > 0) || !(h > 0)) return null;
|
||
return { u: padding / w, v: padding / h };
|
||
}
|
||
|
||
/** What a mesh geometry is built from. */
|
||
interface QuadArrays {
|
||
positions: Float32Array;
|
||
uvs: Float32Array;
|
||
indices: Uint32Array;
|
||
}
|
||
|
||
/** The unit quad with that margin around it, or none; 0..1 is still exactly the picture. */
|
||
export function paddedQuad(pad: QuadPadding | null): QuadArrays {
|
||
const [u, v] = [pad?.u ?? 0, pad?.v ?? 0];
|
||
// `0 - u` rather than `-u`: without a margin that is the plain quad, not one of -0.
|
||
const corners = [0 - u, 0 - v, 1 + u, 0 - v, 1 + u, 1 + v, 0 - u, 1 + v];
|
||
return {
|
||
positions: new Float32Array(corners),
|
||
uvs: new Float32Array(corners),
|
||
indices: new Uint32Array([0, 1, 2, 0, 2, 3]),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* The same rectangle cut into `cells` by `cells` (#79), so a shader's Vertex output has corners to
|
||
* move inside it. Positions and UVs stay one and the same, as on the quad.
|
||
*/
|
||
export function gridQuad(pad: QuadPadding | null, cells: number): QuadArrays {
|
||
const n = Math.max(1, Math.round(cells));
|
||
const [u, v] = [pad?.u ?? 0, pad?.v ?? 0];
|
||
const corners = new Float32Array((n + 1) * (n + 1) * 2);
|
||
for (let row = 0; row <= n; row++) {
|
||
for (let column = 0; column <= n; column++) {
|
||
const at = (row * (n + 1) + column) * 2;
|
||
corners[at] = -u + ((1 + 2 * u) * column) / n;
|
||
corners[at + 1] = -v + ((1 + 2 * v) * row) / n;
|
||
}
|
||
}
|
||
const indices = new Uint32Array(n * n * 6);
|
||
for (let row = 0; row < n; row++) {
|
||
for (let column = 0; column < n; column++) {
|
||
const corner = row * (n + 1) + column;
|
||
const below = corner + n + 1;
|
||
indices.set([corner, corner + 1, below + 1, corner, below + 1, below], (row * n + column) * 6);
|
||
}
|
||
}
|
||
return { positions: corners, uvs: new Float32Array(corners), indices };
|
||
}
|
||
|
||
/** Pixels per side a Pass's picture may have (#301), as many as a field's (#170). */
|
||
export const MAX_PASS_PIXELS = 2048;
|
||
|
||
/**
|
||
* The pixels of a Pass's picture (#301): the wearer's picture of `w`×`h` pixels with its margin, at
|
||
* `scale` of them per pixel – the Pass's Resolution. Never below one, so there is always a pixel.
|
||
*/
|
||
export function passPixels(
|
||
w: number,
|
||
h: number,
|
||
pad: QuadPadding | null,
|
||
scale: number,
|
||
): { w: number; h: number } {
|
||
const side = (n: number, share: number) =>
|
||
Math.min(MAX_PASS_PIXELS, Math.max(1, Math.round(n * (1 + 2 * share) * scale)));
|
||
return { w: side(w, pad?.u ?? 0), h: side(h, pad?.v ?? 0) };
|
||
}
|
||
|
||
export function paddingKey(pad: QuadPadding | null): string {
|
||
return pad ? `${pad.u},${pad.v}` : '';
|
||
}
|