diff --git a/packages-user/client-base/src/material/manager.ts b/packages-user/client-base/src/material/manager.ts index 63dc684..496db91 100644 --- a/packages-user/client-base/src/material/manager.ts +++ b/packages-user/client-base/src/material/manager.ts @@ -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 }; } diff --git a/packages/render/src/assets/splitter.ts b/packages/render/src/assets/splitter.ts index 0c6f805..548fa2d 100644 --- a/packages/render/src/assets/splitter.ts +++ b/packages/render/src/assets/splitter.ts @@ -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 { *split(texture: ITexture, data: number): Generator { - 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 { */ export class TextureColumnSplitter implements ITextureSplitter { *split(texture: ITexture, data: number): Generator { - 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 { *split(texture: ITexture, data: IRect[]): Generator { - 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); } } } diff --git a/packages/render/src/assets/texture.ts b/packages/render/src/assets/texture.ts index 545e2ca..914a8aa 100644 --- a/packages/render/src/assets/texture.ts +++ b/packages/render/src/assets/texture.ts @@ -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) { + 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 { diff --git a/packages/render/src/assets/types.ts b/packages/render/src/assets/types.ts index e1791c0..4f6113f 100644 --- a/packages/render/src/assets/types.ts +++ b/packages/render/src/assets/types.ts @@ -112,7 +112,13 @@ export interface ITexture { clampRect(rect: Readonly): Readonly; /** - * 获取贴图经过指定矩形裁剪后的可渲染对象,并不是简单地对图像源裁剪,还会处理其他情况 + * 将贴图经过指定矩形裁剪,然后形成一个新的贴图对象 + * @param rect 裁剪矩形 + */ + clip(rect: Readonly): ITexture; + + /** + * 获取贴图经过指定矩形裁剪后的可渲染对象,并不是简单地对图像源裁剪,还会处理边界情况,如裁剪矩形超出了贴图范围 * @param rect 裁剪矩形 */ clipped(rect: Readonly): ITextureRenderable;