Skip to content

Commit

Permalink
restructure life cycle (cocos#11971)
Browse files Browse the repository at this point in the history
* restructure life cycle

* fix bugs

* optimize

* remove some code

* refine

* fix dragonbone custom material

* fix particle crash

* changed to CCAssert

Co-authored-by: liqiao <[email protected]>
  • Loading branch information
SantyWang and zhakesi authored Jul 14, 2022
1 parent 2701f96 commit 945e715
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 362 deletions.
3 changes: 1 addition & 2 deletions cocos/2d/assembler/graphics/webgl/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ export class Impl {
renderData.initRenderDrawInfo(this._comp, RenderDrawInfoType.MODEL);
// @ts-expect-error temporary no care
this._comp._renderData = renderData;
this._comp.renderEntity!.assignExtraEntityAttrs(this._comp);
this._comp.renderEntity!.setCommitModelMaterial(this._comp.getMaterialInstance(0));
this._comp.renderEntity.setCommitModelMaterial(this._comp.getMaterialInstance(0));
// @ts-expect-error temporary no care
this._comp._renderData!.material = this._comp.getMaterialInstance(0)!;// hack
}
Expand Down
9 changes: 5 additions & 4 deletions cocos/2d/components/graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ export class Graphics extends UIRenderer {
}

public onLoad () {
super.onLoad();
this.model = director.root!.createModel(scene.Model);
this.model.node = this.model.transform = this.node;
this._flushAssembler();
if (JSB) {
this._graphicsNativeProxy.initModel(this.node);
this._renderEntity = new RenderEntity(this.batcher, RenderEntityType.DYNAMIC);
}
}

Expand All @@ -289,9 +289,6 @@ export class Graphics extends UIRenderer {
if (this.model) {
director.root!.destroyModel(this.model);
this.model = null;
if (JSB) {
this._graphicsNativeProxy.destroy();
}
}

const subMeshLength = this._graphicsUseSubMeshes.length;
Expand Down Expand Up @@ -753,6 +750,10 @@ export class Graphics extends UIRenderer {
}
}
}

protected createRenderEntity () {
return new RenderEntity(RenderEntityType.DYNAMIC);
}
}

legacyCC.Graphics = Graphics;
8 changes: 2 additions & 6 deletions cocos/2d/components/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,7 @@ export class Label extends UIRenderer {
// if (value && this._isSystemFontUsed)
// this._isSystemFontUsed = false;

if (this.renderData) {
this.destroyRenderData();
this.renderData = null;
}
this.destroyRenderData();

this._fontAtlas = null;
this.updateRenderData(true);
Expand All @@ -475,7 +472,6 @@ export class Label extends UIRenderer {
}

this.destroyRenderData();
this.renderData = null;

if (EDITOR) {
if (!value && this._isSystemFontUsed && this._userDefinedFont) {
Expand Down Expand Up @@ -778,7 +774,7 @@ export class Label extends UIRenderer {

if (!this.renderData) {
if (this._assembler && this._assembler.createData) {
this.renderData = this._assembler.createData(this);
this._renderData = this._assembler.createData(this);
this.renderData!.material = this.material;
this._updateColor();
}
Expand Down
53 changes: 21 additions & 32 deletions cocos/2d/components/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ export class Mask extends UIRenderer {
this._changeRenderType();
this._updateGraphics();
if (JSB) {
this.subComp!.renderEntity!.setIsSubMask(true);
this.subComp!.renderEntity.setIsSubMask(true);
// subMask and mask should have the same inverted flag
this.subComp!.renderEntity!.setIsMaskInverted(this._inverted);
this.subComp!.renderEntity.setIsMaskInverted(this._inverted);
}
} else {
if (this._graphics) {
Expand All @@ -164,9 +164,9 @@ export class Mask extends UIRenderer {
this._maskNode!.parent = null;
this._changeRenderType();
if (JSB) {
this.subComp!.renderEntity!.setIsSubMask(true);
this.subComp!.renderEntity.setIsSubMask(true);
// subMask and mask should have the same inverted flag
this.subComp!.renderEntity!.setIsMaskInverted(this._inverted);
this.subComp!.renderEntity.setIsMaskInverted(this._inverted);
}
}
}
Expand Down Expand Up @@ -194,9 +194,7 @@ export class Mask extends UIRenderer {
}

if (JSB) {
if (this._renderEntity) {
this._renderEntity.setIsMaskInverted(this._inverted);
}
this._renderEntity.setIsMaskInverted(this._inverted);
}
}

Expand Down Expand Up @@ -351,18 +349,12 @@ export class Mask extends UIRenderer {
}

public onLoad () {
super.onLoad();
this._createClearModel();
this._changeRenderType();

if (this._graphics) { //isGraphics
this._graphics.onLoad();
}

if (JSB) {
if (!this._renderEntity) {
this.initRenderEntity();
}
if (this._renderEntity && this.renderData && this.subComp && this.subComp.renderEntity) {
if (this.renderData && this.subComp) {
this._renderEntity.setIsMask(true);
this.subComp.renderEntity.setIsSubMask(true);
this._renderEntity.setIsMaskInverted(this._inverted);
Expand Down Expand Up @@ -416,7 +408,7 @@ export class Mask extends UIRenderer {
this._clearStencilMtl.destroy();
}

this._removeGraphics();
this._removeMaskNode();
super.onDestroy();
}

Expand Down Expand Up @@ -624,10 +616,7 @@ export class Mask extends UIRenderer {
owner: this,
subModelIdx: 0,
});
//sync to native
if (this.renderEntity) {
this.renderEntity.setCommitModelMaterial(this._clearStencilMtl);
}
this.renderEntity.setCommitModelMaterial(this._clearStencilMtl);

this._clearModel = director.root!.createModel(scene.Model);
this._clearModel.node = this._clearModel.transform = this.node;
Expand Down Expand Up @@ -658,7 +647,7 @@ export class Mask extends UIRenderer {

// sync to native
if (JSB) {
if (this._renderEntity && this._renderData) {
if (this._renderData) {
const drawInfo = this._renderData.renderDrawInfo;
drawInfo.setModel(this._clearModel);
}
Expand All @@ -684,22 +673,22 @@ export class Mask extends UIRenderer {
}

protected _enableGraphics () {
if (this._graphics) {
// @ts-expect-error hack for mask _graphics renderFlag
this._graphics._renderFlag = this._graphics._canRender();
if (this.subComp) {
this.subComp.enabled = true;
}
}

protected _disableGraphics () {
if (this._graphics) {
this._graphics.onDisable();
if (this.subComp) {
this.subComp.enabled = false;
}
}

protected _removeGraphics () {
if (this._graphics) {
// this._graphics.destroy();
protected _removeMaskNode () {
if (this._maskNode && this._maskNode.isValid) {
this._maskNode.destroy();
// this._graphics._destroyImmediate(); // FIX: cocos-creator/2d-tasks#2511. TODO: cocos-creator/2d-tasks#2516
this._sprite = null;
this._graphics = null;
}
}
Expand All @@ -708,16 +697,16 @@ export class Mask extends UIRenderer {
//if (this._type === MaskType.IMAGE_STENCIL && !this.renderData) {
if (!this.renderData) {
if (this._assembler && this._assembler.createData) {
this.renderData = this._assembler.createData(this);
this._renderData = this._assembler.createData(this);
this.markForUpdateRenderData();
}
}
}

// RenderEntity
// it should be overwritten by inherited classes
protected initRenderEntity () {
this._renderEntity = new RenderEntity(this.batcher, RenderEntityType.DYNAMIC);
protected createRenderEntity () {
return new RenderEntity(RenderEntityType.DYNAMIC);
}
}

Expand Down
3 changes: 1 addition & 2 deletions cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ export class Sprite extends UIRenderer {
if (this._fillType !== value) {
if (value === FillType.RADIAL || this._fillType === FillType.RADIAL) {
this.destroyRenderData();
this.renderData = null;
} else if (this.renderData) {
this.markForUpdateRenderData(true);
}
Expand Down Expand Up @@ -587,7 +586,7 @@ export class Sprite extends UIRenderer {

if (!this.renderData) {
if (this._assembler && this._assembler.createData) {
this.renderData = this._assembler.createData(this);
this._renderData = this._assembler.createData(this);
this.renderData!.material = this.getRenderMaterial(0);
this.markForUpdateRenderData();
if (this.spriteFrame) {
Expand Down
48 changes: 13 additions & 35 deletions cocos/2d/components/ui-mesh-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { uiRendererManager } from '../framework/ui-renderer-manager';
import { RenderEntity, RenderEntityType } from '../renderer/render-entity';
import { director } from '../../core/director';
import { MeshRenderData, RenderData } from '../renderer/render-data';
import { assert } from '../../core';
import { RenderDrawInfoType } from '../renderer/render-draw-info';

/**
Expand All @@ -55,6 +56,14 @@ import { RenderDrawInfoType } from '../renderer/render-draw-info';
@menu('UI/UIMeshRenderer')
@executeInEditMode
export class UIMeshRenderer extends Component {
constructor () {
super();
this._renderEntity = new RenderEntity(RenderEntityType.DYNAMIC);
if (JSB) {
this._UIModelNativeProxy = new NativeUIModelProxy();
}
}

public get modelComponent () {
return this._modelComponent;
}
Expand All @@ -63,17 +72,13 @@ export class UIMeshRenderer extends Component {

//nativeObj
private declare _UIModelNativeProxy: NativeUIModelProxy;
protected _renderEntity: RenderEntity | null = null;
protected declare _renderEntity : RenderEntity;
private modelCount = 0;
public _dirtyVersion = -1;
public _internalId = -1;

public __preload () {
this.node._uiProps.uiComp = this;
if (JSB) {
this._UIModelNativeProxy = new NativeUIModelProxy();
this._renderEntity = new RenderEntity(director.root!.batcher2D, RenderEntityType.DYNAMIC);
}
}

onEnable () {
Expand All @@ -98,12 +103,12 @@ export class UIMeshRenderer extends Component {
}
if (JSB) {
this._UIModelNativeProxy.attachNode(this.node);
// @ts-expect-error temporary no care
this.renderEntity!.assignExtraEntityAttrs(this);
}
this.renderEntity.setNode(this.node);
}

public onDestroy () {
this.renderEntity.setNode(null);
if (this.node._uiProps.uiComp === this) {
this.node._uiProps.uiComp = null;
}
Expand All @@ -113,10 +118,6 @@ export class UIMeshRenderer extends Component {
}

this._modelComponent._sceneGetter = null;

if (JSB) {
this._UIModelNativeProxy.destroy();
}
}

/**
Expand Down Expand Up @@ -240,37 +241,14 @@ export class UIMeshRenderer extends Component {
}

get renderEntity () {
if (!this._renderEntity) {
this.initRenderEntity();
}
assert(this._renderEntity);
return this._renderEntity;
}

protected initRenderEntity () {
this._renderEntity = new RenderEntity(director.root!.batcher2D, RenderEntityType.DYNAMIC);
}

protected _renderData: RenderData | null = null;
get renderData () {
return this._renderData;
}

set renderData (val: RenderData | null) {
if (val === this._renderData) {
return;
}
this._renderData = val;
const entity = this.renderEntity;
if (entity) {
if (entity.renderDrawInfoArr.length === 0) {
entity.addDynamicRenderDrawInfo(this._renderData!.renderDrawInfo);
} else if (entity.renderDrawInfoArr.length > 0) {
if (entity.renderDrawInfoArr[0] !== this._renderData!.renderDrawInfo) {
entity.setDynamicRenderDrawInfo(this._renderData!.renderDrawInfo, 0);
}
}
}
}
}

legacyCC.UIMeshRenderer = UIMeshRenderer;
33 changes: 0 additions & 33 deletions cocos/2d/components/ui-static-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,6 @@ export class UIStaticBatch extends UIRenderer {
protected _dirty = true;
private _uiDrawBatchList: DrawBatch2D[] = [];

public onLoad () {
// const ui = this._getBatcher();
// if (!ui) {
// return;
// }

// const attr = vfmtPosUvColor;
// this._bufferAccessor = ui.switchBufferAccessor(vfmtPosUvColor);
// // buffer.initialize(attr, this._arrivalMaxBuffer.bind(this));
}

public onDestroy () {
// super.onDestroy();

// this._clearData();
// if (this._bufferAccessor) {
// this._bufferAccessor.destroy();
// this._bufferAccessor = null;
// }
}

public updateAssembler (render: IBatcher) {
// render.currIsStatic = true;
// if (this._dirty) {
// render.setupStaticBatch(this, this._bufferAccessor!);
// }

// if (this._init) {
// render.finishMergeBatches();
// render.commitStaticBatch(this);
// }
}

public postUpdateAssembler (render: IBatcher) {
// if (this._dirty) {
// this._dirty = false;
Expand Down
11 changes: 0 additions & 11 deletions cocos/2d/framework/ui-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ export class UIComponent extends Component {
}
}

/**
* @en Render data submission procedure, it update and assemble the render data to 2D data buffers before all children submission process.
* Usually called each frame when the ui flow assemble all render data to geometry buffers.
* Don't call it unless you know what you are doing.
* @zh 渲染数据组装程序,这个方法会在所有子节点数据组装之前更新并组装当前组件的渲染数据到 UI 的顶点数据缓冲区中。
* 一般在 UI 渲染流程中调用,用于组装所有的渲染数据到顶点数据缓冲区。
* 注意:不要手动调用该函数,除非你理解整个流程。
*/
public updateAssembler (render: IBatcher) {
}

/**
* @en Post render data submission procedure, it's executed after assembler updated for all children.
* It may assemble some extra render data to the geometry buffers, or it may only change some render states.
Expand Down
Loading

0 comments on commit 945e715

Please sign in to comment.