Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/shaders/mesh.frag

99 lines
2.7 KiB
GLSL
Raw Normal View History

2018-10-15 19:52:16 -04:00
#version 460 core
2018-10-16 13:03:26 -04:00
layout(location = 0) in vec3 inFragPos;
2018-11-08 08:57:41 -05:00
layout(location = 1) in vec4 inShadowPos;
layout(location = 2) in vec3 inNormal;
layout(location = 3) in vec2 inUV;
2018-10-16 13:03:26 -04:00
2018-10-15 19:52:16 -04:00
layout(location = 0) out vec4 outColor;
2018-12-25 08:32:38 -05:00
layout(push_constant) uniform PushConstants {
mat4 m;
vec3 c;
} pushConstants;
2018-10-18 21:46:48 -04:00
struct Light {
2018-12-19 12:19:52 -05:00
vec4 position, color;
2018-10-18 21:46:48 -04:00
};
2018-12-19 12:19:52 -05:00
layout(set = 0, binding = 1) uniform Lights {
2018-10-18 21:46:48 -04:00
Light lights[32];
};
2018-12-19 12:19:52 -05:00
layout(set = 0, binding = 2) uniform sampler2D shadowSampler;
2018-11-06 09:08:55 -05:00
layout(set = 1, binding = 0) uniform sampler2D albedoSampler;
layout(constant_id = 0) const int shadowFilterPCF = 1;
2018-11-08 08:57:41 -05:00
float textureProj(vec4 P, vec2 off) {
const vec4 shadowCoord = P / P.w; // perspective divide
float shadow = 1.0;
if(shadowCoord.z > -1.0 && shadowCoord.z < 1.0) {
const float dist = texture(shadowSampler, shadowCoord.st + off).r;
if(shadowCoord.w > 0.0 && dist < shadowCoord.z)
shadow = 0.1;
}
return shadow;
}
float filterPCF(vec4 sc) {
const ivec2 texDim = textureSize(shadowSampler, 0);
const float scale = 1.5;
const float dx = scale * 1.0 / float(texDim.x);
const float dy = scale * 1.0 / float(texDim.y);
2018-11-08 12:51:32 -05:00
const int range = 1;
2018-11-08 08:57:41 -05:00
float shadowFactor = 0.0;
int count = 0;
for(int x = -range; x <= range; x++) {
for(int y = -range; y <= range; y++) {
shadowFactor += textureProj(sc, vec2(dx * x, dy * y));
count++;
}
}
return shadowFactor / count;
}
2018-10-15 19:52:16 -04:00
void main() {
2018-12-19 12:19:52 -05:00
vec3 diffuse = vec3(0.0);
2018-10-18 21:46:48 -04:00
for(int i = 0; i < 32; i++) {
2018-11-07 05:27:00 -05:00
vec3 lightDir = vec3(0);
if(lights[i].position.w == 0)
continue;
else if(lights[i].position.w == 1)
2018-11-07 05:27:00 -05:00
lightDir = normalize(lights[i].position.xyz - inFragPos);
else if(lights[i].position.w == 2)
lightDir = normalize(-lights[i].position.xyz);
const vec3 norm = normalize(inNormal);
2018-11-06 09:08:55 -05:00
2018-10-18 21:46:48 -04:00
const float diff = max(dot(norm, lightDir), 0.0);
2018-12-19 12:19:52 -05:00
vec3 add = vec3(diff) * lights[i].color.rgb;
2018-12-19 12:19:52 -05:00
if(lights[i].position.w == 1) {
const float radius = 15.0;
const float dist = distance(inFragPos, lights[i].position.xyz);
float att = clamp(1.0 - dist / radius, 0.0, 1.0);
att *= att;
add *= att;
} else if(lights[i].position.w == 2) {
const float shadow = shadowFilterPCF == 1 ? filterPCF(inShadowPos / inShadowPos.w) : textureProj(inShadowPos / inShadowPos.w, vec2(0, 0));
add *= shadow;
2018-12-19 12:19:52 -05:00
}
diffuse += add;
2018-10-18 21:46:48 -04:00
}
2018-11-06 09:08:55 -05:00
2018-12-25 08:32:38 -05:00
vec3 matColor = pushConstants.c;
if(textureSize(albedoSampler, 0).x > 1)
matColor *= texture(albedoSampler, inUV).rgb;
outColor = vec4((matColor * vec3(0.1)) + diffuse * matColor, 1.0);
2018-10-15 19:52:16 -04:00
}