Shader graph: Clamp is the Output's default edge, and the sheet's own mode is a constant of its own (#81)
game-maker / suite (push) Successful in 9m16s

The default of the Edge param and the address mode the shared sheet physically
carries were the same constant; changing the value would have turned every
sentinel with it and given each shader sprite a full copy of its texture.
SHEET_EDGE ('mirror', what TextureLibrary uploads) is now separate from
DEFAULT_EDGE ('clamp'). A graph whose Sample reads nothing but its own pixel
draws with the plain sheet whatever the Edge says, so the common shader costs
no second copy.

Without a shader the quad's coordinates never leave 0..1, and mirror-repeat
reads like clamp within a texel - so clamp is what a sprite shows anyway.
This commit is contained in:
admin committed 2026-09-13 21:33:41 +02:00
1 parent 03c4323794
commit e0c459ce92
7 files changed
+74 -20

No files matched your search

+16 -2
View File
@@ -10,7 +10,9 @@ import {
floatLiteral,
isShaderNodeDef,
SHADER_OUTPUT,
SHADER_SAMPLE,
SHADER_TIME,
SHEET_EDGE,
STRAIGHT_FN,
TEXEL_ALPHA,
TEXEL_COLOR,
@@ -29,7 +31,10 @@ export interface CompiledShader {
uniforms: ShaderUniform[];
/** A Time node feeds the Output: the fragment takes `uTime`, and a sprite wearing it redraws every tick. */
time: boolean;
/** How the picture continues past its border – the texture variant the sprite's mesh draws with. */
/**
* The texture variant the sprite's mesh draws with – the Output's Edge where the graph samples
* past the border, and the plain sheet where it cannot.
*/
edge: EdgeMode;
}
@@ -177,5 +182,14 @@ export function compileShader(graph: Graph): ShaderBuild {
'}',
'',
].join('\n');
return { compiled: { fragment, uniforms, time, edge: edgeOf(output.params) }, problems, reason: '' };
// The edge only shows where a Sample reads somewhere other than its own pixel. A graph that never
// shifts a UV stays on the plain sheet, so the common shader costs no second copy of the picture.
const shifted = graph.nodes.some(
(n) =>
n.type === SHADER_SAMPLE &&
emitted.has(n.id) &&
(edgesInto(graph, n.id, 'u').length > 0 || edgesInto(graph, n.id, 'v').length > 0),
);
const edge = shifted ? edgeOf(output.params) : SHEET_EDGE;
return { compiled: { fragment, uniforms, time, edge }, problems, reason: '' };
}
+10 -4
View File
@@ -56,16 +56,22 @@ export const STRAIGHT_FN = 'vec3 straight(vec4 c) { return c.a > 0.0 ? c.rgb / c
/**
* How the picture continues past its edge when a `Sample texture` reads outside 0..1 – a setting
* of the Output node, because the sampler state is one per draw. Mirror is what every picture has
* anyway (`TextureLibrary`); repeat is for tiles.
* of the Output node, because the sampler state is one per draw.
*/
export type EdgeMode = 'repeat' | 'mirror' | 'clamp';
export const EDGE_PARAM = 'edge';
export const DEFAULT_EDGE: EdgeMode = 'mirror';
/**
* The address mode the shared sheet carries anyway (`TextureLibrary`): within a texel it reads
* like clamp, and no other mode can have it without a cut of its own. Asking for it costs nothing,
* so it is what a sprite without a shader draws with.
*/
export const SHEET_EDGE: EdgeMode = 'mirror';
/** Clamp: the border texel goes on, which is what a sprite without a shader shows (#81). */
export const DEFAULT_EDGE: EdgeMode = 'clamp';
const EDGE_OPTIONS: { value: EdgeMode; label: string }[] = [
{ value: 'clamp', label: 'Clamp' },
{ value: 'mirror', label: 'Mirror' },
{ value: 'repeat', label: 'Repeat' },
{ value: 'clamp', label: 'Clamp' },
];
export function edgeOf(params: PropertyBag): EdgeMode {
+4 -4
View File
@@ -1,6 +1,6 @@
import { Assets, CanvasSource, Texture } from 'pixi.js';
import type { FrameRect, TextureRef } from '../core/model/texture';
import { DEFAULT_EDGE, type EdgeMode } from '../core/shader/nodes';
import { type EdgeMode, SHEET_EDGE } from '../core/shader/nodes';
const DEFAULT_BASE = '/game-maker/data/textures';
@@ -47,10 +47,10 @@ export class TextureLibrary {
* shared source, so two assets naming the same file with different `smooth` fight over it and
* the last one drawn wins; upload avoids that by reusing the existing asset for a known file.
*/
get(ref: TextureRef, edge: EdgeMode = DEFAULT_EDGE): Texture | null {
get(ref: TextureRef, edge: EdgeMode = SHEET_EDGE): Texture | null {
const sheet = this.load(ref.file);
if (!sheet) return null;
const rect = ref.rect ?? (edge === DEFAULT_EDGE ? null : wholeOf(sheet));
const rect = ref.rect ?? (edge === SHEET_EDGE ? null : wholeOf(sheet));
const texture = rect ? this.crop(ref.file, sheet, rect, edge) : sheet;
if (!texture) return null;
const mode = ref.smooth ? 'linear' : 'nearest';
@@ -96,7 +96,7 @@ export class TextureLibrary {
*/
private crop(file: string, sheet: Texture, rect: FrameRect, edge: EdgeMode): Texture | null {
if (rect.w <= 0 || rect.h <= 0) return null;
const key = edge === DEFAULT_EDGE ? cropKey(file, rect) : `${cropKey(file, rect)}|${edge}`;
const key = edge === SHEET_EDGE ? cropKey(file, rect) : `${cropKey(file, rect)}|${edge}`;
let texture = this.crops.get(key);
if (!texture) {
const canvas = new OffscreenCanvas(rect.w, rect.h);
+2 -2
View File
@@ -3,7 +3,7 @@ import { type Container, type Graphics, Mesh, MeshGeometry, Rectangle, Texture }
import { determinant, IDENTITY, type Mat } from '../core/model/matrix';
import { BEHAVIOR_SYMBOL, CONTAINER_SYMBOL, placementOf, textAnchor } from '../core/properties/bounds';
import { type ColliderShape, colliderShape, isTrigger } from '../core/properties/collider';
import { DEFAULT_EDGE } from '../core/shader/nodes';
import { SHEET_EDGE } from '../core/shader/nodes';
import { Theme } from '../ui';
import type { DrawItem } from './items';
import { disposeShader, type ShaderSource, syncShader, wornShader } from './shaders';
@@ -56,7 +56,7 @@ export function syncSpriteMesh(
): void {
// Null while the picture is still loading: the box shows its plain colour until then, and once
// the picture is there the colour tints it.
const edge = wornShader(item.props, shaders)?.edge ?? DEFAULT_EDGE;
const edge = wornShader(item.props, shaders)?.edge ?? SHEET_EDGE;
const texture = item.texture ? textures.get(item.texture, edge) : null;
placeNode(mesh, item.m, size.w, size.h);
mesh.texture = texture ?? Texture.WHITE;