mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 03:22: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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user