The Code Therapy

Easy Peasy RayMarcheasy

An easy to use basic RayMarcher for all the family to enjoy!

Created by mgshadermaster on Wed, 27 Dec 2023 21:26:22 GMT.


#version 300 es
precision highp float;


// Easy Peasy RayMarcheasy
// ====================
// |     Made By:     |
// |  MgShaderMaster  |
// |       :p         |
// ====================

uniform sampler2D prgm1Texture;
uniform sampler2D prgm2Texture;
uniform vec2 resolution;
uniform float time;
out vec4 fragColor;

const int marchIterations = 60;

// Time functions

float oscillate(float start, float end, float t, float ts){
  return (end - start) * 0.5 + start + sin(t*ts) * (end - start) * 0.5;
}

// Misc functions

mat2 rotate(float angle){
  return mat2(
    cos(angle), -sin(angle),
    sin(angle), cos(angle)
  );
}

float smin( float a, float b, float k )
{
    float res = exp2( -k*a ) + exp2( -k*b );
    return -log2( res )/k;
}

// SD functions

float sdBox(vec3 p, vec3 b){
  vec3 q = abs(p) - b;
  return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}


float sdSphere(vec3 position, float radius, vec3 displacement){
  return length(position - displacement) - radius;
}

// Scene Mapper

float mapper(vec3 rayPosition){

  vec3 spherePosition = rayPosition;
  //spherePosition.z += time;
  spherePosition.xy *= rotate(time*0.4);

  vec3 sphere1Position = spherePosition + vec3(cos(time*0.2), 0.5, 0.0); //+ vec3(0.0 + sin(time) * 1.0, 0.0 + sin(time), 0.0);
  vec3 sphere2Position = spherePosition + vec3(-cos(time*0.4), -0.5, 0.0); //+ vec3(0.0 + cos(time * 1.5) * 1.0, 0.0 - sin(time), 0.0);
  sphere1Position = fract(sphere1Position) - 0.5;
  sphere2Position = fract(sphere2Position) - 0.5;
  float sphere1 = sdSphere(sphere1Position , 0.3, vec3(0.0,0.0, 0.0));
  float sphere2 = sdSphere(sphere2Position , 0.3, vec3(0.0,0.0, 0.0));

  //vec3 cuboidPosition = rayPosition;
  //cuboidPosition = fract(cuboidPosition) - 0.5;
  //float cuboid = sdBox(cuboidPosition, vec3(0.2, 0.2, 0.2));
  return smin(sphere1, sphere2, 16.0);
}

vec3 normalMapper(vec3 rayPosition){
  float rayDistance = mapper(rayPosition);
  vec2 displacement = vec2(0.01,0.0);
  vec3 rayNormal = rayDistance - vec3(
    mapper(rayPosition - displacement.xyy),
    mapper(rayPosition - displacement.yxy),
    mapper(rayPosition - displacement.yyx)
  );
  return normalize(rayNormal) * 0.5 + 0.5;
}

//Raymarcher

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 nUv = (gl_FragCoord.xy * 2.0 - resolution.xy) / resolution.y;
  vec3 color = vec3(0.0,0.0,0.0);

  //RayMarcher
  vec3 rayOrigin = vec3(0.0,0.0,-1.0);
  //rayOrigin.xz *= rotate(time);

  vec3 rayDirection = normalize(vec3(nUv.x, nUv.y, 1.0));

  float rayTravelled = 0.0;
  vec3 surfaceNormal = vec3(0.0);
  for(int marchNum = 0; marchNum < marchIterations; marchNum++){ // Far Limits based on Iterations
    vec3 rayPosition = rayOrigin + rayDirection * rayTravelled;
    float rayDistance = mapper(rayPosition);
    rayTravelled += rayDistance;
    if (rayDistance < 0.001) {
      surfaceNormal = normalMapper(rayOrigin + rayDirection * rayTravelled);
      break;
    }
    if (rayTravelled > 1000.0 || marchNum == marchIterations - 1){ // Close or Far limits
      rayTravelled = 0.0;
      break;
    }
  }


vec4 texA = texture(prgm1Texture, surfaceNormal.xy) * vec4(rayTravelled);
vec4 texB = texture(prgm2Texture, surfaceNormal.xy) * vec4(rayTravelled);
float osc = oscillate(-2.0, 2.0, time, 0.25);
float m = clamp(osc, 0.0, 1.0);
vec4 tex = mix(texA, texB, osc);
fragColor = vec4(tex.rgb, 1.0);

}

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform float time;
out vec4 fragColor;

vec3 palette(float t){
    vec3 a = vec3(0.168, 0.5, 1.108);
    vec3 b = vec3(-0.082, 0.5, 0.528);
    vec3 c = vec3(3.138, 1.998, 0.0);
    vec3 d = vec3(0.0, 0.333, 0.528);

    return a + b*cos(6.28318*(c*t+d));
}

void main(void){
    vec2 uv = gl_FragCoord.xy / resolution.xy * 2.0 - 1.0;
    vec2 uv0 = uv;
    vec3 finalColor = vec3(0.0);
    for (float i = 0.0; i < 1.0; i += 1.0){
        uv *= 1.6;
        uv = fract(uv);
        uv -= 0.5;

        float d0 = length(uv0);
        float d = length(uv) * exp(-length(uv0));


        vec3 col = palette(d0 + i*0.2 + time*0.2);

        d = sin(d*8.0 + time)/8.0;
        d = abs(d);

        d = pow(0.0125 / d, 1.7);

        finalColor += col * d;
    }
    fragColor = vec4(finalColor,1.0);
}

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform float time;
out vec4 fragColor;

vec3 palette(float t){
    vec3 a = vec3(0.168, 0.5, 1.108);
    vec3 b = vec3(-0.082, 0.5, 0.528);
    vec3 c = vec3(3.138, 1.998, 0.0);
    vec3 d = vec3(0.0, 0.333, 0.528);

    return a + b*cos(6.28318*(c*t+d));
}

void main(void){
    vec2 uv = gl_FragCoord.xy / resolution.xy * 2.0 - 1.0;
    vec2 uv0 = uv;
    vec3 finalColor = vec3(0.0);
    for (float i = 0.0; i < 3.0; i += 1.0){
        uv *= 1.6;
        uv = fract(uv);
        uv -= 0.5;

        float d0 = length(uv0);
        float d = length(uv) * exp(-length(uv0));


        vec3 col = palette(d0 + i*0.2 + time*0.2);

        d = sin(d*8.0 + time)/8.0;
        d = abs(d);

        d = pow(0.0125 / d, 1.7);

        finalColor += col * d;
    }
    fragColor = vec4(finalColor,0.0);
}