Skip to content

Instantly share code, notes, and snippets.

@Chovin
Created October 30, 2017 03:30
Show Gist options
  • Save Chovin/b89bb1a10e0ab6409906a065736872fc to your computer and use it in GitHub Desktop.
Save Chovin/b89bb1a10e0ab6409906a065736872fc to your computer and use it in GitHub Desktop.
pretty GLSL using The_Force
// The Force and
vec3 roty(vec3 pt, float amt) {
//vec2 xz = rotate(vec2(pt.x, pt.z),vec2(0,0), amt);
//return vec3(xz.x, pt.y, xz.y);
float angle = atan(pt.x, pt.z)+amt;
float len = length(vec2(pt.x, pt.z));
return vec3(sin(angle)*len, pt.y, cos(angle)*len);
}
vec3 rotz(vec3 pt, float amt) {
//vec2 xz = rotate(vec2(pt.x, pt.z),vec2(0,0), amt);
//return vec3(xz.x, pt.y, xz.y);
float angle = atan(pt.x, pt.y)+amt;
float len = length(vec2(pt.x, pt.y));
return vec3(sin(angle)*len, cos(angle)*len, pt.z);
}
vec3 rotx(vec3 pt, float amt) {
//vec2 xz = rotate(vec2(pt.x, pt.z),vec2(0,0), amt);
//return vec3(xz.x, pt.y, xz.y);
float angle = atan(pt.y, pt.z)+amt;
float len = length(vec2(pt.y, pt.z));
return vec3(pt.x, sin(angle)*len, cos(angle)*len);
}
vec3 rot3d(vec3 pt, vec3 rotv) {
return rotz(roty(rotx(pt,rotv.x),rotv.y),rotv.z);
}
float area(vec2 v0, vec2 v1, vec2 v2, vec2 v3) { //quad
return (v2.x - v0.x) * (v3.y - v1.y) - (v3.x - v1.x) * (v2.y - v0.y);
}
float area(vec2 v0, vec2 v1, vec2 v2) { //triangle
return abs((v1.x - v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y));
}
vec3 project(vec3 pt) { // something is wrong with my projection
return pt; //vec3(pt.x+sign(pt.x)*(pt.z/8.), pt.y+sign(pt.y)*(pt.z/8.), pt.z);
}
bool in_face(vec2 pt, vec3 tl, vec3 tr, vec3 br, vec3 bl, float tolerance) {
float quad = area(vec2(tl), vec2(tr), vec2(br), vec2(bl));
return abs(abs(quad) -
(area(pt, vec2(tl), vec2(tr)) + area(pt, vec2(tr), vec2(br)) +
area(pt, vec2(br), vec2(bl)) + area(pt, vec2(bl), vec2(tl)))) <= clamp(tolerance,0.,.0000007)
&& sign(quad) < 0.; //culling
}
bool in_pt(vec2 pt, vec3 bnd) {
return int(pt.x*20.) == int(bnd.x*20.) && int(pt.y*20.) == int(bnd.y*20.);
}
float getz(vec2 xy, vec3 tl, vec3 tr, vec3 br, vec3 bl) {
vec3 v1 = tr-tl; vec3 v2 = bl-tl;
vec3 crs = cross(v1, v2);
return (1./crs.z) * (crs.x * tl.x + crs.y * tl.y + crs.z * tl.z - crs.x * xy.x - crs.y - xy.y);
//float mx = (tr.z - tl.z)/(tr.x - tl.x);
//float my = (bl.z - tl.z)/(bl.y - tl.y);
//return ((xy.x*mx + tl.z)+(xy.y*my + tl.z))/2.;
}
void main () {
vec2 sto = uv();
vec2 st = uv() + vec2(bands.w-bands.x,bands.x-bands.y), stN = uvN(); vec3 c = black;
vec2 st2 = uv() + vec2(bands.x-bands.w,bands.y-bands.x);
vec2 trot = rotate(st, vec2(0,0), (time - bands.x)/5.);
float theta = atan(trot.x, trot.y) / PI2; float phi = log(length(st));
vec2 trot2 = rotate(st, vec2(0,0), (time - bands.x)/5.);
float theta2 = atan(trot2.x, trot2.y) / PI2;
float phi2 = log(length(st2)*sin(time/2.+bands.y)-.5);
float phi3 = log(length(st)*sin(time/2. +bands.w + PI2/3.) -.5);
//theta = rotate(theta, vec2(0,0), time/30. - bands.x);
float k = 1. * bands.y + 2.; float a = mod(theta, PI2/k); a = abs(a - PI2/k/2.);
c += sin(time)*red;
c += sin(time+PI2/3.)*blue;
c += sin(time+PI2*(2./3.))*green;
c *= bands.x;
float tspd = 4.;
bool fast = mod(time, tspd)>tspd/2.;
float spd = 1.;
if (fast) {
spd = 4.;
}
c += phi3*voronoi(vec2(st.y + bands.w + phi3, phi- time*spd));
//c += phi*voronoi(vec2(st2.x + bands.y + time, phi- time*spd));
c -= theta/phi3 * (sin(time)*blue + sin(time+PI2/3.)*red);
c += phi2*voronoi(vec2(st2.y + bands.w + phi2, phi- time*spd));
c -= theta2/phi2 * sin(lime);
c /= theta*blue;
vec3 cc = vec3(sin(time/2.)/2.,sin(time/3.)/2.,sin(time/5.2)/2.);
vec3 light = vec3(0,0,1);
float rad = .5 + bands.x/2.;
for (int i = 0; i < 5; i++) {
cc = rot3d(cc, vec3(time));
float dst = distance(vec2(cc), sto);
if (dst > rad) {
c = white;
float angle = asin(dst/rad);
float z = cos(angle);
//c += dot(vec3(sto, z) - cc, light) * 2.;
c = red*20.;
c /= voronoi(vec2(st*20.)+vec2(bands))*red;
c *= voronoi(vec2(st2))*(sin(time)*red+sin(time + PI2*1./3.)*blue + sin(time + PI2*2./3.)*green);
}
else {
}
}
gl_FragColor = vec4(c/20., 0.5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment