From fed044e8fbf2bfde2e76eb746a2ab218f4b0c15d Mon Sep 17 00:00:00 2001 From: zhakesi Date: Fri, 19 Nov 2021 17:35:19 +0800 Subject: [PATCH] optimize opacity update (#9679) * optimize opacity update * remove _cacheAlpha * modify code style * Update cocos/core/scene-graph/node-ui-properties.ts * Update cocos/core/scene-graph/node-ui-properties.ts * 1. supplement unit test case 2. supplement default dirty parameter (cherry picked from commit 7e62d265ffbc8566e92bfcc10c1f9eb68e081487) Co-authored-by: Santy-Wang Co-authored-by: zhangmingzhen --- cocos/2d/assembler/label/bmfont.ts | 1 - cocos/2d/assembler/label/letter.ts | 1 - cocos/2d/assembler/label/ttfUtils.ts | 1 - cocos/2d/components/label.ts | 1 - cocos/2d/framework/renderable-2d.ts | 32 +++++------------ cocos/2d/renderer/batcher-2d-gpu.ts | 23 ++++++++---- cocos/2d/renderer/batcher-2d.ts | 23 ++++++++---- cocos/core/scene-graph/node-ui-properties.ts | 37 +++++++++++++++++--- tests/core/node.test.ts | 35 ++++++++++++++++++ 9 files changed, 109 insertions(+), 45 deletions(-) diff --git a/cocos/2d/assembler/label/bmfont.ts b/cocos/2d/assembler/label/bmfont.ts index 721a890feb..6773e6313c 100644 --- a/cocos/2d/assembler/label/bmfont.ts +++ b/cocos/2d/assembler/label/bmfont.ts @@ -50,7 +50,6 @@ export const bmfont: IAssembler = { fillBuffers (comp: Label, renderer: IBatcher) { const node = comp.node; - comp._setCacheAlpha(node._uiProps.opacity); tempColor.set(comp.color); tempColor.a = node._uiProps.opacity * 255; fillMeshVertices3D(node, renderer, comp.renderData!, tempColor); diff --git a/cocos/2d/assembler/label/letter.ts b/cocos/2d/assembler/label/letter.ts index a400cfef50..d31fc273b0 100644 --- a/cocos/2d/assembler/label/letter.ts +++ b/cocos/2d/assembler/label/letter.ts @@ -53,7 +53,6 @@ export const letter = { } const node = comp.node; - comp._setCacheAlpha(node._uiProps.opacity); WHITE.a = node._uiProps.opacity * 255; fillMeshVertices3D(node, renderer, comp.renderData, WHITE); }, diff --git a/cocos/2d/assembler/label/ttfUtils.ts b/cocos/2d/assembler/label/ttfUtils.ts index 177d70a164..396056daf0 100644 --- a/cocos/2d/assembler/label/ttfUtils.ts +++ b/cocos/2d/assembler/label/ttfUtils.ts @@ -114,7 +114,6 @@ export const ttfUtils = { this._updateLabelDimensions(); this._updateTexture(comp); this.updateOpacity(comp); - comp._setCacheAlpha(_alpha); this._calDynamicAtlas(comp); comp.actualFontSize = _fontSize; diff --git a/cocos/2d/components/label.ts b/cocos/2d/components/label.ts index 59f7500ecb..f18db478f2 100644 --- a/cocos/2d/components/label.ts +++ b/cocos/2d/components/label.ts @@ -771,7 +771,6 @@ export class Label extends Renderable2D { // Cannot use the base class methods directly because BMFont and CHAR cannot be updated in assambler with just color. protected _updateColor () { - this._updateWorldAlpha(); if (this._colorDirty) { this.updateRenderData(false); this._colorDirty = false; diff --git a/cocos/2d/framework/renderable-2d.ts b/cocos/2d/framework/renderable-2d.ts index 942247949d..60e9a9f141 100644 --- a/cocos/2d/framework/renderable-2d.ts +++ b/cocos/2d/framework/renderable-2d.ts @@ -48,6 +48,7 @@ import { warnID } from '../../core/platform/debug'; import { legacyCC } from '../../core/global-exports'; import { director } from '../../core'; import { NodeEventType } from '../../core/scene-graph/node-event'; +import { NodeUIProperties } from '../../core/scene-graph/node-ui-properties'; // hack ccenum(BlendFactor); @@ -249,8 +250,11 @@ export class Renderable2D extends RenderableComponent { if (this._color.equals(value)) { return; } - + let oldAlpha = this._color.a; this._color.set(value); + if (oldAlpha !== this.color.a) { + NodeUIProperties.markOpacityTree(this.node); + } this._colorDirty = true; if (EDITOR) { const clone = value.clone(); @@ -309,7 +313,6 @@ export class Renderable2D extends RenderableComponent { protected _blendHash = 0; protected _colorDirty = true; - protected _cacheAlpha = 1; // macro.UI_GPU_DRIVEN protected declare _canDrawByFourVertex: boolean; @@ -470,36 +473,23 @@ export class Renderable2D extends RenderableComponent { protected _updateColor () { if (UI_GPU_DRIVEN && this._canDrawByFourVertex) { - const opacityZero = this._cacheAlpha <= 0; - this._updateWorldAlpha(); if (this._colorDirty) { - if (opacityZero || this._cacheAlpha <= 0) { - this._renderFlag = this._canRender(); - } + this._renderFlag = this._canRender(); this._colorDirty = false; } return; } // Need update rendFlag when opacity changes from 0 to !0 - const opacityZero = this._cacheAlpha <= 0; - this._updateWorldAlpha(); if (this._colorDirty && this._assembler && this._assembler.updateColor) { this._assembler.updateColor(this); // Need update rendFlag when opacity changes from 0 to !0 or 0 to !0 - if (opacityZero || this._cacheAlpha <= 0) { - this._renderFlag = this._canRender(); - } + this._renderFlag = this._canRender(); this._colorDirty = false; } } - protected _updateWorldAlpha () { - let localAlpha = this.color.a / 255; - if (localAlpha === 1) localAlpha = this.node._uiProps.localOpacity; // Hack for Mask use ui-opacity - const alpha = this.node._uiProps.opacity * localAlpha; - this.node._uiProps.opacity = alpha; - this._colorDirty = this._colorDirty || alpha !== this._cacheAlpha; - this._cacheAlpha = alpha; + public markColorDirty() { + this._colorDirty = true; } public _updateBlendFunc () { @@ -567,10 +557,6 @@ export class Renderable2D extends RenderableComponent { } protected _flushAssembler? (): void; - - public _setCacheAlpha (value) { - this._cacheAlpha = value; - } } legacyCC.internal.Renderable2D = Renderable2D; diff --git a/cocos/2d/renderer/batcher-2d-gpu.ts b/cocos/2d/renderer/batcher-2d-gpu.ts index e8c43e64e3..09529d2407 100644 --- a/cocos/2d/renderer/batcher-2d-gpu.ts +++ b/cocos/2d/renderer/batcher-2d-gpu.ts @@ -158,7 +158,6 @@ export class Batcher2D implements IBatcher { private _currLayer = 0; private _currDepthStencilStateStage: any | null = null; private _currIsStatic = false; - private _currOpacity = 1; // macro.UI_GPU_DRIVEN private _currBatch!: DrawBatch2DGPU; @@ -284,7 +283,6 @@ export class Batcher2D implements IBatcher { if (!screen.enabledInHierarchy) { continue; } - this._currOpacity = 1; this._recursiveScreenNode(screen.node); // macro.UI_GPU_DRIVEN this._currTypeIsGPU = false; @@ -357,7 +355,6 @@ export class Batcher2D implements IBatcher { this._currTransform = null; this._currScene = null; this._currMeshBuffer = null; - this._currOpacity = 1; this._meshBufferUseCount.clear(); // macro.UI_GPU_DRIVEN this._batches.clear(); @@ -741,13 +738,12 @@ export class Batcher2D implements IBatcher { } public walk (node: Node, level = 0) { - const len = node.children.length; + const len = node.children.length; this._preProcess(node); if (len > 0 && !node._static) { const children = node.children; for (let i = 0; i < children.length; ++i) { - this._currOpacity = node._uiProps.opacity; const child = children[i]; this.walk(child, level); } @@ -759,9 +755,22 @@ export class Batcher2D implements IBatcher { } private _preProcess (node: Node) { + // update opacity + if (node._uiProps.opacityDirty) { + let opacity = 1.0; + if (node.parent?._uiProps) { + opacity = node.parent._uiProps.opacity; + const render = node._uiProps.uiComp as Renderable2D; + if (render.markColorDirty) { + opacity *= (render.color.a / 255); + render.markColorDirty(); + } + } + node._uiProps.opacityDirty = false; + node._uiProps.applyOpacity(opacity); + } + const render = node._uiProps.uiComp; - const localAlpha = node._uiProps.localOpacity; - node._uiProps.opacity = this._currOpacity * localAlpha; if (!node._uiProps.uiTransformComp) { return; } diff --git a/cocos/2d/renderer/batcher-2d.ts b/cocos/2d/renderer/batcher-2d.ts index b9594953c3..af2887040a 100644 --- a/cocos/2d/renderer/batcher-2d.ts +++ b/cocos/2d/renderer/batcher-2d.ts @@ -156,7 +156,6 @@ export class Batcher2D implements IBatcher { private _currLayer = 0; private _currDepthStencilStateStage: any | null = null; private _currIsStatic = false; - private _currOpacity = 1; // DescriptorSet Cache Map private _descriptorSetCache = new DescriptorSetCache(); @@ -252,7 +251,6 @@ export class Batcher2D implements IBatcher { if (!screen.enabledInHierarchy) { continue; } - this._currOpacity = 1; this._recursiveScreenNode(screen.node); } @@ -319,7 +317,6 @@ export class Batcher2D implements IBatcher { this._currTransform = null; this._currScene = null; this._currMeshBuffer = null; - this._currOpacity = 1; this._meshBufferUseCount.clear(); this._batches.clear(); StencilManager.sharedManager!.reset(); @@ -580,13 +577,12 @@ export class Batcher2D implements IBatcher { } public walk (node: Node, level = 0) { - const len = node.children.length; + const len = node.children.length; this._preProcess(node); if (len > 0 && !node._static) { const children = node.children; for (let i = 0; i < children.length; ++i) { - this._currOpacity = node._uiProps.opacity; const child = children[i]; this.walk(child, level); } @@ -598,9 +594,22 @@ export class Batcher2D implements IBatcher { } private _preProcess (node: Node) { + // update opacity + if (node._uiProps.opacityDirty) { + let opacity = 1.0; + if (node.parent?._uiProps) { + opacity = node.parent._uiProps.opacity; + const render = node._uiProps.uiComp as Renderable2D; + if (render && render.markColorDirty) { + opacity *= (render.color.a / 255); + render.markColorDirty(); + } + } + node._uiProps.opacityDirty = false; + node._uiProps.applyOpacity(opacity); + } + const render = node._uiProps.uiComp; - const localAlpha = node._uiProps.localOpacity; - node._uiProps.opacity = this._currOpacity * localAlpha; if (!node._uiProps.uiTransformComp) { return; } diff --git a/cocos/core/scene-graph/node-ui-properties.ts b/cocos/core/scene-graph/node-ui-properties.ts index 869bdf344a..3c2e2d77a3 100644 --- a/cocos/core/scene-graph/node-ui-properties.ts +++ b/cocos/core/scene-graph/node-ui-properties.ts @@ -71,15 +71,44 @@ export class NodeUIProperties { private _uiComp: UIComponent | Renderable2D | null = null; /** - * @en The opacity of the UI node - * @zh UI 透明度 + * @en The opacity of the UI node for final rendering + * @zh 最终显示的 UI 透明度,受父节点透明度影响 */ - public opacity = 1; - public localOpacity = 1; + private _opacity = 1; + public get opacity () { return this._opacity; } + + /** + * @en The opacity of the UI node itself + * @zh 本节点的 UI 透明度 + */ + private _localOpacity = 1; + get localOpacity () { return this._localOpacity; } + set localOpacity (val) { + this._localOpacity = val; + NodeUIProperties.markOpacityTree(this._node); + } + + public opacityDirty = true; protected _uiTransformComp: UITransform | null = null; private _node: any; constructor (node: any) { this._node = node; } + + public applyOpacity (effectOpacity) { + this._opacity = this._localOpacity * effectOpacity; + } + + /** + * @en Make the opacity state of node tree is dirty + * @zh 为结点树的透明度状态设置脏标签 + */ + public static markOpacityTree (node, isDirty = true) { + node._uiProps.opacityDirty = isDirty; + for (let i = 0, l = node.children.length; i < l; i++) { + const c = node.children[i]; + NodeUIProperties.markOpacityTree(c, isDirty); + } + } } diff --git a/tests/core/node.test.ts b/tests/core/node.test.ts index 4eb4c2d3af..6d462d437e 100644 --- a/tests/core/node.test.ts +++ b/tests/core/node.test.ts @@ -2,8 +2,43 @@ import { Node, Scene } from "../../cocos/core/scene-graph" import { Vec3 } from "../../cocos/core/math" import { director } from "../../cocos/core"; import { NodeEventType } from "../../cocos/core/scene-graph/node-event"; +import { NodeUIProperties } from "../../cocos/core/scene-graph/node-ui-properties"; describe(`Node`, () => { + test('mark-opacity-tree',() => { + let scene = new Scene('scene'); + let rootNode = new Node('root'); + let node_1 = new Node('node_1'); + let node_2 = new Node('node_2'); + let node_1_1 = new Node('node_1_1'); + let node_1_2 = new Node('node_1_2'); + let node_2_1 = new Node('node_2_1'); + let node_2_2 = new Node('node_2_2'); + let node_1_1_1 = new Node('node_1_1_1'); + let node_1_2_1 = new Node('node_1_2_1'); + + rootNode.parent = scene; + node_1.parent = rootNode; + node_2.parent = rootNode; + node_1_1.parent = node_1; + node_1_2.parent = node_1; + node_2_1.parent = node_2; + node_2_2.parent = node_2; + node_1_1_1.parent = node_1_1; + node_1_2_1.parent = node_1_2; + + // set all nodes alpha-dirty to false + NodeUIProperties.markOpacityTree(rootNode, false); + expect(node_2_1._uiProps.opacityDirty).toStrictEqual(false); + + // modify node_1 and its subtree, node_2 and its subtree are not influenced + NodeUIProperties.markOpacityTree(node_1); + expect(node_1._uiProps.opacityDirty).toStrictEqual(true); + expect(node_1_1._uiProps.opacityDirty).toStrictEqual(true); + expect(node_1_2_1._uiProps.opacityDirty).toStrictEqual(true); + expect(node_2_2._uiProps.opacityDirty).toStrictEqual(false); + }); + test('@inverseTransformPoint', () => { let scene = new Scene('temp'); let parentNode = new Node();