Created
January 14, 2016 15:10
-
-
Save nnevatie/347fb62dedbff6363d01 to your computer and use it in GitHub Desktop.
Backdrop Fragment Shader
This file contains 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
#version 150 | |
// Const | |
const float PI = 3.14159265358979323846; | |
// Uniforms | |
uniform sampler2D tex; // Skysphere texture | |
uniform mat4 v; // View matrix | |
uniform vec3 pos; // Camera position | |
uniform vec4 gridColor; // Grid color | |
// Input | |
in Block | |
{ | |
noperspective vec3 viewRay; | |
} | |
ib; | |
// Output | |
out vec4 color; | |
// Grid distance to XZ-plane positioned at [0, 0, 0] | |
float gridDistance(vec3 origin, vec3 dir) | |
{ | |
return length(dir * origin.y / dir.y); | |
} | |
// Grid color | |
// Adjust constants to your liking (line width, etc.) | |
vec4 grid(vec2 p, vec4 color) | |
{ | |
float frq = PI / 8.0; | |
return color * smoothstep(.98, 1.2, | |
max(1.05 * sin((p.x + 4.0) * frq), 1.05 * sin((p.y + 4.0) * frq))); | |
} | |
// Sphere color | |
vec4 sphere(vec3 p, sampler2D tex) | |
{ | |
vec2 uv = vec2(0.5 - atan(p.z, p.x) / (2 * PI), 0.5 - asin(p.y) / PI); | |
return texture(tex, uv); | |
} | |
void main(void) | |
{ | |
// Ray pointing into the scene | |
vec3 ray = normalize(ib.viewRay) * mat3(v); | |
// Grid distance (intersection distance to plane) | |
float gd = gridDistance(pos, ray); | |
// Final color (sphere color + grid color with distance falloff) | |
color = sphere(ray, tex) + | |
max(0, 1 - gd * 0.0015) * grid((pos + gd * ray).xz, gridColor); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment