point light
This commit is contained in:
@@ -11,6 +11,11 @@ uint METAL_ROUGH_TEXTURE_ID = 2;
|
||||
uint NORMAL_TEXTURE_ID = 3;
|
||||
uint POSITION_TEXTIURE_ID = 4;
|
||||
|
||||
|
||||
uint HYDRA_LIGHT_DIRECTIONAL = 1;
|
||||
uint HYDRA_LIGHT_POINT = 2;
|
||||
uint HYDRA_LIGHT_SPOT = 3;
|
||||
|
||||
struct light_structure
|
||||
{
|
||||
vec4 light_colour;
|
||||
@@ -156,6 +161,59 @@ vec3 DirectionalShading(vec3 normal, vec3 diffuse, float roughness, float metall
|
||||
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 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);
|
||||
|
||||
// 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);
|
||||
float attenuation = 1.0;
|
||||
|
||||
attenuation = light_intensity / (distance * distance);
|
||||
|
||||
vec3 radiance = lightColour * attenuation;
|
||||
|
||||
// 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;
|
||||
}
|
||||
mat3 GetRotationOnlyMatrix(mat4 model_mat)
|
||||
{
|
||||
mat3 rot_mat;
|
||||
@@ -192,9 +250,15 @@ void main()
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
vec3 colour = 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 = colour;
|
||||
//uFragColor = vec4(light_direction, 1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user