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 new shading model to Filament for specularGlossiness. #1083

Merged
merged 2 commits into from
Apr 9, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public enum Shading {
UNLIT, // no lighting applied, emissive possible
LIT, // default, standard lighting
SUBSURFACE, // subsurface lighting model
CLOTH // cloth lighting model
CLOTH, // cloth lighting model
SPECULAR_GLOSSINESS // legacy lighting model
}

public enum Interpolation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public enum Shading {
UNLIT,
LIT,
SUBSURFACE,
CLOTH
CLOTH,
SPECULAR_GLOSSINESS
}

public enum Interpolation {
Expand Down
40 changes: 37 additions & 3 deletions docs/Materials.md.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- Subsurface
- Cloth
- Unlit
- Specular glossiness (legacy)

## Lit model

Expand Down Expand Up @@ -522,6 +523,36 @@

![Figure [materialUnlit]: The unlit model is used to render debug information](images/screenshot_unlit.jpg)

## Specular glossiness

This alternative lighting model exists to comply with legacy standards.

Similar to cloth, it encompasses all the parameters previously defined for the standard
lit mode except for _metallic_ and _reflectance_, and adds a _sheenColor_ parameter. The sheen
color is used to formulate the BRDF inputs as follows.

```glsl
pixel.diffuseColor = baseColor * (1.0 - max(sheen.r, sheen.g, sheen.b));
pixel.f0 = sheen;
```

This is not a physically-based formulation, so we do not recommend using the `specularGlossiness`
model except when loading legacy assets.

Parameter | Definition
---------------------:|:---------------------
**baseColor** | Surface diffuse color
**sheenColor** | Specular tint (defaults to $\sqrt{baseColor}$)
[Table [glossinessProperties]: Properties of the specular-glossiness shading model]

The type and range of each property is described in table [glossinessPropertiesTypes].

Property | Type | Range | Note
---------------------:|:--------:|:------------------------:|:-------------------------
**baseColor** | float4 | [0..1] | Pre-multiplied linear RGB
**sheenColor** | float3 | [0..1] | Linear RGB
[Table [glossinessPropertiesTypes]: Range and type of the specular-glossiness model's properties]

# Material definitions

A material definition is a text file that describes all the information required by a material:
Expand Down Expand Up @@ -656,7 +687,7 @@
: `string`

Value
: Any of `lit`, `subsurface`, `cloth`, `unlit`. Defaults to `lit`.
: Any of `lit`, `subsurface`, `cloth`, `unlit`, `specularGlossiness`. Defaults to `lit`.

Description
: Selects the material model as described in the Material models section.
Expand Down Expand Up @@ -1387,8 +1418,8 @@

// no other field is available with the unlit shading model
float roughness; // default: 1.0
float metallic; // default: 0.0, not available with cloth
float reflectance; // default: 0.5, not available with cloth
float metallic; // default: 0.0, not available with cloth or specularGlossiness
float reflectance; // default: 0.5, not available with cloth or specularGlossiness
float ambientOcclusion; // default: 0.0

// not available when the shading model is subsurface or cloth
Expand All @@ -1407,6 +1438,9 @@
float3 sheenColor; // default: sqrt(baseColor)
float3 subsurfaceColor; // default: float3(0.0)

// only available when the shading model is specularGlossiness
float3 sheenColor; // default: sqrt(baseColor)

// not available when the shading model is unlit
// must be set before calling prepareMaterial()
float3 normal; // default: float3(0.0, 0.0, 1.0)
Expand Down
1 change: 1 addition & 0 deletions libs/filabridge/include/filament/MaterialEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum class Shading : uint8_t {
LIT, //!< default, standard lighting
SUBSURFACE, //!< subsurface lighting model
CLOTH, //!< cloth lighting model
SPECULAR_GLOSSINESS, //!< legacy lighting model
};

/**
Expand Down
2 changes: 1 addition & 1 deletion libs/filamat/include/filamat/MaterialBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {
THICKNESS, // float, subsurface shading model only
SUBSURFACE_POWER, // float, subsurface shading model only
SUBSURFACE_COLOR, // float3, subsurface and cloth shading models only
SHEEN_COLOR, // float3, cloth shading model only
SHEEN_COLOR, // float3, cloth and specular-glossiness shading models only
EMISSIVE, // float4, all shading models
NORMAL, // float3, all shading models only, except unlit
POST_LIGHTING_COLOR, // float4, all shading models
Expand Down
1 change: 1 addition & 0 deletions libs/filamat/src/shaders/CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ std::ostream& CodeGenerator::generateShaderLit(std::ostream& out, ShaderType typ
case Shading::UNLIT:
assert("Lit shader generated with unlit shading model");
break;
case Shading::SPECULAR_GLOSSINESS:
case Shading::LIT:
out << SHADERS_SHADING_MODEL_STANDARD_FS_DATA;
break;
Expand Down
9 changes: 5 additions & 4 deletions libs/filamat/src/shaders/ShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ namespace filamat {

static const char* getShadingDefine(filament::Shading shading) noexcept {
switch (shading) {
case filament::Shading::LIT: return "SHADING_MODEL_LIT";
case filament::Shading::UNLIT: return "SHADING_MODEL_UNLIT";
case filament::Shading::SUBSURFACE: return "SHADING_MODEL_SUBSURFACE";
case filament::Shading::CLOTH: return "SHADING_MODEL_CLOTH";
case filament::Shading::LIT: return "SHADING_MODEL_LIT";
case filament::Shading::UNLIT: return "SHADING_MODEL_UNLIT";
case filament::Shading::SUBSURFACE: return "SHADING_MODEL_SUBSURFACE";
case filament::Shading::CLOTH: return "SHADING_MODEL_CLOTH";
case filament::Shading::SPECULAR_GLOSSINESS: return "SHADING_MODEL_SPECULAR_GLOSSINESS";
}
}

Expand Down
2 changes: 1 addition & 1 deletion shaders/src/common_lighting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct PixelParams {
float subsurfacePower;
#endif

#if defined(SHADING_MODEL_CLOTH) && defined(MATERIAL_HAS_SUBSURFACE_COLOR)
#if (defined(SHADING_MODEL_CLOTH) || defined(SHADING_MODEL_SPECULAR_GLOSSINESS)) && defined(MATERIAL_HAS_SUBSURFACE_COLOR)
vec3 subsurfaceColor;
#endif
};
8 changes: 4 additions & 4 deletions shaders/src/common_material.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct MaterialInputs {
vec4 baseColor;
#if !defined(SHADING_MODEL_UNLIT)
float roughness;
#if !defined(SHADING_MODEL_CLOTH)
#if !defined(SHADING_MODEL_CLOTH) && !defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
float metallic;
float reflectance;
#endif
Expand All @@ -81,7 +81,7 @@ struct MaterialInputs {
vec3 subsurfaceColor;
#endif

#if defined(SHADING_MODEL_CLOTH)
#if defined(SHADING_MODEL_CLOTH) || defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
vec3 sheenColor;
#if defined(MATERIAL_HAS_SUBSURFACE_COLOR)
vec3 subsurfaceColor;
Expand All @@ -104,7 +104,7 @@ void initMaterial(out MaterialInputs material) {
material.baseColor = vec4(1.0);
#if !defined(SHADING_MODEL_UNLIT)
material.roughness = 1.0;
#if !defined(SHADING_MODEL_CLOTH)
#if !defined(SHADING_MODEL_CLOTH) && !defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
material.metallic = 0.0;
material.reflectance = 0.5;
#endif
Expand All @@ -128,7 +128,7 @@ void initMaterial(out MaterialInputs material) {
material.subsurfaceColor = vec3(1.0);
#endif

#if defined(SHADING_MODEL_CLOTH)
#if defined(SHADING_MODEL_CLOTH) || defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
material.sheenColor = sqrt(material.baseColor.rgb);
#if defined(MATERIAL_HAS_SUBSURFACE_COLOR)
material.subsurfaceColor = vec3(0.0);
Expand Down
8 changes: 7 additions & 1 deletion shaders/src/shading_lit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ void getPixelParams(const MaterialInputs material, out PixelParams pixel) {
baseColor.rgb /= max(baseColor.a, FLT_EPS);
#endif

#if !defined(SHADING_MODEL_CLOTH)
#if defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
// This is from KHR_materials_pbrSpecularGlossiness.
vec3 specular = material.sheenColor;
float maxSpecularComponent = max(max(specular.r, specular.g), specular.b);
pixel.diffuseColor = baseColor.rgb * (1.0 - maxSpecularComponent);
pixel.f0 = specular;
#elif !defined(SHADING_MODEL_CLOTH)
float metallic = material.metallic;
float reflectance = material.reflectance;

Expand Down
1 change: 1 addition & 0 deletions tools/matc/src/matc/ParametersProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ ParametersProcessor::ParametersProcessor() {
mStringToShading["lit"] = MaterialBuilder::Shading::LIT;
mStringToShading["subsurface"] = MaterialBuilder::Shading::SUBSURFACE;
mStringToShading["unlit"] = MaterialBuilder::Shading::UNLIT;
mStringToShading["specularGlossiness"] = MaterialBuilder::Shading::SPECULAR_GLOSSINESS;

mStringToVariant["directionalLighting"] = filament::Variant::DIRECTIONAL_LIGHTING;
mStringToVariant["dynamicLighting"] = filament::Variant::DYNAMIC_LIGHTING;
Expand Down
1 change: 1 addition & 0 deletions tools/matinfo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ const char* toString(Shading shadingModel) {
case Shading::LIT: return "lit";
case Shading::SUBSURFACE: return "subsurface";
case Shading::CLOTH: return "cloth";
case Shading::SPECULAR_GLOSSINESS: return "specularGlossiness";
}
}

Expand Down