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>
67 lines
2.8 KiB
TypeScript
67 lines
2.8 KiB
TypeScript
import type { Graph, GraphNode } from '../graph/Graph';
|
||
import { colorOf, type PropertyBag, textOf } from '../model/Properties';
|
||
import { declaredRows, numberRow, PROPERTY_DEFAULT_PARAM, PROPERTY_NAME_PARAM } from './declaredRows';
|
||
import { type FlatDef, flatDef, isSpriteProp, isValidFieldName } from './PropertySchema';
|
||
|
||
export { PROPERTY_NAME_PARAM } from './declaredRows';
|
||
|
||
/** The sprite property naming the shader asset it wears; `''` is no shader and no render pass. */
|
||
export const SHADER_KEY = 'shader';
|
||
|
||
/**
|
||
* The node of a shader graph that declares a parameter: its `name` is a sprite property and a
|
||
* GLSL uniform, `default`/`min`/`max` are the row the inspector shows.
|
||
*/
|
||
export const SHADER_PROPERTY = 'ShaderProperty';
|
||
/** The same for a colour (#298): a colour row on the sprite and a vec3 uniform. */
|
||
export const SHADER_COLOR_PROPERTY = 'ShaderColorProperty';
|
||
export const DEFAULT_PROPERTY_COLOR = 0xffffff;
|
||
|
||
/** A row a shader graph adds to its wearers. */
|
||
export type ShaderParamDef = FlatDef<'number'> | FlatDef<'color'>;
|
||
|
||
export function shaderOf(bag: PropertyBag): string {
|
||
return textOf(bag[SHADER_KEY]);
|
||
}
|
||
|
||
export function isPropertyNode(node: GraphNode): boolean {
|
||
return node.type === SHADER_PROPERTY || node.type === SHADER_COLOR_PROPERTY;
|
||
}
|
||
|
||
export function propertyName(node: GraphNode): string {
|
||
return textOf(node.params[PROPERTY_NAME_PARAM] ?? '');
|
||
}
|
||
|
||
/**
|
||
* A property's name is a bag key and a uniform, so it has to be an identifier – and not one of the
|
||
* rows the sprite brings itself: both rows would share the key and the uniform would read the
|
||
* sprite's width instead of the declared default (#63). The compiler names which of the two it is.
|
||
*/
|
||
function isValidPropertyName(name: string): boolean {
|
||
return isValidFieldName(name) && !isSpriteProp(name);
|
||
}
|
||
|
||
/** The properties a shader graph declares: one row on the sprite and one uniform each. */
|
||
export function shaderParamDefs(graph: Graph): ShaderParamDef[] {
|
||
return declaredRows<ShaderParamDef>(graph, {
|
||
isDeclaration: isPropertyNode,
|
||
nameOf: propertyName,
|
||
isValidName: isValidPropertyName,
|
||
rowOf: (name, node) =>
|
||
node.type === SHADER_COLOR_PROPERTY
|
||
? flatDef(name, 'color', { value: propertyColor(node.params) })
|
||
: numberRow(name, node.params),
|
||
});
|
||
}
|
||
|
||
function propertyColor(params: PropertyBag): number {
|
||
return colorOf(params[PROPERTY_DEFAULT_PARAM] ?? DEFAULT_PROPERTY_COLOR, DEFAULT_PROPERTY_COLOR);
|
||
}
|
||
|
||
/** What a property placed from the palette is called: `value`, then `value2`, `value3` … */
|
||
export function nextPropertyName(graph: Graph, base = 'value'): string {
|
||
const taken = new Set(graph.nodes.filter(isPropertyNode).map(propertyName));
|
||
if (!taken.has(base)) return base;
|
||
for (let n = 2; ; n++) if (!taken.has(`${base}${n}`)) return `${base}${n}`;
|
||
}
|