xxxxxxxxxx
//made by twitter.com/mattywillo_
//for twitter.com/clarabellum's #codesketchtober prompt: day and night
let t = 0, res, sh;
function draw() {
t += 0.0001*deltaTime;
sh.setUniform("t", t%1);
sh.setUniform("l", t|0);
shader(sh);
rect(0,0,0);
}
let src = [`
attribute vec3 aPosition;
varying vec2 uv;
void main() {
gl_Position = vec4(aPosition*2.-1., 1.0);
uv = vec2(aPosition.x, 1.-aPosition.y)*2.-1.;
}
`,`
precision highp float;
#define PI `+Math.PI+`
varying vec2 uv;
uniform float t,l;
mat2 rot(float a) {
return mat2(cos(a), -sin(a), sin(a), cos(a));
}
float map(vec3 p) {
p.xy *= p.z*mix(1.,2.,(mod(l,2.)==0.)?t:1.-t);
p.xz *= rot(p.y*mix(.2,.8,sin(t*PI*2.)*.5+.5)) * rot(t*PI*4.);
p.z -= 5.;
return length(p)-.5 + sin(p.x)*sin(p.y)*sin(p.z*.5*sin(t*PI*2.)*.5+.5)*.75;
}
vec3 norm(vec3 p) {
vec2 e = vec2(0.00001, 0.);
return normalize(map(p) - vec3(map(p - e.xyy), map(p - e.yxy), map(p - e.yyx)));
}
void main() {
vec3 n = norm(vec3(uv,0)+vec3(0,0,1)*map(vec3(uv,0)));
gl_FragColor = vec4(vec3(.22,0.,.6)*n.x + vec3(1.,.33,0.)*(mix(n.x,n.y,sin(smoothstep(0.,1.,t)*PI*2.)*.5+.5)) + vec3(1.,.75,0.)*mix(n.z,n.y,sin(smoothstep(0.,1.,t)*PI*2.)*.5+.5), 1);
}`];
function setup() {
res = Math.min(windowWidth, windowHeight);
colorMode(RGB, 12, 100, 5, 1);
createCanvas(res, res, WEBGL);
sh = new p5.Shader(undefined, src);
}
function windowResized() {
res = min(windowWidth, windowHeight);
resizeCanvas(res, res);
}