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
This commit is contained in:
黄森斌
2022-03-22 17:57:56 +08:00
committed by GitHub
parent d3f5e58acc
commit d065ca6aee
2 changed files with 15 additions and 14 deletions
+4 -2
View File
@@ -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();
+11 -12
View File
@@ -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]) {