//*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; uint HYDRA_LIGHT_DIRECTIONAL = 1; uint HYDRA_LIGHT_POINT = 2; uint HYDRA_LIGHT_SPOT = 3; struct LightStructure { vec4 ambient; vec4 diffuse; vec4 specular; float intensity; float constant; float linear; float quadratic; float cutoff; float cutoff_outer; uint entity_id; uint light_type; uint shadow_map_id; uint shadow_caster; uint padding0; uint padding1; }; 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]; }; layout(binding = 0) uniform uniform_per_frame { mat4 view; mat4 proj; vec3 viewer_pos; uint light_count; } ubo_per_frame; layout(binding = 1) uniform uniform_output_textures { uint texture_ids[16]; }; layout(std430, binding = 1) readonly buffer positions_buffer { mat4 model_matrix[]; }; layout( std430, binding = 3) readonly buffer texture_buffer { uvec2 textures[1000]; }; layout (std430, binding = 4) readonly buffer light_buffer { LightStructure lights[500]; }; layout (std430, binding = 5) readonly buffer shadow_map_sets { ShadowMapSet shadows[10]; }; 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); } 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(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); // calculate per-light radiance vec3 L = normalize(light_dir); vec3 H = normalize(V + L); // 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 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) { 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); return Lo; } 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) { float distance = length(light_pos - world_pos); float attenuation = 1.0; 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); return Lo; } vec3 SpotShading(vec3 normal, vec3 diffuse, float roughness, float metallic, float light_intensity, vec3 light_dir, vec3 light_pos, float cutoff, float cutoff_outer, float light_constant, float light_linear, float light_quadratic, vec3 world_pos, 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; falloff = clamp((theta - cutoff_outer) / epsilon, 0.0, 1.0); //} float distance = length(light_pos - world_pos); float attenuation = 1.0; attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance)); vec3 radiance = diffuse * attenuation * falloff; vec3 Lo = CalculatePBR(normal, view_pos, world_pos, direction_to_light, diffuse, radiance, roughness, metallic); return Lo; } 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; } 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.0005); // 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; } 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); if(diffuse[3] <= 0.1) { discard; } vec3 normal = texture(sampler2D(normal_handle), vs_out.vsUV.xy).xyz; 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; float rough = metal_rough[1]; float metal = metal_rough[2]; float ao = 1; vec3 ambient = vec3(0.05) * diffuse.xyz * ao; vec3 Lo = ambient; for(int i = 0; i < ubo_per_frame.light_count; i ++) { mat4 model = model_matrix[lights[i].entity_id]; mat3 rot_mat = GetRotationOnlyMatrix(model); vec3 light_direction = normalize(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].intensity, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos); 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); } } else if(lights[i].light_type == HYDRA_LIGHT_POINT) { 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); } else if(lights[i].light_type == HYDRA_LIGHT_SPOT) { Lo += SpotShading(normal, diffuse.xyz, rough, metal, lights[i].intensity, light_direction, light_pos, lights[i].cutoff, lights[i].cutoff_outer, lights[i].constant, lights[i].linear, lights[i].quadratic, world_pos, ubo_per_frame.viewer_pos); } } float lighting = 0; // ambient lighting (note that the next IBL tutorial will replace // this ambient lighting with environment lighting). vec3 output_color = Lo; // HDR tonemapping output_color = output_color / (output_color + vec3(1.0)); // gamma correct output_color = pow(output_color, vec3(1.0/2.1)); uFragColor = vec4(output_color, 1.0); // float light = dot(lights[0].light_direction, normal); // uFragColor = vec4(diffuse.xyz * light,1); // uFragColor = vec4(normal, 1); }