|
1 | 1 | precision mediump float;
|
2 | 2 |
|
| 3 | +const float epsilon = 0.001; |
| 4 | +const float inf = 1e9; |
| 5 | + |
3 | 6 | uniform vec2 resolution;
|
4 |
| -uniform float time; |
| 7 | +uniform vec3 eye; |
| 8 | +uniform vec3 center; |
| 9 | +uniform vec3 background; |
| 10 | +uniform bool antialias; |
| 11 | + |
| 12 | +struct Ray { |
| 13 | + vec3 origin; |
| 14 | + vec3 dir; |
| 15 | +}; |
| 16 | + |
| 17 | +struct Material { |
| 18 | + vec3 kd; |
| 19 | + vec3 ks; |
| 20 | + bool metal; |
| 21 | + bool checker; |
| 22 | +}; |
| 23 | + |
| 24 | +struct Hit { |
| 25 | + float time; |
| 26 | + vec3 normal; |
| 27 | + Material material; |
| 28 | +}; |
| 29 | + |
| 30 | +// Trace a ray to a sphere, using high school geometry |
| 31 | +void sphere(inout Hit h, Ray r, vec4 s, Material m) { |
| 32 | + // Rescale to unit sphere at the origin |
| 33 | + r.origin = (r.origin - s.xyz) / s.w; |
| 34 | + r.dir = r.dir / s.w; |
| 35 | + |
| 36 | + // Quadratic formula |
| 37 | + float a = dot(r.dir, r.dir); |
| 38 | + float b = dot(r.dir, r.origin); |
| 39 | + float c = dot(r.origin, r.origin) - 1.0; |
| 40 | + |
| 41 | + float d = b * b - a * c; |
| 42 | + if (d < 0.0) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + d = sqrt(d); |
| 47 | + float t = (-b - d) / a; |
| 48 | + if (t < epsilon) { |
| 49 | + t = (-b + d) / a; |
| 50 | + } |
| 51 | + |
| 52 | + if (t >= epsilon && t < h.time) { |
| 53 | + h.time = t; |
| 54 | + h.normal = normalize(r.origin + r.dir * t); |
| 55 | + h.material = m; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void circle(inout Hit h, Ray r, float y, float radius, Material m) { |
| 60 | + float t = (y - r.origin.y) / r.dir.y; |
| 61 | + if (t >= epsilon && t < h.time |
| 62 | + && length(r.origin + t * r.dir) < radius) { |
| 63 | + h.time = t; |
| 64 | + h.normal = vec3(0.0, 1.0, 0.0); |
| 65 | + h.material = m; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Intersect a ray with the scene |
| 70 | +Hit intersect(Ray r) { |
| 71 | + Hit h = Hit(inf, vec3(0.0), Material(vec3(0.0), vec3(0.0), false, false)); |
| 72 | + sphere(h, r, vec4(0.8, -1.0, -10.0, 1.0), |
| 73 | + Material(vec3(0.4, 0.2, 0.8), vec3(0.8), false, false)); |
| 74 | + sphere(h, r, vec4(-2.5, -0.2, -12.0, 1.8), |
| 75 | + Material(vec3(1.0, 0.4, 0.2), vec3(0.8), true, false)); |
| 76 | + sphere(h, r, vec4(-3.5, -1.2, -6.0, 0.8), |
| 77 | + Material(vec3(0.2, 0.6, 0.3), vec3(0.8), false, false)); |
| 78 | + circle(h, r, -2.0, 50.0, |
| 79 | + Material(vec3(0.8, 0.8, 0.8), vec3(0.0), false, true)); |
| 80 | + return h; |
| 81 | +} |
| 82 | + |
| 83 | +// Compute lighting from one light |
| 84 | +vec3 illuminate(vec3 lightPosition, vec3 pos, vec3 wo, Hit h) { |
| 85 | + vec3 wi = lightPosition - pos; |
| 86 | + vec3 kd = h.material.kd; |
| 87 | + if (h.material.checker) { |
| 88 | + // Checkerboard pattern for the floor |
| 89 | + vec2 coords = floor(pos.xz); |
| 90 | + kd = vec3(mod(coords.x + coords.y, 2.0) * 0.8 + 0.2); |
| 91 | + } |
| 92 | + float intensity = 1.0 / dot(wi, wi); // inverse-square law |
| 93 | + vec3 diffuse = kd * max(dot(normalize(wi), h.normal), 0.0); |
| 94 | + |
| 95 | + // Non-dielectric materials have tinted reflections |
| 96 | + vec3 ks = h.material.metal ? h.material.kd : h.material.ks; |
| 97 | + vec3 r = -reflect(normalize(wi), h.normal); |
| 98 | + vec3 specular = ks * pow(max(dot(r, wo), 0.0), 10.0); |
| 99 | + |
| 100 | + return intensity * (diffuse + specular); |
| 101 | +} |
| 102 | + |
| 103 | +// Compute total lighting at a given point |
| 104 | +vec3 calcLighting(vec3 pos, vec3 wo, Hit h) { |
| 105 | + vec3 color = vec3(0.0); |
| 106 | + color += 100.0 * illuminate(vec3(-3.0, 10.0, 0.0), pos, wo, h); |
| 107 | + color += 200000.0 * illuminate(vec3(0.0, 1000.0, 0.0), pos, wo, h); |
| 108 | + return color; |
| 109 | +} |
| 110 | + |
| 111 | +// Trace a ray, returning an RGB color based on its value |
| 112 | +vec3 trace(Ray r) { |
| 113 | + Hit h = intersect(r); |
| 114 | + if (h.time != inf) { |
| 115 | + vec3 pos = r.origin + h.time * r.dir; |
| 116 | + vec3 color = calcLighting(pos, -r.dir, h); |
| 117 | + if (h.material.metal) { |
| 118 | + vec3 dir = reflect(r.dir, h.normal); |
| 119 | + Hit h2 = intersect(Ray(pos, dir)); |
| 120 | + if (h2.time < inf) { |
| 121 | + vec3 pos2 = pos + h2.time * dir; |
| 122 | + color += 0.2 * h.material.ks * calcLighting(pos2, -dir, h2); |
| 123 | + } else { |
| 124 | + color += 0.2 * h.material.ks * background; |
| 125 | + } |
| 126 | + } |
| 127 | + return color; |
| 128 | + } |
| 129 | + return background; |
| 130 | +} |
| 131 | + |
| 132 | +vec3 tracePixel(vec2 coord) { |
| 133 | + // Pixel coordinates, normalized so that p.y in range [-1, 1] |
| 134 | + vec2 p = (2.0 * coord - resolution) / resolution.y; |
| 135 | + |
| 136 | + // View ray from camera |
| 137 | + vec3 ww = normalize(center - eye); |
| 138 | + vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0))); |
| 139 | + vec3 vv = normalize(cross(uu, ww)); |
| 140 | + // (Note: cot(pi/12) = 2 + sqrt(3) = 3.73) |
| 141 | + vec3 dir = normalize(p.x * uu + p.y * vv + 3.73 * ww); |
| 142 | + |
| 143 | + return trace(Ray(eye, dir)); |
| 144 | +} |
5 | 145 |
|
6 | 146 | void main() {
|
7 |
| - vec2 coord = gl_FragCoord.xy / resolution; |
| 147 | + vec3 color = vec3(0.0); |
8 | 148 |
|
9 |
| - // Output RGB color in range from 0.0 to 1.0 |
10 |
| - vec3 color = vec3(coord.x, coord.y, 0.0); |
11 |
| - color.z += abs(sin(time)); |
| 149 | + if (antialias) { |
| 150 | + // Anti-aliasing by supersampling multiple rays |
| 151 | + color += 0.25 * tracePixel(gl_FragCoord.xy + vec2(-0.25, -0.25)); |
| 152 | + color += 0.25 * tracePixel(gl_FragCoord.xy + vec2(-0.25, +0.25)); |
| 153 | + color += 0.25 * tracePixel(gl_FragCoord.xy + vec2(+0.25, -0.25)); |
| 154 | + color += 0.25 * tracePixel(gl_FragCoord.xy + vec2(+0.25, +0.25)); |
| 155 | + } else { |
| 156 | + color += tracePixel(gl_FragCoord.xy); |
| 157 | + } |
12 | 158 |
|
13 | 159 | gl_FragColor = vec4(color, 1.0);
|
14 | 160 | }
|
0 commit comments