Files
4kintro/shader_old.glsl
2024-07-29 08:13:18 +03:00

206 lines
5.3 KiB
GLSL

precision mediump float;
uniform float u_time;
uniform vec2 u_resolution;
uniform sampler2D texture_sampler;
uniform sampler2D texts;
mat2 rot(float a) {
return mat2(cos(a), sin(a), -sin(a), cos(a));
}
float sdSphere(vec3 p, float r) {
return length(p)-r;
}
float pMod1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p + halfsize, size) - halfsize;
return c;
}
vec2 map(vec3 pos) {
vec3 p = pos;
float d = 1e3;
float mat = 0.;
for (float i = 0.; i < 60.; i++) {
vec3 q = p;
pMod1(q.z, 1.);
q.xy *= rot(sin(u_time*0.25)*3.);
q.x -= 1.5 * cos(i / 3.1415);
q.y -= 1.5 * sin(i / 3.1415);
q.z += (8. - i) * 0.01;
float b = sdSphere(q, 0.02);
d = min(d, b);
if (d == b) {
mat = 0.;
}
}
for (float i = 0.; i < 99.; i++) {
vec3 q = p;
pMod1(q.z, 1.);
q.z -= .5;
q.xy *= rot(cos(-u_time*0.25)*2.);
q.x -= 3. * cos(i / 3.1415);
q.y -= 3. * sin(i / 3.1415);
q.z += i * 0.01;
float b = sdSphere(q, 0.03);
d = min(d, b);
if (d == b) {
mat = 1.;
}
}
for (float i = 0.; i < 20.; i++) {
vec3 q = p;
pMod1(q.z, 0.5);
q.xy *= rot(-sin(u_time)*1.5);
q.x -= .25 * cos(i / 3.1415);
q.y -= .25 * sin(i / 3.1415);
float b = sdSphere(q, 0.01);
d = min(d, b);
if (d == b) {
mat = 2.;
}
}
return vec2(d, mat);
}
vec3 palette(float t) {
vec3 a = vec3(0.5, 0.5, 0.5);
vec3 b = vec3(1.0, 1.0, 1.0);
vec3 c = vec3(1.0, 1.0, 1.0);
vec3 d = vec3(0.0, 0.6666, 0.3333);
return a + b * cos(6.28318 * (c*t+d));
}
vec2 rayMarch2(vec3 ro, vec3 rd) {
float t = 0.;
float d;
float ac = 0.;
for (int i = 0; i < 60; i++) {
vec3 p = ro + rd * t; // "cast" rays
d = map(p).x; // Get distance to objects
t += d; // "march" the ray
ac += exp(-d * 10.);
if (abs(d) < .002 || t > 20.) break;
}
return vec2(t, ac);
}
vec4 mainImage2() {
vec2 uv = (gl_FragCoord.xy * 2. - u_resolution.xy) / u_resolution.y;
//vec3 ro = vec3( u_time * 4., -u_time , u_time * 8.);
vec3 ro=vec3(0,0,u_time );
vec3 ray = normalize(vec3(uv, .7));
ray.xy *= rot(sin(u_time)*0.5);
vec2 rm = rayMarch2(ro, ray);
float d = rm.x;
float ac = rm.y;
vec3 p = ro + ray * d;
float mat = map(p).y;
vec3 col = vec3(0.0235, 0.0157, 0.1451);
if (d < 10.) {
if (mat == 0.) {
col = mix(col, vec3( 0., 0.3, 0.6) + 1. * 0.1*d, 0.4) * ac * 0.3;
}
if (mat == 1.) {
col = mix(col, palette(1. - 0.4*d), 0.2)*ac*0.5;
}
if (mat == 2.) {
col = palette( 0.5 + (1. -0.75 * d ) * 0.4);
}
}
return vec4(col, 1.0);
}
void main(void) {
gl_FragColor = mainImage2();
}
/*
float rand(vec2 co) {
float a = 12.9898;
float b = 78.233;
float c = 43758.5453;
float dt= dot(co.xy ,vec2(a,b));
float sn= mod(dt,3.14);
return fract(sin(sn) * c);
}
vec3 noise(vec2 uv) {
float rng = rand(vec2(uv.x+u_time, uv.y));
vec3 col = vec3(0.0+rng*0.1, 0.2+rng*0.1, 0.4+rng*0.1);
col.x += sin(uv.x+u_time*0.2)*0.1;
col.y += sin(uv.y+u_time*0.2)*0.1;
col.z += sin(uv.y+u_time*0.2)*0.1;
return col;
}
vec3 wave(vec2 uv, float phase, float _offset, float height, float waveLenght, float color) {
float val = -(sin(phase+uv.x*waveLenght+u_time) + (uv.y*6.+height)+_offset)*0.055;
val *= 1. + sin(phase + uv.x*waveLenght+u_time) + (uv.y*6.-height+_offset);
//return smoothstep(0.0, 1.0, val) * palette(color/360.);
return clamp(val, 0., .9) * palette(color/360.);
}
float gStartTime = 0.;
float gPrevTime = -0.1;
vec4 waveTexture(vec2 uv) {
uv.y = sin(uv.x*16. + u_time)/32. + uv.y-0.03;
uv.x -= (u_time-gStartTime)*0.2;
vec4 tex = texture2D(texts, uv);
if (tex.y > 0. && (gPrevTime != gStartTime)) {
gStartTime = u_time;
gPrevTime = gStartTime;
}
return vec4(tex.rgb, 1.0);
}
void TextScroller(out vec4 fragColor, in vec2 fragCoord)
{
// Normalized pixel coordinates (from -1 to 1, origo at 0,0)
vec2 uv = (fragCoord.xy * 2. - u_resolution.xy) / min(u_resolution.x, u_resolution.y);
vec3 col = vec3(0., 0.4, 0.6);
col += wave(uv, 0., 1., 3., 1., sin(u_time+0.3)*90.);
col += wave(uv, sin(u_time), 0., 3., -1., sin(u_time-1.2)*120.);
col += wave(uv, -3., 0., 3., 1., sin(u_time+0.5)*90.);
vec4 outBuf = waveTexture(uv * 0.1);
fragColor = max(mainImage2()*0.2, vec4(col*outBuf.xyz, 1.0)); // + vec4(random(uv*2.), 1.0);
}
void main(void) {
gl_FragColor = mainImage2();
// Get the initial color at this pixel.
// Get the initial color at this pixel.
/*else
{
mainImage2(gl_FragColor, gl_FragCoord.xy);
vec4 l;
vec2 texttop = vec2(0., -.35), pt = (gl_FragCoord.xy / u_resolution) - texttop;
if(pt.y > 0.) {
l = gl_FragColor + texture2D(texts, pt);
}
gl_FragColor = l;
}
}
*/