Skip to content

Commit

Permalink
fix: default material
Browse files Browse the repository at this point in the history
  • Loading branch information
QTED committed Jun 15, 2024
1 parent bd4e9a9 commit aa382d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/layaAir/laya/d3/core/MeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/layaAir/laya/d3/core/SkinnedMeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ 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);
} else {
renderElement.setTransform((this.owner as Sprite3D)._transform);
}
renderElement.render = this;
materials[i] = materials[i] || BlinnPhongMaterial.defaultMaterial;
}
renderElement.setGeometry(mesh.getSubMesh(i));
}
Expand Down
51 changes: 28 additions & 23 deletions src/layaAir/laya/d3/core/render/BaseRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}
Expand All @@ -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();
Expand Down

0 comments on commit aa382d9

Please sign in to comment.