js/src/devtools/jint/sunspider/3d-raytrace.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (C) 2007 Apple Inc. All rights reserved.
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 * 1. Redistributions of source code must retain the above copyright
michael@0 8 * notice, this list of conditions and the following disclaimer.
michael@0 9 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer in the
michael@0 11 * documentation and/or other materials provided with the distribution.
michael@0 12 *
michael@0 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
michael@0 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
michael@0 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 24 */
michael@0 25
michael@0 26 function createVector(x,y,z) {
michael@0 27 return new Array(x,y,z);
michael@0 28 }
michael@0 29
michael@0 30 function sqrLengthVector(self) {
michael@0 31 return self[0] * self[0] + self[1] * self[1] + self[2] * self[2];
michael@0 32 }
michael@0 33
michael@0 34 function lengthVector(self) {
michael@0 35 return Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]);
michael@0 36 }
michael@0 37
michael@0 38 function addVector(self, v) {
michael@0 39 self[0] += v[0];
michael@0 40 self[1] += v[1];
michael@0 41 self[2] += v[2];
michael@0 42 return self;
michael@0 43 }
michael@0 44
michael@0 45 function subVector(self, v) {
michael@0 46 self[0] -= v[0];
michael@0 47 self[1] -= v[1];
michael@0 48 self[2] -= v[2];
michael@0 49 return self;
michael@0 50 }
michael@0 51
michael@0 52 function scaleVector(self, scale) {
michael@0 53 self[0] *= scale;
michael@0 54 self[1] *= scale;
michael@0 55 self[2] *= scale;
michael@0 56 return self;
michael@0 57 }
michael@0 58
michael@0 59 function normaliseVector(self) {
michael@0 60 var len = Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]);
michael@0 61 self[0] /= len;
michael@0 62 self[1] /= len;
michael@0 63 self[2] /= len;
michael@0 64 return self;
michael@0 65 }
michael@0 66
michael@0 67 function add(v1, v2) {
michael@0 68 return new Array(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
michael@0 69 }
michael@0 70
michael@0 71 function sub(v1, v2) {
michael@0 72 return new Array(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
michael@0 73 }
michael@0 74
michael@0 75 function scalev(v1, v2) {
michael@0 76 return new Array(v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]);
michael@0 77 }
michael@0 78
michael@0 79 function dot(v1, v2) {
michael@0 80 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
michael@0 81 }
michael@0 82
michael@0 83 function scale(v, scale) {
michael@0 84 return [v[0] * scale, v[1] * scale, v[2] * scale];
michael@0 85 }
michael@0 86
michael@0 87 function cross(v1, v2) {
michael@0 88 return [v1[1] * v2[2] - v1[2] * v2[1],
michael@0 89 v1[2] * v2[0] - v1[0] * v2[2],
michael@0 90 v1[0] * v2[1] - v1[1] * v2[0]];
michael@0 91
michael@0 92 }
michael@0 93
michael@0 94 function normalise(v) {
michael@0 95 var len = lengthVector(v);
michael@0 96 return [v[0] / len, v[1] / len, v[2] / len];
michael@0 97 }
michael@0 98
michael@0 99 function transformMatrix(self, v) {
michael@0 100 var vals = self;
michael@0 101 var x = vals[0] * v[0] + vals[1] * v[1] + vals[2] * v[2] + vals[3];
michael@0 102 var y = vals[4] * v[0] + vals[5] * v[1] + vals[6] * v[2] + vals[7];
michael@0 103 var z = vals[8] * v[0] + vals[9] * v[1] + vals[10] * v[2] + vals[11];
michael@0 104 return [x, y, z];
michael@0 105 }
michael@0 106
michael@0 107 function invertMatrix(self) {
michael@0 108 var temp = new Array(16);
michael@0 109 var tx = -self[3];
michael@0 110 var ty = -self[7];
michael@0 111 var tz = -self[11];
michael@0 112 /* BEGIN LOOP */
michael@0 113 for (h = 0; h < 3; h++) {
michael@0 114 /* BEGIN LOOP */
michael@0 115 for (v = 0; v < 3; v++)
michael@0 116 temp[h + v * 4] = self[v + h * 4];
michael@0 117 /* END LOOP */
michael@0 118 }
michael@0 119 /* END LOOP */
michael@0 120 /* BEGIN LOOP */
michael@0 121 for (i = 0; i < 11; i++)
michael@0 122 self[i] = temp[i];
michael@0 123 /* END LOOP */
michael@0 124 self[3] = tx * self[0] + ty * self[1] + tz * self[2];
michael@0 125 self[7] = tx * self[4] + ty * self[5] + tz * self[6];
michael@0 126 self[11] = tx * self[8] + ty * self[9] + tz * self[10];
michael@0 127 return self;
michael@0 128 }
michael@0 129
michael@0 130
michael@0 131 // Triangle intersection using barycentric coord method
michael@0 132 function Triangle(p1, p2, p3) {
michael@0 133 var edge1 = sub(p3, p1);
michael@0 134 var edge2 = sub(p2, p1);
michael@0 135 var normal = cross(edge1, edge2);
michael@0 136 if (Math.abs(normal[0]) > Math.abs(normal[1]))
michael@0 137 if (Math.abs(normal[0]) > Math.abs(normal[2]))
michael@0 138 this.axis = 0;
michael@0 139 else
michael@0 140 this.axis = 2;
michael@0 141 else
michael@0 142 if (Math.abs(normal[1]) > Math.abs(normal[2]))
michael@0 143 this.axis = 1;
michael@0 144 else
michael@0 145 this.axis = 2;
michael@0 146 var u = (this.axis + 1) % 3;
michael@0 147 var v = (this.axis + 2) % 3;
michael@0 148 var u1 = edge1[u];
michael@0 149 var v1 = edge1[v];
michael@0 150
michael@0 151 var u2 = edge2[u];
michael@0 152 var v2 = edge2[v];
michael@0 153 this.normal = normalise(normal);
michael@0 154 this.nu = normal[u] / normal[this.axis];
michael@0 155 this.nv = normal[v] / normal[this.axis];
michael@0 156 this.nd = dot(normal, p1) / normal[this.axis];
michael@0 157 var det = u1 * v2 - v1 * u2;
michael@0 158 this.eu = p1[u];
michael@0 159 this.ev = p1[v];
michael@0 160 this.nu1 = u1 / det;
michael@0 161 this.nv1 = -v1 / det;
michael@0 162 this.nu2 = v2 / det;
michael@0 163 this.nv2 = -u2 / det;
michael@0 164 this.material = [0.7, 0.7, 0.7];
michael@0 165 }
michael@0 166
michael@0 167 Triangle.prototype.intersect = function(orig, dir, near, far) {
michael@0 168 var u = (this.axis + 1) % 3;
michael@0 169 var v = (this.axis + 2) % 3;
michael@0 170 var d = dir[this.axis] + this.nu * dir[u] + this.nv * dir[v];
michael@0 171 var t = (this.nd - orig[this.axis] - this.nu * orig[u] - this.nv * orig[v]) / d;
michael@0 172 if (t < near || t > far)
michael@0 173 return null;
michael@0 174 var Pu = orig[u] + t * dir[u] - this.eu;
michael@0 175 var Pv = orig[v] + t * dir[v] - this.ev;
michael@0 176 var a2 = Pv * this.nu1 + Pu * this.nv1;
michael@0 177 if (a2 < 0)
michael@0 178 return null;
michael@0 179 var a3 = Pu * this.nu2 + Pv * this.nv2;
michael@0 180 if (a3 < 0)
michael@0 181 return null;
michael@0 182
michael@0 183 if ((a2 + a3) > 1)
michael@0 184 return null;
michael@0 185 return t;
michael@0 186 }
michael@0 187
michael@0 188 function Scene(a_triangles) {
michael@0 189 this.triangles = a_triangles;
michael@0 190 this.lights = [];
michael@0 191 this.ambient = [0,0,0];
michael@0 192 this.background = [0.8,0.8,1];
michael@0 193 }
michael@0 194 var zero = new Array(0,0,0);
michael@0 195
michael@0 196 Scene.prototype.intersect = function(origin, dir, near, far) {
michael@0 197 var closest = null;
michael@0 198 /* BEGIN LOOP */
michael@0 199 for (i = 0; i < this.triangles.length; i++) {
michael@0 200 var triangle = this.triangles[i];
michael@0 201 var d = triangle.intersect(origin, dir, near, far);
michael@0 202 if (d == null || d > far || d < near)
michael@0 203 continue;
michael@0 204 far = d;
michael@0 205 closest = triangle;
michael@0 206 }
michael@0 207 /* END LOOP */
michael@0 208
michael@0 209 if (!closest)
michael@0 210 return [this.background[0],this.background[1],this.background[2]];
michael@0 211
michael@0 212 var normal = closest.normal;
michael@0 213 var hit = add(origin, scale(dir, far));
michael@0 214 if (dot(dir, normal) > 0)
michael@0 215 normal = [-normal[0], -normal[1], -normal[2]];
michael@0 216
michael@0 217 var colour = null;
michael@0 218 if (closest.shader) {
michael@0 219 colour = closest.shader(closest, hit, dir);
michael@0 220 } else {
michael@0 221 colour = closest.material;
michael@0 222 }
michael@0 223
michael@0 224 // do reflection
michael@0 225 var reflected = null;
michael@0 226 if (colour.reflection > 0.001) {
michael@0 227 var reflection = addVector(scale(normal, -2*dot(dir, normal)), dir);
michael@0 228 reflected = this.intersect(hit, reflection, 0.0001, 1000000);
michael@0 229 if (colour.reflection >= 0.999999)
michael@0 230 return reflected;
michael@0 231 }
michael@0 232
michael@0 233 var l = [this.ambient[0], this.ambient[1], this.ambient[2]];
michael@0 234 /* BEGIN LOOP */
michael@0 235 for (var i = 0; i < this.lights.length; i++) {
michael@0 236 var light = this.lights[i];
michael@0 237 var toLight = sub(light, hit);
michael@0 238 var distance = lengthVector(toLight);
michael@0 239 scaleVector(toLight, 1.0/distance);
michael@0 240 distance -= 0.0001;
michael@0 241 if (this.blocked(hit, toLight, distance))
michael@0 242 continue;
michael@0 243 var nl = dot(normal, toLight);
michael@0 244 if (nl > 0)
michael@0 245 addVector(l, scale(light.colour, nl));
michael@0 246 }
michael@0 247 /* END LOOP */
michael@0 248 l = scalev(l, colour);
michael@0 249 if (reflected) {
michael@0 250 l = addVector(scaleVector(l, 1 - colour.reflection), scaleVector(reflected, colour.reflection));
michael@0 251 }
michael@0 252 return l;
michael@0 253 }
michael@0 254
michael@0 255 Scene.prototype.blocked = function(O, D, far) {
michael@0 256 var near = 0.0001;
michael@0 257 var closest = null;
michael@0 258 /* BEGIN LOOP */
michael@0 259 for (i = 0; i < this.triangles.length; i++) {
michael@0 260 var triangle = this.triangles[i];
michael@0 261 var d = triangle.intersect(O, D, near, far);
michael@0 262 if (d == null || d > far || d < near)
michael@0 263 continue;
michael@0 264 return true;
michael@0 265 }
michael@0 266 /* END LOOP */
michael@0 267
michael@0 268 return false;
michael@0 269 }
michael@0 270
michael@0 271
michael@0 272 // this camera code is from notes i made ages ago, it is from *somewhere* -- i cannot remember where
michael@0 273 // that somewhere is
michael@0 274 function Camera(origin, lookat, up) {
michael@0 275 var zaxis = normaliseVector(subVector(lookat, origin));
michael@0 276 var xaxis = normaliseVector(cross(up, zaxis));
michael@0 277 var yaxis = normaliseVector(cross(xaxis, subVector([0,0,0], zaxis)));
michael@0 278 var m = new Array(16);
michael@0 279 m[0] = xaxis[0]; m[1] = xaxis[1]; m[2] = xaxis[2];
michael@0 280 m[4] = yaxis[0]; m[5] = yaxis[1]; m[6] = yaxis[2];
michael@0 281 m[8] = zaxis[0]; m[9] = zaxis[1]; m[10] = zaxis[2];
michael@0 282 invertMatrix(m);
michael@0 283 m[3] = 0; m[7] = 0; m[11] = 0;
michael@0 284 this.origin = origin;
michael@0 285 this.directions = new Array(4);
michael@0 286 this.directions[0] = normalise([-0.7, 0.7, 1]);
michael@0 287 this.directions[1] = normalise([ 0.7, 0.7, 1]);
michael@0 288 this.directions[2] = normalise([ 0.7, -0.7, 1]);
michael@0 289 this.directions[3] = normalise([-0.7, -0.7, 1]);
michael@0 290 this.directions[0] = transformMatrix(m, this.directions[0]);
michael@0 291 this.directions[1] = transformMatrix(m, this.directions[1]);
michael@0 292 this.directions[2] = transformMatrix(m, this.directions[2]);
michael@0 293 this.directions[3] = transformMatrix(m, this.directions[3]);
michael@0 294 }
michael@0 295
michael@0 296 Camera.prototype.generateRayPair = function(y) {
michael@0 297 rays = new Array(new Object(), new Object());
michael@0 298 rays[0].origin = this.origin;
michael@0 299 rays[1].origin = this.origin;
michael@0 300 rays[0].dir = addVector(scale(this.directions[0], y), scale(this.directions[3], 1 - y));
michael@0 301 rays[1].dir = addVector(scale(this.directions[1], y), scale(this.directions[2], 1 - y));
michael@0 302 return rays;
michael@0 303 }
michael@0 304
michael@0 305 function renderRows(camera, scene, pixels, width, height, starty, stopy) {
michael@0 306 /* BEGIN LOOP */
michael@0 307 for (var y = starty; y < stopy; y++) {
michael@0 308 var rays = camera.generateRayPair(y / height);
michael@0 309 /* BEGIN LOOP */
michael@0 310 for (var x = 0; x < width; x++) {
michael@0 311 var xp = x / width;
michael@0 312 var origin = addVector(scale(rays[0].origin, xp), scale(rays[1].origin, 1 - xp));
michael@0 313 var dir = normaliseVector(addVector(scale(rays[0].dir, xp), scale(rays[1].dir, 1 - xp)));
michael@0 314 var l = scene.intersect(origin, dir);
michael@0 315 pixels[y][x] = l;
michael@0 316 }
michael@0 317 /* END LOOP */
michael@0 318 }
michael@0 319 /* END LOOP */
michael@0 320 }
michael@0 321
michael@0 322 Camera.prototype.render = function(scene, pixels, width, height) {
michael@0 323 var cam = this;
michael@0 324 var row = 0;
michael@0 325 renderRows(cam, scene, pixels, width, height, 0, height);
michael@0 326 }
michael@0 327
michael@0 328
michael@0 329
michael@0 330 function raytraceScene()
michael@0 331 {
michael@0 332 var startDate = new Date().getTime();
michael@0 333 var numTriangles = 2 * 6;
michael@0 334 var triangles = new Array();//numTriangles);
michael@0 335 var tfl = createVector(-10, 10, -10);
michael@0 336 var tfr = createVector( 10, 10, -10);
michael@0 337 var tbl = createVector(-10, 10, 10);
michael@0 338 var tbr = createVector( 10, 10, 10);
michael@0 339 var bfl = createVector(-10, -10, -10);
michael@0 340 var bfr = createVector( 10, -10, -10);
michael@0 341 var bbl = createVector(-10, -10, 10);
michael@0 342 var bbr = createVector( 10, -10, 10);
michael@0 343
michael@0 344 // cube!!!
michael@0 345 // front
michael@0 346 var i = 0;
michael@0 347
michael@0 348 triangles[i++] = new Triangle(tfl, tfr, bfr);
michael@0 349 triangles[i++] = new Triangle(tfl, bfr, bfl);
michael@0 350 // back
michael@0 351 triangles[i++] = new Triangle(tbl, tbr, bbr);
michael@0 352 triangles[i++] = new Triangle(tbl, bbr, bbl);
michael@0 353 // triangles[i-1].material = [0.7,0.2,0.2];
michael@0 354 // triangles[i-1].material.reflection = 0.8;
michael@0 355 // left
michael@0 356 triangles[i++] = new Triangle(tbl, tfl, bbl);
michael@0 357 // triangles[i-1].reflection = 0.6;
michael@0 358 triangles[i++] = new Triangle(tfl, bfl, bbl);
michael@0 359 // triangles[i-1].reflection = 0.6;
michael@0 360 // right
michael@0 361 triangles[i++] = new Triangle(tbr, tfr, bbr);
michael@0 362 triangles[i++] = new Triangle(tfr, bfr, bbr);
michael@0 363 // top
michael@0 364 triangles[i++] = new Triangle(tbl, tbr, tfr);
michael@0 365 triangles[i++] = new Triangle(tbl, tfr, tfl);
michael@0 366 // bottom
michael@0 367 triangles[i++] = new Triangle(bbl, bbr, bfr);
michael@0 368 triangles[i++] = new Triangle(bbl, bfr, bfl);
michael@0 369
michael@0 370 //Floor!!!!
michael@0 371 var green = createVector(0.0, 0.4, 0.0);
michael@0 372 var grey = createVector(0.4, 0.4, 0.4);
michael@0 373 grey.reflection = 1.0;
michael@0 374 var floorShader = function(tri, pos, view) {
michael@0 375 var x = ((pos[0]/32) % 2 + 2) % 2;
michael@0 376 var z = ((pos[2]/32 + 0.3) % 2 + 2) % 2;
michael@0 377 if (x < 1 != z < 1) {
michael@0 378 //in the real world we use the fresnel term...
michael@0 379 // var angle = 1-dot(view, tri.normal);
michael@0 380 // angle *= angle;
michael@0 381 // angle *= angle;
michael@0 382 // angle *= angle;
michael@0 383 //grey.reflection = angle;
michael@0 384 return grey;
michael@0 385 } else
michael@0 386 return green;
michael@0 387 }
michael@0 388 var ffl = createVector(-1000, -30, -1000);
michael@0 389 var ffr = createVector( 1000, -30, -1000);
michael@0 390 var fbl = createVector(-1000, -30, 1000);
michael@0 391 var fbr = createVector( 1000, -30, 1000);
michael@0 392 triangles[i++] = new Triangle(fbl, fbr, ffr);
michael@0 393 triangles[i-1].shader = floorShader;
michael@0 394 triangles[i++] = new Triangle(fbl, ffr, ffl);
michael@0 395 triangles[i-1].shader = floorShader;
michael@0 396
michael@0 397 var _scene = new Scene(triangles);
michael@0 398 _scene.lights[0] = createVector(20, 38, -22);
michael@0 399 _scene.lights[0].colour = createVector(0.7, 0.3, 0.3);
michael@0 400 _scene.lights[1] = createVector(-23, 40, 17);
michael@0 401 _scene.lights[1].colour = createVector(0.7, 0.3, 0.3);
michael@0 402 _scene.lights[2] = createVector(23, 20, 17);
michael@0 403 _scene.lights[2].colour = createVector(0.7, 0.7, 0.7);
michael@0 404 _scene.ambient = createVector(0.1, 0.1, 0.1);
michael@0 405 // _scene.background = createVector(0.7, 0.7, 1.0);
michael@0 406
michael@0 407 var size = 30;
michael@0 408 var pixels = new Array();
michael@0 409 /* BEGIN LOOP */
michael@0 410 for (var y = 0; y < size; y++) {
michael@0 411 pixels[y] = new Array();
michael@0 412 /* BEGIN LOOP */
michael@0 413 for (var x = 0; x < size; x++) {
michael@0 414 pixels[y][x] = 0;
michael@0 415 }
michael@0 416 /* END LOOP */
michael@0 417 }
michael@0 418 /* END LOOP */
michael@0 419
michael@0 420 var _camera = new Camera(createVector(-40, 40, 40), createVector(0, 0, 0), createVector(0, 1, 0));
michael@0 421 _camera.render(_scene, pixels, size, size);
michael@0 422
michael@0 423 return pixels;
michael@0 424 }
michael@0 425
michael@0 426 function arrayToCanvasCommands(pixels)
michael@0 427 {
michael@0 428 var s = '<canvas id="renderCanvas" width="30px" height="30px"></canvas><scr' + 'ipt>\nvar pixels = [';
michael@0 429 var size = 30;
michael@0 430 /* BEGIN LOOP */
michael@0 431 for (var y = 0; y < size; y++) {
michael@0 432 s += "[";
michael@0 433 /* BEGIN LOOP */
michael@0 434 for (var x = 0; x < size; x++) {
michael@0 435 s += "[" + pixels[y][x] + "],";
michael@0 436 }
michael@0 437 /* END LOOP */
michael@0 438 s+= "],";
michael@0 439 }
michael@0 440 /* END LOOP */
michael@0 441 s += '];\n var canvas = document.getElementById("renderCanvas").getContext("2d");\n\
michael@0 442 \n\
michael@0 443 \n\
michael@0 444 var size = 30;\n\
michael@0 445 canvas.fillStyle = "red";\n\
michael@0 446 canvas.fillRect(0, 0, size, size);\n\
michael@0 447 canvas.scale(1, -1);\n\
michael@0 448 canvas.translate(0, -size);\n\
michael@0 449 \n\
michael@0 450 if (!canvas.setFillColor)\n\
michael@0 451 canvas.setFillColor = function(r, g, b, a) {\n\
michael@0 452 this.fillStyle = "rgb("+[Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)]+")";\n\
michael@0 453 }\n\
michael@0 454 \n\
michael@0 455 for (var y = 0; y < size; y++) {\n\
michael@0 456 for (var x = 0; x < size; x++) {\n\
michael@0 457 var l = pixels[y][x];\n\
michael@0 458 canvas.setFillColor(l[0], l[1], l[2], 1);\n\
michael@0 459 canvas.fillRect(x, y, 1, 1);\n\
michael@0 460 }\n\
michael@0 461 }</scr' + 'ipt>';
michael@0 462
michael@0 463 return s;
michael@0 464 }
michael@0 465
michael@0 466 testOutput = arrayToCanvasCommands(raytraceScene());

mercurial