lisää optimointia

This commit is contained in:
2024-07-29 08:13:18 +03:00
parent b4c88efb92
commit f5cfde0373
5 changed files with 473 additions and 328 deletions

View File

@ -1,99 +1,74 @@
uniform float time;
uniform vec2 resolution;
precision mediump float;
uniform float u_time;
uniform vec2 u_resolution;
uniform sampler2D texture_sampler;
uniform sampler2D texts;
float zoom = 1.;
float gTime = 0.;
float getBeat(void) {
return floor(abs(time*4.) / (60./145.) );
}
float getBar(void) {
return floor(abs(time) / (60./145.));
}
vec2 csqr(vec2 a) {
return vec2(a.x * a.x - a.y * a.y, 2. * a.x * a.y);
}
mat2 rot(float a) {
return mat2(cos(a), sin(a), -sin(a), cos(a));
}
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
float sdSphere(vec3 p, float r) {
return length(p)-r;
}
float box(vec3 pos, float scale) {
pos *= scale;
float base = sdBox(pos, vec3(.4, .4, .2)) / 1.5;
pos.xy *= 5.;
pos.y -= 3.5;
pos.xy *= rot(.55);
float result = -base;
return result;
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;
}
float box_set(vec3 pos, float time) {
vec3 pos_origin = pos;
pos = pos_origin;
pos.y += sin(gTime * 0.4) * 2.5;
pos.xy *= rot(.8);
float box1 = box(pos, 2. - abs(sin(gTime * 0.4)) * 1.5);
pos = pos_origin;
pos.y -= sin(gTime * 0.4) * 2.5;
pos.xy *= rot(.8);
float box2 = box(pos, 2. - abs(sin(gTime * 0.4)) * 1.5);
pos = pos_origin;
pos.x += sin(gTime * 0.4) * 2.5;
pos.xy *= rot(.8);
float box3 = box(pos, 2. - abs(sin(gTime * 0.4)) * 1.5);
pos = pos_origin;
pos.x -= sin(gTime * 0.4) * 2.5;
pos.xy *= rot(.8);
float box4 = box(pos, 2. - abs(sin(gTime * 0.4)) * 1.5);
pos = pos_origin;
pos.xy *= rot(.8);
float box5 = box(pos, .5) * 6.;
pos = pos_origin;
float box6 = box(pos, .5) * 6.;
float result = max(max(max(max(max(box1, box2), box3), box4), box5), box6);
return result;
}
float map(vec3 pos, float time) {
float box_set1 = box_set(pos, time);
return box_set1;
}
vec4 mainImage2(vec2 fragCoord) {
vec2 p = (fragCoord.xy * 2. - resolution.xy) / min(resolution.x, resolution.y);
vec3 ro = vec3( time * 4., -time , time * 8.);
vec3 ray = normalize(vec3(p, 0.55));
ray.xy = ray.xy * rot(sin(time * .1) * 6.);
ray.yz = ray.yz * rot(sin(time * .01) * .2);
float t = 0.1;
vec3 col = vec3(0.);
float ac = 0.0;
for(int i = 0; i < 99; i++) {
vec3 pos = -ro + ray * t;
pos = mod(pos - 2.5, 4.) - 2.;
gTime = time - float(i) * 0.01;
float d = map(pos, time);
d = max(abs(d), 0.03);
ac += exp(-d * 26.);
t += d * 0.55;
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.;
}
}
col = vec3(ac * 0.025);
col += vec3(0.1 + sin(time) * 0.2, 0.5, 0.);
return vec4(col, 1.0 - t * (0.02 + 0.02 * sin(time)));
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) {
@ -104,6 +79,59 @@ vec3 palette(float t) {
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;
@ -114,18 +142,18 @@ float rand(vec2 co) {
}
vec3 noise(vec2 uv) {
float rng = rand(vec2(uv.x+time, uv.y));
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+time*0.2)*0.1;
col.y += sin(uv.y+time*0.2)*0.1;
col.z += sin(uv.y+time*0.2)*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+time) + (uv.y*6.+height)+_offset)*0.055;
val *= 1. + sin(phase + uv.x*waveLenght+time) + (uv.y*6.-height+_offset);
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.);
}
@ -134,42 +162,44 @@ float gStartTime = 0.;
float gPrevTime = -0.1;
vec4 waveTexture(vec2 uv) {
uv.y = sin(uv.x*16. + time)/32. + uv.y-0.03;
uv.x -= (time-gStartTime)*0.2;
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 = time;
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. - resolution.xy) / min(resolution.x, resolution.y);
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(time+0.3)*90.);
col += wave(uv, sin(time), 0., 3., -1., sin(time-1.2)*120.);
col += wave(uv, -3., 0., 3., 1., sin(time+0.5)*90.);
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(gl_FragCoord.xy)*0.2, vec4(col*outBuf.xyz, 1.0)); // + vec4(random(uv*2.), 1.0);
fragColor = max(mainImage2()*0.2, vec4(col*outBuf.xyz, 1.0)); // + vec4(random(uv*2.), 1.0);
}
void main(void) {
TextScroller(gl_FragColor, gl_FragCoord.xy);
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 / resolution) - texttop;
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;
} */
}
}
*/