Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created September 21, 2017 17:54
Show Gist options
  • Save MagnusThor/27d63a457c0d95e892ccb0f3d2f3cc39 to your computer and use it in GitHub Desktop.
Save MagnusThor/27d63a457c0d95e892ccb0f3d2f3cc39 to your computer and use it in GitHub Desktop.
#ifdef GL_ES
precision mediump float;
#endif
#extension GL_OES_standard_derivatives : enable
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D textureA; //<-- eye
uniform sampler2D textureB; //<-landscape texture
uniform sampler2D textureC; //<-heightmap
uniform sampler2D textureD;
uniform sampler2D fft;
float freqs[4];
#define MaxSteps 30
#define MinimumDistance 0.001
#define normalDistance 0.0005
#define Iterations 7
#define PI 3.141592
#define Scale 3.0
#define FieldOfView 1.0
#define Jitter 0.05
#define FudgeFactor 0.6
#define Ambient 0.32184
#define Diffuse 0.5
#define LightDir vec3(1.0)
#define LightColor vec3(1.0,1.0,0.858824)
#define LightDir2 vec3(1.0,-1.0,1.0)
#define LightColor2 vec3(0.0,0.333333,1.0)
#define Offset vec3(0.92858,0.92858,0.32858)
vec4 orb;
vec2 rotate(vec2 v, float a) {
return vec2(cos(a)*v.x + sin(a)*v.y, -sin(a)*v.x + cos(a)*v.y);
}
float trap(vec3 p){
return length(p.x-0.5-0.5*sin(time/10.0)); // <- cube forms
}
// Kalidoscope
float kalidoscope(in vec3 z)
{
vec3 offset = vec3(1.0+0.2*(cos(time/5.7)),0.3+0.1*(cos(time/1.7)),1.).xzy;
z = abs(1.0-mod(z,2.0));
float d = 1000.0;
float r;
for (int n = 0; n < Iterations; n++) {
z.xz = rotate(z.xz, time/37.0);
if (z.x+z.y<0.0) z.xy = -z.yx;
z = abs(z);
if (z.x+z.z<0.0) z.xz = -z.zx;
z = abs(z);
if (z.x-z.y<0.0) z.xy = z.yx;
z = abs(z);
if (z.x-z.z<0.0) z.xz = z.zx;
z = z*Scale - offset*(Scale-1.0);
//z.yz = rotate(z.yz -time/18.0);
d = min(d, trap(z) * pow(Scale, -float(n+1)));
}
return d;
}
float kaliSet( vec3 p )
{
p = abs(1.0-mod(p,2.0));
float scale = 1.0;
orb = vec4(1000.0);
for( int i=0; i<8;i++ )
{
p = -1.0 + 2.0*fract(0.5*p+0.5);
float r2 = dot(p,p);
orb = min( orb, vec4(abs(p),r2) );
float k = 1.0/r2;
p *= k;
scale *= k;
}
return 0.25*abs(p.y) / scale;
}
float mengerSponge(in vec3 z)
{
vec3 op = z;
z = abs(1.0-mod(z,2.));
float d = 1000.0;
for (int n = 0; n < 7; n++) {
op = -1.0 + 2.0*fract(0.5*op+0.5);
float r2 = dot(op,op);
//z.xy = rotate(z.xy,4.0+2.0*cos( time/16.0));
z = abs(z);
if (z.x<z.y){ z.xy = z.yx;}
if (z.x< z.z){ z.xz = z.zx;}
if (z.y<z.z){ z.yz = z.zy;}
z = 3.0*z-Offset*(3.0-1.0);
if( z.z<-0.5*Offset.z*(3.0-1.0)) z.z+=Offset.z*(3.0-1.0);
d = min(d, length(z) * pow(3.0, float(-n)-1.0));
}
return d-0.0001;
}
const float detail=.00002;
float ot=0.;
float det=0.;
vec3 lightdir=normalize(vec3(0.,-0.3,-1.));
float map(vec3 p){
return mengerSponge(p);
}
vec3 normal(in vec3 p) {
vec2 e = vec2(0.005, -0.005);
return normalize(e.xyy * map(p + e.xyy) + e.yyx * map(p + e.yyx) + e.yxy * map(p + e.yxy) + e.xxx * map(p + e.xxx));
}
float rand(vec2 co){
return fract(cos(dot(co,vec2(4.898,7.23))) * 23421.631);
}
vec3 getColor(vec3 normal, vec3 pos) {
return vec3(1.);
}
float kset(vec3 p) {
p=abs(.5-fract(p*20.));
float es, l=es=0.;
for (int i=0;i<13;i++) {
float pl=l;
l=length(p);
p=abs(p)/dot(p,p)-.5;
es+=exp(-1./abs(l-pl));
}
return es;
}
vec3 light(in vec3 p, in vec3 dir) {
vec3 n=normal(p);
float sh=1.;
float ao=1.;
float diff=max(0.,dot(lightdir,-n))*sh*1.3;
float amb=max(0.2,dot(dir,-n))*.4;
vec3 r = reflect(lightdir,n);
float spec=pow(max(0.,dot(dir,-r))*sh,10.)*(.5+ao*.5);
float k=kset(p)*.18;
vec3 col=mix(vec3(k*1.1,k*k*1.3,k*k*k),vec3(k),.45)*2.;
col=col*ao*(amb*vec3(.9,.85,1.)+diff*vec3(1.,.9,.9))+spec*vec3(1,.9,.5)*.7;
return col;
}
vec4 rayMarch(in vec3 from, in vec3 dir, in vec2 fragCoord) {
vec2 lig=vec2(sin(time*2.)*.6,cos(time)*.25-.25);
vec3 color = vec3(0);
float totalDistance = Jitter*rand(fragCoord.xy+vec2(time));
vec3 dir2 = dir;
float d;
int steps = 0;
vec3 pos;
for (int i=0; i < MaxSteps; i++) {
pos = from + totalDistance * dir;
d = map(pos);
totalDistance += d;
if (d < MinimumDistance) break;
steps = i;
}
float l=pow(max(0.,dot(normalize(-dir),normalize(lightdir))),10.);
vec3 backg=vec3(.8,.85,1.)*.25*(2.-l)+vec3(1.,.9,.65)*l*.4;
color=light(pos-det*dir*1.5, dir);
color*=min(1.2,.5+totalDistance*totalDistance*1.5);
color+=vec3(1,.8,.6)*pow(l,3.)*.5;
return vec4(color ,1.0);
}
vec4 postProcess(vec4 tot){
return tot;
}
void main( void ) {
vec2 uv = (gl_FragCoord.xy / resolution.xy) ;
float t = time ;
vec3 camPos = .1*t*vec3(0.0,0.,0.5);
vec3 target = camPos + vec3(1.,0.0*cos(t),0.);
vec3 camUp = vec3(1.0,0,1.);
vec3 camDir = normalize(target-camPos);
camUp = normalize(camUp-dot(camDir,camUp)*camDir);
vec3 camRight = normalize(cross(camDir,camUp));
vec2 coord = -1.+2.*gl_FragCoord.xy/resolution.xy;
coord.x *= resolution.x/resolution.y;
vec3 rd = normalize(camDir + (coord.x*camRight + coord.y*camUp)*FieldOfView);
vec4 tot = postProcess(rayMarch(camPos, rd, coord ));
gl_FragColor = tot * 0.5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment