diff --git a/src/layaAir/laya/d3/core/MeshRenderer.ts b/src/layaAir/laya/d3/core/MeshRenderer.ts index 0e6daa7751..2151f860ca 100644 --- a/src/layaAir/laya/d3/core/MeshRenderer.ts +++ b/src/layaAir/laya/d3/core/MeshRenderer.ts @@ -323,6 +323,7 @@ export class MeshRenderer extends BaseRender { this.owner && renderElement.setTransform((this.owner as Sprite3D)._transform); renderElement.render = this; } + materials[i] = materials[i] || BlinnPhongMaterial.defaultMaterial; renderElement.setGeometry(mesh.getSubMesh(i)); } diff --git a/src/layaAir/laya/d3/core/SkinnedMeshRenderer.ts b/src/layaAir/laya/d3/core/SkinnedMeshRenderer.ts index dff54fe300..b890d9248d 100644 --- a/src/layaAir/laya/d3/core/SkinnedMeshRenderer.ts +++ b/src/layaAir/laya/d3/core/SkinnedMeshRenderer.ts @@ -229,7 +229,6 @@ export class SkinnedMeshRenderer extends MeshRenderer { for (var i: number = 0; i < count; i++) { var renderElement: SkinRenderElement = this._renderElements[i]; if (!renderElement) { - var material: Material = this.sharedMaterials[i]; renderElement = this._renderElements[i] = this._renderElements[i] ? this._renderElements[i] : this._createRenderElement(); if (this._cacheRootBone) { renderElement.setTransform(this._cacheRootBone._transform); @@ -237,6 +236,7 @@ export class SkinnedMeshRenderer extends MeshRenderer { renderElement.setTransform((this.owner as Sprite3D)._transform); } renderElement.render = this; + materials[i] = materials[i] || BlinnPhongMaterial.defaultMaterial; } renderElement.setGeometry(mesh.getSubMesh(i)); } diff --git a/src/layaAir/laya/d3/core/render/BaseRender.ts b/src/layaAir/laya/d3/core/render/BaseRender.ts index 49fcbf5027..3aef82cbee 100644 --- a/src/layaAir/laya/d3/core/render/BaseRender.ts +++ b/src/layaAir/laya/d3/core/render/BaseRender.ts @@ -349,14 +349,17 @@ export class BaseRender extends Component implements IBoundsCell { } set sharedMaterial(value: Material) { - var lastValue: Material = this._sharedMaterials[0]; - if (lastValue !== value) { - this._sharedMaterials[0] = value; + + let lastMat = this._sharedMaterials[0]; + this._changeMaterialReference(lastMat, value); + this._sharedMaterials[0] = value; + + let element = this._renderElements[0]; + if (element && element.material !== value) { this._materialsInstance[0] = false; - this._changeMaterialReference(lastValue, value); - var renderElement: RenderElement = this._renderElements[0]; - (renderElement) && (renderElement.material = value); + element.material = value; } + this._isSupportRenderFeature(); } @@ -371,28 +374,25 @@ export class BaseRender extends Component implements IBoundsCell { var materialsInstance: boolean[] = this._materialsInstance; var sharedMats: Material[] = this._sharedMaterials; - for (var i: number = 0, n: number = sharedMats.length; i < n; i++) { - var lastMat: Material = sharedMats[i]; - (lastMat) && (lastMat._removeReference()); - } - if (value) { - var count: number = value.length; - for (i = 0; i < count; i++) { - lastMat = sharedMats[i]; - var mat: Material = value[i]; - if (lastMat !== mat) { + let count = value.length; + for (let i = 0; i < count; i++) { + let mat = value[i]; + let lastMat = sharedMats[i]; + this._changeMaterialReference(lastMat, mat); + sharedMats[i] = mat; + + let element = this._renderElements[i]; + if (element && element.material !== mat) { materialsInstance[i] = false; - var renderElement: RenderElement = this._renderElements[i]; - (renderElement) && (renderElement.material = mat); + element.material = mat; } - if (mat) { - mat._addReference(); - } - sharedMats[i] = mat; } - for (i = count, n = sharedMats.length; i < n; i++) { + for (let i = count, n = sharedMats.length; i < n; i++) { + let mat = sharedMats[i]; + mat && mat._removeReference(); + let renderElement = this._renderElements[i]; renderElement && (renderElement.material = null); } @@ -401,6 +401,11 @@ export class BaseRender extends Component implements IBoundsCell { sharedMats.length = count; } else { + for (var i = 0, n = sharedMats.length; i < n; i++) { + let lastMat = sharedMats[i]; + (lastMat) && (lastMat._removeReference()); + } + this._sharedMaterials = []; } this._isSupportRenderFeature();