feat: 将贴图转换为图集的一部分

This commit is contained in:
unanmed 2025-10-24 11:17:02 +08:00
parent f9b6d8271e
commit 4fd2fa168b
2 changed files with 41 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import {
IRect, IRect,
ITexture, ITexture,
ITextureAnimater, ITextureAnimater,
ITextureComposedData,
ITextureRenderable, ITextureRenderable,
ITextureSplitter, ITextureSplitter,
SizedCanvasImageSource SizedCanvasImageSource
@ -13,6 +14,7 @@ export class Texture<T = unknown, A = unknown> implements ITexture<T, A> {
animater: ITextureAnimater<T, A> | null = null; animater: ITextureAnimater<T, A> | null = null;
width: number; width: number;
height: number; height: number;
isBitmap: boolean = false;
/** 裁剪矩形的左边框位置 */ /** 裁剪矩形的左边框位置 */
private cl: number; private cl: number;
@ -59,7 +61,18 @@ export class Texture<T = unknown, A = unknown> implements ITexture<T, A> {
async toBitmap(): Promise<void> { async toBitmap(): Promise<void> {
if (this.source instanceof ImageBitmap) return; if (this.source instanceof ImageBitmap) return;
this.source = await createImageBitmap(this.source); this.source = await createImageBitmap(
this.source,
this.cl,
this.ct,
this.width,
this.height
);
this.cl = 0;
this.ct = 0;
this.cr = this.width;
this.cb = this.height;
this.isBitmap = true;
} }
split<U>(splitter: ITextureSplitter<U>, data: U): Generator<ITexture> { split<U>(splitter: ITextureSplitter<U>, data: U): Generator<ITexture> {
@ -110,6 +123,23 @@ export class Texture<T = unknown, A = unknown> implements ITexture<T, A> {
this.animater = null; this.animater = null;
} }
toAsset(asset: ITextureComposedData): boolean {
const rect = asset.assetMap.get(this);
if (!rect) return false;
if (this.isBitmap && this.source instanceof ImageBitmap) {
this.source.close();
}
this.isBitmap = false;
this.source = asset.texture.source;
this.cl = rect.x;
this.ct = rect.y;
this.width = rect.w;
this.height = rect.h;
this.cr = rect.x + rect.w;
this.cb = rect.y + rect.h;
return true;
}
/** /**
* *
* *

View File

@ -98,6 +98,9 @@ export interface ITexture<T = unknown, A = unknown> {
/** 贴图高度 */ /** 贴图高度 */
readonly height: number; readonly height: number;
/** 当前贴图是否是完整 bitmap 图像 */
readonly isBitmap: boolean;
/** /**
* bitmap ImageBitmap * bitmap ImageBitmap
*/ */
@ -151,6 +154,13 @@ export interface ITexture<T = unknown, A = unknown> {
* 使 * 使
*/ */
dispose(): void; dispose(): void;
/**
*
* @param asset
* @returns `false`
*/
toAsset(asset: ITextureComposedData): boolean;
} }
export interface ITextureStore { export interface ITextureStore {