Created
May 16, 2020 16:27
-
-
Save CharStiles/aa187e08448252e002f8df2c378830a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define some constants | |
const int steps = 64; // This is the maximum amount a ray can march. | |
const float smallNumber = 0.001; | |
const float maxDist = 10.; // This is the maximum distance a ray can travel. | |
// smoothly interpolate between two fuctions with k that controls the radious/distance of the smoothness. | |
// read more here about smin: https://iquilezles.org/www/articles/smin/smin.htm | |
float smin( float a, float b, float k ) | |
{ | |
float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); | |
return mix( b, a, h ) - k*h*(1.0-h); | |
} | |
// Box: correct distance to corners | |
float fBox(vec3 p, vec3 b) { | |
vec3 d = abs(p) - b; | |
vec3 toMax = min(d, vec3(0)); | |
return length(max(d, vec3(0))) + max(max(toMax.x,toMax.y), toMax.z); | |
} | |
// Repeat around the origin by a fixed angle. | |
// For easier use, num of repetitions is use to specify the angle. | |
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 pR(inout vec2 p, float a) { | |
p = cos(a)*p + sin(a)*vec2(p.y, -p.x); | |
} | |
// Intersection has to deal with what is normally the inside of the resulting object | |
// when using union, which we normally don't care about too much. Thus, intersection | |
// implementations sometimes differ from union implementations. | |
float fOpIntersectionChamfer(float a, float b, float r) { | |
return max(max(a, b), (a + r + b)*sqrt(0.5)); | |
} | |
// Difference can be built from Intersection or Union: | |
float dif (float a, float b, float r) { | |
return fOpIntersectionChamfer(a, -b, r); | |
} | |
float scene(vec3 position){ | |
vec3 pos = vec3(position.xy,0.); | |
pR(pos.xy,time); // rotate the box | |
float b = fBox(pos, vec3(0.2) ); | |
float m = 0.2; | |
float c = 0.9; | |
float b2 = fBox(pos, vec3(m,m,c) ); | |
b = dif(b,b2,0.001); | |
b2 = fBox(pos, vec3(m,c, m) ); | |
b = b; | |
// here im using position instead of pos which is wy the floor itsnt roatating | |
float ground2 = position.y + sin(position.x * 10.) / 10. * pos.x | |
+ cos(position.z * 10.) / 10. * pos.y + 1.; | |
position.y = 1. - position.y; | |
float ground = position.y + sin(position.x * 10.) / 10. * pos.y | |
+ cos(position.z * 10.) / 10.*pos.x ; | |
ground = smin(ground2,ground,0.1); // change this last number to 3.5 see what it does | |
// We want to return whichever one is closest to the ray, so we return the | |
// minimum distance. | |
return min(b,ground); | |
} | |
vec4 trace (vec3 origin, vec3 direction){ | |
float dist = 0.; | |
float totalDistance = 0.; | |
vec3 positionOnRay = origin; | |
for(int i = 0 ; i < steps; i++){ | |
dist = scene(positionOnRay); | |
// Advance along the ray trajectory the amount that we know the ray | |
// can travel without going through an object. | |
positionOnRay += dist * direction; | |
// Total distance is keeping track of how much the ray has traveled | |
// thus far. | |
totalDistance += dist; | |
// If we hit an object or are close enough to an object, | |
if (dist < smallNumber){ | |
// return the distance the ray had to travel normalized so be white | |
// at the front and black in the back. | |
return 1. - (vec4(totalDistance) / maxDist) / pow((float(i)/float(steps)),(sin(time/10.) + 1.9)/2.); | |
} | |
if (totalDistance > maxDist){ | |
return vec4(0.); // Background color. | |
} | |
} | |
return vec4(0.);// Background color. | |
} | |
void main() { | |
vec2 pos = uv(); | |
vec3 camOrigin = vec3(0,0,time/3.); | |
vec3 rayOrigin = vec3(pos + camOrigin.xy, camOrigin.z + 1.); | |
vec3 dir = rayOrigin - camOrigin ; | |
vec4 color = vec4(trace(rayOrigin,dir)); | |
if(color.x <0.5){ // if the pixel is dark get the previous frame pixels | |
float scaleTrailMove = 0.004; | |
// play around with this number see what it does | |
color = texture2D(backbuffer,uvN() + | |
(vec2(scaleTrailMove) * | |
vec2( | |
snoise(vec2(time/20.)) * 0.2, | |
// moves around the trails left & right a lil | |
noise(vec2(time/20. + 593.)) | |
// i want the trails to only move down some random amount | |
// thats why i use noise instead of snoise (signed noise) | |
// the vlaue is always positive | |
// here down is adding a positive value | |
) | |
)) * 0.989; // change this number to something in [0.0-1.0] inclusive ;) | |
} | |
gl_FragColor = clamp(color,0.,0.75); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment