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

Feat: support pbr clearcoat #669

Merged
merged 19 commits into from
Apr 21, 2022
Prev Previous commit
Next Next commit
refactor: implements clearcoat in pbr base material
shensi.zxd committed Mar 10, 2022
commit d5a51186cbe2cf83ae70ab75c9f78ce0d9a87485
90 changes: 89 additions & 1 deletion packages/core/src/material/PBRBaseMaterial.ts
Original file line number Diff line number Diff line change
@@ -15,10 +15,15 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
private static _normalTextureProp = Shader.getPropertyByName("u_normalTexture");
private static _normalTextureIntensityProp = Shader.getPropertyByName("u_normalIntensity");
private static _occlusionTextureIntensityProp = Shader.getPropertyByName("u_occlusionStrength");

private static _emissiveTextureProp = Shader.getPropertyByName("u_emissiveSampler");
private static _occlusionTextureProp = Shader.getPropertyByName("u_occlusionSampler");

private static _clearcoatProp = Shader.getPropertyByName("u_clearcoat");
private static _clearcoatTextureProp = Shader.getPropertyByName("u_clearcoatTexture");
private static _clearcoatRoughnessProp = Shader.getPropertyByName("u_clearcoatRoughness");
private static _clearcoatRoughnessTextureProp = Shader.getPropertyByName("u_clearcoatRoughnessTexture");
private static _clearcoatNormalTextureProp = Shader.getPropertyByName("u_clearcoatNormalTexture");

/**
* Base color.
*/
@@ -148,6 +153,86 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
}
}


/**
* The clearcoat layer intensity, default 0.
*/
get clearcoat(): number {
return this.shaderData.getFloat(PBRBaseMaterial._clearcoatProp);
}

set clearcoat(value: number) {
this.shaderData.setFloat(PBRBaseMaterial._clearcoatProp, value);

if (value === 0) {
this.shaderData.disableMacro("CLEARCOAT");
} else {
this.shaderData.enableMacro("CLEARCOAT");
}
}

/**
* The clearcoat layer intensity texture.
*/
get clearcoatTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRBaseMaterial._clearcoatTextureProp);
}

set clearcoatTexture(value: Texture2D) {
this.shaderData.setTexture(PBRBaseMaterial._clearcoatTextureProp, value);

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATTEXTURE");
} else {
this.shaderData.disableMacro("HAS_CLEARCOATTEXTURE");
}
}

/**
* The clearcoat layer roughness, default 0.
*/
get clearcoatRoughness(): number {
return this.shaderData.getFloat(PBRBaseMaterial._clearcoatRoughnessProp);
}

set clearcoatRoughness(value: number) {
this.shaderData.setFloat(PBRBaseMaterial._clearcoatRoughnessProp, value);
}

/**
* The clearcoat layer roughness texture.
*/
get clearcoatRoughnessTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRBaseMaterial._clearcoatRoughnessTextureProp);
}

set clearcoatRoughnessTexture(value: Texture2D) {
this.shaderData.setTexture(PBRBaseMaterial._clearcoatRoughnessTextureProp, value);

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATROUGHNESSTEXTURE");
} else {
this.shaderData.disableMacro("HAS_CLEARCOATROUGHNESSTEXTURE");
}
}

/**
* The clearcoat normal map texture.
*/
get clearcoatNormalTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRBaseMaterial._clearcoatNormalTextureProp);
}

set clearcoatNormalTexture(value: Texture2D) {
this.shaderData.setTexture(PBRBaseMaterial._clearcoatNormalTextureProp, value);

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATNORMALTEXTURE");
} else {
this.shaderData.disableMacro("HAS_CLEARCOATNORMALTEXTURE");
}
}

/**
* Create a pbr base material instance.
* @param engine - Engine to which the material belongs
@@ -167,5 +252,8 @@ export abstract class PBRBaseMaterial extends BaseMaterial {

shaderData.setFloat(PBRBaseMaterial._normalTextureIntensityProp, 1);
shaderData.setFloat(PBRBaseMaterial._occlusionTextureIntensityProp, 1);

this.shaderData.setFloat(PBRBaseMaterial._clearcoatProp, 0);
this.shaderData.setFloat(PBRBaseMaterial._clearcoatRoughnessProp, 0);
}
}
86 changes: 0 additions & 86 deletions packages/core/src/material/PBRMaterial.ts
Original file line number Diff line number Diff line change
@@ -10,11 +10,6 @@ export class PBRMaterial extends PBRBaseMaterial {
private static _metallicProp = Shader.getPropertyByName("u_metal");
private static _roughnessProp = Shader.getPropertyByName("u_roughness");
private static _metallicRoughnessTextureProp = Shader.getPropertyByName("u_metallicRoughnessSampler");
private static _clearcoatProp = Shader.getPropertyByName("u_clearcoat");
private static _clearcoatTextureProp = Shader.getPropertyByName("u_clearcoatTexture");
private static _clearcoatRoughnessProp = Shader.getPropertyByName("u_clearcoatRoughness");
private static _clearcoatRoughnessTextureProp = Shader.getPropertyByName("u_clearcoatRoughnessTexture");
private static _clearcoatNormalTextureProp = Shader.getPropertyByName("u_clearcoatNormalTexture");

/**
* Metallic, default 1.
@@ -55,85 +50,6 @@ export class PBRMaterial extends PBRBaseMaterial {
}
}

/**
* The clearcoat layer intensity, default 0.
*/
get clearcoat(): number {
return this.shaderData.getFloat(PBRMaterial._clearcoatProp);
}

set clearcoat(value: number) {
this.shaderData.setFloat(PBRMaterial._clearcoatProp, value);

if (value === 0) {
this.shaderData.disableMacro("CLEARCOAT");
} else {
this.shaderData.enableMacro("CLEARCOAT");
}
}

/**
* The clearcoat layer intensity texture.
*/
get clearcoatTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._clearcoatTextureProp);
}

set clearcoatTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._clearcoatTextureProp, value);

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATTEXTURE");
} else {
this.shaderData.disableMacro("HAS_CLEARCOATTEXTURE");
}
}

/**
* The clearcoat layer roughness, default 0.
*/
get clearcoatRoughness(): number {
return this.shaderData.getFloat(PBRMaterial._clearcoatRoughnessProp);
}

set clearcoatRoughness(value: number) {
this.shaderData.setFloat(PBRMaterial._clearcoatRoughnessProp, value);
}

/**
* The clearcoat layer roughness texture.
*/
get clearcoatRoughnessTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._clearcoatRoughnessTextureProp);
}

set clearcoatRoughnessTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._clearcoatRoughnessTextureProp, value);

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATROUGHNESSTEXTURE");
} else {
this.shaderData.disableMacro("HAS_CLEARCOATROUGHNESSTEXTURE");
}
}

/**
* The clearcoat normal map texture.
*/
get clearcoatNormalTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._clearcoatNormalTextureProp);
}

set clearcoatNormalTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._clearcoatNormalTextureProp, value);

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATNORMALTEXTURE");
} else {
this.shaderData.disableMacro("HAS_CLEARCOATNORMALTEXTURE");
}
}

/**
* Create a pbr metallic-roughness workflow material instance.
* @param engine - Engine to which the material belongs
@@ -142,8 +58,6 @@ export class PBRMaterial extends PBRBaseMaterial {
super(engine, Shader.find("pbr"));
this.shaderData.setFloat(PBRMaterial._metallicProp, 1);
this.shaderData.setFloat(PBRMaterial._roughnessProp, 1);
this.shaderData.setFloat(PBRMaterial._clearcoatProp, 0);
this.shaderData.setFloat(PBRMaterial._clearcoatRoughnessProp, 0);
}

/**