diff options
Diffstat (limited to 'src/shaders')
| -rw-r--r-- | src/shaders/main.glsl | 97 |
1 files changed, 95 insertions, 2 deletions
diff --git a/src/shaders/main.glsl b/src/shaders/main.glsl index 4e2d4ea..784176f 100644 --- a/src/shaders/main.glsl +++ b/src/shaders/main.glsl @@ -11,22 +11,115 @@ layout(binding=0) uniform vs_params { }; in vec2 vpos; +out vec2 fpos; void main() { gl_Position = vec4(vpos + position, 0, 1); + fpos = (vpos + 1) * 0.5; } @end @fs fs_offscreen +layout(binding=1) uniform texture2D off_tex; +layout(binding=1) uniform sampler off_smp; layout(binding=1) uniform fs_params { - vec4 color; + vec2 resolution; + int rayCount; + int maxSteps; + float size; }; +in vec2 fpos; out vec4 frag_color; +bool outOfBounds(vec2 uv) { + return uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0; +} + +#define TAU 6.28318530718 +// #define RAYCOUNT 16 +// #define MAXSTEPS 8 + +float rand(vec2 seed) +{ + return fract(sin(dot(seed.xy, vec2(12.9898, 78.233))) * 43758.5453); +} + +vec4 raymarch() { + vec2 scale = resolution.xy / resolution.xy; + + // sample texture at pixel + vec4 light = textureLod(sampler2D(off_tex, off_smp), fpos * scale, 0); + // if it is a light source, color that pixel in this color + if (light.a > 0.1) + { + return light; + } + + float oneOverRayCount = 1.0 / float(rayCount); + float tauOverRayCount = TAU * oneOverRayCount; + + float noise = rand(fpos); + + vec4 radiance = vec4(0.0); + + for (int i = 0; i < rayCount; i++) + { + float angle = tauOverRayCount * (float(i) + noise); + vec2 rayDirectionUv = vec2(cos(angle), -sin(angle)) / size; + + vec2 sampleUv = fpos + rayDirectionUv; + + for (int step = 0; step < maxSteps; step++) + { + if (outOfBounds(sampleUv)) + break; + + vec4 sampleLight = textureLod(sampler2D(off_tex, off_smp), sampleUv, 0); + if (sampleLight.a > 0.1) + { + radiance += sampleLight; + break; + } + + sampleUv += rayDirectionUv; + } + } + + return radiance * oneOverRayCount; +} + void main() { - frag_color = color; + frag_color = raymarch(); } @end @program offscreen vs_offscreen fs_offscreen + + +// default-pass shaders +@vs vs_default + +in vec2 vpos; +out vec2 fpos; + +void main() { + gl_Position = vec4(vpos, 0, 1); + fpos = (vpos + 1) * 0.5; +} +@end + +@fs fs_default +layout(binding=0) uniform texture2D scr_tex; +layout(binding=0) uniform sampler scr_smp; + +in vec2 fpos; + +out vec4 frag_color; + +void main() { + frag_color = texture(sampler2D(scr_tex, scr_smp), fpos); +} +@end + +@program default vs_default fs_default |
