diff --git a/tests/src/core/MeshRenderer.test.ts b/tests/src/core/MeshRenderer.test.ts index 7338478069..726fdcb4a4 100644 --- a/tests/src/core/MeshRenderer.test.ts +++ b/tests/src/core/MeshRenderer.test.ts @@ -1,4 +1,13 @@ -import { BlinnPhongMaterial, MeshRenderer, PrimitiveMesh, Entity, Camera, ModelMesh } from "@galacean/engine-core"; +import { + BlinnPhongMaterial, + PBRMaterial, + UnlitMaterial, + MeshRenderer, + PrimitiveMesh, + Entity, + Camera, + ModelMesh +} from "@galacean/engine-core"; import { Vector3 } from "@galacean/engine-math"; import { WebGLEngine } from "@galacean/engine-rhi-webgl"; import { describe, beforeAll, expect, it } from "vitest"; @@ -108,6 +117,132 @@ describe("MeshRenderer", async function () { cloneCube.destroy(); }); + it("receiveShadows", () => { + // Test that set and get receiveShadows work correctly. + const mr = cubeEntity.getComponent(MeshRenderer); + mr.receiveShadows = true; + expect(mr.receiveShadows).to.be.true; + + // Test that set false value works correctly. + mr.receiveShadows = false; + expect(mr.receiveShadows).to.be.false; + + // Test that repeated assignment works correctly. + mr.receiveShadows = true; + expect(mr.receiveShadows).to.be.true; + }); + + it("material", () => { + const mr = cubeEntity.getComponent(MeshRenderer); + + // Add BlinnPhong, Unlit and PBR materials. + mr.setMaterial(0, new BlinnPhongMaterial(engine)); + mr.setMaterial(0, new UnlitMaterial(engine)); + mr.setMaterial(1, new PBRMaterial(engine)); + + // Test that get material works correctly. + expect(mr.getMaterial()).to.be.instanceOf(UnlitMaterial); + expect(mr.getMaterial(1)).to.be.instanceOf(PBRMaterial); + + // Test that return null when index is out of range. + expect(mr.getMaterial(2)).to.be.null; + expect(mr.getMaterial(-1)).to.be.null; + + mr.getInstanceMaterials(); + mr.setMaterial(1, new UnlitMaterial(engine)); + expect(mr.getMaterial(1)).to.be.instanceOf(UnlitMaterial); + }); + + it("materials", () => { + const mr = cubeEntity.getComponent(MeshRenderer); + + // Test that set materials works correctly. + mr.setMaterials([new UnlitMaterial(engine), new PBRMaterial(engine), null, new BlinnPhongMaterial(engine)]); + + // Test that get materials works correctly. + const materials = mr.getMaterials(); + expect(materials[0]).to.be.instanceOf(UnlitMaterial); + expect(materials[1]).to.be.instanceOf(PBRMaterial); + expect(materials[2]).to.be.null; + expect(materials[3]).to.be.instanceOf(BlinnPhongMaterial); + }); + + it("materialCount", () => { + // Test that get materialCount works correctly. + const mr = cubeEntity.getComponent(MeshRenderer); + mr.setMaterials([new UnlitMaterial(engine), new PBRMaterial(engine), new BlinnPhongMaterial(engine)]); + mr.getInstanceMaterials(); + expect(mr.materialCount).to.be.equal(3); + + // Test that set materialCount works correctly. + mr.materialCount = 2; + expect(mr.materialCount).to.be.equal(2); + + // Test that set materialCount with negative value works correctly. + mr.materialCount = 0; + expect(mr.materialCount).to.be.equal(0); + }); + + it("getInstanceMaterial", () => { + const mr = cubeEntity.getComponent(MeshRenderer); + + // Test that getInstanceMaterial works correctly. + expect(mr.getInstanceMaterial()).to.be.null; + + const unlitMaterial = new UnlitMaterial(engine); + const pbrMaterial = new PBRMaterial(engine); + mr.setMaterials([unlitMaterial, null, pbrMaterial]); + + // Test that getInstanceMaterial works correctly. + const material = mr.getInstanceMaterial(); + expect(material).to.be.instanceOf(UnlitMaterial); + expect(material.name).to.be.equal("undefined(Instance)"); + + // Test that material0 is same as material. + const material0 = mr.getInstanceMaterial(0); + expect(material0).to.be.eq(material); + + const material2 = mr.getInstanceMaterial(2); + expect(material2).to.be.instanceOf(PBRMaterial); + expect(material2.name).to.be.equal("undefined(Instance)"); + + expect(mr.getInstanceMaterial(1)).to.be.null; + + // Test that return null when index is out of range. + expect(mr.getInstanceMaterial(3)).to.be.null; + expect(mr.getInstanceMaterial(-1)).to.be.null; + }); + + it("getInstanceMaterials", () => { + const mr = cubeEntity.getComponent(MeshRenderer); + mr.setMaterials([new UnlitMaterial(engine), new PBRMaterial(engine)]); + + // Test that getInstanceMaterials works correctly. + const materials = mr.getInstanceMaterials(); + expect(materials[0]).to.be.instanceOf(UnlitMaterial); + expect(materials[0].name).to.be.equal("undefined(Instance)"); + expect(materials[1]).to.be.instanceOf(PBRMaterial); + expect(materials[1].name).to.be.equal("undefined(Instance)"); + }); + + it("priority", () => { + const mr = cubeEntity.getComponent(MeshRenderer); + + // Test that set and get priority works correctly. + expect(mr.priority).to.be.equal(0); + + mr.priority = 1; + expect(mr.priority).to.be.equal(1); + + // Test that repeated assignment works correctly. + mr.priority = 1; + expect(mr.priority).to.be.equal(1); + + // Test that set negative value works correctly. + mr.priority = -1; + expect(mr.priority).to.be.equal(-1); + }); + it("destroy", () => { const mr = cubeEntity.getComponent(MeshRenderer); cubeEntity.destroy();