Skip to content

Commit

Permalink
Fix material destoroy bug (#1847)
Browse files Browse the repository at this point in the history
* fix: material bug
  • Loading branch information
GuoLei1990 authored Oct 30, 2023
1 parent 46397db commit 50eb31b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
17 changes: 12 additions & 5 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export class Engine extends EventDispatcher {
/* @internal */
_magentaTexture2DArray: Texture2DArray;
/* @internal */
_magentaMaterial: Material;
_meshMagentaMaterial: Material;
/* @internal */
_particleMagentaMaterial: Material;
/* @internal */
_depthTexture2D: Texture2D;

Expand Down Expand Up @@ -262,10 +264,15 @@ export class Engine extends EventDispatcher {
this._depthTexture2D = depthTexture2D;
}

const magentaMaterial = new Material(this, Shader.find("unlit"));
magentaMaterial.isGCIgnored = true;
magentaMaterial.shaderData.setColor("material_BaseColor", new Color(1.0, 0.0, 1.01, 1.0));
this._magentaMaterial = magentaMaterial;
const meshMagentaMaterial = new Material(this, Shader.find("unlit"));
meshMagentaMaterial.isGCIgnored = true;
meshMagentaMaterial.shaderData.setColor("material_BaseColor", new Color(1.0, 0.0, 1.01, 1.0));
this._meshMagentaMaterial = meshMagentaMaterial;

const particleMagentaMaterial = new Material(this, Shader.find("particle-shader"));
particleMagentaMaterial.isGCIgnored = true;
particleMagentaMaterial.shaderData.setColor("material_BaseColor", new Color(1.0, 0.0, 1.01, 1.0));
this._particleMagentaMaterial = particleMagentaMaterial;

const innerSettings = this._settings;
const colorSpace = configuration.colorSpace || ColorSpace.Linear;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/RenderPipeline/RenderQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class RenderQueue {
const compileMacros = Shader._compileMacros;
const primitive = data.primitive;
const renderer = data.component;
const material = data.material.destroyed ? engine._magentaMaterial : data.material;
const material = data.material;
const rendererData = renderer.shaderData;
const materialData = material.shaderData;
const renderStates = material.renderStates;
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/mesh/MeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ export class MeshRenderer extends Renderer {
const renderPipeline = context.camera._renderPipeline;
const meshRenderDataPool = this._engine._renderDataPool;
for (let i = 0, n = subMeshes.length; i < n; i++) {
const material = materials[i];
let material = materials[i];
if (!material) {
continue;
}
if (material.destroyed) {
material = this.engine._meshMagentaMaterial;
}

const renderData = meshRenderDataPool.getFromPool();
renderData.setX(this, material, mesh._primitive, subMeshes[i]);
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/particle/ParticleMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export class ParticleMaterial extends BaseMaterial {
super(engine, Shader.find("particle-shader"));

const shaderData = this.shaderData;
shaderData.enableMacro("MATERIAL_OMIT_NORMAL");
shaderData.setColor(BaseMaterial._baseColorProp, new Color(1, 1, 1, 1));

this.isTransparent = true;
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/particle/ParticleRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { BoundingBox, Vector3 } from "@galacean/engine-math";
import { Entity } from "../Entity";
import { RenderContext } from "../RenderPipeline/RenderContext";
import { Renderer } from "../Renderer";
import { GLCapabilityType } from "../base/Constant";
import { deepClone, shallowClone } from "../clone/CloneManager";
import { ModelMesh } from "../mesh/ModelMesh";
import { ShaderMacro } from "../shader/ShaderMacro";
import { ShaderProperty } from "../shader/ShaderProperty";
import { ParticleGenerator } from "./ParticleGenerator";
import { ParticleRenderMode } from "./enums/ParticleRenderMode";
import { ParticleStopMode } from "./enums/ParticleStopMode";
import { GLCapabilityType } from "../base/Constant";

/**
* Particle Renderer Component.
Expand Down Expand Up @@ -192,7 +192,15 @@ export class ParticleRenderer extends Renderer {
primitive.instanceCount = instanceCount;
}

const material = this.getMaterial();
let material = this.getMaterial();
if (!material) {
return;
}

if (material.destroyed) {
material = this.engine._particleMagentaMaterial;
}

const renderData = this._engine._renderDataPool.getFromPool();
renderData.setX(this, material, generator._primitive, generator._subPrimitive);
context.camera._renderPipeline.pushRenderData(context, renderData);
Expand Down

0 comments on commit 50eb31b

Please sign in to comment.