Skip to content

Instantly share code, notes, and snippets.

@tynrare
Last active January 5, 2024 07:16
Show Gist options
  • Save tynrare/575ca9062042fc42767e44706476bfc6 to your computer and use it in GitHub Desktop.
Save tynrare/575ca9062042fc42767e44706476bfc6 to your computer and use it in GitHub Desktop.
stream-dump
@tynrare
Copy link
Author

tynrare commented Jan 5, 2024

https://www.shadertoy.com/view/MfX3WH

// Scene distance
float map(vec3 p) {
    return length(p) - 1.; // distance to a sphere of radius 1
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
    vec2 uv = (fragCoord * 2. - iResolution.xy) / iResolution.y;

    // Initialization
    vec3 ro = vec3(0, 0, -3);         // ray origin
    vec3 rd = normalize(vec3(uv, 1)); // ray direction
    vec3 col = vec3(0);               // final pixel color

    float t = 0.; // total distance travelled

    // Raymarching
    for (int i = 0; i < 80; i++) {
        vec3 p = ro + rd * t;     // position along the ray

        float d = map(p);         // current distance to the scene

        t += d;                   // "march" the ray

        if (d < .001) break;      // early stop if close enough
        if (t > 100.) break;      // early stop if too far
    }

    // Coloring
    col = vec3(t * .2);           // color based on distance

    fragColor = vec4(col, 1);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment