1
0
Files
pixelthud/shaders/raymarching_template_2026_Y28.frag
2026-02-04 19:29:12 +01:00

87 lines
2.2 KiB
GLSL

/* Based on shader by Yusef28: https://www.shadertoy.com/view/WXGyDG
* CC BY-NC-SA 3.0 - https://creativecommons.org/licenses/by-nc-sa/3.0/
* Minor modifications by m11
*/
/*
*
I *f you write code you might be able to understand a week later,
You might come back to it in a month.
If you write code that you will probably understand 5 years later,
You will probably come back to it tomorrow.
~ Proverbs by a Shady Character, Jan 2026
*/
float map(vec3 p){
p = mod(p, 2.)-1.;
return length(p)-0.5;
}
//basic high accuracy raymarch
float trace(vec3 ro, vec3 rd){
#define FAR 50.
float t = 0., d;
for (int i = 0; i < 96; i++){
d = map(ro + rd*t);
if(abs(d)<.0001 || t>FAR) break;
t += d*.95; // Lot's of accuracy, not as efficient
}
return t;
}
//Normal function using a matrix found here
vec3 normal(vec3 p){
//https://www.shadertoy.com/view/l3fSDr
//but originally by blackle:
mat3 k = mat3(p,p,p) - mat3(0.0001);
return normalize(map(p) - vec3(map(k[0]), map(k[1]) ,map(k[2])));
}
//moving cam function by elsio
vec3 camRay(vec2 u, out vec3 ro){
///https://www.shadertoy.com/view/33V3R3
float t = iTime;
ro = mix(
vec3(0, 15. - 15. * sin(t * .2), 20.),
vec3(
-18. * sin(t * .2),
(10. - 20. * ceil(sin(t * .2))) * sign(sin(t * .1)),
15.
),
ceil(cos(t * .2))
);
vec3
cw = normalize(0. - ro),
cu = normalize(cross(cw, vec3(0, 1, 0))),
cv = normalize(cross(cu, cw));
return normalize(mat3(cu, cv, cw) * vec3(u, 1));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (fragCoord - iResolution.xy*.5)/iResolution.y;
vec3 ro = vec3(0, .0, 1.5);
vec3 rd = camRay(uv, ro);
float t = trace(ro, rd);
vec3 sp = ro + rd*t;
vec3 sn = normal(sp);
vec3 col = vec3(0.);
if(t < FAR){
col = sn*0.5+0.5;
}
//Fog function based on one by Elsio with the exp
//https://www.shadertoy.com/view/33V3R3
col = mix(vec3(0.0),col, exp(-.001 * t * t * t));
// Output to screen
fragColor = vec4(col,1.0);
}