Add textures for sprites

Images are uploaded to a content-addressed blob endpoint and stored as
data/textures/<sha256>.<ext>; the project only keeps the file name. The
sprite points at a texture asset through an ordinary property of the new
kind `asset`, so instance overrides, "Apply to container", "Reset to
asset", undo and `Set property` work without any extra code. The new kind
also replaces the ad-hoc asset picker the graph nodes faked as an enum.

Import goes through + -> Texture, drag & drop onto the canvas or Ctrl+V.
Images wider than 2048px are downscaled client-side and re-encoded to
WebP if needed, because nginx caps uploads at 1MB; an image that already
fits is uploaded byte for byte so pixel art is never resampled.

The sprite colour default changes from red to white, since colour tints
the texture multiplicatively and a tint of white shows an image as it is.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
rootandClaude Opus 5 committed 2026-09-08 13:43:08 +02:00
1 parent 6168df41f8
commit 5a6af74e44
47 files changed
+1995 -110

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
import { Assets, type Texture } from 'pixi.js';
import type { TextureRef } from '../core/model/texture';
const DEFAULT_BASE = '/game-maker/data/textures';
/**
* Keeps uploaded images on the GPU, keyed by their file name.
*
* Nothing is ever evicted, and that is safe rather than sloppy: file names are content
* addressed, so a name cannot come to mean different bytes and a cached texture cannot go
* stale. It also means the same picture used by a hundred sprites is uploaded once.
*
* A miss is not an error – it starts the load and reports "not yet", so the caller draws its
* plain colour in the meantime and gets a redraw when the picture arrives. That is why a
* sprite never flickers and why nothing here needs to be awaited.
*/
export class TextureLibrary {
private base = DEFAULT_BASE;
private readonly loaded = new Map<string, Texture>();
private readonly loading = new Set<string>();
/** Files whose load failed; retried never, so one broken reference cannot spam the network. */
private readonly failed = new Set<string>();
private readonly listeners = new Set<() => void>();
/** Where the files are served from. Set once at start-up, before anything draws. */
configure(base: string): void {
this.base = base.replace(/\/+$/, '');
}
/**
* The picture if it is already in memory, otherwise null while it loads.
*
* Caveat of content addressing: two texture *assets* can name the same file (the same image
* uploaded twice) while disagreeing about `smooth`. The scale mode belongs to the shared
* source, so the last one drawn wins – deterministically, since draw order is stable. The
* upload avoids creating that situation by reusing an existing asset for a known file.
*/
get(ref: TextureRef): Texture | null {
const texture = this.loaded.get(ref.file);
if (!texture) {
this.startLoading(ref.file);
return null;
}
const mode = ref.smooth ? 'linear' : 'nearest';
if (texture.source.scaleMode !== mode) texture.source.scaleMode = mode;
return texture;
}
/** Runs `fn` whenever a picture has arrived. Returns the unsubscribe. */
onChange(fn: () => void): () => void {
this.listeners.add(fn);
return () => this.listeners.delete(fn);
}
private startLoading(file: string): void {
if (this.loading.has(file) || this.failed.has(file)) return;
this.loading.add(file);
void Assets.load<Texture>(`${this.base}/${file}`).then(
(texture) => {
this.loading.delete(file);
this.loaded.set(file, texture);
for (const listener of this.listeners) listener();
},
(error: unknown) => {
this.loading.delete(file);
this.failed.add(file);
console.error(`[textures] cannot load ${file}`, error);
},
);
}
}
/**
* The one library. A second instance would only duplicate GPU uploads – the cache is keyed by
* content, so there is nothing an instance could hold that another should not share.
*/
export const textureLibrary = new TextureLibrary();