Begin work on Disney BRDF (Diffuse, Specular, Ambient)

This commit is contained in:
Jack Christensen 2024-08-18 22:25:11 -04:00
parent 514b05120a
commit dcc610b245
2 changed files with 76 additions and 8 deletions

View File

@ -9,14 +9,82 @@ uniform sampler2D albedoGbuffer;
uniform sampler2D positionGbuffer;
uniform sampler2D normalGbuffer;
layout(std140, binding = 0) uniform SceneUniforms
{
mat4 PV; //camera projection * view matrix
vec4 eye_w; //world-space eye position
};
vec3 calculateDisneySpecular(vec3 N, vec3 V, vec3 L, vec3 albedo) {
vec3 H = normalize(V + L);
// Fresnel - Schlick's Approximation
vec3 F0 = vec3(0.04f);
vec3 F = F0 + (1.0f - F0) * pow(1.0f - dot(H, V), 5.0f);
// Normal Distribution Function (GGX)
float roughness = 0.6f;
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
float NdotH = max(dot(N, H), 0.0f);
float D = alpha2 / (3.14159265359 * pow(NdotH * NdotH * (alpha2 - 1.0f) + 1.0f, 2.0f));
// Geometry Function (Smith's G)
float NdotV = max(dot(N, V), 0.0f);
float NdotL = max(dot(N, L), 0.0f);
float k = (roughness + 1.0f) * (roughness + 1.0f) / 8.0f;
float G1V = NdotV / (NdotV * (1.0f - k) + k);
float G1L = NdotL / (NdotL * (1.0f - k) + k);
float G = G1V * G1L;
//vec3 specular = vec3(F);
vec3 specular = (D * G * F) / (4.0f * NdotL * NdotV + 0.001); // Adding a small bias to prevent division by zero
return specular;
}
void main()
{
vec3 hdr_color = texture(albedoGbuffer, tex_coords).rgb;
vec3 tone_mapped_color = hdr_color / (hdr_color + vec3(1.0));
tone_mapped_color = pow(tone_mapped_color, vec3(1.0/2.2));
vec4 albedo = texture(albedoGbuffer, tex_coords);
//albedo = vec4(vec3(0.18f), texture(albedoGbuffer, tex_coords).a);
vec4 position = texture(positionGbuffer, tex_coords);
vec4 normal = texture(normalGbuffer, tex_coords);
vec3 position = texture(positionGbuffer, tex_coords).rgb;
vec3 normal = texture(normalGbuffer, tex_coords).rgb;
vec4 lightDirection = vec4(0.707f, 0.707f, 0.0f, 0.0f);
frag_color = vec4(normal, 1.0);
vec3 N = normalize(normal.xyz);
vec3 L = normalize(lightDirection.xyz);
vec3 V = normalize(eye_w.xyz);
float NdotL = max(dot(N, L), 0.0f);
vec4 skyColor = vec4(0.6f, 0.7f, 0.8f, 0.0f); // Light blue sky
vec4 groundColor = vec4(0.2f, 0.2f, 0.2f, 0.0f); // Dark gray ground
float factor = 0.5f * (N.y + 1.0f); // Blend factor based on normal's Y component
vec4 ambientLight = mix(groundColor, skyColor, factor);
// Disney Diffuse Fresnel
vec3 F0 = vec3(0.04f);
vec3 fresnel = F0 + (1.0f - F0) * (pow(1.0f - NdotL, 5.0f));
// Disney Diffuse Term
vec4 diffuse = vec4(vec3((albedo.rgb / 3.14159265359f) * NdotL * fresnel), albedo.a);
// Disney Specular Term
vec4 specular = vec4(calculateDisneySpecular(N, V, L, albedo.rgb), albedo.a);
// Light Intensity
vec4 lightColor = vec4(vec3(50.0f), 1.0f);
vec4 finalDiffuse = (diffuse + specular) * lightColor + (ambientLight * 0.1f);
vec4 hdr_color = finalDiffuse;
vec4 tone_mapped_color = hdr_color / (hdr_color + vec4(vec3(1.0f), 0.0f));
tone_mapped_color = pow(tone_mapped_color, vec4(vec3(1.0f/2.2f), 1.0f));
frag_color = tone_mapped_color;
}

View File

@ -5,7 +5,7 @@ layout(quads, fractional_even_spacing, cw) in;
layout(binding = 0) uniform sampler2D heightTexture;
layout(location = 0) uniform mat4 M;
uniform float displacementScale = 0.1f;
uniform float displacementScale = 0.025f;
uniform float tessellationFactor;
layout(std140, binding = 0) uniform SceneUniforms
@ -30,7 +30,7 @@ float getHeight(vec2 uv) {
}
vec3 calculateSmoothNormal(vec3 pos, vec2 coord) {
float step = 1.0f / (tessellationFactor + 1.0f);
float step = 1.0f / (16.0f * tessellationFactor + 1.0f);
float hLeft = getHeight(coord + vec2(-step, 0.0f));
float hRight = getHeight(coord + vec2(step, 0.0f));