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: rename
shensi.zxd committed Apr 21, 2022
commit 8c2291da5f096243e38c479765856159784ad13f
66 changes: 33 additions & 33 deletions packages/core/src/material/PBRBaseMaterial.ts
Original file line number Diff line number Diff line change
@@ -21,11 +21,11 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
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");
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.
@@ -171,32 +171,32 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
}

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

set clearcoat(value: number) {
if (!!this.shaderData.getFloat(PBRBaseMaterial._clearcoatProp) !== !!value) {
set clearCoat(value: number) {
if (!!this.shaderData.getFloat(PBRBaseMaterial._clearCoatProp) !== !!value) {
if (value === 0) {
this.shaderData.disableMacro("CLEARCOAT");
} else {
this.shaderData.enableMacro("CLEARCOAT");
}
}
this.shaderData.setFloat(PBRBaseMaterial._clearcoatProp, value);
this.shaderData.setFloat(PBRBaseMaterial._clearCoatProp, value);
}

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

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

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATTEXTURE");
@@ -206,25 +206,25 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
}

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

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

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

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

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATROUGHNESSTEXTURE");
@@ -234,14 +234,14 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
}

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

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

if (value) {
this.shaderData.enableMacro("HAS_CLEARCOATNORMALTEXTURE");
@@ -271,7 +271,7 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
shaderData.setFloat(PBRBaseMaterial._occlusionTextureIntensityProp, 1);
shaderData.setFloat(PBRBaseMaterial._occlusionTextureCoordProp, TextureCoordinate.UV0);

shaderData.setFloat(PBRBaseMaterial._clearcoatProp, 0);
shaderData.setFloat(PBRBaseMaterial._clearcoatRoughnessProp, 0);
shaderData.setFloat(PBRBaseMaterial._clearCoatProp, 0);
shaderData.setFloat(PBRBaseMaterial._clearCoatRoughnessProp, 0);
}
}
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Ma
float attenuation = 1.0;

#ifdef CLEARCOAT
float clearcoatDotNL = saturate( dot( geometry.clearcoatNormal, incidentDirection ) );
vec3 clearcoatIrradiance = clearcoatDotNL * color;
float clearCoatDotNL = saturate( dot( geometry.clearCoatNormal, incidentDirection ) );
vec3 clearCoatIrradiance = clearCoatDotNL * color;

reflectedLight.directSpecular += material.clearcoat * clearcoatIrradiance * BRDF_Specular_GGX( incidentDirection, geometry.viewDir, geometry.clearcoatNormal, vec3( 0.04 ), material.clearcoatRoughness );
attenuation -= material.clearcoat * F_Schlick(geometry.clearcoatDotNV);
reflectedLight.directSpecular += material.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, geometry.viewDir, geometry.clearCoatNormal, vec3( 0.04 ), material.clearCoatRoughness );
attenuation -= material.clearCoat * F_Schlick(geometry.clearCoatDotNV);
#endif

float dotNL = saturate( dot( geometry.normal, incidentDirection ) );
6 changes: 3 additions & 3 deletions packages/core/src/shaderlib/pbr/pbr_frag.glsl
Original file line number Diff line number Diff line change
@@ -27,10 +27,10 @@ vec3 radiance = getLightProbeRadiance(geometry.viewDir, geometry.normal, materia
float radianceAttenuation = 1.0;

#ifdef CLEARCOAT
vec3 clearcoatRadiance = getLightProbeRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness, int(u_envMapLight.mipMapLevel), u_envMapLight.specularIntensity );
vec3 clearCoatRadiance = getLightProbeRadiance( geometry.viewDir, geometry.clearCoatNormal, material.clearCoatRoughness, int(u_envMapLight.mipMapLevel), u_envMapLight.specularIntensity );

reflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * envBRDFApprox(vec3( 0.04 ), material.clearcoatRoughness, geometry.clearcoatDotNV);
radianceAttenuation -= material.clearcoat * F_Schlick(geometry.clearcoatDotNV);
reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * envBRDFApprox(vec3( 0.04 ), material.clearCoatRoughness, geometry.clearCoatDotNV);
radianceAttenuation -= material.clearCoat * F_Schlick(geometry.clearCoatDotNV);
#endif

reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(material.specularColor, material.roughness, geometry.dotNV );
18 changes: 9 additions & 9 deletions packages/core/src/shaderlib/pbr/pbr_frag_define.glsl
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ uniform float u_glossiness;
uniform vec3 u_emissiveColor;

#ifdef CLEARCOAT
uniform float u_clearcoat;
uniform float u_clearcoatRoughness;
uniform float u_clearCoat;
uniform float u_clearCoatRoughness;
#endif

uniform float u_normalIntensity;
@@ -42,15 +42,15 @@ uniform float u_occlusionTextureCoord;
#endif

#ifdef HAS_CLEARCOATTEXTURE
uniform sampler2D u_clearcoatTexture;
uniform sampler2D u_clearCoatTexture;
#endif

#ifdef HAS_CLEARCOATROUGHNESSTEXTURE
uniform sampler2D u_clearcoatRoughnessTexture;
uniform sampler2D u_clearCoatRoughnessTexture;
#endif

#ifdef HAS_CLEARCOATNORMALTEXTURE
uniform sampler2D u_clearcoatNormalTexture;
uniform sampler2D u_clearCoatNormalTexture;
#endif


@@ -70,8 +70,8 @@ struct Geometry {
float dotNV;

#ifdef CLEARCOAT
vec3 clearcoatNormal;
float clearcoatDotNV;
vec3 clearCoatNormal;
float clearCoatDotNV;
#endif

};
@@ -82,8 +82,8 @@ struct Material {
vec3 specularColor;
float opacity;
#ifdef CLEARCOAT
float clearcoat;
float clearcoatRoughness;
float clearCoat;
float clearCoatRoughness;
#endif

};
18 changes: 9 additions & 9 deletions packages/core/src/shaderlib/pbr/pbr_helper.glsl
Original file line number Diff line number Diff line change
@@ -34,15 +34,15 @@ void initGeometry(out Geometry geometry){


#ifdef CLEARCOAT
geometry.clearcoatNormal = getNormal(
geometry.clearCoatNormal = getNormal(
#ifdef HAS_CLEARCOATNORMALTEXTURE
tbn,
u_clearcoatNormalTexture,
u_clearCoatNormalTexture,
u_normalIntensity,
v_uv
#endif
);
geometry.clearcoatDotNV = saturate( dot(geometry.clearcoatNormal, geometry.viewDir) );
geometry.clearCoatDotNV = saturate( dot(geometry.clearCoatNormal, geometry.viewDir) );
#endif

}
@@ -104,16 +104,16 @@ void initMaterial(out Material material, const in Geometry geometry){
material.roughness = max(material.roughness, getAARoughnessFactor(geometry.normal));

#ifdef CLEARCOAT
material.clearcoat = u_clearcoat;
material.clearcoatRoughness = u_clearcoatRoughness;
material.clearCoat = u_clearCoat;
material.clearCoatRoughness = u_clearCoatRoughness;
#ifdef HAS_CLEARCOATTEXTURE
material.clearcoat *= texture2D( u_clearcoatTexture, v_uv ).r;
material.clearCoat *= texture2D( u_clearCoatTexture, v_uv ).r;
#endif
#ifdef HAS_CLEARCOATROUGHNESSTEXTURE
material.clearcoatRoughness *= texture2D( u_clearcoatRoughnessTexture, v_uv ).g;
material.clearCoatRoughness *= texture2D( u_clearCoatRoughnessTexture, v_uv ).g;
#endif
material.clearcoat = saturate( material.clearcoat );
material.clearcoatRoughness = max(material.clearcoatRoughness, getAARoughnessFactor(geometry.clearcoatNormal));
material.clearCoat = saturate( material.clearCoat );
material.clearCoatRoughness = max(material.clearCoatRoughness, getAARoughnessFactor(geometry.clearCoatNormal));
#endif

material.opacity = baseColor.a;
10 changes: 5 additions & 5 deletions packages/loader/src/gltf/extensions/KHR_materials_clearcoat.ts
Original file line number Diff line number Diff line change
@@ -17,19 +17,19 @@ class KHR_materials_clearcoat extends ExtensionParser {
clearcoatNormalTexture
} = schema;

material.clearcoat = clearcoatFactor;
material.clearcoatRoughness = clearcoatRoughnessFactor;
material.clearCoat = clearcoatFactor;
material.clearCoatRoughness = clearcoatRoughnessFactor;

if (clearcoatTexture) {
material.clearcoatTexture = textures[clearcoatTexture.index];
material.clearCoatTexture = textures[clearcoatTexture.index];
MaterialParser._parseTextureTransform(material, clearcoatTexture.extensions, context);
}
if (clearcoatRoughnessTexture) {
material.clearcoatRoughnessTexture = textures[clearcoatRoughnessTexture.index];
material.clearCoatRoughnessTexture = textures[clearcoatRoughnessTexture.index];
MaterialParser._parseTextureTransform(material, clearcoatRoughnessTexture.extensions, context);
}
if (clearcoatNormalTexture) {
material.clearcoatNormalTexture = textures[clearcoatNormalTexture.index];
material.clearCoatNormalTexture = textures[clearcoatNormalTexture.index];
MaterialParser._parseTextureTransform(material, clearcoatNormalTexture.extensions, context);
}
}