Test/material (#422)

* feat: add material test
This commit is contained in:
zhuxudong
2021-08-13 18:23:18 +08:00
committed by GitHub
parent 1848946650
commit 794c5f2ef5
13 changed files with 395 additions and 13 deletions

View File

@@ -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);
});
});

View File

@@ -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));
});
});

View File

@@ -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);
});
});

View File

@@ -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);
});
});

View File

@@ -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);
});
});

View File

@@ -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);
});
});

View File

@@ -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);
});
});

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
});
});