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]) {