Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Created December 19, 2018 06:20
Show Gist options
  • Save CharStiles/ef2da76755816b8ec54f5b3b8daca719 to your computer and use it in GitHub Desktop.
Save CharStiles/ef2da76755816b8ec54f5b3b8daca719 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 shared1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D prevFrame;
uniform sampler2D prevPass;
in VertexData
{
vec4 v_position;
vec3 v_normal;
vec2 v_texcoord;
} inData;
out vec4 fragColor;
const int MAX_ITERATIONS = 1;
const vec4 background = vec4(0,1,0,1);
vec4 getColor (vec3 pos){
vec4 norm = vec4(pos,1);
float cirScale = 0;
vec3 normPos = pos+ 1.3;
normPos.x += cos(time) * cirScale;
normPos.y += sin(time) * cirScale;
normPos.x -=0.4;
normPos.y += 0.522;
normPos /= 2;
vec4 texCol = texture(shared1, (normPos.xy -.1 )* (normPos.z ));
texCol.y += cos(time);
return mix(texCol ,1-norm, 0);
}
float sphere(vec3 pos, float rad){
vec4 n = getColor(pos);
float nn = (n.r + n.g +n.b)/3.;
return length(pos) - (rad + (nn* 0.9));
}
float scene(vec3 pos){
return sphere(pos,0.4);
}
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));
}
vec3 rotatex(in vec3 p, float ang)
{
return vec3(p.x,p.y*cos(ang)-p.z*sin(ang),p.y*sin(ang)+p.z*cos(ang));
}
float rm2(in vec3 ro, in vec3 rd)
{
vec3 pos = ro;
float dist = 1.0;
float d;
for (int i = 0; i < 5; i++) {
d = scene(pos);
pos += rd*d;
dist -= d;
}
return dist;
}
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);
return vec4((5*n.g)+3) * 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) * 0.5;
place.z += distToScene;
if (distToScene < .01){
return getMaterial(place,place.z);
}
}
return texture(prevFrame,uv);
}
void main(void)
{
vec2 uv = -1. + (2. * inData.v_texcoord);
uv.x *= (resolution.x/resolution.y);
fragColor = trace(uv, 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