cub3d/assets/shaders/frag.glsl

162 lines
5.2 KiB
GLSL

#version 330 core
in vec2 TexCoord;
flat in int TexIndex;
flat in vec3 originalTexCoord;
out vec4 FragColor;
uniform sampler2D Texture0;
uniform sampler2D Texture1;
uniform sampler2D Texture2;
uniform sampler2D Texture3;
uniform sampler2D Texture4;
uniform sampler2D Texture5;
uniform sampler2D Texture6;
uniform sampler2D Texture7;
uniform sampler2D Texture8;
uniform sampler2D Texture9;
uniform sampler2D Texture10;
uniform sampler2D Texture11;
uniform sampler2D Texture12;
uniform sampler2D Texture13;
uniform sampler2D Texture14;
uniform sampler2D Texture15;
uniform float u_time;
uniform float u_battery;
uniform vec2 u_resolution;
uniform float u_hit_timer;
uniform int u_enabled;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898,78.233))) * 43758.5453);
}
float hash(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
vec2 fisheyeDistort(vec2 coord, float strength) {
// Scale down input coordinates to leave room for distortion
vec2 safeCoord = mix(vec2(0.5), coord, 0.85); // Scale down by 15%
vec2 centered = safeCoord * 2.0 - 1.0;
float distance = length(centered);
// Reduce strength near edges
float adjustedStrength = strength * (1.0 - smoothstep(0.7, 0.9, distance));
float distorted = distance * (1.0 + adjustedStrength * distance * distance);
vec2 distortedCoord;
if (distance < 0.001) {
distortedCoord = centered;
} else {
distortedCoord = centered * (distorted / distance);
}
distortedCoord = distortedCoord * 0.5 + 0.5;
return distortedCoord;
}
float calculateMask(vec2 coord, float fisheyeStrength, float maskThreshold, float softness) {
vec2 centered = coord * 2.0 - 1.0;
float distance = length(centered);
if (distance == 0.0) {
return 1.0;
}
float distorted = distance * (1.0 + fisheyeStrength * distance * distance);
vec2 distortedCoord = centered * (distorted / distance);
distortedCoord = distortedCoord * 0.5 + 0.5;
float maxDistortion = max(
max(abs(distortedCoord.x - 0.5), abs(distortedCoord.y - 0.5)) - 0.5,
0.0
);
float mask = 1.0 - smoothstep(maskThreshold - softness, maskThreshold + softness, maxDistortion);
return mask;
}
void main()
{
vec4 texColor;
vec4 noiseColor;
vec2 distortedCoord;
switch (TexIndex) {
case 0: texColor = texture(Texture0, TexCoord); break;
case 1: texColor = texture(Texture1, TexCoord); break;
case 2: texColor = texture(Texture2, TexCoord); break;
case 3: texColor = texture(Texture3, TexCoord); break;
case 4: texColor = texture(Texture4, TexCoord); break;
case 5: texColor = texture(Texture5, TexCoord); break;
case 6: texColor = texture(Texture6, TexCoord); break;
case 7: texColor = texture(Texture7, TexCoord); break;
case 8: texColor = texture(Texture8, TexCoord); break;
case 9: texColor = texture(Texture9, TexCoord); break;
case 10: texColor = texture(Texture10, TexCoord); break;
case 11: texColor = texture(Texture11, TexCoord); break;
case 12: texColor = texture(Texture12, TexCoord); break;
case 13: texColor = texture(Texture13, TexCoord); break;
case 14: texColor = texture(Texture14, TexCoord); break;
case 15: texColor = texture(Texture15, TexCoord); break;
default: texColor = vec4(1.0, 0.0, 0.0, 1.0); break;
}
if (u_enabled == 0) {
FragColor = texColor;
return;
}
if (TexIndex == 1) {
vec2 uv = gl_FragCoord.xy / u_resolution.xy;
uv.x *= u_resolution.x / u_resolution.y;
float blockSize = 6.0;
float strength = 1.0 - u_battery;
vec2 blockUV = floor(gl_FragCoord.xy / blockSize);
float noise = hash(blockUV + vec2(u_time * 3.0, u_time * 7.0));
float noise2 = hash(blockUV + vec2(u_time * 13.0, u_time * 3.0));
float noise3 = hash(blockUV + vec2(u_time * 17.0, u_time * 13.0));
// Horizontal bands
noise *= 0.9 + 0.1 * sin(uv.y * 400.0);
noise2 *= 0.9 + 0.1 * sin(uv.y * 400.0);
noise3 *= 0.9 + 0.1 * sin(uv.y * 400.0);
distortedCoord = fisheyeDistort(TexCoord, u_battery * 1.2);
float offset = 0.001 / (u_battery * u_battery + 0.001 );
offset = offset > .05 ? .05 : offset;
float r = texture(Texture1, distortedCoord + vec2(offset * 5, 0.0)).r;
float g = texture(Texture1, distortedCoord + vec2(0.0, offset * 2.0)).g;
float b = texture(Texture1, distortedCoord - vec2(offset, 0.0)).b;
vec3 color = vec3(r, g, b);
vec3 noisyColor = mix(color.rgb, vec3(noise, noise2, noise3), (strength / 2 + .2));
float flicker = (1 - strength * 0.5) + 0.25 * (strength * sin(u_time + sin(u_time * 7.0) + 1));
float dim = u_battery < 0.1 ? u_battery * 10.0 : 1;
noiseColor = vec4(noisyColor, texColor.a * flicker * dim);
if (u_hit_timer > 0.0) {
vec2 uv = gl_FragCoord.xy / u_resolution.xy;
uv *= 1.0 - uv.yx;
float vig = uv.x*uv.y * 15.0;
vig = pow(vig, 0.25);
float inv_hit = 1.0 - u_hit_timer;
noiseColor *= vec4(vig) * vec4(1.0, inv_hit, inv_hit, 1.0);
}
float mask = calculateMask(gl_FragCoord.xy / u_resolution, 1, 0.08f, 0.02f);
FragColor = vec4(noiseColor.rgb * mask, noiseColor.a);
// FragColor = noiseColor;
} else {
FragColor = texColor;
}
}