From efe693179916a41206795ca8eafb5e1ac686f237 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Wed, 19 Nov 2025 18:09:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9B=BE=E9=9B=86=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E6=9B=B4=E6=96=B0=EF=BC=8C=E4=BD=86=E6=8A=9B?= =?UTF-8?q?=E5=87=BA=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client-base/src/material/asset.ts | 35 ------------------- .../client-base/src/material/builder.ts | 6 ++++ .../client-base/src/material/manager.ts | 27 +++++++------- .../client-modules/src/render/map/renderer.ts | 1 + packages/common/src/logger.json | 1 + 5 files changed, 20 insertions(+), 50 deletions(-) delete mode 100644 packages-user/client-base/src/material/asset.ts diff --git a/packages-user/client-base/src/material/asset.ts b/packages-user/client-base/src/material/asset.ts deleted file mode 100644 index 95b23f6..0000000 --- a/packages-user/client-base/src/material/asset.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { ITextureComposedData } from '@motajs/render-assets'; -import { IMaterialAsset } from './types'; -import { IDirtyMark } from '@motajs/common'; - -export class MaterialAsset implements IMaterialAsset { - /** 标记列表 */ - private readonly marks: WeakMap = new WeakMap(); - /** 脏标记,所有值小于此标记的都视为需要更新 */ - private dirtyFlag: number = 0; - - constructor(readonly data: ITextureComposedData) {} - - dirty(): void { - this.dirtyFlag++; - } - - mark(): IDirtyMark { - const symbol = {}; - this.marks.set(symbol, this.dirtyFlag); - return symbol; - } - - unmark(mark: IDirtyMark): void { - this.marks.delete(mark); - } - - dirtySince(mark: IDirtyMark): boolean { - const value = this.marks.get(mark) ?? -1; - return value < this.dirtyFlag; - } - - hasMark(symbol: IDirtyMark): boolean { - return this.marks.has(symbol); - } -} diff --git a/packages-user/client-base/src/material/builder.ts b/packages-user/client-base/src/material/builder.ts index 695ef20..4d1307a 100644 --- a/packages-user/client-base/src/material/builder.ts +++ b/packages-user/client-base/src/material/builder.ts @@ -113,10 +113,16 @@ class TrackedAssetData } markDirty(index: number) { + if (index >= this.length) { + this.updateLength(index + 1); + } this.dirty(index); } async updateSource(index: number, source: SizedCanvasImageSource) { + if (index >= this.length) { + this.updateLength(this.length + 1); + } const origin = this.originSourceMap.get(index); const prev = this.sourceList.get(index); if (origin) { diff --git a/packages-user/client-base/src/material/manager.ts b/packages-user/client-base/src/material/manager.ts index 955acce..4098395 100644 --- a/packages-user/client-base/src/material/manager.ts +++ b/packages-user/client-base/src/material/manager.ts @@ -155,21 +155,10 @@ export class MaterialManager implements IMaterialManager { addAutotile( source: SizedCanvasImageSource, identifier: IBlockIdentifier - ): IMaterialData | null { - const frames = source.width === 96 ? 1 : 4; - const flattened = AutotileProcessor.flatten({ source, frames }); - if (!flattened) return null; - const texture = new Texture(flattened); - this.tileStore.addTexture(identifier.num, texture); + ): void { + this.autotileSource.set(identifier.num, source); this.tileStore.alias(identifier.num, identifier.id); this.clsMap.set(identifier.num, BlockCls.Autotile); - const data: IMaterialData = { - store: this.tileStore, - texture, - identifier: identifier.num, - alias: identifier.id - }; - return data; } addTileset( @@ -231,9 +220,15 @@ export class MaterialManager implements IMaterialManager { getTile(identifier: number): IMaterialFramedData | null { if (identifier < 10000) { + const cls = this.clsMap.get(identifier) ?? BlockCls.Unknown; + if ( + cls === BlockCls.Autotile && + this.autotileSource.has(identifier) + ) { + this.cacheAutotile(identifier); + } const texture = this.tileStore.getTexture(identifier); if (!texture) return null; - const cls = this.clsMap.get(identifier) ?? BlockCls.Unknown; return { texture, cls, @@ -402,6 +397,7 @@ export class MaterialManager implements IMaterialManager { this.tileStore.addTexture(identifier, tex); const data = this.assetBuilder.addTexture(tex); tex.toAsset(data); + this.autotileSource.delete(identifier); this.checkAssetDirty(data); return tex; } @@ -418,6 +414,7 @@ export class MaterialManager implements IMaterialManager { const tex = new Texture(flattened); this.tileStore.addTexture(v, tex); toAdd.push(tex); + this.autotileSource.delete(v); }); const data = this.assetBuilder.addTextureList(toAdd); @@ -542,7 +539,7 @@ export class MaterialManager implements IMaterialManager { } getIfBigImage(identifier: number): IMaterialFramedData | null { - const bigImage = this.bigImageData.get(identifier) ?? null; + const bigImage = this.bigImageData.get(identifier); if (bigImage) return bigImage; else return this.getTile(identifier); } diff --git a/packages-user/client-modules/src/render/map/renderer.ts b/packages-user/client-modules/src/render/map/renderer.ts index efea1b3..f9ad3b5 100644 --- a/packages-user/client-modules/src/render/map/renderer.ts +++ b/packages-user/client-modules/src/render/map/renderer.ts @@ -785,6 +785,7 @@ export class MapRenderer if (dirty.size === 0) return; this.assetData.unmark(data.tileTextureMark); data.tileTextureMark = this.assetData.mark(); + logger.warn(87); gl.bindTexture(gl.TEXTURE_2D_ARRAY, tile); const sizeChanged = this.checkTextureArraySize( gl, diff --git a/packages/common/src/logger.json b/packages/common/src/logger.json index 80cd1ae..c54c87d 100644 --- a/packages/common/src/logger.json +++ b/packages/common/src/logger.json @@ -134,6 +134,7 @@ "84": "Cannot set alias '$1' for layer, since '$1' is already an alias for another layer.", "85": "Hook to load does not belong to current hookable object.", "86": "Cannot restore vertex data since delivered state does not belong to current vertex generator instance.", + "87": "Map texture asset cache not hit. This has no effect on rendering, but may extremely affect performance, please see the following link to find solution.", "1001": "Item-detail extension needs 'floor-binder' and 'floor-damage' extension as dependency.", "1101": "Cannot add new effect to point effect instance, for there's no more reserve space for it. Please increase the max count of the instance." }