Skip to content

Instantly share code, notes, and snippets.

@gordonnl
Created October 10, 2016 23:02
Show Gist options
  • Select an option

  • Save gordonnl/a094476040e45c612194dba9cdac06de to your computer and use it in GitHub Desktop.

Select an option

Save gordonnl/a094476040e45c612194dba9cdac06de to your computer and use it in GitHub Desktop.

Revisions

  1. gordonnl created this gist Oct 10, 2016.
    72 changes: 72 additions & 0 deletions raytrace_background.fs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    // Author: Nathan Gordon
    // Title: Raytrace Background for http://patriciogonzalezvivo.github.io/glslEditor/

    #ifdef GL_ES
    precision mediump float;
    #endif

    uniform vec2 u_resolution;
    uniform vec2 u_mouse;
    uniform float u_time;

    mat3 rotationXY(vec2 angle) {
    float cp = cos(angle.x);
    float sp = sin(angle.x);
    float cy = cos(angle.y);
    float sy = sin(angle.y);

    return mat3(
    cy , 0.0, -sy,
    sy * sp, cp, cy * sp,
    sy * cp, -sp, cy * cp
    );
    }

    vec3 mixColor(vec3 normal, vec3 baseColor, vec3 addColor, vec3 position, float amount, float falloff) {

    // Compares the color's vector to the ray's vector and fades it out exponentially
    float strength = pow(max(0.0, dot(normal, normalize(position))), 2.0 * falloff);
    return mix(baseColor, addColor, strength * amount);
    }

    vec3 background(vec3 normal) {
    vec3 color = vec3(1.000,0.288,0.741) * 1.000;
    // EDIT BASE COLOUR HERE ^^^^

    vec3 color1 = vec3(0.418,1.000,0.461) * 1.024;
    vec3 color2 = vec3(0.966,1.000,0.258) * 1.000;
    // EDIT ADDED COLORS ^^^^^ AND STRENGTH ^^^^

    // Calculate colors' positions
    vec3 mixPos1 = vec3(0.000,1.000,-0.000);
    vec3 mixPos2 = vec3(0.000,-1.000,-0.00);
    // EDIT COLORS' POSITIONS ^^^^^^

    color = mixColor(normal, color, color1, mixPos1, 0.976, 0.376);
    color = mixColor(normal, color, color2, mixPos2, 1.000, 0.256);
    // EDIT COLORS' OPACITY AND FALLOFF HERE ^^^^^ ^^^^^

    return color;
    }

    void main() {
    vec2 uv = gl_FragCoord.xy / u_resolution.xy;
    uv.x *= u_resolution.x / u_resolution.y;

    float perspective = 1.352;
    // EDIT FOV HERE ^^^^^^

    vec2 setPosition = vec2(0.580,-0.210);
    // MOVE CAMERA HERE ^^^^^^^^^^^^

    vec4 direction = vec4(normalize(vec3(uv * perspective - vec2(perspective * 0.5), 1.0)), 1.0);
    mat3 rot = rotationXY(setPosition.yx * vec2(6.28));
    direction.xyz *= rot;
    vec4 sphere = vec4(background(-direction.xyz), 1.0);
    gl_FragColor = sphere;
    }