diff --git a/packages/core/tests/material/BaseMaterial.test.ts b/packages/core/tests/material/BaseMaterial.test.ts new file mode 100644 index 000000000..5d60ff297 --- /dev/null +++ b/packages/core/tests/material/BaseMaterial.test.ts @@ -0,0 +1,79 @@ +// @ts-nocheck +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { BaseMaterial, BlendMode, RenderFace } from "../../src/material"; +import { CullMode, Shader } from "../../src/shader"; + +describe("BaseMaterial", () => { + const canvas = document.createElement("canvas"); + const engine = new WebGLEngine(canvas); + + class TestMaterial extends BaseMaterial { + constructor(engine) { + super(engine, Shader.find("blinn-phong")); + } + + clone(): TestMaterial { + const dest = new TestMaterial(this._engine); + this.cloneTo(dest); + return dest; + } + } + + it("base 参数测试", () => { + const material = new TestMaterial(engine); + + expect(material.alphaCutoff).toBe(0); + expect(material.isTransparent).toBe(false); + expect(material.blendMode).toBe(BlendMode.Normal); + expect(material.renderFace).toBe(RenderFace.Front); + + material.alphaCutoff = 0.5; + material.isTransparent = true; + material.blendMode = BlendMode.Additive; + material.renderFace = RenderFace.Double; + + expect(material.alphaCutoff).toBe(0.5); + expect(material.isTransparent).toBe(true); + expect(material.blendMode).toBe(BlendMode.Additive); + expect(material.renderFace).toBe(RenderFace.Double); + }); + + it("renderFace", () => { + const material = new TestMaterial(engine); + + material.renderFace = RenderFace.Front; + expect(material.renderState.rasterState.cullMode).toBe(CullMode.Back); + material.renderFace = RenderFace.Back; + expect(material.renderState.rasterState.cullMode).toBe(CullMode.Front); + material.renderFace = RenderFace.Double; + expect(material.renderState.rasterState.cullMode).toBe(CullMode.Off); + }); + + it("isTransparent", () => { + const material = new TestMaterial(engine); + + expect(material.renderState.blendState.targetBlendState.enabled).toBeFalsy(); + expect(material.renderState.depthState.writeEnabled).toBeTruthy(); + + material.isTransparent = true; + + expect(material.renderState.blendState.targetBlendState.enabled).toBeTruthy(); + expect(material.renderState.depthState.writeEnabled).toBeFalsy(); + }); + + it("clone", () => { + const material = new TestMaterial(engine); + + material.alphaCutoff = 0.5; + material.isTransparent = true; + material.blendMode = BlendMode.Additive; + material.renderFace = RenderFace.Double; + + const clone = material.clone(); + + expect(clone.alphaCutoff).toBe(0.5); + expect(clone.isTransparent).toBe(true); + expect(clone.blendMode).toBe(BlendMode.Additive); + expect(clone.renderFace).toBe(RenderFace.Double); + }); +}); diff --git a/packages/core/tests/material/BlinnPhongMaterial.test.ts b/packages/core/tests/material/BlinnPhongMaterial.test.ts new file mode 100644 index 000000000..01fce008d --- /dev/null +++ b/packages/core/tests/material/BlinnPhongMaterial.test.ts @@ -0,0 +1,87 @@ +// @ts-nocheck +import { Color, Vector4 } from "@oasis-engine/math"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { BlinnPhongMaterial } from "../../src/material"; +import { Texture2D } from "../../src/texture"; + +describe("BlinnPhongMaterial", () => { + const canvas = document.createElement("canvas"); + const engine = new WebGLEngine(canvas); + + it("参数测试", () => { + const material = new BlinnPhongMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + expect(material.baseColor).toEqual(new Color(1, 1, 1, 1)); + expect(material.specularColor).toEqual(new Color(1, 1, 1, 1)); + expect(material.emissiveColor).toEqual(new Color(0, 0, 0, 1)); + expect(material.baseTexture).toBeUndefined(); + expect(material.specularTexture).toBeUndefined(); + expect(material.emissiveTexture).toBeUndefined(); + expect(material.normalTexture).toBeUndefined(); + expect(material.normalIntensity).toBe(1); + expect(material.shininess).toBe(16); + expect(material.tilingOffset).toEqual(new Vector4(1, 1, 0, 0)); + + material.baseColor.setValue(1, 0, 0, 1); + material.specularColor.setValue(1, 0, 0, 1); + material.emissiveColor.setValue(1, 0, 0, 1); + material.baseTexture = texture; + material.specularTexture = texture; + material.emissiveTexture = texture; + material.normalTexture = texture; + material.normalIntensity = 2; + material.shininess = 32; + material.tilingOffset.setValue(1, 1, 1, 1); + + expect(material.baseColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.specularColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.emissiveColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.baseTexture).toBe(texture); + expect(material.specularTexture).toBe(texture); + expect(material.emissiveTexture).toBe(texture); + expect(material.normalTexture).toBe(texture); + expect(material.normalIntensity).toBe(2); + expect(material.shininess).toBe(32); + expect(material.tilingOffset).toEqual(new Vector4(1, 1, 1, 1)); + + material.baseTexture = null; + material.specularTexture = null; + material.emissiveTexture = null; + material.normalTexture = null; + + expect(material.baseTexture).toBeNull(); + expect(material.specularTexture).toBeNull(); + expect(material.emissiveTexture).toBeNull(); + expect(material.normalTexture).toBeNull(); + }); + + it("clone", () => { + const material = new BlinnPhongMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + material.baseColor = new Color(1, 0, 0, 1); + material.specularColor = new Color(1, 0, 0, 1); + material.emissiveColor = new Color(1, 0, 0, 1); + material.baseTexture = texture; + material.specularTexture = texture; + material.emissiveTexture = texture; + material.normalTexture = texture; + material.normalIntensity = 2; + material.shininess = 32; + material.tilingOffset = new Vector4(1, 1, 1, 1); + + const clone = material.clone(); + + expect(clone.baseColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.specularColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.emissiveColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.baseTexture).toBe(material.baseTexture); + expect(clone.specularTexture).toBe(material.specularTexture); + expect(clone.emissiveTexture).toBe(material.emissiveTexture); + expect(clone.normalTexture).toBe(material.normalTexture); + expect(clone.normalIntensity).toBe(2); + expect(clone.shininess).toBe(32); + expect(clone.tilingOffset).toEqual(new Vector4(1, 1, 1, 1)); + }); +}); diff --git a/packages/core/tests/material/PBRBaseMaterial.test.ts b/packages/core/tests/material/PBRBaseMaterial.test.ts new file mode 100644 index 000000000..80c90dfce --- /dev/null +++ b/packages/core/tests/material/PBRBaseMaterial.test.ts @@ -0,0 +1,83 @@ +// @ts-nocheck +import { Color, Vector4 } from "@oasis-engine/math"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { PBRMaterial } from "../../src/material"; +import { Texture2D } from "../../src/texture"; + +describe("PBRBaseMaterial", () => { + const canvas = document.createElement("canvas"); + const engine = new WebGLEngine(canvas); + + it("pbr base 参数测试", () => { + const material = new PBRMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + expect(material.baseColor).toEqual(new Color(1, 1, 1, 1)); + expect(material.emissiveColor).toEqual(new Color(0, 0, 0, 1)); + expect(material.tilingOffset).toEqual(new Vector4(1, 1, 0, 0)); + expect(material.normalTextureIntensity).toBe(1); + expect(material.occlusionTextureIntensity).toBe(1); + + expect(material.baseTexture).toBeUndefined(); + expect(material.emissiveTexture).toBeUndefined(); + expect(material.normalTexture).toBeUndefined(); + expect(material.occlusionTexture).toBeUndefined(); + + material.baseColor.setValue(1, 0, 0, 1); + material.emissiveColor.setValue(1, 0, 0, 1); + material.tilingOffset.setValue(1, 1, 1, 1); + material.normalTextureIntensity = 2; + material.occlusionTextureIntensity = 2; + material.baseTexture = texture; + material.emissiveTexture = texture; + material.normalTexture = texture; + material.occlusionTexture = texture; + + expect(material.baseColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.emissiveColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.tilingOffset).toEqual(new Vector4(1, 1, 1, 1)); + expect(material.normalTextureIntensity).toBe(2); + expect(material.occlusionTextureIntensity).toBe(2); + expect(material.baseTexture).toBe(texture); + expect(material.emissiveTexture).toBe(texture); + expect(material.normalTexture).toBe(texture); + expect(material.occlusionTexture).toBe(texture); + + material.baseTexture = null; + material.emissiveTexture = null; + material.normalTexture = null; + material.occlusionTexture = null; + + expect(material.baseTexture).toBeNull(); + expect(material.emissiveTexture).toBeNull(); + expect(material.normalTexture).toBeNull(); + expect(material.occlusionTexture).toBeNull(); + }); + + it("clone", () => { + const material = new PBRMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + material.baseColor = new Color(1, 0, 0, 1); + material.emissiveColor = new Color(1, 0, 0, 1); + material.tilingOffset = new Vector4(1, 1, 1, 1); + material.normalTextureIntensity = 2; + material.occlusionTextureIntensity = 2; + material.baseTexture = texture; + material.emissiveTexture = texture; + material.normalTexture = texture; + material.occlusionTexture = texture; + + const clone = material.clone(); + + expect(clone.baseColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.emissiveColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.tilingOffset).toEqual(new Vector4(1, 1, 1, 1)); + expect(clone.normalTextureIntensity).toBe(2); + expect(clone.occlusionTextureIntensity).toBe(2); + expect(clone.baseTexture).toBe(material.baseTexture); + expect(clone.emissiveTexture).toBe(material.emissiveTexture); + expect(clone.normalTexture).toBe(material.normalTexture); + expect(clone.occlusionTexture).toBe(material.occlusionTexture); + }); +}); diff --git a/packages/core/tests/material/PBRMaterial.test.ts b/packages/core/tests/material/PBRMaterial.test.ts new file mode 100644 index 000000000..1db3492f2 --- /dev/null +++ b/packages/core/tests/material/PBRMaterial.test.ts @@ -0,0 +1,45 @@ +// @ts-nocheck +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { PBRMaterial } from "../../src/material"; +import { Texture2D } from "../../src/texture"; + +describe("PBRMaterial", () => { + const canvas = document.createElement("canvas"); + const engine = new WebGLEngine(canvas); + + it("pbr 参数测试", () => { + const material = new PBRMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + expect(material.metallic).toBe(1); + expect(material.roughness).toBe(1); + expect(material.roughnessMetallicTexture).toBeUndefined(); + + material.metallic = 2; + material.roughness = 2; + material.roughnessMetallicTexture = texture; + + expect(material.metallic).toBe(2); + expect(material.roughness).toBe(2); + expect(material.roughnessMetallicTexture).toBe(texture); + + material.roughnessMetallicTexture = null; + + expect(material.roughnessMetallicTexture).toBeNull(); + }); + + it("clone", () => { + const material = new PBRMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + material.metallic = 2; + material.roughness = 2; + material.roughnessMetallicTexture = texture; + + const clone = material.clone(); + + expect(clone.metallic).toBe(2); + expect(clone.roughness).toBe(2); + expect(clone.roughnessMetallicTexture).toBe(material.roughnessMetallicTexture); + }); +}); diff --git a/packages/core/tests/material/PBRSpecularMaterial.test.ts b/packages/core/tests/material/PBRSpecularMaterial.test.ts new file mode 100644 index 000000000..9d82bacf1 --- /dev/null +++ b/packages/core/tests/material/PBRSpecularMaterial.test.ts @@ -0,0 +1,46 @@ +// @ts-nocheck +import { Color } from "@oasis-engine/math"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { PBRSpecularMaterial } from "../../src/material"; +import { Texture2D } from "../../src/texture"; + +describe("PBRSpecularMaterial", () => { + const canvas = document.createElement("canvas"); + const engine = new WebGLEngine(canvas); + + it("pbr specular 参数测试", () => { + const material = new PBRSpecularMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + expect(material.specularColor).toEqual(new Color(1, 1, 1, 1)); + expect(material.glossiness).toBe(1); + expect(material.specularGlossinessTexture).toBeUndefined(); + + material.specularColor.setValue(1, 0, 0, 1); + material.glossiness = 2; + material.specularGlossinessTexture = texture; + + expect(material.specularColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.glossiness).toBe(2); + expect(material.specularGlossinessTexture).toBe(texture); + + material.specularGlossinessTexture = null; + + expect(material.specularGlossinessTexture).toBeNull(); + }); + + it("clone", () => { + const material = new PBRSpecularMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + material.specularColor = new Color(1, 0, 0, 1); + material.glossiness = 2; + material.specularGlossinessTexture = texture; + + const clone = material.clone(); + + expect(clone.specularColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.glossiness).toBe(2); + expect(clone.specularGlossinessTexture).toBe(material.specularGlossinessTexture); + }); +}); diff --git a/packages/core/tests/material/UnlitMaterial.test.ts b/packages/core/tests/material/UnlitMaterial.test.ts new file mode 100644 index 000000000..f53faaa0f --- /dev/null +++ b/packages/core/tests/material/UnlitMaterial.test.ts @@ -0,0 +1,43 @@ +// @ts-nocheck +import { Color, Vector4 } from "@oasis-engine/math"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { UnlitMaterial } from "../../src/material"; +import { Texture2D } from "../../src/texture"; + +describe("UnlitMaterial", () => { + const canvas = document.createElement("canvas"); + const engine = new WebGLEngine(canvas); + + it("参数测试", () => { + const material = new UnlitMaterial(engine); + const texture = new Texture2D(engine, 1024, 1024); + + expect(material.baseColor).toEqual(new Color(1, 1, 1, 1)); + expect(material.tilingOffset).toEqual(new Vector4(1, 1, 0, 0)); + expect(material.baseTexture).toBeUndefined(); + + material.baseColor.setValue(1, 0, 0, 1); + material.tilingOffset.setValue(1, 1, 1, 1); + material.baseTexture = texture; + + expect(material.baseColor).toEqual(new Color(1, 0, 0, 1)); + expect(material.tilingOffset).toEqual(new Vector4(1, 1, 1, 1)); + expect(material.baseTexture).toBe(texture); + + material.baseTexture = null; + expect(material.baseTexture).toBeNull(); + }); + + it("clone", () => { + const material = new UnlitMaterial(engine); + + material.baseColor = new Color(1, 0, 0, 1); + material.tilingOffset = new Vector4(1, 1, 1, 1); + material.baseTexture = new Texture2D(engine, 1024, 1024); + + const clone = material.clone(); + expect(clone.baseColor).toEqual(new Color(1, 0, 0, 1)); + expect(clone.tilingOffset).toEqual(new Vector4(1, 1, 1, 1)); + expect(clone.baseTexture).toBe(material.baseTexture); + }); +}); diff --git a/packages/core/tests/script.test.ts b/packages/core/tests/script.test.ts index 6d0873e35..4f08bfb31 100644 --- a/packages/core/tests/script.test.ts +++ b/packages/core/tests/script.test.ts @@ -281,7 +281,7 @@ describe("Script", () => { const root = scene.getRootEntity(); const component = root.addComponent(TheScript); component.destroy(); - engine._componentsManager.callComponentDestory(); + engine._componentsManager.callComponentDestroy(); expect(component.onDestroy).toHaveBeenCalledTimes(1); }); }); diff --git a/packages/core/tests/RenderColorTexture.test.ts b/packages/core/tests/texture/RenderColorTexture.test.ts similarity index 96% rename from packages/core/tests/RenderColorTexture.test.ts rename to packages/core/tests/texture/RenderColorTexture.test.ts index 1cb364fc8..d9ce1f10a 100644 --- a/packages/core/tests/RenderColorTexture.test.ts +++ b/packages/core/tests/texture/RenderColorTexture.test.ts @@ -1,6 +1,6 @@ // @ts-nocheck -import { WebGLEngine } from "../../rhi-webgl/src/WebGLEngine"; -import { RenderBufferColorFormat, RenderColorTexture } from "../src/texture"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { RenderBufferColorFormat, RenderColorTexture } from "../../src/texture"; describe("RenderColorTexture", () => { const width = 1024; diff --git a/packages/core/tests/RenderDepthTexture.test.ts b/packages/core/tests/texture/RenderDepthTexture.test.ts similarity index 96% rename from packages/core/tests/RenderDepthTexture.test.ts rename to packages/core/tests/texture/RenderDepthTexture.test.ts index 083070046..91a6fd2d1 100644 --- a/packages/core/tests/RenderDepthTexture.test.ts +++ b/packages/core/tests/texture/RenderDepthTexture.test.ts @@ -1,6 +1,6 @@ // @ts-nocheck -import { WebGLEngine } from "../../rhi-webgl/src/WebGLEngine"; -import { RenderBufferDepthFormat, RenderDepthTexture } from "../src/texture"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { RenderBufferDepthFormat, RenderDepthTexture } from "../../src/texture"; describe("RenderDepthTexture", () => { const width = 1024; diff --git a/packages/core/tests/RenderTarget.test.ts b/packages/core/tests/texture/RenderTarget.test.ts similarity index 97% rename from packages/core/tests/RenderTarget.test.ts rename to packages/core/tests/texture/RenderTarget.test.ts index aa7a890f2..0603c0dd3 100644 --- a/packages/core/tests/RenderTarget.test.ts +++ b/packages/core/tests/texture/RenderTarget.test.ts @@ -1,8 +1,8 @@ // @ts-nocheck // @todo: jest `_depth instanceof RenderDepthTexture` in `GLRenderTarget.ts` always return `false`, so test with depthTexture in renderTarget is ignored. -import { WebGLEngine } from "../../rhi-webgl/src/WebGLEngine"; -import { RenderBufferDepthFormat, RenderColorTexture, RenderDepthTexture, RenderTarget } from "../src/texture"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; +import { RenderBufferDepthFormat, RenderColorTexture, RenderDepthTexture, RenderTarget } from "../../src/texture"; describe("RenderTarget", () => { const width = 1024; diff --git a/packages/core/tests/Texture2D.test.ts b/packages/core/tests/texture/Texture2D.test.ts similarity index 96% rename from packages/core/tests/Texture2D.test.ts rename to packages/core/tests/texture/Texture2D.test.ts index 59bba21bc..a65bcd87a 100644 --- a/packages/core/tests/Texture2D.test.ts +++ b/packages/core/tests/texture/Texture2D.test.ts @@ -1,6 +1,6 @@ // @ts-nocheck -import { Texture2D, TextureFormat } from "../src/texture"; -import { WebGLEngine } from "../../rhi-webgl/src/WebGLEngine"; +import { Texture2D, TextureFormat } from "../../src/texture"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; describe("Texture2D", () => { const width = 1024; diff --git a/packages/core/tests/TextureCubeMap.test.ts b/packages/core/tests/texture/TextureCubeMap.test.ts similarity index 98% rename from packages/core/tests/TextureCubeMap.test.ts rename to packages/core/tests/texture/TextureCubeMap.test.ts index d6c58cb18..5c084848b 100644 --- a/packages/core/tests/TextureCubeMap.test.ts +++ b/packages/core/tests/texture/TextureCubeMap.test.ts @@ -1,6 +1,6 @@ // @ts-nocheck -import { TextureCubeFace, TextureCubeMap, TextureFormat } from "../src/texture"; -import { WebGLEngine } from "../../rhi-webgl/src/WebGLEngine"; +import { TextureCubeFace, TextureCubeMap, TextureFormat } from "../../src/texture"; +import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine"; describe("TextureCubeMap", () => { const width = 1024; diff --git a/packages/loader/tests/gltf/parser.test.ts b/packages/loader/tests/gltf/parser.test.ts index 9810bca9a..3d315d01b 100644 --- a/packages/loader/tests/gltf/parser.test.ts +++ b/packages/loader/tests/gltf/parser.test.ts @@ -69,7 +69,6 @@ describe("AnimationClipLoader Test", () => { expect(animations.length === 1).toEqual(true); const animation = animations[0]; expect(animation.name === "animation_AnimatedCube").toEqual(true); - expect(animation.channels.length === 1).toEqual(true); - expect(animation.samplers.length === 1).toEqual(true); + expect(animation._curves.length).toEqual(1); }); });