refactor: 调整 Texture.clip 方法

This commit is contained in:
AncTe committed 2026-11-20 16:41:37 +08:00
1 parent 69109d7372
commit 9ac2898b15
4 files changed
+34 -38

No files matched your search

@@ -352,9 +352,9 @@ export class TextureManager implements ITextureManager {
// 计算图块位置,首先计算没有偏移时图块的索引,即假如 Tileset 的左上角是 0,计算 num 的索引应该是什么
const unoffset = adjustedNum - offset * this.tilesetUnit;
const { width, height } = tileset;
const [cellWidth, cellHeight] = cell;
const tileWidth = Math.floor(width / cellWidth);
const tileHeight = Math.floor(height / cellHeight);
const [cw, ch] = cell;
const tileWidth = Math.floor(width / cw);
const tileHeight = Math.floor(height / ch);
// 如果图块位置超出了贴图范围
if (unoffset > tileWidth * tileHeight) {
logger.warn(188, num.toString());
@@ -363,8 +363,7 @@ export class TextureManager implements ITextureManager {
// 裁剪 tileset,生成贴图
const x = unoffset % tileWidth;
const y = Math.floor(unoffset / tileWidth);
const newTexture = new Texture(tileset.source);
newTexture.clip(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
const newTexture = tileset.clip({ x: x * cw, y: y * ch, w: cw, h: ch });
return { existed: false, texture: newTexture };
}
+11 -16
View File
@@ -1,4 +1,3 @@
import { Texture } from './texture';
import { ITexture, ITextureSplitter, IRect } from './types';
/**
@@ -7,12 +6,12 @@ import { ITexture, ITextureSplitter, IRect } from './types';
*/
export class TextureRowSplitter implements ITextureSplitter<number> {
*split(texture: ITexture, data: number): Generator<ITexture> {
const lines = Math.ceil(texture.height / data);
const { width, height } = texture;
const lines = Math.ceil(height / data);
const { x, y } = texture.render().rect;
for (let i = 0; i < lines; i++) {
const tex = new Texture(texture.source);
tex.clip(x, y + i * data, texture.width, data);
yield tex;
const oy = y + i * data;
yield texture.clip({ x, y: oy, w: width, h: data });
}
}
}
@@ -23,12 +22,12 @@ export class TextureRowSplitter implements ITextureSplitter<number> {
*/
export class TextureColumnSplitter implements ITextureSplitter<number> {
*split(texture: ITexture, data: number): Generator<ITexture> {
const lines = Math.ceil(texture.width / data);
const { width, height } = texture;
const lines = Math.ceil(width / data);
const { x, y } = texture.render().rect;
for (let i = 0; i < lines; i++) {
const tex = new Texture(texture.source);
tex.clip(x + i * data, y, data, texture.height);
yield tex;
const ox = x + i * data;
yield texture.clip({ x: ox, y, w: data, h: height });
}
}
}
@@ -45,9 +44,7 @@ export class TextureGridSplitter implements ITextureSplitter<[number, number]> {
const { x, y } = texture.render().rect;
for (let ny = 0; ny < lines; ny++) {
for (let nx = 0; nx < rows; nx++) {
const tex = new Texture(texture.source);
tex.clip(x + nx * w, y + ny * h, w, h);
yield tex;
yield texture.clip({ x: x + nx * w, y: y + ny * h, w, h });
}
}
}
@@ -59,10 +56,8 @@ export class TextureGridSplitter implements ITextureSplitter<[number, number]> {
*/
export class TextureAssetSplitter implements ITextureSplitter<IRect[]> {
*split(texture: ITexture, data: IRect[]): Generator<ITexture> {
for (const { x, y, w, h } of data) {
const tex = new Texture(texture.source);
tex.clip(x, y, w, h);
yield tex;
for (const rect of data) {
yield texture.clip(rect);
}
}
}
+12 -16
View File
@@ -34,34 +34,30 @@ export class Texture implements ITexture {
this.cb = source.height;
}
/**
* 对纹理进行裁剪操作,不会改变图像源
* @param x 裁剪左上角横坐标
* @param y 裁剪左上角纵坐标
* @param w 裁剪宽度
* @param h 裁剪高度
*/
clip(x: number, y: number, w: number, h: number) {
clip(rect: Readonly<IRect>) {
const { x, y, w, h } = rect;
const left = clamp(this.cl + x, this.cl, this.cr);
const top = clamp(this.ct + y, this.ct, this.cb);
const right = clamp(this.cl + x + w, this.cl, this.cr);
const bottom = clamp(this.ct + y + h, this.ct, this.cb);
if (left === right || top === bottom) {
logger.warn(69);
return;
return new Texture(this.source);
}
const width = right - left;
const height = bottom - top;
if (width <= 0 || height <= 0) {
logger.warn(77);
return;
return new Texture(this.source);
}
this.cl = left;
this.ct = top;
this.cr = right;
this.cb = bottom;
this.width = right - left;
this.height = bottom - top;
const newTexture = new Texture(this.source);
newTexture.cl = left;
newTexture.ct = top;
newTexture.cr = right;
newTexture.cb = bottom;
newTexture.width = right - left;
newTexture.height = bottom - top;
return newTexture;
}
async toBitmap(): Promise<void> {
+7 -1
View File
@@ -112,7 +112,13 @@ export interface ITexture {
clampRect(rect: Readonly<IRect>): Readonly<IRect>;
/**
* 获取贴图经过指定矩形裁剪后的可渲染对象,并不是简单地对图像源裁剪,还会处理其他情况
* 将贴图经过指定矩形裁剪,然后形成一个新的贴图对象
* @param rect 裁剪矩形
*/
clip(rect: Readonly<IRect>): ITexture;
/**
* 获取贴图经过指定矩形裁剪后的可渲染对象,并不是简单地对图像源裁剪,还会处理边界情况,如裁剪矩形超出了贴图范围
* @param rect 裁剪矩形
*/
clipped(rect: Readonly<IRect>): ITextureRenderable;