A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(asset): wire AssetRefResolver + TextureImportSettings through typed load methods
esengine24f46bd8039c
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.tsindex 85083c1be..54d44d5f6 100644--- a/sdk/src/asset/Assets.ts+++ b/sdk/src/asset/Assets.ts@@ -9,6 +9,7 @@ import { AsyncCache } from './AsyncCache';import type { ESEngineModule } from '../wasm';import type { CppResourceManager } from '../wasm';import { requireResourceManager, evictTextureDimensions } from '../resourceManager';+import type { TextureImportSettings, TextureImportSettingsResolver } from './loaders/TextureLoader';import { TextureLoader } from './loaders/TextureLoader';import { SpineAssetLoader } from './loaders/SpineAssetLoader';import { MaterialAssetLoader } from './loaders/MaterialAssetLoader';@@ -133,6 +134,7 @@ export class Assets {private getAudio_: () => import('../audio/Audio').AudioAPI | null;private loaders_ = new Map<string, AssetLoader<unknown>>();private textureLoader_: TextureLoader;+ private textureImportResolver_: TextureImportSettingsResolver | null = null;private spineLoader_: SpineAssetLoader;private textureCache_ = new AsyncCache<TextureResult>();@@ -177,35 +179,48 @@ export class Assets {// =========================================================================async loadTexture(ref: string): Promise<TextureResult> {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);const cacheKey = this.textureCacheKey_(path, true);- const result = await this.textureCache_.getOrLoad(cacheKey, () =>- this.textureLoader_.load(path, this.getLoadContext_()),- );+ const settings = this.textureImportResolver_?.(ref);+ const result = await this.textureCache_.getOrLoad(cacheKey, () => {+ this.textureLoader_.setPendingSettings(settings);+ return this.textureLoader_.load(path, this.getLoadContext_());+ });this.textureRefCounts_.set(cacheKey, (this.textureRefCounts_.get(cacheKey) ?? 0) + 1);return result;}async loadTextureRaw(ref: string): Promise<TextureResult> {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);const cacheKey = this.textureCacheKey_(path, false);- const result = await this.textureCache_.getOrLoad(cacheKey, () =>- this.textureLoader_.loadRaw(path, this.getLoadContext_()),- );+ const settings = this.textureImportResolver_?.(ref);+ const result = await this.textureCache_.getOrLoad(cacheKey, () => {+ this.textureLoader_.setPendingSettings(settings);+ return this.textureLoader_.loadRaw(path, this.getLoadContext_());+ });this.textureRefCounts_.set(cacheKey, (this.textureRefCounts_.get(cacheKey) ?? 0) + 1);return result;}+ /**+ * Provide per-asset texture import settings at load time (filter, wrap,+ * mipmaps). The resolver is called with the original ref — keyed off+ * `@uuid:...` in editor scenarios. Returning `undefined` uses defaults.+ */+ setTextureImportSettingsResolver(resolver: TextureImportSettingsResolver | null): void {+ this.textureImportResolver_ = resolver;+ }+getTexture(ref: string): TextureResult | undefined {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);return this.textureCache_.get(this.textureCacheKey_(path, true));}async loadSpine(skeletonRef: string, atlasRef?: string): Promise<SpineResult> {- const skelPath = this.catalog.resolve(skeletonRef);+ const skelPath = this.resolveLoadPath_(skeletonRef);const ctx = this.getLoadContext_();if (atlasRef) {- const atlasPath = this.catalog.resolve(atlasRef);+ const atlasPath = this.resolveLoadPath_(atlasRef);return this.spineLoader_.loadWithAtlas(skelPath, atlasPath, ctx);}return this.spineLoader_.load(skelPath, ctx);@@ -252,7 +267,7 @@ export class Assets {// =========================================================================getAtlasFrame(ref: string): AtlasFrameInfo | null {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);return this.catalog.getAtlasFrame(path);}@@ -320,20 +335,20 @@ export class Assets {// =========================================================================async fetchJson<T = unknown>(ref: string): Promise<T> {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);const url = this.backend.resolveUrl(this.catalog.getBuildPath(path));const text = await this.backend.fetchText(url);return JSON.parse(text) as T;}async fetchBinary(ref: string): Promise<ArrayBuffer> {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);const url = this.backend.resolveUrl(this.catalog.getBuildPath(path));return this.backend.fetchBinary(url);}async fetchText(ref: string): Promise<string> {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);const url = this.backend.resolveUrl(this.catalog.getBuildPath(path));return this.backend.fetchText(url);}@@ -504,7 +519,7 @@ export class Assets {// =========================================================================releaseTexture(ref: string): void {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);for (const flip of [true, false]) {const key = this.textureCacheKey_(path, flip);const count = this.textureRefCounts_.get(key);@@ -551,7 +566,7 @@ export class Assets {}private releaseTyped(type: string, ref: string): void {- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);const cache = this.genericCache_.get(type);if (!cache) return;const entry = cache.get(path);@@ -710,6 +725,24 @@ export class Assets {return ref;}+ /**+ * Canonical path-resolution for all typed load methods.+ * Runs the AssetRefResolver first so `@uuid:...` refs map to their real+ * project path, then applies the addressable Catalog for any further+ * indirection. If the resolver returns null (unknown UUID), we fall back+ * to the Catalog on the original ref — the loader will 404 with a clear+ * error rather than silently succeeding on a nonsense URL.+ *+ * Historically each load method called `this.catalog.resolve(ref)`+ * directly, which completely bypassed the AssetRefResolver. Editors that+ * serialized asset refs as `@uuid:...` could never resolve them at load+ * time — textures silently rendered white. This helper closes that gap.+ */+ private resolveLoadPath_(ref: string): string {+ const resolved = this.assetRefResolver_?.(ref) ?? ref;+ return this.catalog.resolve(resolved);+ }+// =========================================================================// Private// =========================================================================@@ -738,7 +771,7 @@ export class Assets {if (!loader) {throw new Error(`No loader registered for type: ${type}`);}- const path = this.catalog.resolve(ref);+ const path = this.resolveLoadPath_(ref);let cache = this.genericCache_.get(type);if (!cache) {diff --git a/sdk/src/asset/index.ts b/sdk/src/asset/index.tsindex 1f3842186..f7c5c53ab 100644--- a/sdk/src/asset/index.ts+++ b/sdk/src/asset/index.ts@@ -23,6 +23,7 @@ export {type AssetManifest,} from './AssetRegistry';export { type AssetLoader, type LoadContext, type TextureResult, type TextureResult as TextureInfo, type SpineResult, type SpineLoadResult, type MaterialResult, type FontResult, type AudioResult } from './AssetLoader';+export { TextureLoader, type TextureImportSettings, type TextureImportSettingsResolver } from './loaders/TextureLoader';export { SceneHandle } from './SceneHandle';export {registerAssetFields, registerCompoundAssetFields,diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.tsindex 08848b9f5..d1bcb828a 100644--- a/sdk/src/asset/loaders/TextureLoader.ts+++ b/sdk/src/asset/loaders/TextureLoader.ts@@ -3,6 +3,20 @@ import { platformCreateCanvas, platformCreateImage } from '../../platform';import { requireResourceManager } from '../../resourceManager';import type { ESEngineModule } from '../../wasm';+/**+ * Texture import-time settings. Applied when the GL texture is first uploaded,+ * because WebGL sampler state lives on the texture object — the only way to+ * change filter/wrap after the fact is to hold the GL texture id and call+ * `texParameteri`. See {@link TextureLoader#importSettingsResolver}.+ */+export interface TextureImportSettings {+ readonly filter?: 'linear' | 'nearest';+ readonly wrap?: 'repeat' | 'clamp' | 'mirror';+ readonly mipmaps?: boolean;+}++export type TextureImportSettingsResolver = (ref: string) => TextureImportSettings | undefined;+export class TextureLoader implements AssetLoader<TextureResult> {readonly type = 'texture';readonly extensions = ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp'];@@ -10,11 +24,24 @@ export class TextureLoader implements AssetLoader<TextureResult> {private module_: ESEngineModule;private canvas_: HTMLCanvasElement | OffscreenCanvas | null = null;private ctx_: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null = null;+ /**+ * Optional hook that returns per-asset import settings. Invoked at+ * load-time with the ORIGINAL ref (pre-resolution), so callers can key+ * off `@uuid:...` directly. Undefined-result falls back to defaults.+ */+ importSettingsResolver: TextureImportSettingsResolver | null = null;constructor(module: ESEngineModule) {this.module_ = module;}+ /** Currently-effective settings for the in-flight load. Set by Assets.ts+ * before delegating to load(); cleared when done. Avoids threading a new+ * param through the AssetLoader interface, which is also implemented by+ * non-texture loaders that don't need it. */+ private pendingSettings_: TextureImportSettings | undefined;+ setPendingSettings(s: TextureImportSettings | undefined): void { this.pendingSettings_ = s; }+private ensureCanvas_(): { canvas: HTMLCanvasElement | OffscreenCanvas; ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D } {if (this.canvas_ && this.ctx_) return { canvas: this.canvas_, ctx: this.ctx_ };this.canvas_ = platformCreateCanvas(256, 256);@@ -25,11 +52,15 @@ export class TextureLoader implements AssetLoader<TextureResult> {}async load(path: string, ctx: LoadContext): Promise<TextureResult> {- return this.loadWithFlip(path, ctx, true);+ const settings = this.pendingSettings_;+ this.pendingSettings_ = undefined;+ return this.loadWithFlip(path, ctx, true, settings);}async loadRaw(path: string, ctx: LoadContext): Promise<TextureResult> {- return this.loadWithFlip(path, ctx, false);+ const settings = this.pendingSettings_;+ this.pendingSettings_ = undefined;+ return this.loadWithFlip(path, ctx, false, settings);}async loadFromPixels(@@ -48,10 +79,12 @@ export class TextureLoader implements AssetLoader<TextureResult> {rm.releaseTexture(asset.handle);}- private async loadWithFlip(path: string, ctx: LoadContext, flip: boolean): Promise<TextureResult> {+ private async loadWithFlip(+ path: string, ctx: LoadContext, flip: boolean, settings?: TextureImportSettings,+ ): Promise<TextureResult> {const url = ctx.backend.resolveUrl(ctx.catalog.getBuildPath(path));const img = await this.loadImage(url);- return this.createTextureFromImage(img, flip);+ return this.createTextureFromImage(img, flip, settings);}private loadImage(src: string): Promise<HTMLImageElement | ImageBitmap> {@@ -79,13 +112,13 @@ export class TextureLoader implements AssetLoader<TextureResult> {}private createTextureFromImage(- img: HTMLImageElement | ImageBitmap, flip: boolean,+ img: HTMLImageElement | ImageBitmap, flip: boolean, settings?: TextureImportSettings,): TextureResult {const { width, height } = img;const gl = this.getWebGL2Context();if (gl) {- return this.createTextureWebGL2(gl, img, width, height, flip);+ return this.createTextureWebGL2(gl, img, width, height, flip, settings);}return this.createTextureFallback(img, width, height, flip);}@@ -106,17 +139,32 @@ export class TextureLoader implements AssetLoader<TextureResult> {gl: WebGL2RenderingContext,img: HTMLImageElement | ImageBitmap,width: number, height: number, flip: boolean,+ settings?: TextureImportSettings,): TextureResult {+ const filter = settings?.filter ?? 'linear';+ const wrap = settings?.wrap ?? 'repeat';+ const useMipmaps = settings?.mipmaps ?? true;+ const glMinFilter = filter === 'nearest'+ ? (useMipmaps ? gl.NEAREST_MIPMAP_NEAREST : gl.NEAREST)+ : (useMipmaps ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);+ const glMagFilter = filter === 'nearest' ? gl.NEAREST : gl.LINEAR;+ const glWrap =+ wrap === 'clamp' ? gl.CLAMP_TO_EDGE :+ wrap === 'mirror' ? gl.MIRRORED_REPEAT :+ gl.REPEAT;+const texture = gl.createTexture()!;gl.bindTexture(gl.TEXTURE_2D, texture);gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip ? 1 : 0);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img as any);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);- gl.generateMipmap(gl.TEXTURE_2D);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glMinFilter);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glMagFilter);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, glWrap);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, glWrap);+ if (useMipmaps) {+ gl.generateMipmap(gl.TEXTURE_2D);+ }gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);const glObj = this.module_.GL;