-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathworker.js
More file actions
73 lines (63 loc) · 1.98 KB
/
worker.js
File metadata and controls
73 lines (63 loc) · 1.98 KB
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
importScripts('gridhopping.js');
importScripts('polygonize.js');
importScripts('extensions.js');
const cuboid = primitives3d.cuboid;
const sphere = primitives3d.sphere;
const capsule = primitives3d.capsule;
const torus = primitives3d.torus;
const cylinder = primitives3d.cylinder;
const plane = primitives3d.plane;
const cone = primitives3d.cone;
const octahedron = primitives3d.octahedron;
const union = ops.union;
const intersection = ops.intersection;
const difference = ops.difference;
const translate = ops.translate;
const rotate = ops.rotate;
const scale = ops.scale;
const shell = ops.shell;
const extrusion = ops.extrusion;
const round = ops.round;
onmessage = function(event) {
console.log('* <worker.js> message received from main script ...');
const job = event.data;
let t0, t1, log;
const triangles = [], normals = [];
function emit_triang(v0, v1, v2)
{
triangles.push([v2, v1, v0]);
}
function compute_normals(sde_func, eps)
{
for (let i=0; i<triangles.length; ++i)
normals.push([
get_normal_at(sde_func, triangles[i][0][0], triangles[i][0][1], triangles[i][0][2], eps),
get_normal_at(sde_func, triangles[i][1][0], triangles[i][1][1], triangles[i][1][2], eps),
get_normal_at(sde_func, triangles[i][2][0], triangles[i][2][1], triangles[i][2][2], eps)
]);
return normals;
}
try {
const sde_func = new Function(job.sde_func)();
t0 = performance.now();
polygonize_grid_fast(sde_func, job.side, job.n, polygonize_cell_mc, emit_triang);
t1 = performance.now();
log = "Generated " + triangles.length + " triangles in " + Math.trunc(t1 - t0) + " miliseconds.";
if (job.normals)
{
t0 = performance.now();
const eps = job.side/(2*job.n);
compute_normals(sde_func, eps);
t1 = performance.now();
log += " Normals computed in " + (t1 - t0) + " miliseconds.";
}
}
catch (e) {
log = e.toString().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
postMessage({
log: log,
triangles: triangles,
normals: normals
});
}