1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| function RandomLCG(seed) { return function() { seed = (214013 * seed + 2531011) % 0x100000000; return seed * (1.0 / 4294967296.0); }; } function Vec(x, y, z) { this.x = x; this.y = y; this.z = z; } Vec.prototype = { add: function(b) { return new Vec(this.x + b.x, this.y + b.y, this.z + b.z); }, sub: function(b) { return new Vec(this.x - b.x, this.y - b.y, this.z - b.z); }, mul: function(b) { return new Vec(this.x * b, this.y * b, this.z * b); }, mult: function(b) { return new Vec(this.x * b.x, this.y * b.y, this.z * b.z); }, norm: function() { return this.mul(1.0 / Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z)); }, dot: function(b) { return this.x * b.x + this.y * b.y + this.z * b.z; }, cross: function(b) { return new Vec(this.y * b.z - this.z * b.y, this.z * b.x - this.x * b.z, this.x * b.y - this.y * b.x); } }; Vec.Zero = new Vec(0, 0, 0) Vec.XAxis = new Vec(1, 0, 0) Vec.YAxis = new Vec(0, 1, 0) Vec.ZAxis = new Vec(0, 0, 1) Refl = {DIFF: 0,SPEC: 1,REFR: 2}; function Ray(o, d) { this.o = o; this.d = d } function Sphere(rad, p, e, c, refl) { this.rad = rad; this.p = p; this.e = e; this.c = c; this.refl = refl; this.sqRad = rad * rad; this.maxC = Math.max(Math.max(c.x, c.y), c.z); this.cc = c.mul(1.0 / this.maxC); } Sphere.prototype = { intersect: function(r) { var op = this.p.sub(r.o); var b = op.dot(r.d); var det = b * b - op.dot(op) + this.sqRad; var eps = 1e-4; if (det < 0) return 0; else { var dets = Math.sqrt(det) if (b - dets > eps) return b - dets; else if (b + dets > eps) return b + dets; else return 0; } } };
spheres = [ new Sphere(1e5, new Vec(1e5 + 1, 40.8, 81.6), Vec.Zero, new Vec(.75, .25, .25), Refl.DIFF), new Sphere(1e5, new Vec(-1e5 + 99, 40.8, 81.6), Vec.Zero, new Vec(.25, .25, .75), Refl.DIFF), new Sphere(1e5, new Vec(50, 40.8, 1e5), Vec.Zero, new Vec(.75, .75, .75), Refl.DIFF), new Sphere(1e5, new Vec(50, 40.8, -1e5 + 170), Vec.Zero, Vec.Zero, Refl.DIFF), new Sphere(1e5, new Vec(50, 1e5, 81.6), Vec.Zero, new Vec(.75, .75, .75), Refl.DIFF), new Sphere(1e5, new Vec(50, -1e5 + 81.6, 81.6), Vec.Zero, new Vec(.75, .75, .75), Refl.DIFF), new Sphere(16.5, new Vec(27, 16.5, 47), Vec.Zero, new Vec(1, 1, 1).mul(.999), Refl.SPEC), new Sphere(16.5, new Vec(73, 16.5, 78), Vec.Zero, new Vec(1, 1, 1).mul(.999), Refl.REFR), new Sphere(600, new Vec(50, 681.6 - .27, 81.6), new Vec(12, 12, 12), Vec.Zero, Refl.DIFF) ]; var rand = RandomLCG(0) function clamp(x) { if (x < 0) return 0; else if (x > 1) return 1; else return x; } function toInt(x) { return Math.pow(clamp(x), 1 / 2.2) * 255 + .5 } function intersect(r) { var t = 1e20; var obj; for (var i in spheres) { var s = spheres[i]; var d = s.intersect(r); if (d != 0 && d < t) { t = d; obj = s; } } return [obj, t]; } function radiance(r, depth) { var ires = intersect(r); var obj = ires[0]; var t = ires[1]; if (obj == null) { return Vec.Zero; } else { var newDepth = depth + 1; var isMaxDepth = newDepth > 100; var isUseRR = newDepth > 5; var isRR = isUseRR && rand() < obj.maxC; if (isMaxDepth || (isUseRR && !isRR)) return obj.e; else { var f = (isUseRR && isRR) ? obj.cc : obj.c; var x = r.o.add(r.d.mul(t)); var n = x.sub(obj.p).norm(); var nl = n.dot(r.d) < 0 ? n : n.mul(-1); if (obj.refl == Refl.DIFF) { var r1 = 2 * Math.PI * rand(); var r2 = rand(); var r2s = Math.sqrt(r2); var w = nl; var wo = Math.abs(w.x) > .1 ? new Vec(0, 1, 0) : new Vec(1, 0, 0); var u = wo.cross(w).norm(); var v = w.cross(u);
var d = (u.mul(Math.cos(r1) * r2s).add(v.mul(Math.sin(r1) * r2s)).add(w.mul(Math.sqrt(1 - r2)))).norm(); return obj.e.add(f.mult(radiance(new Ray(x, d), newDepth))); } else if (obj.refl == Refl.SPEC) { return obj.e.add(f.mult(radiance(new Ray(x, r.d.sub(n.mul(2 * n.dot(r.d)))), newDepth))); } else { var reflRay = new Ray(x, r.d.sub(n.mul(2 * n.dot(r.d)))); var into = n.dot(nl) > 0; var nc = 1; var nt = 1.5; var nnt = into ? nc / nt : nt / nc; var ddn = r.d.dot(nl); var cos2t = 1 - nnt * nnt * (1 - ddn * ddn); if (cos2t < 0) { return obj.e.add(f.mult(radiance(reflRay, newDepth))); } else { var tdir = (r.d.mul(nnt).sub(n.mul((into ? 1 : -1) * (ddn * nnt + Math.sqrt(cos2t))))).norm(); var a = nt - nc; var b = nt + nc; var R0 = a * a / (b * b); var c = 1 - (into ? -ddn : tdir.dot(n)); var Re = R0 + (1 - R0) * c * c * c * c * c; var Tr = 1 - Re; var P = .25 + .5 * Re; var RP = Re / P; var TP = Tr / (1 - P); var result; if (newDepth > 2) { if (rand() < P) result = radiance(reflRay, newDepth).mul(RP); else result = radiance(new Ray(x, tdir), newDepth).mul(TP); } else result = radiance(reflRay, newDepth).mul(Re).add(radiance(new Ray(x, tdir), newDepth).mul(Tr)); return obj.e.add(f.mult(result)); } } } } } function render(canvas, status) { var start = new Date().getTime(); var w = canvas.attributes.width.value; var h = canvas.attributes.height.value; var samps = 25; var cam = new Ray(new Vec(50, 52, 295.6), new Vec(0, -0.042612, -1).norm()); var cx = new Vec(w * .5135 / h, 0, 0); var cy = (cx.cross(cam.d)).norm().mul(.5135); var c = new Array(w * h); for (var i = 0; i < w * h; i++) c[i] = Vec.Zero; var ctx = canvas.getContext("2d"); var imgdata = ctx.getImageData(0, 0, w, h); var pixels = imgdata.data; var y = 0; setTimeout(renderLine, 0); function renderLine() { status.innerHTML = "Rendering (" + samps * 4 + " spp) " + (100.0 * y / (h - 1)).toFixed(2) + "%"; for (var x = 0; x < w; x++) { for (var sy = 0; sy < 2; sy++) { var i = (h - y - 1) * w + x; for (var sx = 0; sx < 2; sx++) { var r = Vec.Zero; for (var s = 0; s < samps; s++) { var r1 = 2 * rand(); var r2 = 2 * rand(); var dx = r1 < 1 ? Math.sqrt(r1) - 1 : 1 - Math.sqrt(2 - r1); var dy = r2 < 1 ? Math.sqrt(r2) - 1 : 1 - Math.sqrt(2 - r2); var d = cx.mul(((sx + .5 + dx) / 2 + x) / w - .5).add( cy.mul(((sy + .5 + dy) / 2 + y) / h - .5)).add(cam.d); var camRay = new Ray(cam.o.add(d.mul(140)), d.norm()); r = r.add(radiance(camRay, 0).mul(1.0 / samps)); } c[i] = c[i].add((new Vec(clamp(r.x), clamp(r.y), clamp(r.z))).mul(.25)); } } } renderOutput(); y++; if (y < h) setTimeout(renderLine, 0); else status.innerHTML = (new Date().getTime() - start) / 1000 + " sec"; } function renderOutput() { var i = (h - y - 1) * w * 4, j = (h - y - 1) * w; for (var x = 0; x < w; x++) { pixels[i++] = toInt(c[j].x); pixels[i++] = toInt(c[j].y); pixels[i++] = toInt(c[j].z); pixels[i++] = 255; j++; } ctx.putImageData(imgdata, 0, 0); } }
|