Kelsidavis-WoWee/assets/shaders/terrain.frag.glsl
Kelsi 83b576e8d9 Vulcan Nightmare
Experimentally bringing up vulcan support
2026-02-21 22:04:17 -08:00

96 lines
3.2 KiB
GLSL

#version 450
layout(set = 0, binding = 0) uniform PerFrame {
mat4 view;
mat4 projection;
mat4 lightSpaceMatrix;
vec4 lightDir;
vec4 lightColor;
vec4 ambientColor;
vec4 viewPos;
vec4 fogColor;
vec4 fogParams;
vec4 shadowParams;
};
layout(set = 1, binding = 0) uniform sampler2D uBaseTexture;
layout(set = 1, binding = 1) uniform sampler2D uLayer1Texture;
layout(set = 1, binding = 2) uniform sampler2D uLayer2Texture;
layout(set = 1, binding = 3) uniform sampler2D uLayer3Texture;
layout(set = 1, binding = 4) uniform sampler2D uLayer1Alpha;
layout(set = 1, binding = 5) uniform sampler2D uLayer2Alpha;
layout(set = 1, binding = 6) uniform sampler2D uLayer3Alpha;
layout(set = 1, binding = 7) uniform TerrainParams {
int layerCount;
int hasLayer1;
int hasLayer2;
int hasLayer3;
};
layout(set = 0, binding = 1) uniform sampler2DShadow uShadowMap;
layout(location = 0) in vec3 FragPos;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 TexCoord;
layout(location = 3) in vec2 LayerUV;
layout(location = 0) out vec4 outColor;
float sampleAlpha(sampler2D tex, vec2 uv) {
vec2 edge = min(uv, 1.0 - uv);
float border = min(edge.x, edge.y);
float doBlur = step(border, 2.0 / 64.0);
if (doBlur < 0.5) {
return texture(tex, uv).r;
}
vec2 texel = vec2(1.0 / 64.0);
float a = 0.0;
a += texture(tex, uv + vec2(-texel.x, 0.0)).r;
a += texture(tex, uv + vec2(texel.x, 0.0)).r;
a += texture(tex, uv + vec2(0.0, -texel.y)).r;
a += texture(tex, uv + vec2(0.0, texel.y)).r;
return a * 0.25;
}
void main() {
vec4 baseColor = texture(uBaseTexture, TexCoord);
float a1 = hasLayer1 != 0 ? sampleAlpha(uLayer1Alpha, LayerUV) : 0.0;
float a2 = hasLayer2 != 0 ? sampleAlpha(uLayer2Alpha, LayerUV) : 0.0;
float a3 = hasLayer3 != 0 ? sampleAlpha(uLayer3Alpha, LayerUV) : 0.0;
float w0 = 1.0, w1 = a1, w2 = a2, w3 = a3;
float sum = w0 + w1 + w2 + w3;
if (sum > 0.0) { w0 /= sum; w1 /= sum; w2 /= sum; w3 /= sum; }
vec4 finalColor = baseColor * w0;
if (hasLayer1 != 0) finalColor += texture(uLayer1Texture, TexCoord) * w1;
if (hasLayer2 != 0) finalColor += texture(uLayer2Texture, TexCoord) * w2;
if (hasLayer3 != 0) finalColor += texture(uLayer3Texture, TexCoord) * w3;
vec3 norm = normalize(Normal);
vec3 lightDir2 = normalize(-lightDir.xyz);
vec3 ambient = ambientColor.rgb * finalColor.rgb;
float diff = max(abs(dot(norm, lightDir2)), 0.2);
vec3 diffuse = diff * lightColor.rgb * finalColor.rgb;
float shadow = 1.0;
if (shadowParams.x > 0.5) {
vec4 lsPos = lightSpaceMatrix * vec4(FragPos, 1.0);
vec3 proj = lsPos.xyz / lsPos.w;
proj.xy = proj.xy * 0.5 + 0.5;
if (proj.x >= 0.0 && proj.x <= 1.0 && proj.y >= 0.0 && proj.y <= 1.0 && proj.z <= 1.0) {
float bias = 0.002;
shadow = texture(uShadowMap, vec3(proj.xy, proj.z - bias));
shadow = mix(1.0, shadow, shadowParams.y);
}
}
vec3 result = ambient + shadow * diffuse;
float distance = length(viewPos.xyz - FragPos);
float fogFactor = clamp((fogParams.y - distance) / (fogParams.y - fogParams.x), 0.0, 1.0);
result = mix(fogColor.rgb, result, fogFactor);
outColor = vec4(result, 1.0);
}