struct Ray { vec3 org, dir; float energy, ior; }; struct Material { vec3 diffuse, specular; float shin; float refl, refr; float ior; }; struct HitPoint { float dist; vec3 pos, norm; vec2 surfpos; Material mtl; float shadow; /* opacity along the line of sight up to dist=1 */ }; varying vec3 v_rorg, v_rdir; vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit); vec3 backdrop(in vec3 dir); bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit); bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit); bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit); bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit); vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos); const Material mtl_sph = Material(vec3(1.0, 1.0, 1.0) * 0.1, vec3(1.0, 1.0, 1.0), 80.0, 0.8, 0.0, 1.0); const Material mtl_glass = Material(vec3(1.0, 1.0, 1.0) * 0.01, vec3(1.0, 1.0, 1.0), 80.0, 0.0, 0.99, 1.52); const Material mtl_air = Material(vec3(1.0, 1.0, 1.0) * 0.01, vec3(1.0, 1.0, 1.0), 80.0, 0.0, 0.99, 1.0); const Material mtl_floor = Material(vec3(0.5, 0.5, 0.5), vec3(0.0, 0.0, 0.0), 1.0, 0.0, 0.0, 1.0); const vec3 light_pos = vec3(-10, 50, 30); bool cast_ray(in Ray ray, inout vec3 color, out HitPoint hit) { if(isect_scene(ray.org, ray.dir, hit)) { color += shade(ray.org, ray.dir, hit) * ray.energy; return true; } color += backdrop(ray.dir); return false; } #define MAX_LEVEL 8 struct StackFrame { int op; Ray ray; HitPoint hit; }; void main() { StackFrame stack[MAX_LEVEL]; vec3 color = vec3(0.0, 0.0, 0.0); stack[0].op = 0; stack[0].ray.org = v_rorg; stack[0].ray.dir = normalize(v_rdir); stack[0].ray.energy = stack[0].ray.ior = 1.0; int top = 0; while(top >= 0) { if(top >= MAX_LEVEL - 1) { color += backdrop(stack[top].ray.dir) * stack[top].ray.energy; top--; continue; } if(stack[top].ray.energy < 1e-3) { top--; continue; } if(!cast_ray(stack[top].ray, color, stack[top].hit)) { /* no hit, return */ top--; continue; } /* found a hit, recurse for reflection/refraction */ HitPoint hit = stack[top].hit; // 1.0 when entering, 0.0 when leaving float entering = step(0.0, dot(-stack[top].ray.dir, hit.norm)); vec3 norm = faceforward(hit.norm, stack[top].ray.dir, hit.norm); int op = stack[top].op++; if(op == 0) { // reflection float energy = stack[top].ray.energy * hit.mtl.refl; if(energy > 1e-4) { int next = top + 1; stack[next].op = 0; stack[next].ray.org = hit.pos + norm * 1e-5; stack[next].ray.dir = reflect(stack[top].ray.dir, norm); stack[next].ray.energy = energy; top = next; } } else if(op == 1) { // refraction float energy = stack[top].ray.energy * hit.mtl.refr; if(energy > 1e-4) { float next_ior = mix(stack[top - 1].ray.ior, hit.mtl.ior, entering); float ior = stack[top].ray.ior / next_ior; int next = top + 1; stack[next].op = 0; stack[next].ray.org = hit.pos - norm * 1e-5; stack[next].ray.dir = refract(stack[top].ray.dir, norm, ior); stack[next].ray.energy = energy; stack[next].ray.ior = next_ior; top = next; } } else if(op == 2) { // return top--; } } gl_FragColor = vec4(color, 1.0); } vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit) { HitPoint shadow_hit; vec3 norm = faceforward(hit.norm, rd, hit.norm); vec3 ldir = light_pos - hit.pos; vec3 amb = hit.mtl.diffuse * 0.02; isect_scene(hit.pos + norm * 0.01, ldir, shadow_hit); vec3 l = normalize(ldir); vec3 v = normalize(-rd); vec3 h = normalize(v + l); float ndotl = max(dot(norm, l), 0.0); float ndoth = max(dot(norm, h), 0.0); vec3 lit = hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin); return amb + lit * shadow_hit.shadow; } #define M_PI 3.1415926 #define M_2PI (M_PI * 2.0) vec3 backdrop(in vec3 dir) { return vec3(0.1, 0.15, 1.0); } #define FLOOR_OFFS vec3(3.0, 0.0, 0.0) #define FLOOR_SIZE vec2(5.5, 15.0) #define GLASS_POS vec3(0.0, 0.2, 1.2) bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res) { float opacity = 1.0; HitPoint hit, nearest; nearest.dist = 10000.0; if(isect_sphere(ro, rd, vec3(1.5, -0.5, 0.0), 0.85, hit)) { nearest = hit; nearest.mtl = mtl_sph; if(hit.dist < 1.0) { opacity *= mtl_sph.refr; } } if(isect_sphere(ro, rd, GLASS_POS, 0.9, hit)) { if(hit.dist < nearest.dist) { nearest = hit; nearest.mtl = mtl_glass; } if(hit.dist < 1.0) { opacity *= mtl_glass.refr; } } if(isect_sphere(ro, rd, GLASS_POS, 0.86, hit)) { if(hit.dist < nearest.dist) { nearest = hit; nearest.mtl = mtl_air; } } if(isect_floor(ro, rd, FLOOR_SIZE, hit) && hit.dist < nearest.dist) { nearest = hit; nearest.mtl = mtl_floor; nearest.mtl.diffuse = tex_chess(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), hit.surfpos); } if(nearest.dist >= 10000.0) { hit_res.shadow = 1.0; return false; } hit_res = nearest; hit_res.shadow = opacity; return true; } bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit) { if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) { return false; } if(abs(hit.pos.x) >= sz.x || abs(hit.pos.z) >= sz.y) { return false; } hit.pos += FLOOR_OFFS; hit.surfpos /= sz * 2.0; return true; } bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit) { float ndotrd = dot(rd, plane.xyz); if(abs(ndotrd) < 1e-6) { return false; } vec3 pp = plane.xyz * plane.w; vec3 pdir = pp - ro; float t = dot(pdir, plane.xyz) / ndotrd; if(t < 1e-6) { return false; } hit.dist = t; hit.pos = ro + rd * t; hit.norm = plane.xyz; hit.surfpos = hit.pos.xz; /* XXX */ return true; } bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit) { float a = dot(rd, rd); float b = dot(rd * 2.0, (ro - pos)); float c = dot(ro, ro) + dot(pos, pos) - 2.0 * dot(ro, pos) - rad * rad; float d = b * b - 4.0 * a * c; if(d < 1e-6) { return false; } float t0 = (-b + sqrt(d)) / (2.0 * a); float t1 = (-b - sqrt(d)) / (2.0 * a); if(t0 < 0.0) t0 = t1; if(t1 < 0.0) t1 = t0; float t = min(t0, t1); if(t < 1e-6) { return false; } hit.dist = t; hit.pos = ro + rd * t; hit.norm = normalize(hit.pos - pos); hit.surfpos.x = atan(hit.norm.z, hit.norm.x); hit.surfpos.y = acos(hit.norm.y); return true; } vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos) { float foo = step(0.5, mod(spos.x * 8.0, 1.0)) * 2.0 - 1.0; float bar = step(0.5, mod(spos.y * 24.0, 1.0)) * 2.0 - 1.0; float xor = (foo * bar) * 0.5 + 0.5; return mix(col1, col2, xor); }