Created
July 28, 2017 11:53
-
-
Save eliemichel/491aa871469bed97cb99da2bfa8acb2f to your computer and use it in GitHub Desktop.
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
struct Ray { | |
vec3 origin; | |
vec3 direction; | |
}; | |
bool intersectRaySphere(out vec3 intersection, in Ray ray, in vec3 center, in float radius) { | |
vec3 o = center - ray.origin; | |
float d2 = dot(ray.direction, ray.direction); | |
float r2 = radius * radius; | |
float l2 = dot(o, o); | |
float dp = dot(ray.direction, o); | |
float delta = dp * dp / d2 - l2 + r2; | |
if (delta < 0.0) { | |
return false; | |
} | |
float lambda = dp / d2 - sqrt(delta / d2); | |
intersection = ray.origin + lambda * ray.direction; | |
return true; | |
} | |
void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
{ | |
vec2 uv = fragCoord.xy / iResolution.xy; | |
fragColor = vec4(uv,0.5+0.5*sin(iTime),1.0); | |
vec2 f = 0.1 * iResolution.xx/iResolution.xy; | |
mat3 m = mat3( | |
1.0, 0.0, 0.0, | |
0.0, 0.0, 1.0, | |
0.0, 1.0, 0.0 | |
); | |
Ray ray = Ray( | |
vec3(0.0, -0.6, 0.0), | |
m * vec3((uv*2.0-1.0) / f, -1.0) | |
); | |
vec3 intersection; | |
if (intersectRaySphere(intersection, ray, vec3(0.0), 0.5)) { | |
fragColor = vec4(0.0); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment