From d065ca6aeefc0ba25f745cf47ce1faab246777b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=A3=AE=E6=96=8C?= Date: Tue, 22 Mar 2022 17:57:56 +0800 Subject: [PATCH] V3.5.0 fix bug: reset renderer-component would not update model (#10342) * fix bug: reset materials would not update model * add comment * reset _materials.length * fix bug: model in scene would not disappear when remove mesh * modify code written --- cocos/3d/framework/mesh-renderer.ts | 6 +++-- cocos/core/components/renderable-component.ts | 23 +++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cocos/3d/framework/mesh-renderer.ts b/cocos/3d/framework/mesh-renderer.ts index 1dcf4e2cb1..dd03c34966 100644 --- a/cocos/3d/framework/mesh-renderer.ts +++ b/cocos/3d/framework/mesh-renderer.ts @@ -465,7 +465,7 @@ export class MeshRenderer extends RenderableComponent { } protected _updateModels () { - if (!this.enabledInHierarchy || !this._mesh) { + if (!this.enabledInHierarchy) { return; } @@ -479,7 +479,9 @@ export class MeshRenderer extends RenderableComponent { } if (this._model) { - this._model.createBoundingShape(this._mesh.struct.minPosition, this._mesh.struct.maxPosition); + if (this._mesh) { + this._model.createBoundingShape(this._mesh.struct.minPosition, this._mesh.struct.maxPosition); + } this._updateModelParams(); this._onUpdateLightingmap(); this._onUpdateLocalShadowBias(); diff --git a/cocos/core/components/renderable-component.ts b/cocos/core/components/renderable-component.ts index 7fda1a6eac..f1c95528a2 100644 --- a/cocos/core/components/renderable-component.ts +++ b/cocos/core/components/renderable-component.ts @@ -48,9 +48,6 @@ const _matInsInfo: IMaterialInstanceInfo = { @ccclass('cc.RenderableComponent') export class RenderableComponent extends Component { - @type([Material]) - protected _materials: (Material | null)[] = []; - @serializable protected _visFlags = Layers.Enum.NONE; @@ -85,6 +82,10 @@ export class RenderableComponent extends Component { } } + // _materials should be defined after sharedMaterials for Editor reset component reason + @type([Material]) + protected _materials: (Material | null)[] = []; + /** * @en The materials of the model. * @zh 模型材质。 @@ -97,16 +98,14 @@ export class RenderableComponent extends Component { } set materials (val: (Material | MaterialInstance | null)[]) { - const dLen = val.length - this._materials.length; - if (dLen > 0) { - this._materials.length = val.length; - this._materialInstances.length = val.length; - } else if (dLen < 0) { - for (let i = this._materials.length - dLen; i < this._materials.length; ++i) { - this.setMaterialInstance(null, i); - } + const newLength = val.length; + const oldLength = this._materials.length; + for (let i = newLength; i < oldLength; i++) { + this.setMaterialInstance(null, i); } - for (let i = 0; i < this._materialInstances.length; i++) { + this._materials.length = newLength; + this._materialInstances.length = newLength; + for (let i = 0; i < newLength; i++) { // they could be either undefined or null // eslint-disable-next-line eqeqeq if (this._materialInstances[i] != val[i]) {