From 3334b78d36bdd039f89767f8cd1d4119538af65c Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Thu, 12 Dec 2019 19:21:17 +0800 Subject: [PATCH] adjust ui-static-batch-component interface (#5891) --- cocos/core/renderer/core/program-lib.ts | 2 +- cocos/core/renderer/ui/ui.ts | 2 +- .../components/ui-static-batch-component.ts | 82 +++++++++---------- 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/cocos/core/renderer/core/program-lib.ts b/cocos/core/renderer/core/program-lib.ts index 08db68ebe2..9bb0cee9ab 100644 --- a/cocos/core/renderer/core/program-lib.ts +++ b/cocos/core/renderer/core/program-lib.ts @@ -165,7 +165,7 @@ function getShaderBindings ( } } -interface IShaderResources { +export interface IShaderResources { shader: GFXShader; bindings: IGFXBinding[]; } diff --git a/cocos/core/renderer/ui/ui.ts b/cocos/core/renderer/ui/ui.ts index 34354ab3b8..ccba39d7d3 100644 --- a/cocos/core/renderer/ui/ui.ts +++ b/cocos/core/renderer/ui/ui.ts @@ -418,7 +418,7 @@ export class UI { StencilManager.sharedManager!.handleMaterial(mat); - const curDrawBatch = this._currStaticRoot ? this._currStaticRoot.requireDrawBatch() : this._drawBatchPool.alloc(); + const curDrawBatch = this._currStaticRoot ? this._currStaticRoot._requireDrawBatch() : this._drawBatchPool.alloc(); curDrawBatch.camera = uiCanvas && uiCanvas.camera; curDrawBatch.bufferBatch = buffer; curDrawBatch.material = mat; diff --git a/cocos/ui/components/ui-static-batch-component.ts b/cocos/ui/components/ui-static-batch-component.ts index f68d0f1255..762e515a4a 100644 --- a/cocos/ui/components/ui-static-batch-component.ts +++ b/cocos/ui/components/ui-static-batch-component.ts @@ -119,50 +119,32 @@ export class UIStaticBatchComponent extends UIRenderComponent { } } - /** - * @zh 开始采集数据标识。调用时存储 ia 数据。 - * 注意:尽量不要频繁调用此接口,因为会清空原先存储的 ia 数据重新采集,会有一定内存损耗。 - */ - @property({ - tooltip: '开始采集数据标识', - }) - get collectMark () { - return this._collectMark; - } - - set collectMark (value) { - const ui = this._getUI(); - if (this._collectMark === value || !ui){ - return; - } - - this.node._static = false; - this._collectMark = true; - this._init = false; - - if (!this._meshBuffer) { - const attr = vfmt; - const buffer = new MeshBuffer(ui); - buffer.initialize(attr, this._arrivalMaxBuffer); - this._meshBuffer = buffer; - } - - this.clearData(); - } - get drawBatchList (){ return this._uiDrawBatchList; } protected _init = false; protected _meshBuffer: MeshBuffer | null = null; - protected _collectMark = false; + protected _dirty = true; private _lastMeshBuffer: MeshBuffer | null = null; private _uiDrawBatchList: UIDrawBatch[] = []; + public onLoad() { + const ui = this._getUI(); + if (!ui) { + return; + } + + const attr = vfmt; + const buffer = new MeshBuffer(ui); + buffer.initialize(attr, this._arrivalMaxBuffer); + this._meshBuffer = buffer; + } + public onDestroy (){ super.onDestroy(); - this.clearData(); + + this._clearData(); if(this._meshBuffer){ this._meshBuffer.destroy(); this._meshBuffer = null; @@ -171,9 +153,9 @@ export class UIStaticBatchComponent extends UIRenderComponent { public updateAssembler (render: UI) { - if (this._collectMark) { + if (this._dirty) { render.finishMergeBatches(); - this._lastMeshBuffer = render.currBufferBatch;; + this._lastMeshBuffer = render.currBufferBatch; render.currBufferBatch = this._meshBuffer; render.currStaticRoot = this; } @@ -185,11 +167,11 @@ export class UIStaticBatchComponent extends UIRenderComponent { } public postUpdateAssembler (render: UI) { - if (this._collectMark) { + if (this._dirty) { render.finishMergeBatches(); render.currBufferBatch = this._lastMeshBuffer; render.currStaticRoot = null; - this._collectMark = false; + this._dirty = false; this._init = true; this.node._static = true; @@ -197,20 +179,34 @@ export class UIStaticBatchComponent extends UIRenderComponent { } } - public requireDrawBatch (){ + /** + * @zh 重新采集数据标记,会在当前帧的渲染阶段重新采集渲染数据,下一帧开始将会使用固定数据进行渲染。 + * 注意:尽量不要频繁调用此接口,因为会清空原先存储的 ia 数据重新采集,会有一定内存损耗。 + */ + public markAsDirty() { + if (!this._getUI()) { + return; + } + + this.node._static = false; + this._dirty = true; + this._init = false; + + this._clearData(); + } + + public _requireDrawBatch() { const batch = new UIDrawBatch(); batch.isStatic = true; this._uiDrawBatchList.push(batch); return batch; } - public clearData(){ - if(this._meshBuffer){ + protected _clearData(){ + if (this._meshBuffer) { this._meshBuffer!.reset(); - } - const ui = this._getUI(); - if(ui){ + const ui = this._getUI()!; for (let i = 0; i < this._uiDrawBatchList.length; i++) { const element = this._uiDrawBatchList[i]; element.destroy(ui);