Created
November 26, 2014 15:24
-
-
Save fjolnir/63a2af853d346698a3fa 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
// Ray intersection point = ro + rd*t | t > 0 | |
float sphere(vec3 center, float r, vec3 ro, vec3 rd) | |
{ | |
vec3 ray = ro - center; | |
float c = dot(ray, ray) - (r*r); | |
float b = dot(rd, ray); | |
float d = b*b - c; | |
float t = -b - sqrt(abs(d)); | |
float st = step(0.0, min(t, d)); | |
return mix(-1.0, t, st); | |
} | |
void main() | |
{ | |
vec2 uv = (-1.0 + 2.0*gl_FragCoord.xy / iResolution.xy) * vec2(iResolution.x/iResolution.y, 1.0); | |
vec3 ro = vec3(0.0, 0.0, -6.0); | |
vec3 rd = normalize(vec3(uv, 1.0)); | |
vec3 sphereCenter = vec3(sin(iGlobalTime)*2.0, 0.0, cos(iGlobalTime)*2.0 + 4.0); | |
float sphereRadius = 3.0; | |
// Reflect a ray off the sphere | |
float t = sphere(sphereCenter, sphereRadius, ro, rd); | |
vec3 nml = normalize(sphereCenter - (ro+rd*t)); | |
rd = reflect(rd, nml); | |
gl_FragColor.xyz = mix(vec3(0), rd, step(0.0, t)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment