diff --git a/cocos/core/components/ui-base/ui-render-component.ts b/cocos/core/components/ui-base/ui-render-component.ts index c173cfea44..bb81614d4d 100644 --- a/cocos/core/components/ui-base/ui-render-component.ts +++ b/cocos/core/components/ui-base/ui-render-component.ts @@ -45,7 +45,6 @@ import { RenderableComponent } from '../../../core/3d/framework/renderable-compo import { IAssembler, IAssemblerManager } from '../../renderer/ui/base'; import { UIComponent } from './ui-component'; import { UITransformComponent } from './ui-transfrom-component'; -import { director } from '../../director'; // hack ccenum(GFXBlendFactor); @@ -329,7 +328,7 @@ export class UIRenderComponent extends UIComponent { } } - public postUpdateAssembler(render: UI) { + public postUpdateAssembler (render: UI) { super.postUpdateAssembler(render); if (this._renderFlag) { this._postRender(render); @@ -337,10 +336,10 @@ export class UIRenderComponent extends UIComponent { } // 开始提交渲染数据给中转站 - protected _render(render: UI) { } + protected _render (render: UI) { } // 开始提交渲染数据给中转站 - protected _postRender(render: UI) { } + protected _postRender (render: UI) { } // 像组装器更新渲染数据 protected _checkAndUpdateRenderData (){ diff --git a/cocos/core/platform/SubContextView.ts b/cocos/core/platform/SubContextView.ts index 0af0599a30..4ec7c28d7a 100644 --- a/cocos/core/platform/SubContextView.ts +++ b/cocos/core/platform/SubContextView.ts @@ -1,3 +1,27 @@ +/* + Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ import { Component } from '../components/component'; import { property, ccclass, menu, executionOrder } from '../data/class-decorator'; @@ -47,7 +71,7 @@ export class SubContextView extends Component { this._updateSubContextFrameRate(); } - @property({ type: Number }) + @property private _fps = 60; private _sprite: SpriteComponent | null; private _imageAsset: ImageAsset; diff --git a/cocos/core/renderer/index.ts b/cocos/core/renderer/index.ts index 8898abbc9d..c9f66c6c8e 100644 --- a/cocos/core/renderer/index.ts +++ b/cocos/core/renderer/index.ts @@ -19,4 +19,5 @@ export { Model } from './scene/model'; export { SkinningModel } from './models/skinning-model'; export * from './models/joints-texture-utils'; -import './scene/deprecated'; \ No newline at end of file +import './scene/deprecated'; +import './ui/render-data'; diff --git a/cocos/core/renderer/ui/mesh-buffer.ts b/cocos/core/renderer/ui/mesh-buffer.ts index d912964b14..ccc1335442 100644 --- a/cocos/core/renderer/ui/mesh-buffer.ts +++ b/cocos/core/renderer/ui/mesh-buffer.ts @@ -40,10 +40,6 @@ export class MeshBuffer { public vb: GFXBuffer | null = null; public ib: GFXBuffer | null = null; public ia: GFXInputAssembler | null = null; - // public vbCount: number = 0; - // public vbSize: number = 0; - // public ibCount: number = 0; - // public ibSize: number = 0; public byteStart: number = 0; public byteOffset: number = 0; @@ -93,15 +89,6 @@ export class MeshBuffer { }); this._reallocBuffer(); - - // const batch = this._bufferBatchPool.add(); - // batch.vb = vb; - // batch.ib = ib; - // batch.ia = ia; - // batch.vf32 = null; - // batch.vui16 = null; - // batch.vbSize = vb.size; - // batch.ibSize = ib.size; } public request (vertexCount = 4, indiceCount = 6) { @@ -140,10 +127,6 @@ export class MeshBuffer { } public reset () { - // this._arrOffset = 0; - // this._vb = this._vbArr[0]; - // this._ib = this._ibArr[0]; - this.byteStart = 0; this.byteOffset = 0; this.indiceStart = 0; @@ -155,17 +138,6 @@ export class MeshBuffer { } public destroy () { - // for (const key in this._vbArr) { - // const vb = this._vbArr[key]; - // vb.destroy(); - // } - // this._vbArr = undefined; - - // for (const key in this._ibArr) { - // const ib = this._ibArr[key]; - // ib.destroy(); - // } - // this._ibArr = undefined; this.ib!.destroy(); this.vb!.destroy(); this.ia!.destroy(); @@ -198,7 +170,7 @@ export class MeshBuffer { this._reallocIData(true); } - private _reallocVData (copyOldData) { + private _reallocVData (copyOldData: boolean) { let oldVData; if (this.vData) { oldVData = new Uint8Array(this.vData.buffer); @@ -214,7 +186,7 @@ export class MeshBuffer { } } - private _reallocIData (copyOldData) { + private _reallocIData (copyOldData: boolean) { const oldIData = this.iData; this.iData = new Uint16Array(this._initIDataCount); diff --git a/cocos/core/renderer/ui/render-data.ts b/cocos/core/renderer/ui/render-data.ts index 504eda2e4d..5fe751b517 100644 --- a/cocos/core/renderer/ui/render-data.ts +++ b/cocos/core/renderer/ui/render-data.ts @@ -30,6 +30,8 @@ import { Pool, RecyclePool } from '../../memop'; import { Material } from '../../assets'; import { Color } from '../../math'; +import { GFXTextureView } from '../../gfx'; +import { MeshBuffer } from '../../../ui'; export interface IRenderData { x: number; @@ -126,7 +128,7 @@ export class RenderData extends BaseRenderData { } } -export class IARenderData extends BaseRenderData { +export class MeshRenderData extends BaseRenderData { public vData: Float32Array = new Float32Array(256 * 9 * Float32Array.BYTES_PER_ELEMENT); public iData: Uint16Array = new Uint16Array(256 * 6); public vertexStart = 0; @@ -189,7 +191,7 @@ const _dataPool = new Pool(() => { u: 0, v: 0, color: Color.WHITE.clone(), - } as IRenderData; + }; }, 128); const _pool = new RecyclePool(() => { diff --git a/cocos/core/renderer/ui/ui-batch-model.ts b/cocos/core/renderer/ui/ui-batch-model.ts index 2bba8db22a..ba80c9d690 100644 --- a/cocos/core/renderer/ui/ui-batch-model.ts +++ b/cocos/core/renderer/ui/ui-batch-model.ts @@ -32,7 +32,7 @@ import { GFXPipelineState } from '../../gfx/pipeline-state'; import { Model } from '../scene/model'; import { RenderScene } from '../scene/render-scene'; import { SubModel } from '../scene/submodel'; -import { UIDrawBatch } from './ui'; +import { UIDrawBatch } from './ui-draw-batch'; export class UIBatchModel extends Model { diff --git a/cocos/core/renderer/ui/ui-draw-batch.ts b/cocos/core/renderer/ui/ui-draw-batch.ts new file mode 100644 index 0000000000..90bb4f28a1 --- /dev/null +++ b/cocos/core/renderer/ui/ui-draw-batch.ts @@ -0,0 +1,48 @@ +import { UI } from "./ui"; +import { Camera } from "../scene/camera"; +import { MeshBuffer } from "../../../ui"; +import { Model } from "../scene/model"; +import { Material } from "../../assets"; +import { GFXTextureView, GFXPipelineState } from "../../gfx"; +import { INode } from "../../utils/interfaces"; +import { GFXBindingLayout } from "../../gfx/binding-layout"; + +export class UIDrawBatch { + public camera: Camera | null = null; + public bufferBatch: MeshBuffer | null = null; + public model: Model | null = null; + public material: Material | null = null; + public texView: GFXTextureView | null = null; + public firstIdx: number = 0; + public idxCount: number = 0; + public pipelineState: GFXPipelineState | null = null; + public bindingLayout: GFXBindingLayout | null = null; + public useLocalData: INode | null = null; + public isStatic = false; + + public destroy (ui: UI) { + if (this.pipelineState) { + ui._getUIMaterial(this.material!).revertPipelineState(this.pipelineState); + this.pipelineState = null; + } + + if (this.bindingLayout) { + this.bindingLayout = null; + } + } + + public clear (ui: UI) { + if (this.pipelineState) { + ui._getUIMaterial(this.material!).revertPipelineState(this.pipelineState); + this.pipelineState = null; + } + this.camera = null; + this.bufferBatch = null; + this.material = null; + this.texView = null; + this.firstIdx = 0; + this.idxCount = 0; + this.model = null; + this.isStatic = false; + } +} diff --git a/cocos/core/renderer/ui/ui.ts b/cocos/core/renderer/ui/ui.ts index 491610e927..3ccc776f57 100644 --- a/cocos/core/renderer/ui/ui.ts +++ b/cocos/core/renderer/ui/ui.ts @@ -28,12 +28,10 @@ import { Material } from '../../assets/material'; import { CanvasComponent, UIComponent, UIRenderComponent } from '../../components/ui-base'; -import { GFXBindingLayout } from '../../gfx/binding-layout'; import { GFXCommandBuffer } from '../../gfx/command-buffer'; import { GFXCommandBufferType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { IGFXAttribute } from '../../gfx/input-assembler'; -import { GFXPipelineState } from '../../gfx/pipeline-state'; import { GFXTextureView } from '../../gfx/texture-view'; import { Pool, RecyclePool } from '../../memop'; import { CachedArray } from '../../memop/cached-array'; @@ -49,44 +47,8 @@ import { StencilManager } from './stencil-manager'; import { UIBatchModel } from './ui-batch-model'; import { UIMaterial } from './ui-material'; import * as UIVertexFormat from './ui-vertex-format'; - -export class UIDrawBatch { - public camera: Camera | null = null; - public bufferBatch: MeshBuffer | null = null; - public model: Model | null = null; - public material: Material | null = null; - public texView: GFXTextureView | null = null; - public firstIdx: number = 0; - public idxCount: number = 0; - public pipelineState: GFXPipelineState | null = null; - public bindingLayout: GFXBindingLayout | null = null; - public useLocalData: INode | null = null; - - public destroy (ui: UI) { - if (this.pipelineState) { - ui._getUIMaterial(this.material!).revertPipelineState(this.pipelineState); - this.pipelineState = null; - } - - if (this.bindingLayout) { - this.bindingLayout = null; - } - } - - public clear (ui: UI) { - if (this.pipelineState) { - ui._getUIMaterial(this.material!).revertPipelineState(this.pipelineState); - this.pipelineState = null; - } - this.camera = null; - this.bufferBatch = null; - this.material = null; - this.texView = null; - this.firstIdx = 0; - this.idxCount = 0; - this.model = null; - } -} +import { UIStaticBatchComponent } from '../../../ui'; +import { UIDrawBatch } from './ui-draw-batch'; /** * @zh @@ -102,6 +64,18 @@ export class UI { return this._currMeshBuffer; } + set currBufferBatch (value) { + if(!value){ + return; + } + + this._currMeshBuffer = value; + } + + set currStaticRoot (value: UIStaticBatchComponent | null) { + this._currStaticRoot = value; + } + public device: GFXDevice; private _screens: CanvasComponent[] = []; private _bufferBatchPool: RecyclePool = new RecyclePool(() => { @@ -122,10 +96,11 @@ export class UI { private _modelInUse: CachedArray; // batcher private _emptyMaterial = new Material(); - private _currMeshBuffer: MeshBuffer | null = null; private _currMaterial: Material = this._emptyMaterial; private _currTexView: GFXTextureView | null = null; private _currCanvas = -1; + private _currMeshBuffer: MeshBuffer | null = null; + private _currStaticRoot: UIStaticBatchComponent | null = null; constructor (private _root: Root) { this.device = _root.device; @@ -352,7 +327,7 @@ export class UI { * @param frame - 当前执行组件贴图。 * @param assembler - 当前组件渲染数据组装器。 */ - public commitComp (comp: UIRenderComponent, frame: GFXTextureView | null = null, assembler?: any) { + public commitComp (comp: UIRenderComponent, frame: GFXTextureView | null = null, assembler: any) { const renderComp = comp; const texView = frame; if (this._currMaterial.hash !== renderComp.material!.hash || @@ -414,6 +389,11 @@ export class UI { this._batches.push(curDrawBatch); } + public commitStaticBatch (comp: UIStaticBatchComponent) { + this._batches.append(comp.drawBatchList); + this.finishMergeBatches(); + } + /** * @zh * UI 渲染数据合批 @@ -431,9 +411,9 @@ export class UI { StencilManager.sharedManager!.handleMaterial(mat); - const curDrawBatch = this._drawBatchPool.alloc(); + const curDrawBatch = this._currStaticRoot ? this._currStaticRoot.requireDrawBatch(): this._drawBatchPool.alloc(); curDrawBatch.camera = uiCanvas && uiCanvas.camera; - curDrawBatch.bufferBatch = this._currMeshBuffer; + curDrawBatch.bufferBatch = buffer; curDrawBatch.material = mat; curDrawBatch.texView = this._currTexView!; curDrawBatch.firstIdx = indicsStart; @@ -462,6 +442,12 @@ export class UI { this.autoMergeBatches(); } + public finishMergeBatches (){ + this.autoMergeBatches(); + this._currMaterial = this._emptyMaterial; + this._currTexView = null; + } + private _deleteUIMaterial (mat: Material) { if (this._uiMaterials.has(mat.hash)) { this._uiMaterials.get(mat.hash)!.destroy(); @@ -484,7 +470,7 @@ export class UI { const len = node.children.length; this._preprocess(node); - if (len > 0) { + if (len > 0 && !node._static) { const children = node.children; for (let i = 0; i < children.length; ++i) { const child = children[i]; @@ -531,6 +517,10 @@ export class UI { private _reset () { for (let i = 0; i < this._batches.length; ++i) { const batch = this._batches.array[i]; + if (batch.isStatic) { + continue; + } + batch.clear(this); this._drawBatchPool.free(batch); } diff --git a/cocos/core/scene-graph/node.ts b/cocos/core/scene-graph/node.ts index bf1fb0aa7b..a0fb29dfbc 100644 --- a/cocos/core/scene-graph/node.ts +++ b/cocos/core/scene-graph/node.ts @@ -74,14 +74,10 @@ export class Node extends BaseNode implements INode { */ public static TransformDirtyBit = TransformDirtyBit; - /** - * @zh - * 指定对象是否是普通的场景节点? - * @param obj 待测试的节点 - */ - public static isNode (obj: object | null): obj is Node { - return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene)); - } + // UI 部分的脏数据 + public _uiTransfromComp: UITransformComponent | null = null; + public _uiComp: UIComponent | null = null; + public _static = false; // world transform, don't access this directly protected _pos = new Vec3(); @@ -106,9 +102,14 @@ export class Node extends BaseNode implements INode { protected _dirtyFlags = TransformDirtyBit.NONE; // does the world transform need to update? protected _eulerDirty = false; - private _uiTransfromComp: UITransformComponent | null = null; - // tslint:disable-next-line: member-ordering - public _uiComp: UIComponent | null = null; + /** + * @zh + * 指定对象是否是普通的场景节点? + * @param obj 待测试的节点 + */ + public static isNode (obj: object | null): obj is Node { + return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene)); + } /** * @zh diff --git a/cocos/core/utils/interfaces.ts b/cocos/core/utils/interfaces.ts index f7ed0227fe..bde8752f8e 100644 --- a/cocos/core/utils/interfaces.ts +++ b/cocos/core/utils/interfaces.ts @@ -533,6 +533,8 @@ export interface INode extends IBaseNode { uiTransfromComp; + _static: boolean; + /** * @zh * 获取本地坐标 diff --git a/cocos/ui/assembler/graphics/webgl/graphics-assembler.ts b/cocos/ui/assembler/graphics/webgl/graphics-assembler.ts index d435461c0f..f20bb9793e 100644 --- a/cocos/ui/assembler/graphics/webgl/graphics-assembler.ts +++ b/cocos/ui/assembler/graphics/webgl/graphics-assembler.ts @@ -31,7 +31,7 @@ import { Color, Vec3 } from '../../../../core/math'; import { GFXPrimitiveMode } from '../../../../core/gfx'; import { Model } from '../../../../core/renderer'; import { RenderScene } from '../../../../core/renderer/scene/render-scene'; -import { IARenderData } from '../../../../core/renderer/ui/render-data'; +import { MeshRenderData } from '../../../../core/renderer/ui/render-data'; import { UI } from '../../../../core/renderer/ui/ui'; import { createMesh } from '../../../../core/3d/misc/utils'; import { GraphicsComponent } from '../../../components'; @@ -63,7 +63,7 @@ const uvs: number[] = []; const colors: number[] = []; const indices: number[] = []; -let _renderData: IARenderData | null = null; +let _renderData: MeshRenderData | null = null; let _impl: Impl | null = null; const _curColor = new Color(); @@ -109,40 +109,6 @@ export const graphicsAssembler: IAssembler = { }, renderIA (graphics: GraphicsComponent, renderer: UI) { - // const impl = graphics.impl; - // let renderDatas = impl && impl.getRenderDatas(); - // if (!renderDatas) { - // renderDatas = []; - // } - - // let bufferBatch = renderer.currBufferBatch!; - // let vertexId = 0; - // let byteoffset = 0; - // let indicesOffset = 0; - // for (const renderData of renderDatas) { - // if (!graphics.material || renderData.byteCount <= 0 || renderData.indiceCount <= 0) { - // continue; - // } - - // vertexId = bufferBatch.vertexOffset; - // byteoffset = bufferBatch.byteOffset; - // indicesOffset = bufferBatch.indiceOffset; - // bufferBatch.request(renderData.vertexCount, renderData.indiceCount); - // bufferBatch = renderer.currBufferBatch!; - // const vData = bufferBatch.vData!; - // const iData = bufferBatch.iData!; - // // the data format is strictly in accordance with pos uv color - // const vDataCopy = renderData.vData.slice(0, renderData.byteCount >> 2); - // vData.set(vDataCopy, byteoffset >> 2); - - // let iDataCopy = renderData.iData.slice(0, renderData.indiceCount); - // iDataCopy = iDataCopy.map((v) => { - // return v + vertexId; - // }); - // iData.set(iDataCopy, indicesOffset); - - // renderer.forceMergeBatches(graphics.material, null); - // } }, getRenderData (graphics: GraphicsComponent, cverts: number) { @@ -307,7 +273,7 @@ export const graphicsAssembler: IAssembler = { } } - const meshbuffer: IARenderData | null = _renderData = this.getRenderData!(graphics, cverts); + const meshbuffer: MeshRenderData | null = _renderData = this.getRenderData!(graphics, cverts); if (!meshbuffer) { return; } diff --git a/cocos/ui/assembler/graphics/webgl/impl.ts b/cocos/ui/assembler/graphics/webgl/impl.ts index 8c1c71d93e..12610f8740 100644 --- a/cocos/ui/assembler/graphics/webgl/impl.ts +++ b/cocos/ui/assembler/graphics/webgl/impl.ts @@ -1,6 +1,6 @@ import { Color, Vec2 } from '../../../../core/math'; -import { IARenderData } from '../../../../core/renderer/ui/render-data'; +import { MeshRenderData } from '../../../../core/renderer/ui/render-data'; import RecyclePool from '../../../../core/memop/recycle-pool'; import { arc, ellipse, roundRect, tesselateBezier } from '../helper'; import { LineCap, LineJoin, PointFlags} from '../types'; @@ -74,10 +74,10 @@ export class Impl { private _commandy = 0; private _points: Point[] = []; - private _renderDatasPool: RecyclePool = new RecyclePool(() => { - return new IARenderData(); + private _renderDatasPool: RecyclePool = new RecyclePool(() => { + return new MeshRenderData(); }, 16); - private _renderDatas: IARenderData[] = []; + private _renderDatas: MeshRenderData[] = []; private _curPath: Path | null = null; diff --git a/cocos/ui/components/index.ts b/cocos/ui/components/index.ts index dee67a6804..0844b32957 100644 --- a/cocos/ui/components/index.ts +++ b/cocos/ui/components/index.ts @@ -24,5 +24,6 @@ export { GraphicsComponent } from './graphics-component'; export { UIReorderComponent } from './ui-reorder-component'; export { PageViewComponent } from './page-view-component'; export { PageViewIndicatorComponent } from './page-view-indicator-component'; +export { UIStaticBatchComponent } from './ui-static-batch-component'; export { widgetManager } from './widget-manager'; diff --git a/cocos/ui/components/ui-static-batch-component.ts b/cocos/ui/components/ui-static-batch-component.ts new file mode 100644 index 0000000000..6e1ef9c1a3 --- /dev/null +++ b/cocos/ui/components/ui-static-batch-component.ts @@ -0,0 +1,231 @@ +/* + Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @category ui + */ + +import { UIRenderComponent } from '../../core/components/ui-base/ui-render-component'; +import { UI } from '../../core/renderer/ui/ui'; +import { ccclass, menu, executionOrder, property } from '../../core/data/class-decorator'; +import { MeshBuffer, UIVertexFormat } from '..'; +import { UIDrawBatch } from '../../core/renderer/ui/ui-draw-batch'; +import { director, Color, GFXBlendFactor } from '../../core'; +import { Material } from '@cocos/cannon'; + +/** + * @zh UI 静态合批组件。 + * 放置方式必须是放在在所有需要被静态合批的节点对象的根节点上,子节点放置对象必须是精灵和文本,其余对象不参与静态合批。 + * 用户必须通过手动方式启用收集静态合批数据,否则合批方式仍然采用动态合批(采集数据的流程相同)。此后渲染的内容是采用收集到的合批渲染数据,子节点的任何修改将不再有效。 + * 注意:子节点下不要放置 Mask,Graphics,以及 UI 模型或者粒子之类对象,否则会在启用完静态合批后跳过渲染。 + */ +@ccclass('cc.UIStaticBatchComponent') +@menu('UI/UIStaticBatchComponent') +@executionOrder(110) +export class UIStaticBatchComponent extends UIRenderComponent { + @property({ + visible: false, + override: true, + }) + get dstBlendFactor () { + return this._dstBlendFactor; + } + + set dstBlendFactor (value: GFXBlendFactor) { + if (this._dstBlendFactor === value) { + return; + } + + this._dstBlendFactor = value; + this._updateBlendFunc(); + } + + @property({ + visible: false, + override: true, + }) + get srcBlendFactor () { + return this._srcBlendFactor; + } + + set srcBlendFactor (value: GFXBlendFactor) { + if (this._srcBlendFactor === value) { + return; + } + + this._srcBlendFactor = value; + this._updateBlendFunc(); + } + + @property({ + visible: false, + override: true, + }) + get color (): Readonly { + return this._color; + } + + set color (value) { + if (this._color === value) { + return; + } + + this._color.set(value); + this._updateColor(); + this.markForUpdateRenderData(); + } + + @property({ + type: Material, + displayOrder: 3, + visible: false, + override: true, + }) + get sharedMaterial () { + return this._sharedMaterial; + } + + set sharedMaterial (value) { + if (this._sharedMaterial === value) { + return; + } + + this._sharedMaterial = value; + if (this._instanceMaterial) { + this._instanceMaterial(); + } + } + + /** + * @zh 开始采集数据。调用时存储 ia 数据。 + * 注意:尽量不要频繁调用此接口,因为会清空原先存储的 ia 数据重新采集,会有一定内存损耗。 + */ + @property({ + tooltip: CC_DEV ? '点击采集数据': '' + }) + get collect () { + return this._collect; + } + + set collect (value) { + const ui = this._getUI(); + if (this._collect === value || !ui){ + return; + } + + this.node._static = false; + this._collect = true; + this._init = false; + + if (!this._meshBuffer) { + const attr = UIVertexFormat.vfmt; + const buffer = new MeshBuffer(ui); + buffer.initialize(attr, this._arrivalMaxBuffer); + this._meshBuffer = buffer; + } + + for (let i = 0; i < this._uiDrawBathcList.length; i++) { + const element = this._uiDrawBathcList[i]; + element.destroy(ui); + } + + this._uiDrawBathcList.length = 0; + } + + get drawBatchList (){ + return this._uiDrawBathcList; + } + + protected _init = false; + protected _meshBuffer: MeshBuffer | null = null; + protected _collect = false; + private _lastMeshBuffer: MeshBuffer | null = null; + private _uiDrawBathcList: UIDrawBatch[] = []; + + public onDestroy (){ + super.onDestroy(); + const ui = this._getUI(); + if (ui) { + for (let i = 0; i < this._uiDrawBathcList.length; i++) { + const element = this._uiDrawBathcList[i]; + element.destroy(ui); + } + } + + this._uiDrawBathcList.length = 0; + if(this._meshBuffer){ + this._meshBuffer.destroy(); + this._meshBuffer = null; + } + } + + + public updateAssembler (render: UI) { + if (this._collect) { + render.finishMergeBatches(); + this._lastMeshBuffer = render.currBufferBatch;; + render.currBufferBatch = this._meshBuffer; + render.currStaticRoot = this; + } + + if (this._init) { + render.finishMergeBatches(); + render.commitStaticBatch(this); + } + } + + public postUpdateAssembler (render: UI) { + if (this._collect) { + render.finishMergeBatches(); + render.currBufferBatch = this._lastMeshBuffer; + render.currStaticRoot = null; + this._collect = false; + this._init = true; + this.node._static = true; + + this._meshBuffer!.uploadData(); + } + } + + public requireDrawBatch (){ + const batch = new UIDrawBatch(); + batch.isStatic = true; + this._uiDrawBathcList.push(batch); + return batch; + } + + protected _getUI (){ + if(director.root && director.root.ui){ + return director.root.ui; + } + + cc.warn('The UI has not been initialized'); + return null; + } + + protected _arrivalMaxBuffer (){ + cc.warn('The current buffer beyond the limit, please reduce the amount'); + } +}