browser/devtools/tilt/TiltWorkerCrafter.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 /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6 "use strict";
michael@0 7
michael@0 8 /**
michael@0 9 * Given the initialization data (sizes and information about
michael@0 10 * each DOM node) this worker sends back the arrays representing
michael@0 11 * vertices, texture coords, colors, indices and all the needed data for
michael@0 12 * rendering the DOM visualization mesh.
michael@0 13 *
michael@0 14 * Used in the TiltVisualization.Presenter object.
michael@0 15 */
michael@0 16 self.onmessage = function TWC_onMessage(event)
michael@0 17 {
michael@0 18 let data = event.data;
michael@0 19 let maxGroupNodes = parseInt(data.maxGroupNodes);
michael@0 20 let style = data.style;
michael@0 21 let texWidth = data.texWidth;
michael@0 22 let texHeight = data.texHeight;
michael@0 23 let nodesInfo = data.nodesInfo;
michael@0 24
michael@0 25 let mesh = {
michael@0 26 allVertices: [],
michael@0 27 groups: [],
michael@0 28 width: 0,
michael@0 29 height: 0
michael@0 30 };
michael@0 31
michael@0 32 let vertices;
michael@0 33 let texCoord;
michael@0 34 let color;
michael@0 35 let stacksIndices;
michael@0 36 let wireframeIndices;
michael@0 37 let index;
michael@0 38
michael@0 39 // seed the random function to get the same values each time
michael@0 40 // we're doing this to avoid ugly z-fighting with overlapping nodes
michael@0 41 self.random.seed(0);
michael@0 42
michael@0 43 // go through all the dom nodes and compute the verts, texcoord etc.
michael@0 44 for (let n = 0, len = nodesInfo.length; n < len; n++) {
michael@0 45
michael@0 46 // check if we need to start creating a new group
michael@0 47 if (n % maxGroupNodes === 0) {
michael@0 48 vertices = []; // recreate the arrays used to construct the 3D mesh data
michael@0 49 texCoord = [];
michael@0 50 color = [];
michael@0 51 stacksIndices = [];
michael@0 52 wireframeIndices = [];
michael@0 53 index = 0;
michael@0 54 }
michael@0 55
michael@0 56 let info = nodesInfo[n];
michael@0 57 let coord = info.coord;
michael@0 58
michael@0 59 // calculate the stack x, y, z, width and height coordinates
michael@0 60 let z = coord.depth + coord.thickness;
michael@0 61 let y = coord.top;
michael@0 62 let x = coord.left;
michael@0 63 let w = coord.width;
michael@0 64 let h = coord.height;
michael@0 65
michael@0 66 // the maximum texture size slices the visualization mesh where needed
michael@0 67 if (x + w > texWidth) {
michael@0 68 w = texWidth - x;
michael@0 69 }
michael@0 70 if (y + h > texHeight) {
michael@0 71 h = texHeight - y;
michael@0 72 }
michael@0 73
michael@0 74 x += self.random.next();
michael@0 75 y += self.random.next();
michael@0 76 w -= self.random.next() * 0.1;
michael@0 77 h -= self.random.next() * 0.1;
michael@0 78
michael@0 79 let xpw = x + w;
michael@0 80 let yph = y + h;
michael@0 81 let zmt = coord.depth;
michael@0 82
michael@0 83 let xotw = x / texWidth;
michael@0 84 let yoth = y / texHeight;
michael@0 85 let xpwotw = xpw / texWidth;
michael@0 86 let yphoth = yph / texHeight;
michael@0 87
michael@0 88 // calculate the margin fill color
michael@0 89 let fill = style[info.name] || style.highlight.defaultFill;
michael@0 90
michael@0 91 let r = fill[0];
michael@0 92 let g = fill[1];
michael@0 93 let b = fill[2];
michael@0 94 let g10 = r * 1.1;
michael@0 95 let g11 = g * 1.1;
michael@0 96 let g12 = b * 1.1;
michael@0 97 let g20 = r * 0.6;
michael@0 98 let g21 = g * 0.6;
michael@0 99 let g22 = b * 0.6;
michael@0 100
michael@0 101 // compute the vertices
michael@0 102 vertices.push(x, y, z, /* front */ // 0
michael@0 103 x, yph, z, // 1
michael@0 104 xpw, yph, z, // 2
michael@0 105 xpw, y, z, // 3
michael@0 106 // we don't duplicate vertices for the left and right faces, because
michael@0 107 // they can be reused from the bottom and top faces; we do, however,
michael@0 108 // duplicate some vertices from front face, because it has custom
michael@0 109 // texture coordinates which are not shared by the other faces
michael@0 110 x, y, z, /* front */ // 4
michael@0 111 x, yph, z, // 5
michael@0 112 xpw, yph, z, // 6
michael@0 113 xpw, y, z, // 7
michael@0 114 x, y, zmt, /* back */ // 8
michael@0 115 x, yph, zmt, // 9
michael@0 116 xpw, yph, zmt, // 10
michael@0 117 xpw, y, zmt); // 11
michael@0 118
michael@0 119 // compute the texture coordinates
michael@0 120 texCoord.push(xotw, yoth,
michael@0 121 xotw, yphoth,
michael@0 122 xpwotw, yphoth,
michael@0 123 xpwotw, yoth,
michael@0 124 -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0);
michael@0 125
michael@0 126 // compute the colors for each vertex in the mesh
michael@0 127 color.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
michael@0 128 g10, g11, g12,
michael@0 129 g10, g11, g12,
michael@0 130 g10, g11, g12,
michael@0 131 g10, g11, g12,
michael@0 132 g20, g21, g22,
michael@0 133 g20, g21, g22,
michael@0 134 g20, g21, g22,
michael@0 135 g20, g21, g22);
michael@0 136
michael@0 137 let i = index; // number of vertex points, used to create the indices array
michael@0 138 let ip1 = i + 1;
michael@0 139 let ip2 = ip1 + 1;
michael@0 140 let ip3 = ip2 + 1;
michael@0 141 let ip4 = ip3 + 1;
michael@0 142 let ip5 = ip4 + 1;
michael@0 143 let ip6 = ip5 + 1;
michael@0 144 let ip7 = ip6 + 1;
michael@0 145 let ip8 = ip7 + 1;
michael@0 146 let ip9 = ip8 + 1;
michael@0 147 let ip10 = ip9 + 1;
michael@0 148 let ip11 = ip10 + 1;
michael@0 149
michael@0 150 // compute the stack indices
michael@0 151 stacksIndices.unshift(i, ip1, ip2, i, ip2, ip3,
michael@0 152 ip8, ip9, ip5, ip8, ip5, ip4,
michael@0 153 ip7, ip6, ip10, ip7, ip10, ip11,
michael@0 154 ip8, ip4, ip7, ip8, ip7, ip11,
michael@0 155 ip5, ip9, ip10, ip5, ip10, ip6);
michael@0 156
michael@0 157 // compute the wireframe indices
michael@0 158 if (coord.thickness !== 0) {
michael@0 159 wireframeIndices.unshift(i, ip1, ip1, ip2,
michael@0 160 ip2, ip3, ip3, i,
michael@0 161 ip8, i, ip9, ip1,
michael@0 162 ip11, ip3, ip10, ip2);
michael@0 163 }
michael@0 164
michael@0 165 // there are 12 vertices in a stack representing a node
michael@0 166 index += 12;
michael@0 167
michael@0 168 // set the maximum mesh width and height to calculate the center offset
michael@0 169 mesh.width = Math.max(w, mesh.width);
michael@0 170 mesh.height = Math.max(h, mesh.height);
michael@0 171
michael@0 172 // check if we need to save the currently active group; this happens after
michael@0 173 // we filled all the "slots" in a group or there aren't any remaining nodes
michael@0 174 if (((n + 1) % maxGroupNodes === 0) || (n === len - 1)) {
michael@0 175 mesh.groups.push({
michael@0 176 vertices: vertices,
michael@0 177 texCoord: texCoord,
michael@0 178 color: color,
michael@0 179 stacksIndices: stacksIndices,
michael@0 180 wireframeIndices: wireframeIndices
michael@0 181 });
michael@0 182 mesh.allVertices = mesh.allVertices.concat(vertices);
michael@0 183 }
michael@0 184 }
michael@0 185
michael@0 186 self.postMessage(mesh);
michael@0 187 close();
michael@0 188 };
michael@0 189
michael@0 190 /**
michael@0 191 * Utility functions for generating random numbers using the Alea algorithm.
michael@0 192 */
michael@0 193 self.random = {
michael@0 194
michael@0 195 /**
michael@0 196 * The generator function, automatically created with seed 0.
michael@0 197 */
michael@0 198 _generator: null,
michael@0 199
michael@0 200 /**
michael@0 201 * Returns a new random number between [0..1)
michael@0 202 */
michael@0 203 next: function RNG_next()
michael@0 204 {
michael@0 205 return this._generator();
michael@0 206 },
michael@0 207
michael@0 208 /**
michael@0 209 * From http://baagoe.com/en/RandomMusings/javascript
michael@0 210 * Johannes Baagoe <baagoe@baagoe.com>, 2010
michael@0 211 *
michael@0 212 * Seeds a random generator function with a set of passed arguments.
michael@0 213 */
michael@0 214 seed: function RNG_seed()
michael@0 215 {
michael@0 216 let s0 = 0;
michael@0 217 let s1 = 0;
michael@0 218 let s2 = 0;
michael@0 219 let c = 1;
michael@0 220
michael@0 221 if (arguments.length === 0) {
michael@0 222 return this.seed(+new Date());
michael@0 223 } else {
michael@0 224 s0 = this.mash(" ");
michael@0 225 s1 = this.mash(" ");
michael@0 226 s2 = this.mash(" ");
michael@0 227
michael@0 228 for (let i = 0, len = arguments.length; i < len; i++) {
michael@0 229 s0 -= this.mash(arguments[i]);
michael@0 230 if (s0 < 0) {
michael@0 231 s0 += 1;
michael@0 232 }
michael@0 233 s1 -= this.mash(arguments[i]);
michael@0 234 if (s1 < 0) {
michael@0 235 s1 += 1;
michael@0 236 }
michael@0 237 s2 -= this.mash(arguments[i]);
michael@0 238 if (s2 < 0) {
michael@0 239 s2 += 1;
michael@0 240 }
michael@0 241 }
michael@0 242
michael@0 243 let random = function() {
michael@0 244 let t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
michael@0 245 s0 = s1;
michael@0 246 s1 = s2;
michael@0 247 return (s2 = t - (c = t | 0));
michael@0 248 };
michael@0 249 random.uint32 = function() {
michael@0 250 return random() * 0x100000000; // 2^32
michael@0 251 };
michael@0 252 random.fract53 = function() {
michael@0 253 return random() +
michael@0 254 (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
michael@0 255 };
michael@0 256 return (this._generator = random);
michael@0 257 }
michael@0 258 },
michael@0 259
michael@0 260 /**
michael@0 261 * From http://baagoe.com/en/RandomMusings/javascript
michael@0 262 * Johannes Baagoe <baagoe@baagoe.com>, 2010
michael@0 263 */
michael@0 264 mash: function RNG_mash(data)
michael@0 265 {
michael@0 266 let h, n = 0xefc8249d;
michael@0 267
michael@0 268 for (let i = 0, data = data.toString(), len = data.length; i < len; i++) {
michael@0 269 n += data.charCodeAt(i);
michael@0 270 h = 0.02519603282416938 * n;
michael@0 271 n = h >>> 0;
michael@0 272 h -= n;
michael@0 273 h *= n;
michael@0 274 n = h >>> 0;
michael@0 275 h -= n;
michael@0 276 n += h * 0x100000000; // 2^32
michael@0 277 }
michael@0 278 return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
michael@0 279 }
michael@0 280 };

mercurial