Files
4kintro/template.frag

129 lines
2.7 KiB
GLSL

#ifdef GL_ES
precision mediump float;
#endif
#define MAX_STEPS 50
#define MAX_DIST 50.
#define SURF_DIST .001
#define TAU 6.283185
#define PI 3.141592
#define S smoothstep
#define T u_time
uniform vec2 u_resolution;
uniform float u_time;
mat2 Rot(float a) {
float s=sin(a), c=cos(a);
return mat2(c, -s, s, c);
}
float sdBox(vec3 p, vec3 s) {
p = abs(p)-s;
return length(max(p, 0.))+min(max(p.x, max(p.y, p.z)), 0.);
}
vec3 translate(vec3 p) {
p.xy *= Rot(u_time*0.4);
p.yz *= Rot(u_time*0.4);
return p;
}
float GetDist(vec3 p) {
float d = sdBox(p, vec3(1));
/*
d += sin(p.x*5.+u_time)*0.5;
d += sin(p.y*2.+u_time)*0.5;
d += sin(p.z*5.+u_time)*0.5; */
return d;
}
float RayMarch(vec3 ro, vec3 rd) {
float dO=0.;
for(int i=0; i<MAX_STEPS; i++) {
vec3 p = ro + rd*dO;
float dS = GetDist(p);
dO += dS;
if(dO>MAX_DIST || abs(dS)<SURF_DIST) break;
}
return dO;
}
vec3 GetNormal(vec3 p) {
vec2 e = vec2(.001, 0);
vec3 n = GetDist(p) - vec3(GetDist(p-e.xyy), GetDist(p-e.yxy),GetDist(p-e.yyx));
return normalize(n);
}
vec3 GetRayDir(vec2 uv, vec3 p, vec3 l, float z) {
vec3
f = normalize(l-p),
r = normalize(cross(vec3(0,1,0), f)),
u = cross(f,r),
c = f*z,
i = c + uv.x*r + uv.y*u;
return normalize(i);
}
float star(vec2 position, float radius, float sides){
float a = atan(position.x, position.y);
float slice = PI * 2. / sides;
return 1. - step(radius, cos(floor(-2.0 + a / slice) * slice - a));
}
vec3 textu(vec2 uv) {
vec3 color = vec3(0.08, 0.22, 0.39);
vec3 mixer = vec3(0.);
for(float i = 1.; i < 3.; i++){
vec2 translate = uv;
// translate *= Rot(i*1.4/5. + u_time);
float sides = 11.;
mixer += vec3(star(translate, 0.06, sides))*vec3(0.5725, 0.8275, 0.9451)*(i*0.7)*0.2;
}
return mix( color, mixer, 0.5);
}
void main()
{
vec2 uv = (gl_FragCoord.xy -.5 * u_resolution.xy) / u_resolution.y;
vec3 ro = vec3(0., 3., -3.);
vec3 rd = GetRayDir(uv, ro, vec3(0.,0., 0.), 1.);
vec3 col = vec3(0.);
float d = RayMarch(ro, rd);
if (d < MAX_DIST) {
vec3 p = ro + rd * d;
vec3 n = GetNormal(p);
vec3 r = reflect(rd, n);
float dif = dot(n, normalize(vec3(1,2,3)))*.5+.5;
// col = vec3(dif);
vec3 colXZ = textu(p.xz).rgb;
vec3 colYZ = textu(p.yz).rgb;
vec3 colXY = textu(p.xy).rgb;
n = abs(n);
n *= pow(n, vec3(10));
n /= n.x + n.y + n.z;
col = (colXZ*n.y + colXY*n.z + colYZ * n.x)*dif;
}
col = pow(col, vec3(.4545)); // gamma correction
gl_FragColor = vec4(col,1.0);
}