Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Created September 18, 2018 01:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharStiles/47328642cac9fc423c159f9491516b7d to your computer and use it in GitHub Desktop.
Save CharStiles/47328642cac9fc423c159f9491516b7d to your computer and use it in GitHub Desktop.
#version 150
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D prevFrame;
uniform sampler2D prevPass;
const int MAX_ITERATIONS = 64;
const vec4 background = vec4(0,1,0,1);
const float PI = 3.1415;
in VertexData
{
vec4 v_position;
vec3 v_normal;
vec2 v_texcoord;
} inData;
out vec4 fragColor;
vec4 getColor (vec3 pos){
vec4 norm = vec4(pos,1);
float cirScale = .2;
vec3 normPos =(pos+1)/2;
//normPos.x += 1+cos(time) * cirScale;
//normPos.y += sin(time) * cirScale;//
normPos *= (0.6 +spectrum.y* abs(2+sin(time/10)));
//normPos.x *= resolution.y/ resolution.x;
normPos.x +=0.;
normPos.y -= 0.332;
vec4 texCol = 1.5-texture(texture0, (1-normPos.xy));
texCol.g += abs(cos(time))-0.5;
// return mix(texCol ,norm, norm.zyxw);
return mix(texCol ,1-norm, 0.);
}
float pModPolar(inout vec2 p, float repetitions) {
float angle = 2*PI/repetitions;
float a = atan(p.y, p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = mod(a,angle) - angle/2.;
p = vec2(cos(a), sin(a))*r;
// For an odd number of repetitions, fix cell index of the cell in -x direction
// (cell index would be e.g. -5 and 5 in the two halves of the cell):
if (abs(c) >= (repetitions/2)) c = abs(c);
return c;
}
void pR45(inout vec2 p) {
p = (p + vec2(p.y, -p.x))*sqrt(0.5);
}
void pR(inout vec2 p, float a) {
p = cos(a)*p + sin(a)*vec2(p.y, -p.x);
}
vec3 pMod3(inout vec3 p, vec3 size) {
vec3 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5, size) - size*0.5;
return c;
}
float sphere(vec3 pos, float rad){
//vec4 n = vec4(0);//
vec4 n = getColor(pos);
float nn = (n.r + n.g +n.b)/3.;
//if (nn < 0.1){
//return 1;
//}
return length(pos) - (rad + (n.r*.3));
}
float fHexagonCircumcircle(vec3 p, vec2 h) {
vec3 q = abs(p);
h.xy /= getColor(p).xz;
h.yx -= getColor(p).zy ;
return max(q.y - h.y, max(q.x*sqrt(3)*0.5 + q.z*0.5, q.z) - h.x);
//this is mathematically equivalent to this line, but less efficient:
//return max(q.y - h.y, max(dot(vec2(cos(PI/3), sin(PI/3)), q.zx), q.z) - h.x);
}
float scene(vec3 pos){
//float s1 = min(sphere(mod(pos,4)-1,0.7), sphere(vec3(pos.x-.2,pos.y,pos.z),0.2));
//float s2 = sphere(vec3(pos.x-.2,mod(pos.y,5)-2.5,mod(pos.z,4) ),0.5);
vec2 h = vec2(.5,.1);
// pR(pos.yz,time * 1.2+(spectrum.x*0.5));
pR(pos.xy,time *0.3);
//pModPolar(pos.xy,8+(7* spectrum.y));
pMod3(pos,vec3(90));
//return fHexagonCircumcircle(pos, vec2(h.x*sqrt(3)*0.5, h.y));
return sphere(pos,abs(cos(time/2)));
//float c = 4;
//return sphere(mod(pos,c)-(c/2.),0.);
}
vec3 get_normal(in vec3 p)
{
vec3 eps = vec3(0.001, 0, 0);
float nx = scene(p + eps.xyy) - scene(p - eps.xyy);
float ny = scene(p + eps.yxy) - scene(p - eps.yxy);
float nz = scene(p + eps.yyx) - scene(p - eps.yyx);
return normalize(vec3(nx,ny,nz));
}
vec4 getMaterial(vec3 pos, float dist){
//vec3 rd = normalize(vec3(pos.x,pos.y,-2.0));
//rd = rotatex(rd, 1.2);
//vec3 l = normalize(vec3(1,1,1));
vec3 n = get_normal(pos);
// vec3 r = reflect(rd, n);
// float fres = clamp(dot(n, -rd),0.0, 1.0);
// float diff = clamp(dot(n, l), 0.0, 1.0);
// float spec = pow(clamp(dot(r, l), 0.0, 1.0), 40.0);
// float shade = rm2(pos+0.05*n, n);
// vec3 color = mix(vec3(1,1,1)*0.8, getColor(pos).xyz, fres);
// color += 0.1*vec3(1,1,1)*diff*pos.y;
// color += vec3(1,1,1)*spec;
// color += 0.1*shade;
// color /= dist;
return vec4(((n)),1);//* getColor(pos);
}
vec4 trace(vec2 uv, vec3 camPos, vec3 lookAt){
vec3 startingPoint = (vec3(uv,0) - camPos); // this is -1 to 1 where the sceen is in our scene
vec3 place = startingPoint; // place along ray
float distToScene = 0;
if (MAX_ITERATIONS > 100){
return background;
}
for (int i = 0 ; i < MAX_ITERATIONS; i ++){
distToScene = scene(place) ;
place.z += distToScene;
if (distToScene < .01){
return getMaterial(place,place.z);
}
}
return vec4(0);//texture(prevFrame,inData.v_texcoord);
}
void main(void)
{
vec2 uv = -1. + (2. * inData.v_texcoord);
uv.x *= (resolution.x/resolution.y);
fragColor = trace(uv.xy * vec2(1,1), vec3(0,0,-1), vec3(0,0,0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment