Add directional light to enhance contrast in models

This commit is contained in:
Magnus Norddahl 2020-03-18 06:22:57 +01:00
parent d32e0b91c2
commit a84905e28d
3 changed files with 66 additions and 0 deletions

View file

@ -35,11 +35,24 @@ vec3 lightContribution(int i, vec3 normal)
}
}
float lightLevelContrastAttenuation(vec3 normal)
{
vec3 lightdir = vec3(0.55708601453, -0.7427813527, 0.37139067635);
return clamp(dot(lightdir, normal), 0.0, 1.0);
}
vec3 ProcessMaterialLight(Material material, vec3 color)
{
vec4 dynlight = uDynLightColor;
vec3 normal = material.Normal;
if (normal != vec3(0.0))
{
float lightLevelContrastStrength = 0.25;
dynlight.rgb += color * lightLevelContrastAttenuation(normal) * lightLevelContrastStrength;
color *= 1.0 - lightLevelContrastStrength;
}
if (uLightIndex >= 0)
{
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);

View file

@ -80,6 +80,36 @@ vec3 ProcessMaterialLight(Material material, vec3 ambientLight)
vec3 Lo = uDynLightColor.rgb;
if (N != vec3(0.0))
{
float lightLevelContrastStrength = 0.25;
vec3 L = vec3(0.55708601453, -0.7427813527, 0.37139067635);
float attenuation = clamp(dot(N, L), 0.0, 1.0);
if (attenuation > 0.0)
{
vec3 H = normalize(V + L);
vec3 radiance = ambientLight.rgb * attenuation * lightLevelContrastStrength;
// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
vec3 kS = F;
vec3 kD = (vec3(1.0) - kS) * (1.0 - metallic);
vec3 nominator = NDF * G * F;
float denominator = 4.0 * clamp(dot(N, V), 0.0, 1.0) * clamp(dot(N, L), 0.0, 1.0);
vec3 specular = nominator / max(denominator, 0.001);
Lo += (kD * albedo / PI + specular) * radiance;
}
ambientLight *= 1.0 - lightLevelContrastStrength;
}
if (uLightIndex >= 0)
{
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);

View file

@ -34,6 +34,20 @@ vec2 lightAttenuation(int i, vec3 normal, vec3 viewdir, float lightcolorA)
return vec2(attenuation, attenuation * specularLevel * pow(specAngle, phExp));
}
vec2 lightLevelContrastAttenuation(vec3 normal, vec3 viewdir)
{
vec3 lightdir = vec3(0.55708601453, -0.7427813527, 0.37139067635);
float attenuation = clamp(dot(lightdir, normal), 0.0, 1.0);
float glossiness = uSpecularMaterial.x;
float specularLevel = uSpecularMaterial.y;
vec3 halfdir = normalize(viewdir + lightdir);
float specAngle = clamp(dot(halfdir, normal), 0.0f, 1.0f);
float phExp = glossiness * 4.0f;
return vec2(attenuation, attenuation * specularLevel * pow(specAngle, phExp));
}
vec3 ProcessMaterialLight(Material material, vec3 color)
{
vec4 dynlight = uDynLightColor;
@ -42,6 +56,15 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
vec3 normal = material.Normal;
vec3 viewdir = normalize(uCameraPos.xyz - pixelpos.xyz);
if (normal != vec3(0.0))
{
float lightLevelContrastStrength = 0.25;
vec2 lightLevelAttenuation = lightLevelContrastAttenuation(normal, viewdir) * lightLevelContrastStrength;
dynlight.rgb += color * lightLevelAttenuation.x;
specular.rgb += color * lightLevelAttenuation.y;
color *= 1.0 - lightLevelContrastStrength;
}
if (uLightIndex >= 0)
{
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);