Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uniform decorator for improving custom material code #1713

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 45 additions & 102 deletions packages/core/src/material/BlinnPhongMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Color, Vector4 } from "@galacean/engine-math";
import { Engine } from "../Engine";
import { ShaderUniformType, uniform } from "../shader";
import { Shader } from "../shader/Shader";
import { ShaderProperty } from "../shader/ShaderProperty";
import { Texture2D } from "../texture/Texture2D";
Expand All @@ -9,126 +10,78 @@ import { BaseMaterial } from "./BaseMaterial";
* Blinn-phong Material.
*/
export class BlinnPhongMaterial extends BaseMaterial {
private static _specularColorProp = ShaderProperty.getByName("material_SpecularColor");
private static _shininessProp = ShaderProperty.getByName("material_Shininess");
private static _specularTextureProp = ShaderProperty.getByName("material_SpecularTexture");

/**
* Base color.
*/
get baseColor(): Color {
return this.shaderData.getColor(BlinnPhongMaterial._baseColorProp);
}

set baseColor(value: Color) {
const baseColor = this.shaderData.getColor(BlinnPhongMaterial._baseColorProp);
if (value !== baseColor) {
baseColor.copyFrom(value);
}
}
@uniform(ShaderUniformType.Color, {
varName: "material_BaseColor",
keepRef: true
})
baseColor: Color = new Color(1, 1, 1, 1);

/**
* Base texture.
*/
get baseTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(BlinnPhongMaterial._baseTextureProp);
}

set baseTexture(value: Texture2D) {
this.shaderData.setTexture(BlinnPhongMaterial._baseTextureProp, value);
if (value) {
this.shaderData.enableMacro(BlinnPhongMaterial._baseTextureMacro);
} else {
this.shaderData.disableMacro(BlinnPhongMaterial._baseTextureMacro);
}
}
@uniform(ShaderUniformType.Texture, {
varName: "material_BaseTexture",
macroName: "MATERIAL_HAS_BASETEXTURE"
})
baseTexture: Texture2D;

/**
* Specular color.
*/
get specularColor(): Color {
return this.shaderData.getColor(BlinnPhongMaterial._specularColorProp);
}

set specularColor(value: Color) {
const specularColor = this.shaderData.getColor(BlinnPhongMaterial._specularColorProp);
if (value !== specularColor) {
specularColor.copyFrom(value);
}
}
@uniform(ShaderUniformType.Color, {
varName: "material_SpecularColor",
keepRef: true
})
specularColor: Color = new Color(1, 1, 1, 1);

/**
* Specular texture.
*/
get specularTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(BlinnPhongMaterial._specularTextureProp);
}

set specularTexture(value: Texture2D) {
this.shaderData.setTexture(BlinnPhongMaterial._specularTextureProp, value);
if (value) {
this.shaderData.enableMacro("MATERIAL_HAS_SPECULAR_TEXTURE");
} else {
this.shaderData.disableMacro("MATERIAL_HAS_SPECULAR_TEXTURE");
}
}
@uniform(ShaderUniformType.Texture, {
varName: "material_SpecularTexture",
macroName: "MATERIAL_HAS_SPECULAR_TEXTURE"
})
specularTexture: Texture2D;

/**
* Emissive color.
*/
get emissiveColor(): Color {
return this.shaderData.getColor(BlinnPhongMaterial._emissiveColorProp);
}

set emissiveColor(value: Color) {
const emissiveColor = this.shaderData.getColor(BlinnPhongMaterial._emissiveColorProp);
if (value !== emissiveColor) {
emissiveColor.copyFrom(value);
}
}
@uniform(ShaderUniformType.Color, {
varName: "material_EmissiveColor",
keepRef: true
})
emissiveColor: Color = new Color(0, 0, 0, 1);

/**
* Emissive texture.
*/
get emissiveTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(BlinnPhongMaterial._emissiveTextureProp);
}

set emissiveTexture(value: Texture2D) {
this.shaderData.setTexture(BlinnPhongMaterial._emissiveTextureProp, value);
if (value) {
this.shaderData.enableMacro(BlinnPhongMaterial._emissiveTextureMacro);
} else {
this.shaderData.disableMacro(BlinnPhongMaterial._emissiveTextureMacro);
}
}
@uniform(ShaderUniformType.Texture, {
varName: "material_EmissiveTexture",
macroName: "MATERIAL_HAS_EMISSIVETEXTURE"
})
emissiveTexture: Texture2D;

/**
* Normal texture.
*/
get normalTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(BlinnPhongMaterial._normalTextureProp);
}

set normalTexture(value: Texture2D) {
this.shaderData.setTexture(BlinnPhongMaterial._normalTextureProp, value);
if (value) {
this.shaderData.enableMacro(BlinnPhongMaterial._normalTextureMacro);
} else {
this.shaderData.disableMacro(BlinnPhongMaterial._normalTextureMacro);
}
}
@uniform(ShaderUniformType.Texture, {
varName: "material_NormalTexture",
macroName: "MATERIAL_HAS_NORMALTEXTURE"
})
normalTexture: Texture2D;

/**
* Normal texture intensity.
*/
get normalIntensity(): number {
return this.shaderData.getFloat(BlinnPhongMaterial._normalIntensityProp);
}

set normalIntensity(value: number) {
this.shaderData.setFloat(BlinnPhongMaterial._normalIntensityProp, value);
}
@uniform(ShaderUniformType.Float, {
varName: "material_NormalIntensity"
})
normalIntensity: number = 1;

/**
* Set the specular reflection coefficient, the larger the value, the more convergent the specular reflection effect.
Expand All @@ -144,16 +97,11 @@ export class BlinnPhongMaterial extends BaseMaterial {
/**
* Tiling and offset of main textures.
*/
get tilingOffset(): Vector4 {
return this.shaderData.getVector4(BlinnPhongMaterial._tilingOffsetProp);
}

set tilingOffset(value: Vector4) {
const tilingOffset = this.shaderData.getVector4(BlinnPhongMaterial._tilingOffsetProp);
if (value !== tilingOffset) {
tilingOffset.copyFrom(value);
}
}
@uniform(ShaderUniformType.Vector4, {
varName: "material_TilingOffset",
keepRef: true
})
tilingOffset: Vector4 = new Vector4(1, 1, 0, 0);

/**
* Create a BlinnPhong material instance.
Expand All @@ -167,12 +115,7 @@ export class BlinnPhongMaterial extends BaseMaterial {
shaderData.enableMacro("MATERIAL_NEED_WORLD_POS");
shaderData.enableMacro("MATERIAL_NEED_TILING_OFFSET");

shaderData.setColor(BlinnPhongMaterial._baseColorProp, new Color(1, 1, 1, 1));
shaderData.setColor(BlinnPhongMaterial._specularColorProp, new Color(1, 1, 1, 1));
shaderData.setColor(BlinnPhongMaterial._emissiveColorProp, new Color(0, 0, 0, 1));
shaderData.setVector4(BlinnPhongMaterial._tilingOffsetProp, new Vector4(1, 1, 0, 0));
shaderData.setFloat(BlinnPhongMaterial._shininessProp, 16);
shaderData.setFloat(BlinnPhongMaterial._normalIntensityProp, 1);
}

override clone(): BlinnPhongMaterial {
Expand Down
Loading