Files
Seele/res/shaders/lib/BRDF.slang
T

222 lines
4.9 KiB
Plaintext
Raw Normal View History

2020-06-02 11:46:18 +02:00
import Common;
interface IBRDF
{
2024-07-15 17:55:22 +02:00
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor);
float3 evaluateAmbient();
2024-07-16 16:44:56 +02:00
float getAlpha();
2024-07-17 14:34:00 +02:00
float3 getTangentNormal();
};
struct Phong : IBRDF
{
float3 baseColor;
2024-07-16 16:44:56 +02:00
float alpha;
float3 specular;
float3 normal;
float3 ambient;
float shininess;
__init()
{
2024-07-16 16:44:56 +02:00
alpha = 1;
normal = float3(0, 0, 1);
}
2024-07-15 17:55:22 +02:00
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
{
2024-05-15 15:27:13 +02:00
float3 normal_TS = normal;
2024-07-15 17:55:22 +02:00
float3 nDotL = dot(normal_TS, lightDir_TS);
float3 r = 2 * (nDotL) * normal_TS - lightDir_TS;
float rDotV = dot(r, viewDir_TS);
2024-05-15 15:27:13 +02:00
return lightColor * (baseColor * max(nDotL, 0.0));// + specular * pow(max(rDotV, 0.0), max(shininess, 1)));
}
float3 evaluateAmbient()
{
return ambient;
}
2024-07-16 16:44:56 +02:00
float getAlpha()
{
return alpha;
}
2024-07-17 14:34:00 +02:00
float3 getTangentNormal()
{
return normal;
}
2020-06-02 11:46:18 +02:00
};
struct BlinnPhong : IBRDF
{
float3 baseColor;
2024-07-16 16:44:56 +02:00
float alpha;
float3 specularColor;
2023-07-13 21:01:20 +02:00
float3 normal;
2024-05-06 18:36:16 +02:00
float shininess;
float3 ambient;
2023-07-13 21:01:20 +02:00
__init()
{
2024-07-16 16:44:56 +02:00
alpha = 1;
2023-07-13 21:01:20 +02:00
normal = float3(0, 0, 1);
}
2020-06-02 11:46:18 +02:00
2024-07-15 17:55:22 +02:00
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
2020-06-02 11:46:18 +02:00
{
2023-12-03 23:12:20 +01:00
float3 normal_TS = normalize(normal);
2024-07-15 17:55:22 +02:00
float diffuse = max(dot(normal_TS, lightDir_TS), 0);
float3 h = normalize(lightDir_TS + viewDir_TS);
2024-05-06 18:36:16 +02:00
float specular = pow(saturate(dot(normal_TS, h)), shininess);
2020-06-02 11:46:18 +02:00
2024-05-06 18:36:16 +02:00
return (baseColor * diffuse * lightColor) + (specularColor * specular);
}
float3 evaluateAmbient()
{
return ambient;
2020-06-02 11:46:18 +02:00
}
2024-07-16 16:44:56 +02:00
float getAlpha()
{
return alpha;
}
2024-07-17 14:34:00 +02:00
float3 getTangentNormal()
{
return normal;
}
2020-06-02 11:46:18 +02:00
};
2024-01-02 16:48:03 +01:00
struct CelShading : IBRDF
2020-06-02 11:46:18 +02:00
{
float3 baseColor;
2024-07-16 16:44:56 +02:00
float alpha;
2024-01-02 16:48:03 +01:00
float3 normal;
__init()
{
2024-07-16 16:44:56 +02:00
alpha = 1;
2024-01-02 16:48:03 +01:00
normal = float3(0, 0, 1);
}
2020-06-02 11:46:18 +02:00
2024-07-15 17:55:22 +02:00
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
2020-06-02 11:46:18 +02:00
{
2024-01-02 16:48:03 +01:00
float3 normal_TS = normalize(normal);
2024-07-15 17:55:22 +02:00
float nDotL = dot(normal_TS, lightDir_TS);
2024-01-02 16:48:03 +01:00
float diffuse = max(nDotL, 0);
float3 darkenedBase = baseColor * 0.8;
if(diffuse > 0.5)
{
return baseColor * lightColor;
}
else
{
return darkenedBase * lightColor;
}
2020-06-02 11:46:18 +02:00
}
float3 evaluateAmbient()
{
return float3(0, 0, 0);
}
2024-07-16 16:44:56 +02:00
float getAlpha()
{
return alpha;
}
2024-07-17 14:34:00 +02:00
float3 getTangentNormal()
{
return normal;
}
};
// https://learnopengl.com/PBR/Theory
struct CookTorrance : IBRDF
{
float3 baseColor;
2024-07-16 16:44:56 +02:00
float alpha;
float3 normal;
float roughness;
float metallic;
float ambientOcclusion;
__init()
{
2024-07-16 16:44:56 +02:00
alpha = 1;
normal = float3(0, 0, 1);
roughness = 0;
metallic = 0;
ambientOcclusion = 1;
}
float TrowbridgeReitzGGX(float3 normal, float3 halfway)
{
float a_sqr = roughness * roughness;
float nDotH = max(dot(normal, halfway), 0.0);
float nDotH_sqr = nDotH * nDotH;
float denom = (nDotH_sqr * (a_sqr - 1.0) + 1.0);
return a_sqr / (PI * denom * denom);
}
float SchlickGGX(float nDotV, float k)
{
return nDotV / (nDotV * (1.0 - k) + k);
}
float Smith(float3 normal, float3 view, float3 light)
{
float k = (roughness + 1);
k = (k * k) / 8;
float nDotV = max(dot(normal, view), 0.0);
float nDotL = max(dot(normal, light), 0.0);
float ggx1 = SchlickGGX(nDotV, k);
float ggx2 = SchlickGGX(nDotL, k);
return ggx1 * ggx2;
}
float3 FresnelSchlick(float cosTheta, float3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
2024-07-15 17:55:22 +02:00
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
{
2024-07-15 17:55:22 +02:00
float3 n = normal;//normalize(mul(tbn, normal));
float3 h = normalize(lightDir_TS + viewDir_TS);
float3 F0 = float3(0.04);
F0 = lerp(F0, baseColor, metallic);
2024-07-15 17:55:22 +02:00
float3 F = FresnelSchlick(max(dot(h, viewDir_TS), 0.0), F0);
float NDF = TrowbridgeReitzGGX(n, h);
2024-07-15 17:55:22 +02:00
float G = Smith(n, viewDir_TS, lightDir_TS);
float3 num = NDF * G * F;
2024-07-15 17:55:22 +02:00
float denom = 4.0 * max(dot(n, viewDir_TS), 0.0) * max(dot(n, lightDir_TS), 0.0) + 0.000001;
float3 specular = num / denom;
float3 k_s = F;
float3 k_d = float3(1.0) - k_s;
k_d *= 1.0 - metallic;
2024-07-15 17:55:22 +02:00
float nDotL = max(dot(n, lightDir_TS), 0.0);
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
2024-05-04 13:25:19 +02:00
return result * ambientOcclusion;
}
float3 evaluateAmbient()
{
return float3(0.03) * baseColor * ambientOcclusion;
}
2024-07-16 16:44:56 +02:00
float getAlpha()
{
return alpha;
}
2024-07-17 14:34:00 +02:00
float3 getTangentNormal()
{
return normal;
}
2020-06-02 11:46:18 +02:00
};