2026-07-17 15:30:29 +01:00
|
|
|
//*PIXEL*
|
|
|
|
|
#version 460 core
|
|
|
|
|
|
|
|
|
|
#extension GL_ARB_bindless_texture : require
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint INVALID_HYDRA_ID = 4294967295;
|
|
|
|
|
uint DEPTH_TEXTURE_ID = 0;
|
|
|
|
|
uint DIFFUSE_TEXTURE_ID = 1;
|
|
|
|
|
uint METAL_ROUGH_TEXTURE_ID = 2;
|
|
|
|
|
uint NORMAL_TEXTURE_ID = 3;
|
|
|
|
|
uint POSITION_TEXTIURE_ID = 4;
|
|
|
|
|
|
2026-07-17 21:06:44 +01:00
|
|
|
|
|
|
|
|
uint HYDRA_LIGHT_DIRECTIONAL = 1;
|
|
|
|
|
uint HYDRA_LIGHT_POINT = 2;
|
|
|
|
|
uint HYDRA_LIGHT_SPOT = 3;
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
struct LightStructure
|
|
|
|
|
{
|
2026-07-18 18:39:17 +01:00
|
|
|
vec4 ambient;
|
|
|
|
|
vec4 diffuse;
|
|
|
|
|
vec4 specular;
|
|
|
|
|
float intensity;
|
|
|
|
|
float constant;
|
|
|
|
|
float linear;
|
|
|
|
|
float quadratic;
|
2026-07-24 19:03:34 +01:00
|
|
|
float cutoff;
|
|
|
|
|
float cutoff_outer;
|
2026-07-18 18:39:17 +01:00
|
|
|
uint entity_id;
|
|
|
|
|
uint light_type;
|
2026-08-03 22:34:52 +01:00
|
|
|
uint shadow_map_id;
|
|
|
|
|
uint shadow_caster;
|
|
|
|
|
uint padding0;
|
|
|
|
|
uint padding1;
|
2026-07-17 15:30:29 +01:00
|
|
|
};
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
struct ShadowMapSet
|
|
|
|
|
{
|
|
|
|
|
uint entity_id;
|
|
|
|
|
uint cascade_count;
|
|
|
|
|
uint shadow_texture_id;
|
|
|
|
|
uint frame_buffer_id;
|
|
|
|
|
float near_plane[8];
|
|
|
|
|
float far_plane[8];
|
|
|
|
|
mat4 light_space_proj[6];
|
|
|
|
|
mat4 light_space_view[6];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
layout(binding = 0) uniform uniform_per_frame
|
2026-08-03 22:34:52 +01:00
|
|
|
{
|
2026-07-17 15:30:29 +01:00
|
|
|
mat4 view;
|
|
|
|
|
mat4 proj;
|
2026-08-03 22:34:52 +01:00
|
|
|
vec3 viewer_pos;
|
2026-07-17 15:30:29 +01:00
|
|
|
uint light_count;
|
|
|
|
|
} ubo_per_frame;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
layout(binding = 1) uniform uniform_output_textures
|
2026-08-03 22:34:52 +01:00
|
|
|
{
|
2026-07-17 15:30:29 +01:00
|
|
|
uint texture_ids[16];
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
layout(std430, binding = 1) readonly buffer positions_buffer
|
2026-07-17 15:30:29 +01:00
|
|
|
{
|
|
|
|
|
mat4 model_matrix[];
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
layout( std430, binding = 3) readonly buffer texture_buffer {
|
2026-07-17 15:30:29 +01:00
|
|
|
uvec2 textures[1000];
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
layout (std430, binding = 4) readonly buffer light_buffer
|
2026-07-17 15:30:29 +01:00
|
|
|
{
|
2026-08-03 22:34:52 +01:00
|
|
|
LightStructure lights[500];
|
2026-07-17 15:30:29 +01:00
|
|
|
};
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
layout (std430, binding = 5) readonly buffer shadow_map_sets
|
|
|
|
|
{
|
|
|
|
|
ShadowMapSet shadows[10];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
layout (location=0) in VS_OUT
|
|
|
|
|
{
|
|
|
|
|
vec2 vsUV;
|
|
|
|
|
}vs_out;
|
|
|
|
|
|
|
|
|
|
layout (location=0) out vec4 uFragColor;
|
|
|
|
|
|
|
|
|
|
const float PI = 3.14159265358979323846;
|
|
|
|
|
const float M_INV_PI = 0.31830988618379067153776752674503;
|
|
|
|
|
float sqr(float x) { return x*x; }
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
|
|
|
|
{
|
|
|
|
|
float a = roughness*roughness;
|
|
|
|
|
float a2 = a*a;
|
|
|
|
|
float NdotH = max(dot(N, H), 0.0);
|
|
|
|
|
float NdotH2 = NdotH*NdotH;
|
|
|
|
|
|
|
|
|
|
float nom = a2;
|
|
|
|
|
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
|
|
|
|
denom = PI * denom * denom;
|
|
|
|
|
|
|
|
|
|
return nom / denom;
|
|
|
|
|
}
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
|
|
|
|
{
|
|
|
|
|
float r = (roughness + 1.0);
|
|
|
|
|
float k = (r*r) / 8.0;
|
|
|
|
|
|
|
|
|
|
float nom = NdotV;
|
|
|
|
|
float denom = NdotV * (1.0 - k) + k;
|
|
|
|
|
|
|
|
|
|
return nom / denom;
|
|
|
|
|
}
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
|
|
|
|
{
|
|
|
|
|
float NdotV = max(dot(N, V), 0.0);
|
|
|
|
|
float NdotL = max(dot(N, L), 0.0);
|
|
|
|
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
|
|
|
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
|
|
|
|
|
|
|
|
|
return ggx1 * ggx2;
|
|
|
|
|
}
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
|
|
|
|
{
|
|
|
|
|
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 CalculatePBR(vec3 normal, vec3 view_pos, vec3 world_pos,vec3 light_dir, vec3 diffuse, vec3 radiance, float roughness, float metallic )
|
2026-07-17 15:30:29 +01:00
|
|
|
{
|
|
|
|
|
vec3 N = normalize(normal);
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 V = normalize(view_pos - world_pos);
|
2026-07-17 15:30:29 +01:00
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
// 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);
|
2026-07-17 15:30:29 +01:00
|
|
|
F0 = mix(F0, diffuse, metallic);
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
// calculate per-light radiance
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 L = normalize(light_dir);
|
2026-07-17 15:30:29 +01:00
|
|
|
vec3 H = normalize(V + L);
|
|
|
|
|
|
|
|
|
|
// Cook-Torrance BRDF
|
2026-08-03 22:34:52 +01:00
|
|
|
float NDF = DistributionGGX(N, H, roughness);
|
|
|
|
|
float G = GeometrySmith(N, V, L, roughness);
|
2026-07-17 15:30:29 +01:00
|
|
|
vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
|
2026-08-03 22:34:52 +01:00
|
|
|
|
|
|
|
|
vec3 numerator = NDF * G * F;
|
2026-07-17 15:30:29 +01:00
|
|
|
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;
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
// 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;
|
2026-08-03 22:34:52 +01:00
|
|
|
// multiply kD by the inverse metalness such that only non-metals
|
2026-07-17 15:30:29 +01:00
|
|
|
// have diffuse lighting, or a linear blend if partly metal (pure metals
|
|
|
|
|
// have no diffuse light).
|
2026-08-03 22:34:52 +01:00
|
|
|
kD *= 1.0 - metallic;
|
2026-07-17 15:30:29 +01:00
|
|
|
|
|
|
|
|
// scale light by NdotL
|
2026-08-03 22:34:52 +01:00
|
|
|
float NdotL = max(dot(N, L), 0.0);
|
2026-07-17 15:30:29 +01:00
|
|
|
|
|
|
|
|
// add to outgoing radiance Lo
|
2026-07-18 18:39:17 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
vec3 DirectionalShading(vec3 normal,
|
|
|
|
|
vec3 diffuse,
|
|
|
|
|
float roughness,
|
|
|
|
|
float metallic,
|
|
|
|
|
float light_intensity,
|
|
|
|
|
vec3 light_dir,
|
|
|
|
|
vec3 light_pos,
|
|
|
|
|
vec3 world_pos,
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 view_pos)
|
|
|
|
|
{
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
float distance = length(light_pos - world_pos);
|
2026-07-18 18:39:17 +01:00
|
|
|
float attenuation = 1.0;
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 radiance = diffuse * attenuation;
|
|
|
|
|
|
|
|
|
|
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
|
2026-07-17 15:30:29 +01:00
|
|
|
|
|
|
|
|
return Lo;
|
|
|
|
|
}
|
2026-07-17 21:06:44 +01:00
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
|
|
|
|
|
vec3 PointShading(vec3 normal,
|
|
|
|
|
vec3 diffuse,
|
|
|
|
|
float roughness,
|
|
|
|
|
float metallic,
|
|
|
|
|
float light_intensity,
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 light_pos,
|
|
|
|
|
float light_constant,
|
|
|
|
|
float light_linear,
|
2026-08-03 22:34:52 +01:00
|
|
|
float light_quadratic,
|
|
|
|
|
vec3 world_pos,
|
2026-07-18 18:39:17 +01:00
|
|
|
vec3 view_pos)
|
2026-07-17 21:06:44 +01:00
|
|
|
{
|
2026-08-03 22:34:52 +01:00
|
|
|
float distance = length(light_pos - world_pos);
|
2026-07-17 21:06:44 +01:00
|
|
|
float attenuation = 1.0;
|
2026-08-03 22:34:52 +01:00
|
|
|
|
|
|
|
|
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
|
2026-07-18 18:39:17 +01:00
|
|
|
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);
|
2026-07-17 21:06:44 +01:00
|
|
|
|
2026-07-24 19:03:34 +01:00
|
|
|
return Lo;
|
|
|
|
|
}
|
2026-08-03 22:34:52 +01:00
|
|
|
vec3 SpotShading(vec3 normal,
|
|
|
|
|
vec3 diffuse,
|
|
|
|
|
float roughness,
|
|
|
|
|
float metallic,
|
|
|
|
|
float light_intensity,
|
|
|
|
|
vec3 light_dir,
|
2026-07-24 19:03:34 +01:00
|
|
|
vec3 light_pos,
|
|
|
|
|
float cutoff,
|
2026-08-03 22:34:52 +01:00
|
|
|
float cutoff_outer,
|
2026-07-24 19:03:34 +01:00
|
|
|
float light_constant,
|
|
|
|
|
float light_linear,
|
2026-08-03 22:34:52 +01:00
|
|
|
float light_quadratic,
|
|
|
|
|
vec3 world_pos,
|
2026-07-24 19:03:34 +01:00
|
|
|
vec3 view_pos)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
vec3 direction_to_light = normalize(light_pos - world_pos);
|
|
|
|
|
float theta = dot(direction_to_light, light_dir);
|
|
|
|
|
float falloff = 0.0f;
|
|
|
|
|
// if(theta > cutoff)
|
|
|
|
|
// {
|
|
|
|
|
float epsilon = cutoff - cutoff_outer;
|
2026-08-03 22:34:52 +01:00
|
|
|
falloff = clamp((theta - cutoff_outer) / epsilon, 0.0, 1.0);
|
|
|
|
|
|
2026-07-24 19:03:34 +01:00
|
|
|
//}
|
2026-07-17 21:06:44 +01:00
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
float distance = length(light_pos - world_pos);
|
2026-07-24 19:03:34 +01:00
|
|
|
float attenuation = 1.0;
|
2026-08-03 22:34:52 +01:00
|
|
|
|
|
|
|
|
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
|
2026-07-24 19:03:34 +01:00
|
|
|
vec3 radiance = diffuse * attenuation * falloff;
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-24 19:03:34 +01:00
|
|
|
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, direction_to_light, diffuse, radiance, roughness, metallic);
|
2026-07-17 21:06:44 +01:00
|
|
|
|
|
|
|
|
return Lo;
|
|
|
|
|
}
|
2026-07-24 19:03:34 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
mat3 GetRotationOnlyMatrix(mat4 model_mat)
|
|
|
|
|
{
|
|
|
|
|
mat3 rot_mat;
|
|
|
|
|
rot_mat[0] = model_mat[0].xyz;
|
|
|
|
|
rot_mat[1] = model_mat[1].xyz;
|
|
|
|
|
rot_mat[2] = model_mat[2].xyz;
|
|
|
|
|
return rot_mat;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
float SimpleSampleShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint shadow_trans_idx)
|
|
|
|
|
{
|
|
|
|
|
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
|
|
|
|
|
frag_light_space = (frag_light_space * 0.5) + 0.5;
|
|
|
|
|
|
|
|
|
|
float shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy, shadow_trans_idx)).r;
|
|
|
|
|
// return frag_light_space.z - shadow_map_depth;
|
|
|
|
|
// return shadow_map_depth ;
|
|
|
|
|
if(frag_light_space.z > shadow_map_depth)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float SampleShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint shadow_trans_idx, vec3 normal, vec3 light_dir)
|
|
|
|
|
{
|
|
|
|
|
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
|
|
|
|
|
frag_light_space = (frag_light_space * 0.5) + 0.5;
|
|
|
|
|
|
|
|
|
|
float shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy, shadow_trans_idx)).r;
|
|
|
|
|
// calculate bias (based on depth map resolution and slope)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float bias = max(0.005 * (1.0 - dot(normal, light_dir)), 0.0001);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check whether current frag pos is in shadow
|
|
|
|
|
// float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
|
|
|
|
|
// PCF
|
|
|
|
|
float shadow = 0.0;
|
|
|
|
|
vec2 texel_size = 1.0 / textureSize(sampler_shadow_map, 0).xy;
|
|
|
|
|
|
|
|
|
|
for(int x = -1; x <= 1; ++x)
|
|
|
|
|
{
|
|
|
|
|
for(int y = -1; y <= 1; ++y)
|
|
|
|
|
{
|
|
|
|
|
shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy + vec2(x, y) * texel_size, shadow_trans_idx)).r;
|
|
|
|
|
shadow += frag_light_space.z - bias > shadow_map_depth ? 1.0 : 0.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
shadow /= 9.0;
|
|
|
|
|
|
|
|
|
|
// keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
|
|
|
|
|
if(frag_light_space.z > 1.0)
|
|
|
|
|
shadow = 0.0;
|
|
|
|
|
|
|
|
|
|
return shadow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float CalculateDirectionalShadow(vec4 world_pos, uint shadow_idx, vec3 normal, vec3 light_dir)
|
|
|
|
|
{
|
|
|
|
|
float shadow = 0;
|
|
|
|
|
//Move the fragment to light space
|
|
|
|
|
vec4 frag_in_light_space = shadows[shadow_idx].light_space_view[5] * world_pos;
|
|
|
|
|
//measure the distance of the fragment from the light
|
|
|
|
|
float frag_distance = length(frag_in_light_space.xyz);
|
|
|
|
|
|
|
|
|
|
uint shadow_trans_idx = 0;
|
|
|
|
|
uint ignore_shadow = 0;
|
|
|
|
|
|
|
|
|
|
shadow_idx = 0;
|
|
|
|
|
//Work out which shadowmap is appropriate for this distance
|
|
|
|
|
for(uint i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
if(frag_distance <= shadows[shadow_idx].far_plane[i])
|
|
|
|
|
{
|
|
|
|
|
//record the shadow map level
|
|
|
|
|
shadow_trans_idx = i;
|
|
|
|
|
//and indicate that this has a level
|
|
|
|
|
ignore_shadow = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(ignore_shadow == 1)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
vec4 translated_pixel = shadows[shadow_idx].light_space_proj[shadow_trans_idx] *
|
|
|
|
|
shadows[shadow_idx].light_space_view[shadow_trans_idx] *
|
|
|
|
|
world_pos;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uvec2 shadow_handle = textures[shadows[shadow_idx].shadow_texture_id];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shadow = SampleShadow(translated_pixel, sampler2DArray(shadow_handle), shadow_trans_idx, normal, light_dir );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shadow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
uvec2 diffuse_handle = textures[texture_ids[DIFFUSE_TEXTURE_ID]];
|
|
|
|
|
uvec2 normal_handle = textures[texture_ids[NORMAL_TEXTURE_ID]];
|
|
|
|
|
uvec2 metal_rough_handle = textures[texture_ids[METAL_ROUGH_TEXTURE_ID]];
|
|
|
|
|
uvec2 world_pos_handle = textures[texture_ids[POSITION_TEXTIURE_ID]];
|
|
|
|
|
vec4 diffuse = texture(sampler2D(diffuse_handle), vs_out.vsUV.xy);
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
if(diffuse[3] <= 0.1)
|
2026-08-03 22:34:52 +01:00
|
|
|
{
|
2026-07-17 15:30:29 +01:00
|
|
|
discard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 normal = texture(sampler2D(normal_handle), vs_out.vsUV.xy).xyz;
|
2026-08-03 22:34:52 +01:00
|
|
|
vec3 metal_rough = texture(sampler2D(metal_rough_handle), vs_out.vsUV.xy).xyz;
|
|
|
|
|
vec3 world_pos = texture(sampler2D(world_pos_handle), vs_out.vsUV.xy).xyz;
|
2026-07-17 15:30:29 +01:00
|
|
|
float rough = metal_rough[1];
|
|
|
|
|
float metal = metal_rough[2];
|
2026-08-03 22:34:52 +01:00
|
|
|
float ao = 1;
|
|
|
|
|
vec3 ambient = vec3(0.05) * diffuse.xyz * ao;
|
|
|
|
|
vec3 Lo = ambient;
|
2026-07-17 15:30:29 +01:00
|
|
|
for(int i = 0; i < ubo_per_frame.light_count; i ++)
|
|
|
|
|
{
|
2026-07-18 18:39:17 +01:00
|
|
|
mat4 model = model_matrix[lights[i].entity_id];
|
2026-07-17 15:30:29 +01:00
|
|
|
mat3 rot_mat = GetRotationOnlyMatrix(model);
|
2026-07-24 19:03:34 +01:00
|
|
|
vec3 light_direction = normalize(rot_mat * vec3(0,0,1));
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
vec3 light_pos = model[3].xyz;
|
2026-07-17 21:06:44 +01:00
|
|
|
if(lights[i].light_type == HYDRA_LIGHT_DIRECTIONAL)
|
|
|
|
|
{
|
2026-07-24 19:03:34 +01:00
|
|
|
Lo += DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].intensity, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
|
2026-08-03 22:34:52 +01:00
|
|
|
if(lights[i].shadow_caster == 1)
|
|
|
|
|
{
|
|
|
|
|
float shadow = CalculateDirectionalShadow(vec4(world_pos, 1.0), lights[i].shadow_map_id, normal, light_direction);
|
|
|
|
|
Lo = Lo * vec3(1-shadow);
|
|
|
|
|
}
|
2026-07-17 21:06:44 +01:00
|
|
|
}
|
|
|
|
|
else if(lights[i].light_type == HYDRA_LIGHT_POINT)
|
|
|
|
|
{
|
2026-07-18 18:39:17 +01:00
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
Lo += PointShading(normal,
|
|
|
|
|
diffuse.xyz,
|
|
|
|
|
rough,
|
|
|
|
|
metal,
|
|
|
|
|
lights[i].intensity,
|
|
|
|
|
light_pos,
|
2026-07-18 18:39:17 +01:00
|
|
|
lights[i].constant,
|
2026-08-03 22:34:52 +01:00
|
|
|
lights[i].linear,
|
|
|
|
|
lights[i].quadratic,
|
|
|
|
|
world_pos,
|
2026-07-18 18:39:17 +01:00
|
|
|
ubo_per_frame.viewer_pos);
|
2026-07-17 21:06:44 +01:00
|
|
|
}
|
2026-07-24 19:03:34 +01:00
|
|
|
else if(lights[i].light_type == HYDRA_LIGHT_SPOT)
|
|
|
|
|
{
|
2026-08-03 22:34:52 +01:00
|
|
|
Lo += SpotShading(normal,
|
|
|
|
|
diffuse.xyz,
|
|
|
|
|
rough,
|
|
|
|
|
metal,
|
|
|
|
|
lights[i].intensity,
|
2026-07-24 19:03:34 +01:00
|
|
|
light_direction,
|
2026-08-03 22:34:52 +01:00
|
|
|
light_pos,
|
2026-07-24 19:03:34 +01:00
|
|
|
lights[i].cutoff,
|
|
|
|
|
lights[i].cutoff_outer,
|
|
|
|
|
lights[i].constant,
|
2026-08-03 22:34:52 +01:00
|
|
|
lights[i].linear,
|
|
|
|
|
lights[i].quadratic,
|
|
|
|
|
world_pos,
|
2026-07-24 19:03:34 +01:00
|
|
|
ubo_per_frame.viewer_pos);
|
|
|
|
|
}
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
}
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
float lighting = 0;
|
2026-08-03 22:34:52 +01:00
|
|
|
// ambient lighting (note that the next IBL tutorial will replace
|
2026-07-17 15:30:29 +01:00
|
|
|
// this ambient lighting with environment lighting).
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
|
2026-08-03 22:34:52 +01:00
|
|
|
vec3 output_color = Lo;
|
2026-07-17 15:30:29 +01:00
|
|
|
|
|
|
|
|
// HDR tonemapping
|
|
|
|
|
output_color = output_color / (output_color + vec3(1.0));
|
|
|
|
|
// gamma correct
|
2026-08-03 22:34:52 +01:00
|
|
|
output_color = pow(output_color, vec3(1.0/2.1));
|
2026-07-17 15:30:29 +01:00
|
|
|
|
|
|
|
|
uFragColor = vec4(output_color, 1.0);
|
|
|
|
|
// float light = dot(lights[0].light_direction, normal);
|
|
|
|
|
|
|
|
|
|
// uFragColor = vec4(diffuse.xyz * light,1);
|
2026-08-03 22:34:52 +01:00
|
|
|
|
2026-07-17 15:30:29 +01:00
|
|
|
// uFragColor = vec4(normal, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|