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

pbr sheen #312

Merged
merged 41 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
be59c8f
feat: support iridescence in ShaderLab PBR
hhhhkrx Nov 6, 2024
cac0c74
Merge branch 'dev/1.4' of github.com:galacean/engine-toolkit into GLT…
hhhhkrx Nov 6, 2024
3984cee
Merge branch 'dev/1.4' of github.com:galacean/engine-toolkit into GLT…
hhhhkrx Nov 6, 2024
2f0e91a
fix: merge
hhhhkrx Nov 6, 2024
e15d3e8
fix: iridescnence shader
hhhhkrx Nov 11, 2024
dd0e023
fix: delate sheen
hhhhkrx Nov 11, 2024
329e0f8
fix: Iridescnence name
hhhhkrx Nov 11, 2024
451b976
fix: pow2
hhhhkrx Nov 14, 2024
6d89218
fix: iridescence name
hhhhkrx Nov 19, 2024
6c6e710
fix: iridesceceIOR name
hhhhkrx Nov 19, 2024
128094c
fix: topIOR name
hhhhkrx Nov 19, 2024
8efd617
fix: ior name
hhhhkrx Nov 19, 2024
59b4ca5
fix: name of hump
hhhhkrx Nov 19, 2024
09a5ebb
fix: name
hhhhkrx Nov 19, 2024
3c0a0d6
fix: name
hhhhkrx Nov 19, 2024
ba66708
fix: iridescence
hhhhkrx Nov 19, 2024
adb8bde
fix: iridescence
hhhhkrx Nov 19, 2024
1021e27
Merge branch 'dev/1.4' of github.com:galacean/engine-toolkit into dev…
hhhhkrx Nov 20, 2024
eb32da3
Merge branch 'dev/1.4' of github.com:galacean/engine-toolkit into dev…
hhhhkrx Nov 20, 2024
99a6d8e
feat: shaderlab PBR support sheen
hhhhkrx Dec 3, 2024
f550f37
fix: sheen layer
hhhhkrx Dec 10, 2024
1e76033
fix: shader name
hhhhkrx Dec 11, 2024
58131a7
fix: sheen
hhhhkrx Dec 11, 2024
727c199
fix: sheen scaling name
hhhhkrx Dec 11, 2024
b9874f2
fix: sheen
hhhhkrx Dec 11, 2024
a794dc6
fix: sheen
hhhhkrx Dec 11, 2024
65d1bee
fix: sheen lobe name
hhhhkrx Dec 11, 2024
f356d82
fix: sheen
hhhhkrx Dec 12, 2024
8650e81
fix: indirect brdf
hhhhkrx Dec 12, 2024
17ccc8e
fix: sheen
hhhhkrx Dec 12, 2024
c7d37fe
fix: sheen
hhhhkrx Dec 12, 2024
27cedec
fix: sheenDFG name
hhhhkrx Dec 12, 2024
6099ed5
fix: sheen
hhhhkrx Dec 12, 2024
be36842
fix: sheen
hhhhkrx Dec 12, 2024
a69698e
fix: sheen dfg
hhhhkrx Dec 12, 2024
7b8f3d0
fix: sheen
hhhhkrx Dec 12, 2024
87444e9
fix: lut name
hhhhkrx Dec 12, 2024
731eeaf
fix: sheen
hhhhkrx Dec 12, 2024
c9c8640
fix: sheen ao
hhhhkrx Dec 12, 2024
ed241f2
fix: sheen texture lod
hhhhkrx Dec 12, 2024
1da200f
fix: sheen 0.0
hhhhkrx Dec 12, 2024
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
@@ -1,7 +1,7 @@
#define FUNCTION_SPECULAR_IBL evaluateSpecularIBLIridescence
#include "BRDF.glsl"
#include "./IridescenceFunction.glsl"
#include "LightProbe.glsl"
#include "LightIndirectFunctions.glsl"

void evaluateSpecularIBLIridescence(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, float radianceAttenuation, inout vec3 specularColor){

Expand Down
31 changes: 9 additions & 22 deletions packages/shaderlab/src/shaders/PBR.gs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,20 @@ Shader "PBR.gs" {
material_IridescenceTexture("IridescenceTexture", Texture2D);
}

Header("Sheen"){
UI_SheenIntensity("Intensity", Range(0, 1, 0.01)) = 0;
UI_SheenColor("Color", Color ) = (0, 0, 0, 0);
material_SheenRoughness("Roughness", Range(0, 1, 0.01)) = 0;
material_SheenTexture("ColorTexture", Texture2D);
material_SheenRoughnessTexture("RoughnessTexture", Texture2D);
}

Header("Common") {
material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0;
material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0);
}
}

EditorMacros {
Header("Conditional Macors") {
MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE");
MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE");
MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY");
MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE")
MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE");
MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE");
MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE");
MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT");
MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE");
MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE");
MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE");
MATERIAL_ENABLE_IRIDESCENCE("ENABLE_IRIDESCENCE");
MATERIAL_HAS_IRIDESCENCE_THICKNESS_TEXTURE("HAS_IRIDESCENCE_THICKNESS_TEXTURE");
MATERIAL_HAS_IRIDESCENCE_TEXTURE("HAS_IRIDESCENCE_TEXTURE");
MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT");
MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF");
}
}


SubShader "Default" {
UsePass "pbr/Default/ShadowCaster"

Expand Down
45 changes: 45 additions & 0 deletions packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ struct SurfaceData{
float iridescenceThickness;
#endif

#ifdef MATERIAL_ENABLE_SHEEN
float sheenRoughness;
vec3 sheenColor;
#endif

};


Expand All @@ -72,8 +77,15 @@ struct BRDFData{
#ifdef MATERIAL_ENABLE_IRIDESCENCE
vec3 iridescenceSpecularColor;
#endif

#ifdef MATERIAL_ENABLE_SHEEN
float sheenRoughness;
float sheenEnergy;
#endif

};

#include "LightIndirectFunctions.glsl"

float getAARoughnessFactor(vec3 normal) {
// Kaplanyan 2016, "Stable specular highlights"
Expand Down Expand Up @@ -305,6 +317,31 @@ vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor) {
}
#endif

#ifdef MATERIAL_ENABLE_SHEEN
// http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
float D_Charlie(float roughness, float dotNH) {
float invAlpha = 1.0 / roughness;
float cos2h = dotNH * dotNH;
float sin2h = max(1.0 - cos2h, 0.0078125); // 2^(-14/2), so sin2h^2 > 0 in fp16
return (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);
}

// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886".
float V_Neubelt(float NoV, float NoL) {
return saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));
}

vec3 sheenBRDF(vec3 incidentDirection, SurfaceData surfaceData, vec3 sheenColor, float sheenRoughness) {
vec3 halfDir = normalize(incidentDirection + surfaceData.viewDir);
float dotNL = saturate(dot(surfaceData.normal, incidentDirection));
float dotNH = saturate(dot(surfaceData.normal, halfDir));
float D = D_Charlie(sheenRoughness, dotNH);
float V = V_Neubelt(surfaceData.dotNV, dotNL);
vec3 F = sheenColor;
return D * V * F;
}
#endif

void initBRDFData(SurfaceData surfaceData, out BRDFData brdfData){
vec3 albedoColor = surfaceData.albedoColor;
vec3 specularColor = surfaceData.specularColor;
Expand Down Expand Up @@ -332,6 +369,14 @@ void initBRDFData(SurfaceData surfaceData, out BRDFData brdfData){
float topIOR = 1.0;
brdfData.iridescenceSpecularColor = evalIridescenceSpecular(topIOR, surfaceData.dotNV, surfaceData.iridesceceIOR, brdfData.specularColor, surfaceData.iridescenceThickness);
#endif

#ifdef MATERIAL_ENABLE_SHEEN
brdfData.sheenRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(surfaceData.sheenRoughness + getAARoughnessFactor(surfaceData.normal), 1.0));
// sheen energy compensation approximation calculation in ‘Sheen DFG LUT integrated over diffuse IBL’
// https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
float IBLSheenDFG = IBLSheenDFG(surfaceData, brdfData.sheenRoughness);
brdfData.sheenScaling = 1.0 - IBLSheenDFG * max(max(surfaceData.sheenColor.r, surfaceData.sheenColor.g), surfaceData.sheenColor.b);
#endif
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
}

#endif
29 changes: 29 additions & 0 deletions packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ float material_OcclusionTextureCoord;
#endif
#endif

#ifdef MATERIAL_ENABLE_SHEEN
float material_SheenRoughness;
vec3 material_SheenColor;
#ifdef MATERIAL_HAS_SHEEN_TEXTURE
sampler2D material_SheenTexture;
#endif

#ifdef MATERIAL_HAS_SHEEN_ROUGHNESS_TEXTURE
sampler2D material_SheenRoughnessTexture;
#endif
#endif

// Texture
#ifdef MATERIAL_HAS_BASETEXTURE
sampler2D material_BaseTexture;
Expand Down Expand Up @@ -263,6 +275,23 @@ SurfaceData getSurfaceData(Varyings v, vec2 aoUV, bool isFrontFacing){
#endif
#endif

#ifdef MATERIAL_ENABLE_SHEEN
vec3 sheenColor = material_SheenColor;
#ifdef MATERIAL_HAS_SHEEN_TEXTURE
vec4 sheenTextureColor = texture2D(material_SheenTexture, uv);
#ifndef ENGINE_IS_COLORSPACE_GAMMA
sheenTextureColor = gammaToLinear(sheenTextureColor);
#endif
sheenColor *= sheenTextureColor.rgb;
#endif
surfaceData.sheenColor = sheenColor;

surfaceData.sheenRoughness = material_SheenRoughness;
#ifdef MATERIAL_HAS_SHEEN_ROUGHNESS_TEXTURE
surfaceData.sheenRoughness *= texture2D(material_SheenRoughnessTexture, uv).a;
#endif
#endif

// AO
float diffuseAO = 1.0;
float specularAO = 1.0;
Expand Down
5 changes: 5 additions & 0 deletions packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#ifndef FUNCTION_CLEAR_COAT_LOBE
#define FUNCTION_CLEAR_COAT_LOBE clearCoatLobe
#endif
#ifndef FUNCTION_SHEEN_LOBE
#define FUNCTION_SHEEN_LOBE SheenLobe
#endif

#include "BRDF.glsl"
#include "Light.glsl"
Expand All @@ -34,6 +37,8 @@ void surfaceShading(Varyings varyings, SurfaceData surfaceData, BRDFData brdfDat
FUNCTION_DIFFUSE_LOBE(varyings, surfaceData, brdfData, attenuationIrradiance, diffuseColor);
// Specular Lobe
FUNCTION_SPECULAR_LOBE(varyings, surfaceData, brdfData, incidentDirection, attenuationIrradiance, specularColor);
// Lobe
FUNCTION_SHEEN_LOBE(varyings, surfaceData, brdfData, incidentDirection, attenuationIrradiance, diffuseColor, specularColor);

color += diffuseColor + specularColor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef LIGHT_PROBE
#define LIGHT_PROBE

#ifndef LIGHT_INDIRECT_FUNCTIONS_INCLUDED
#define LIGHT_INDIRECT_FUNCTIONS_INCLUDED
#include "Light.glsl"
// ------------------------Specular------------------------
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved

// ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile
vec3 envBRDFApprox(vec3 specularColor, float roughness, float dotNV ) {

Expand Down Expand Up @@ -68,4 +67,16 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness
#endif
}

// This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from
// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
// in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
float IBLSheenDFG(SurfaceData surfaceData, float roughness) {
float dotNV = surfaceData.dotNV;
float r2 = roughness * roughness;
float a = roughness < 0.25 ? -339.2 * r2 + 161.4 * roughness - 25.9 : -8.48 * r2 + 14.3 * roughness - 9.95;
float b = roughness < 0.25 ? 44.0 * r2 - 23.7 * roughness + 3.26 : 1.97 * r2 - 3.27 * roughness + 0.72;
float DG = exp( a * dotNV + b ) + ( roughness < 0.25 ? 0.0 : 0.1 * ( roughness - 0.25 ) );
return saturate( DG * RECIPROCAL_PI );
}

#endif
21 changes: 18 additions & 3 deletions packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#ifndef FUNCTION_CLEAR_COAT_IBL
#define FUNCTION_CLEAR_COAT_IBL evaluateClearCoatIBL
#endif

#ifndef FUNCTION_SHEEN_IBL
#define FUNCTION_SHEEN_IBL evaluateSheenIBL
#endif
#include "BRDF.glsl"
#include "Light.glsl"
#include "LightProbe.glsl"
#include "LightIndirectFunctions.glsl"

// ------------------------Diffuse------------------------

Expand Down Expand Up @@ -77,6 +79,15 @@ void evaluateSpecularIBL(Varyings varyings, SurfaceData surfaceData, BRDFData br
outSpecularColor += surfaceData.specularAO * radianceAttenuation * radiance * envBRDFApprox(speculaColor, brdfData.roughness, surfaceData.dotNV);
}

void evaluateSheenIBL(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, float radianceAttenuation, inout vec3 diffuseColor, inout vec3 specularColor){
#ifdef MATERIAL_ENABLE_SHEEN
diffuseColor *= brdfData.sheenScaling;
specularColor *= brdfData.sheenScaling;

vec3 reflectance = radianceAttenuation * IBLSheenDFG(surfaceData, brdfData.sheenRoughness) * surfaceData.sheenColor;
specularColor += reflectance;
#endif
}

void evaluateIBL(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, inout vec3 color){
vec3 diffuseColor = vec3(0);
Expand All @@ -90,8 +101,12 @@ void evaluateIBL(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData,

// IBL specular
FUNCTION_SPECULAR_IBL(varyings, surfaceData, brdfData, radianceAttenuation, specularColor);


// IBL sheen
FUNCTION_SHEEN_IBL(varyings, surfaceData, brdfData, radianceAttenuation, diffuseColor, specularColor);

color += diffuseColor + specularColor;

}


Expand Down
13 changes: 11 additions & 2 deletions packages/shaderlab/src/shaders/shadingPBR/ReflectionLobe.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ void diffuseLobe(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData,
}

void specularLobe(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 attenuationIrradiance, inout vec3 specularColor){
specularColor += attenuationIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, brdfData, surfaceData.normal, brdfData.specularColor, brdfData.roughness);
specularColor += attenuationIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, brdfData, surfaceData.normal, brdfData.specularColor, brdfData.roughness);
}

void SheenLobe(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 attenuationIrradiance, inout vec3 diffuseColor, inout vec3 specularColor){
#ifdef MATERIAL_ENABLE_SHEEN
diffuseColor *= brdfData.sheenScaling;
specularColor *= brdfData.sheenScaling;
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved

specularColor += attenuationIrradiance * sheenBRDF(incidentDirection, surfaceData, surfaceData.sheenColor, brdfData.sheenRoughness);
#endif
}

float clearCoatLobe(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 color, inout vec3 specularColor){
Expand All @@ -16,7 +25,7 @@ float clearCoatLobe(Varyings varyings, SurfaceData surfaceData, BRDFData brdfDat
float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) );
vec3 clearCoatIrradiance = clearCoatDotNL * color;

specularColor += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, brdfData.clearCoatSpecularColor, brdfData.clearCoatRoughness );
specularColor += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, brdfData, surfaceData.clearCoatNormal, brdfData.clearCoatSpecularColor, brdfData.clearCoatRoughness );
attenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV);
#endif

Expand Down
4 changes: 2 additions & 2 deletions packages/shaderlab/src/shaders/shadingPBR/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AttributesPBR from "./AttributesPBR.glsl";
import BRDF from "./BRDF.glsl";
import LightProbe from "./LightProbe.glsl";
import LightIndirectFunctions from "./LightIndirectFunctions.glsl";
import ForwardPassPBR from "./ForwardPassPBR.glsl";
import FragmentPBR from "./FragmentPBR.glsl";
import LightDirectPBR from "./LightDirectPBR.glsl";
Expand All @@ -18,6 +18,6 @@ export default [
{ source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" },
{ source: VertexPBR, includeKey: "VertexPBR.glsl" },
{ source: BRDF, includeKey: "BRDF.glsl" },
{ source: LightProbe, includeKey: "LightProbe.glsl" },
{ source: LightIndirectFunctions, includeKey: "LightIndirectFunctions.glsl" },
{ source: ReflectionLobe, includeKey: "ReflectionLobe.glsl" }
];
Loading