treesummaryrefslogcommitdiff
path: root/src/shaders/main.glsl
diff options
context:
space:
mode:
authorPatrick2026-10-05 17:38:38 +0200
committerPatrick2026-10-05 17:38:38 +0200
commit9a7d2d44dc17603ff705be5d00416f01704459ef (patch)
tree7014ec0089e76e513249dfdba4e9a0f55163b313 /src/shaders/main.glsl
parentc511ae90162f506f34ca0b98732577dc688ebcca (diff)
downloadsg3-9a7d2d44dc17603ff705be5d00416f01704459ef.tar.gz
sg3-9a7d2d44dc17603ff705be5d00416f01704459ef.zip
update
Diffstat (limited to 'src/shaders/main.glsl')
-rw-r--r--src/shaders/main.glsl97
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