Skip to content

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.

refactor: single-source two duplicated mappings (timeline wrapMode, texture wrap→GL)

yhh274a65be4e36
diff --git a/sdk/src/asset/compressed.ts b/sdk/src/asset/compressed.ts
index 5411ae0ea..ea4e1c6bc 100644
--- a/sdk/src/asset/compressed.ts
+++ b/sdk/src/asset/compressed.ts
@@ -16,6 +16,7 @@
*/
import type { ESEngineModule } from '../wasm';
import { requireResourceManager } from '../resourceManager';
+import { glWrapMode } from './glTexParams';
// =============================================================================
// Format vocabulary
@@ -142,10 +143,7 @@ function applyParams(gl: WebGL2RenderingContext, opts?: CompressedUploadOptions)
// Single-level textures: never select a mipmap min-filter (would be incomplete).
const mag = opts?.filter === 'nearest' ? gl.NEAREST : gl.LINEAR;
const min = mag;
- const wrap =
- opts?.wrap === 'clamp' ? gl.CLAMP_TO_EDGE :
- opts?.wrap === 'mirror' ? gl.MIRRORED_REPEAT :
- gl.REPEAT;
+ const wrap = glWrapMode(gl, opts?.wrap);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, min);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, mag);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
diff --git a/sdk/src/asset/glTexParams.ts b/sdk/src/asset/glTexParams.ts
new file mode 100644
index 000000000..6ee08dafc
--- /dev/null
+++ b/sdk/src/asset/glTexParams.ts
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file glTexParams.ts
+ * @brief Shared WebGL texture-parameter mapping. The wrap-mode → GL enum was
+ * duplicated byte-for-byte in the RGBA (TextureLoader) and compressed
+ * (compressed.ts) upload paths; this is its single source. (Filter →
+ * GL stays per-path: the RGBA path chooses mipmap min-filters, the
+ * single-level compressed path never does.)
+ */
+
+export type TextureWrap = 'repeat' | 'clamp' | 'mirror';
+
+/** WebGL wrap-mode enum for a wrap string. Unknown / undefined → `REPEAT`. */
+export function glWrapMode(gl: WebGL2RenderingContext, wrap: TextureWrap | undefined): number {
+ return wrap === 'clamp' ? gl.CLAMP_TO_EDGE
+ : wrap === 'mirror' ? gl.MIRRORED_REPEAT
+ : gl.REPEAT;
+}
diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.ts
index 48755092e..fa792f9d3 100644
--- a/sdk/src/asset/loaders/TextureLoader.ts
+++ b/sdk/src/asset/loaders/TextureLoader.ts
@@ -7,6 +7,7 @@ import { requireResourceManager } from '../../resourceManager';
import type { ESEngineModule } from '../../wasm';
import { withMalloc } from '../../wasmScratch';
import { isKtx2, loadCompressedTexture, type BasisTranscoder } from '../compressed';
+import { glWrapMode } from '../glTexParams';
import { createTextureFromPixels, type TextureParams } from '../../runtimeAssets';
/**
@@ -239,10 +240,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {
? (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 glWrap = glWrapMode(gl, wrap);
// createTexture returns null on a lost context — don't `!`-assert it
// into the calls below.
diff --git a/sdk/src/timeline/TimelineLoader.ts b/sdk/src/timeline/TimelineLoader.ts
index 5f920547e..263abe619 100644
--- a/sdk/src/timeline/TimelineLoader.ts
+++ b/sdk/src/timeline/TimelineLoader.ts
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import {
- WrapMode,
+ wrapModeFromName,
TrackType,
type TimelineAsset,
type Track,
@@ -53,17 +53,6 @@ function migrateAsset(raw: any): void {
raw.version = CURRENT_VERSION;
}
-const WRAP_MODE_MAP: Record<string, WrapMode> = {
- once: WrapMode.Once,
- loop: WrapMode.Loop,
- pingPong: WrapMode.PingPong,
-};
-
-function parseWrapMode(value: string | undefined): WrapMode {
- if (!value) return WrapMode.Once;
- return WRAP_MODE_MAP[value] ?? WrapMode.Once;
-}
-
function parseTrack(raw: any): Track {
const base = {
name: raw.name ?? '',
@@ -153,7 +142,7 @@ export function parseTimelineAsset(raw: any): TimelineAsset {
version: CURRENT_VERSION,
type: 'timeline',
duration: raw.duration ?? 0,
- wrapMode: parseWrapMode(raw.wrapMode),
+ wrapMode: wrapModeFromName(raw.wrapMode),
tracks: (raw.tracks ?? []).map(parseTrack).filter((t: Track | null) => t !== null),
};
}
diff --git a/sdk/src/timeline/TimelinePlugin.ts b/sdk/src/timeline/TimelinePlugin.ts
index da99269e6..b4e1de069 100644
--- a/sdk/src/timeline/TimelinePlugin.ts
+++ b/sdk/src/timeline/TimelinePlugin.ts
@@ -7,7 +7,7 @@ import { Time, type TimeData } from '../resource';
import { defineComponent, getComponent } from '../component';
import { playModeOnly } from '../env';
import { Audio, type AudioAPI } from '../audio/Audio';
-import { WrapMode, TrackType, type TimelineAsset, type AnimFramesTrack } from './TimelineTypes';
+import { wrapModeFromName, TrackType, type TimelineAsset, type AnimFramesTrack } from './TimelineTypes';
import { Timeline, TimelineApi } from './TimelineControl';
import { resolveChildEntity } from './TimelineRuntime';
import { advanceTimelineTS } from './TimelineDrive';
@@ -50,12 +50,6 @@ export function getTimelineTextureHandle(timelinePath: string, textureUuid: stri
return activeTimelinePlugin?.getTextureHandle(timelinePath, textureUuid) ?? 0;
}
-const WRAP_MODE_MAP: Record<string, WrapMode> = {
- once: WrapMode.Once,
- loop: WrapMode.Loop,
- pingPong: WrapMode.PingPong,
-};
-
interface AnimFramesState {
tracks: AnimFramesTrack[];
lastFrameIndices: number[];
@@ -115,7 +109,7 @@ export class TimelinePlugin implements Plugin {
const asset = this.loadedAssets_.get(player.timeline);
if (!asset) continue;
- const wrapMode = WRAP_MODE_MAP[player.wrapMode] ?? WrapMode.Once;
+ const wrapMode = wrapModeFromName(player.wrapMode);
const state = tl.ensureState(entity, wrapMode, player.speed);
state.speed = player.speed;
state.wrapMode = wrapMode;
diff --git a/sdk/src/timeline/TimelineSerializer.ts b/sdk/src/timeline/TimelineSerializer.ts
index c0f25c153..b53c5c973 100644
--- a/sdk/src/timeline/TimelineSerializer.ts
+++ b/sdk/src/timeline/TimelineSerializer.ts
@@ -12,16 +12,10 @@
* reads `raw.animFrames` into the in-memory `frames`).
*/
-import { WrapMode, TrackType, type TimelineAsset, type Track } from './TimelineTypes';
+import { wrapModeToName, TrackType, type TimelineAsset, type Track } from './TimelineTypes';
const CURRENT_VERSION = '1.1';
-const WRAP_MODE_NAMES: Record<WrapMode, string> = {
- [WrapMode.Once]: 'once',
- [WrapMode.Loop]: 'loop',
- [WrapMode.PingPong]: 'pingPong',
-};
-
function serializeTrack(track: Track): Record<string, unknown> {
const base = { type: track.type, name: track.name, childPath: track.childPath };
@@ -67,7 +61,7 @@ export function serializeTimelineAsset(asset: TimelineAsset): Record<string, unk
version: CURRENT_VERSION,
type: 'timeline',
duration: asset.duration,
- wrapMode: WRAP_MODE_NAMES[asset.wrapMode] ?? 'once',
+ wrapMode: wrapModeToName(asset.wrapMode),
tracks: asset.tracks.map(serializeTrack),
};
}
diff --git a/sdk/src/timeline/TimelineTypes.ts b/sdk/src/timeline/TimelineTypes.ts
index 4d97fe210..608f5696d 100644
--- a/sdk/src/timeline/TimelineTypes.ts
+++ b/sdk/src/timeline/TimelineTypes.ts
@@ -8,6 +8,31 @@ export const WrapMode = {
export type WrapMode = (typeof WrapMode)[keyof typeof WrapMode];
+// Single source for the wrapMode ↔ on-disk-string mapping, shared by the loader,
+// the plugin, and the serializer so the three can't drift.
+const WRAP_MODE_BY_NAME: Record<string, WrapMode> = {
+ once: WrapMode.Once,
+ loop: WrapMode.Loop,
+ pingPong: WrapMode.PingPong,
+};
+
+const WRAP_MODE_NAMES: Record<WrapMode, string> = {
+ [WrapMode.Once]: 'once',
+ [WrapMode.Loop]: 'loop',
+ [WrapMode.PingPong]: 'pingPong',
+};
+
+/** Decode a serialized wrapMode string; unknown / missing → `Once`. */
+export function wrapModeFromName(value: string | undefined): WrapMode {
+ if (!value) return WrapMode.Once;
+ return WRAP_MODE_BY_NAME[value] ?? WrapMode.Once;
+}
+
+/** Encode a wrapMode to its on-disk string (inverse of {@link wrapModeFromName}). */
+export function wrapModeToName(mode: WrapMode): string {
+ return WRAP_MODE_NAMES[mode] ?? 'once';
+}
+
export const TrackType = {
Property: 'property',
Spine: 'spine',