Skip to content

Instantly share code, notes, and snippets.

@solsarratea
Last active August 1, 2020 22:57
Show Gist options
  • Save solsarratea/83d3b4feaac0681b5879ac7bf72cfde9 to your computer and use it in GitHub Desktop.
Save solsarratea/83d3b4feaac0681b5879ac7bf72cfde9 to your computer and use it in GitHub Desktop.
vec3 cosPalette(float t){
vec3 a = vec3(0.6,0.5,0.25);
vec3 b = vec3(0.5,0.5,0.4);
vec3 c = vec3(.2,.3,1.);
vec3 d = vec3(.2,0.3,0.32);
return a + b*cos( 6.28318*(c*t+d));
}
void pMod3(inout vec3 p, vec3 size) {
vec3 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5, size) - size*0.5;
}
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;
}
float sdBox( vec3 p, vec3 b, float r )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) -r;
}
const int MAX_STEPS = 12;
const int Iterations = 9;
const float Phi = 1.618;
float Scale =4.3;
const float Bailout =5.2;
vec4 orbitTrap = vec4(100.0);
float smallNumber = 0.001;
float maxDistance = 10.;
vec3 n1 = normalize(vec3(-1.,Phi-1.0,1.0/(Phi)));
vec3 n2 = normalize(vec3(Phi-1.0,1.0/(Phi-1.0),-1.0));
vec3 n3 = normalize(vec3(1.0/(Phi-1.0),-1.0,Phi-1.0));
vec3 Rot1 = vec3(1.);
float Angle1 = 3.14 +time;
vec3 Rot2 = vec3(1.,0.,-1.);
float Angle2 = 1. ;//tan(time*0.2);
mat3 rotationMatrix3(vec3 v, float angle)
{
float c = cos(radians(angle));
float s = sin(radians(angle));
return mat3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y,
(1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x,
(1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z
);
}
float DE(vec3 z)
{
mat3 fracRotation2 = rotationMatrix3(normalize(Rot2), Angle2);
mat3 fracRotation1 = rotationMatrix3(normalize(Rot1), Angle1);
float r;
float bailout2 = pow(2., Bailout);
vec3 offset = vec3(2.5);
// Prefolds.
float t;
// Iterate to compute the distance estimator.
int m = 0;
for( int n = 0; n < Iterations; n++) {
z *= fracRotation1;
for (int j= 0; j<4; j++){
z-= 2.0 * min(0.0, dot(z, n1)) * n1;
z-= 2.0 * min(0.0, dot(z, n2)) * n2;
z-= 2.0 * min(0.0, dot(z, n3)) * n3;
}
z = z*Scale - offset*(Scale-1.0);
z *= fracRotation2;
r = dot(z, z);
z +=0.1;
orbitTrap = min(orbitTrap, abs(vec4(0.0,0.0,0.0,r)));
if (r > bailout2) break;
m = n;
}
return (sdBox(z,vec3(3.),0.)*0.1) * pow(Scale, float(-m-1));
}
float scene(vec3 pos){
float dF = pos.y - sin(pos.x*12.+time/5.) +2.;
// pMod3(pos,vec3(0.,0.,4.));
// float dB = sdBox(pos, vec3(3.),0.01);
// return dB;
return DE(pos);
}
vec3 estimateNormal(vec3 p) {
vec2 e = vec2 (0.002, 0.);
vec3 n = scene(p)- vec3(
scene(p-e.xyy),
scene(p-e.yxy),
scene(p-e.yyx));
return normalize(n);
}
vec4 lighting(vec3 pos, vec3 viewDir){
vec3 lightPos = vec3(0.04*cos(time*.1),0.3,0.2);
vec3 normal = estimateNormal(pos);
vec3 reflectDir = reflect(-lightPos, normal);
float specularStrength =2.;
vec3 specColor = hsv2rgb(cosPalette(normal.y))+green*pos.z;
float spec = pow( max(dot(viewDir, reflectDir), -0.20), 2.2);
vec3 specular = specularStrength * spec * specColor;
return vec4(specular,1.);
}
vec4 lighting2(vec3 pos, vec3 viewDir){
vec3 lightPos = vec3(0.,222.,10);
vec3 normal = estimateNormal(pos);
vec3 reflectDir = reflect(-lightPos, normal);
float specularStrength =10.8;
vec3 specColor = hsv2rgb(cosPalette(normal.z));
float spec = pow( max(dot(viewDir, reflectDir), -2.20), .4);
vec3 specular = specularStrength * spec * specColor;
return vec4(specular,1.);
}
const int max_it = 80;
float maxD=2.;
float eps = 0.001;
float trace(vec3 origin, vec3 dir){
float d0 = 0.;
for(int i =0; i < max_it; i++){
vec3 pos = origin + dir *d0;
float d = scene(pos);
d0 +=d;
if (d0 > maxD) return 0.;
if (d < eps) break;
}
return d0;
}
void main() {
vec2 pos = uv();
float cs = cos( time*0.0175 ), si = sin( time*0.1375 );
vec3 origin = vec3(1.2,2.,1.5);
float fish = (1.-dot(pos, pos)*.5)*.5;
vec3 dir = normalize(vec3(pos,fish));
dir.xz = mat2(cs, si,-si, cs)*dir.xz;
dir.xy = mat2(cs, si,-si, cs)*dir.xy;
vec4 color;
float tr = trace(origin,dir);
if (tr>0.){
vec3 surfP = origin + tr * dir;
color = mix(lighting(surfP,dir), lighting2(surfP, dir),0.011);
}
gl_FragColor = color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment