mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 15:42:39 +08:00
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 <wh19930829@qq.com> Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
|
||||
@@ -114,7 +114,6 @@ export const ttfUtils = {
|
||||
this._updateLabelDimensions();
|
||||
this._updateTexture(comp);
|
||||
this.updateOpacity(comp);
|
||||
comp._setCacheAlpha(_alpha);
|
||||
this._calDynamicAtlas(comp);
|
||||
|
||||
comp.actualFontSize = _fontSize;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user