|
|
|
@@ -18,11 +18,17 @@ uint HYDRA_LIGHT_SPOT = 3;
|
|
|
|
|
|
|
|
|
|
struct light_structure
|
|
|
|
|
{
|
|
|
|
|
vec4 light_colour;
|
|
|
|
|
float light_intensity;
|
|
|
|
|
uint chunk_id;
|
|
|
|
|
uint light_type;
|
|
|
|
|
float padding[1];
|
|
|
|
|
vec4 ambient;
|
|
|
|
|
vec4 diffuse;
|
|
|
|
|
vec4 specular;
|
|
|
|
|
float intensity;
|
|
|
|
|
float constant;
|
|
|
|
|
float linear;
|
|
|
|
|
float quadratic;
|
|
|
|
|
float falloff;
|
|
|
|
|
float falloff_smooth;
|
|
|
|
|
uint entity_id;
|
|
|
|
|
uint light_type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
layout(binding = 0) uniform uniform_per_frame
|
|
|
|
@@ -105,32 +111,20 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
|
|
|
|
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 DirectionalShading(vec3 normal, vec3 diffuse, float roughness, float metallic, float light_intensity, vec3 lightColour, vec3 lightDir, vec3 lightPos, vec3 worldPos, vec3 viewPos)
|
|
|
|
|
vec3 CalculatePBR(vec3 normal, vec3 view_pos, vec3 world_pos,vec3 light_dir, vec3 diffuse, vec3 radiance, float roughness, float metallic )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
vec3 N = normalize(normal);
|
|
|
|
|
vec3 V = normalize(viewPos - worldPos);
|
|
|
|
|
vec3 V = normalize(view_pos - world_pos);
|
|
|
|
|
|
|
|
|
|
// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
|
|
|
|
|
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
|
|
|
|
|
vec3 F0 = vec3(0.04);
|
|
|
|
|
F0 = mix(F0, diffuse, metallic);
|
|
|
|
|
|
|
|
|
|
// reflectance equation
|
|
|
|
|
vec3 Lo = vec3(0.0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// calculate per-light radiance
|
|
|
|
|
vec3 L = normalize(lightDir);
|
|
|
|
|
vec3 L = normalize(light_dir);
|
|
|
|
|
vec3 H = normalize(V + L);
|
|
|
|
|
float distance = length(lightPos - worldPos);
|
|
|
|
|
float attenuation = 1.0;
|
|
|
|
|
|
|
|
|
|
// if(light_intensity > 0)
|
|
|
|
|
// {
|
|
|
|
|
// attenuation = light_intensity / (distance * distance);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
vec3 radiance = lightColour * attenuation;
|
|
|
|
|
|
|
|
|
|
// Cook-Torrance BRDF
|
|
|
|
|
float NDF = DistributionGGX(N, H, roughness);
|
|
|
|
@@ -156,61 +150,133 @@ vec3 DirectionalShading(vec3 normal, vec3 diffuse, float roughness, float metall
|
|
|
|
|
float NdotL = max(dot(N, L), 0.0);
|
|
|
|
|
|
|
|
|
|
// add to outgoing radiance Lo
|
|
|
|
|
Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
|
|
|
|
|
vec3 Lo = (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
|
|
|
|
|
|
|
|
|
|
return Lo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 DirectionalShading(vec3 normal,
|
|
|
|
|
vec3 diffuse,
|
|
|
|
|
float roughness,
|
|
|
|
|
float metallic,
|
|
|
|
|
float light_intensity,
|
|
|
|
|
vec3 light_dir,
|
|
|
|
|
vec3 light_pos,
|
|
|
|
|
vec3 world_pos,
|
|
|
|
|
vec3 view_pos)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// vec3 N = normalize(normal);
|
|
|
|
|
// vec3 V = normalize(vieview_pos - world_pos);
|
|
|
|
|
|
|
|
|
|
// // calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
|
|
|
|
|
// // of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
|
|
|
|
|
// vec3 F0 = vec3(0.04);
|
|
|
|
|
// F0 = mix(F0, diffuse, metallic);
|
|
|
|
|
|
|
|
|
|
// // reflectance equation
|
|
|
|
|
// vec3 Lo = vec3(0.0);
|
|
|
|
|
|
|
|
|
|
// // calculate per-light radiance
|
|
|
|
|
// vec3 L = normalize(lightDir);
|
|
|
|
|
// vec3 H = normalize(V + L);
|
|
|
|
|
float distance = length(light_pos - world_pos);
|
|
|
|
|
float attenuation = 1.0;
|
|
|
|
|
|
|
|
|
|
vec3 radiance = diffuse * attenuation;
|
|
|
|
|
|
|
|
|
|
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
|
|
|
|
|
// // 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 numerator = NDF * G * F;
|
|
|
|
|
// float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
|
|
|
|
|
// vec3 specular = numerator / denominator;
|
|
|
|
|
|
|
|
|
|
// // kS is equal to Fresnel
|
|
|
|
|
// vec3 kS = F;
|
|
|
|
|
// // for energy conservation, the diffuse and specular light can't
|
|
|
|
|
// // be above 1.0 (unless the surface emits light); to preserve this
|
|
|
|
|
// // relationship the diffuse component (kD) should equal 1.0 - kS.
|
|
|
|
|
// vec3 kD = vec3(1.0) - kS;
|
|
|
|
|
// // multiply kD by the inverse metalness such that only non-metals
|
|
|
|
|
// // have diffuse lighting, or a linear blend if partly metal (pure metals
|
|
|
|
|
// // have no diffuse light).
|
|
|
|
|
// kD *= 1.0 - metallic;
|
|
|
|
|
|
|
|
|
|
// // scale light by NdotL
|
|
|
|
|
// float NdotL = max(dot(N, L), 0.0);
|
|
|
|
|
|
|
|
|
|
// // add to outgoing radiance Lo
|
|
|
|
|
// Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
|
|
|
|
|
|
|
|
|
|
return Lo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vec3 PointShading(vec3 normal, vec3 diffuse, float roughness, float metallic, float light_intensity, vec3 lightColour, vec3 lightDir, vec3 lightPos, vec3 worldPos, vec3 viewPos)
|
|
|
|
|
vec3 PointShading(vec3 normal,
|
|
|
|
|
vec3 diffuse,
|
|
|
|
|
float roughness,
|
|
|
|
|
float metallic,
|
|
|
|
|
float light_intensity,
|
|
|
|
|
vec3 light_pos,
|
|
|
|
|
float light_constant,
|
|
|
|
|
float light_linear,
|
|
|
|
|
float light_quadratic,
|
|
|
|
|
vec3 world_pos,
|
|
|
|
|
vec3 view_pos)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
vec3 N = normalize(normal);
|
|
|
|
|
vec3 V = normalize(viewPos - worldPos);
|
|
|
|
|
// vec3 N = normalize(normal);
|
|
|
|
|
// vec3 V = normalize(viewPos - worldPos);
|
|
|
|
|
|
|
|
|
|
// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
|
|
|
|
|
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
|
|
|
|
|
vec3 F0 = vec3(0.04);
|
|
|
|
|
F0 = mix(F0, diffuse, metallic);
|
|
|
|
|
// // calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
|
|
|
|
|
// // of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
|
|
|
|
|
// vec3 F0 = vec3(0.04);
|
|
|
|
|
// F0 = mix(F0, diffuse, metallic);
|
|
|
|
|
|
|
|
|
|
// reflectance equation
|
|
|
|
|
vec3 Lo = vec3(0.0);
|
|
|
|
|
// // reflectance equation
|
|
|
|
|
// vec3 Lo = vec3(0.0);
|
|
|
|
|
|
|
|
|
|
// calculate per-light radiance
|
|
|
|
|
vec3 L = normalize(lightPos - worldPos);
|
|
|
|
|
vec3 H = normalize(V + L);
|
|
|
|
|
float distance = length(lightPos - worldPos);
|
|
|
|
|
// // calculate per-light radiance
|
|
|
|
|
// vec3 L = normalize(lightPos - worldPos);
|
|
|
|
|
// vec3 H = normalize(V + L);
|
|
|
|
|
float distance = length(light_pos - world_pos);
|
|
|
|
|
float attenuation = 1.0;
|
|
|
|
|
|
|
|
|
|
attenuation = light_intensity / (distance * distance);
|
|
|
|
|
|
|
|
|
|
vec3 radiance = lightColour * attenuation;
|
|
|
|
|
//attenuation = light_intensity / (distance * distance);
|
|
|
|
|
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
|
|
|
|
|
vec3 radiance = diffuse * attenuation;
|
|
|
|
|
vec3 light_dir = normalize(light_pos - world_pos);
|
|
|
|
|
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
// // 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 numerator = NDF * G * F;
|
|
|
|
|
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
|
|
|
|
|
vec3 specular = numerator / denominator;
|
|
|
|
|
// vec3 numerator = NDF * G * F;
|
|
|
|
|
// float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
|
|
|
|
|
// vec3 specular = (numerator / denominator) * attenuation;
|
|
|
|
|
|
|
|
|
|
// kS is equal to Fresnel
|
|
|
|
|
vec3 kS = F;
|
|
|
|
|
// for energy conservation, the diffuse and specular light can't
|
|
|
|
|
// be above 1.0 (unless the surface emits light); to preserve this
|
|
|
|
|
// relationship the diffuse component (kD) should equal 1.0 - kS.
|
|
|
|
|
vec3 kD = vec3(1.0) - kS;
|
|
|
|
|
// multiply kD by the inverse metalness such that only non-metals
|
|
|
|
|
// have diffuse lighting, or a linear blend if partly metal (pure metals
|
|
|
|
|
// have no diffuse light).
|
|
|
|
|
kD *= 1.0 - metallic;
|
|
|
|
|
// // kS is equal to Fresnel
|
|
|
|
|
// vec3 kS = F;
|
|
|
|
|
// // for energy conservation, the diffuse and specular light can't
|
|
|
|
|
// // be above 1.0 (unless the surface emits light); to preserve this
|
|
|
|
|
// // relationship the diffuse component (kD) should equal 1.0 - kS.
|
|
|
|
|
// vec3 kD = vec3(1.0) - kS;
|
|
|
|
|
// // multiply kD by the inverse metalness such that only non-metals
|
|
|
|
|
// // have diffuse lighting, or a linear blend if partly metal (pure metals
|
|
|
|
|
// // have no diffuse light).
|
|
|
|
|
// kD *= 1.0 - metallic;
|
|
|
|
|
|
|
|
|
|
// scale light by NdotL
|
|
|
|
|
float NdotL = max(dot(N, L), 0.0);
|
|
|
|
|
// // scale light by NdotL
|
|
|
|
|
// float NdotL = max(dot(N, L), 0.0);
|
|
|
|
|
|
|
|
|
|
// add to outgoing radiance Lo
|
|
|
|
|
Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
|
|
|
|
|
// // add to outgoing radiance Lo
|
|
|
|
|
// Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
|
|
|
|
|
|
|
|
|
|
return Lo;
|
|
|
|
|
}
|
|
|
|
@@ -245,21 +311,31 @@ void main()
|
|
|
|
|
vec3 Lo = vec3(0,0,0);
|
|
|
|
|
for(int i = 0; i < ubo_per_frame.light_count; i ++)
|
|
|
|
|
{
|
|
|
|
|
mat4 model = model_matrix[lights[i].chunk_id];
|
|
|
|
|
mat4 model = model_matrix[lights[i].entity_id];
|
|
|
|
|
mat3 rot_mat = GetRotationOnlyMatrix(model);
|
|
|
|
|
vec3 light_direction = rot_mat * vec3(0,0,1);
|
|
|
|
|
|
|
|
|
|
vec3 light_pos = model[3].xyz;
|
|
|
|
|
if(lights[i].light_type == HYDRA_LIGHT_DIRECTIONAL)
|
|
|
|
|
{
|
|
|
|
|
Lo+= DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].light_intensity, lights[i].light_colour.xyz, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
|
|
|
|
|
Lo+= DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].intensity, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
|
|
|
|
|
}
|
|
|
|
|
else if(lights[i].light_type == HYDRA_LIGHT_POINT)
|
|
|
|
|
{
|
|
|
|
|
Lo+= PointShading(normal, diffuse.xyz, rough, metal, lights[i].light_intensity, lights[i].light_colour.xyz, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
|
|
|
|
|
|
|
|
|
|
Lo+= PointShading(normal,
|
|
|
|
|
diffuse.xyz,
|
|
|
|
|
rough,
|
|
|
|
|
metal,
|
|
|
|
|
lights[i].intensity,
|
|
|
|
|
light_pos,
|
|
|
|
|
lights[i].constant,
|
|
|
|
|
lights[i].linear,
|
|
|
|
|
lights[i].quadratic,
|
|
|
|
|
world_pos,
|
|
|
|
|
ubo_per_frame.viewer_pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//uFragColor = vec4(light_direction, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float lighting = 0;
|
|
|
|
@@ -271,7 +347,7 @@ void main()
|
|
|
|
|
float ao = 1;
|
|
|
|
|
vec3 ambient = vec3(0.05) * diffuse.xyz * ao;
|
|
|
|
|
|
|
|
|
|
vec3 output_color = ambient + Lo;
|
|
|
|
|
vec3 output_color = Lo;
|
|
|
|
|
|
|
|
|
|
// HDR tonemapping
|
|
|
|
|
output_color = output_color / (output_color + vec3(1.0));
|
|
|
|
|