-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
threejs_worker_ES6.js
180 lines (152 loc) · 4.47 KB
/
threejs_worker_ES6.js
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
function isMobile () {
return /Android|mobile|iPad|iPhone/i.test(navigator.userAgent);
}
var setMatrix = function (matrix, value) {
var array = [];
for (var key in value) {
array[key] = value[key];
}
if (typeof matrix.elements.set === "function") {
matrix.elements.set(array);
} else {
matrix.elements = [].slice.call(array);
}
};
function start(markerUrl, video, input_width, input_height, render_update, track_update) {
var vw, vh;
var sw, sh;
var pscale, sscale;
var w, h;
var pw, ph;
var ox, oy;
var worker;
var camera_para = './../examples/Data/camera_para.dat'
var canvas_process = document.createElement('canvas');
var context_process = canvas_process.getContext('2d', { willReadFrequently: true });
var targetCanvas = document.querySelector("#canvas");
var renderer = new THREE.WebGLRenderer({ canvas: targetCanvas, alpha: true, antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
var scene = new THREE.Scene();
var camera = new THREE.Camera();
camera.matrixAutoUpdate = false;
scene.add(camera);
var sphere = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 8, 8),
new THREE.MeshNormalMaterial()
);
var root = new THREE.Object3D();
scene.add(root);
var marker;
sphere.material.flatShading;
sphere.scale.set(200, 200, 200);
root.matrixAutoUpdate = false;
root.add(sphere);
var load = function () {
vw = input_width;
vh = input_height;
pscale = 320 / Math.max(vw, vh / 3 * 4);
sscale = isMobile() ? window.outerWidth / input_width : 1;
sw = vw * sscale;
sh = vh * sscale;
w = vw * pscale;
h = vh * pscale;
pw = Math.max(w, h / 3 * 4);
ph = Math.max(h, w / 4 * 3);
ox = (pw - w) / 2;
oy = (ph - h) / 2;
canvas_process.style.clientWidth = pw + "px";
canvas_process.style.clientHeight = ph + "px";
canvas_process.width = pw;
canvas_process.height = ph;
renderer.setSize(sw, sh);
worker = new Worker('../js/artoolkitNFT_ES6.worker.js')
worker.postMessage({ type: "load", pw: pw, ph: ph, camera_para: camera_para, marker: markerUrl });
worker.onmessage = function (ev) {
var msg = ev.data;
switch (msg.type) {
case "loaded": {
var proj = JSON.parse(msg.proj);
var ratioW = pw / w;
var ratioH = ph / h;
proj[0] *= ratioW;
proj[4] *= ratioW;
proj[8] *= ratioW;
proj[12] *= ratioW;
proj[1] *= ratioH;
proj[5] *= ratioH;
proj[9] *= ratioH;
proj[13] *= ratioH;
setMatrix(camera.projectionMatrix, proj);
break;
}
case "endLoading": {
if (msg.end == true) {
// removing loader page if present
var loader = document.getElementById('loading');
if (loader) {
loader.querySelector('.loading-text').innerText = 'Start the tracking!';
setTimeout(function(){
loader.parentElement.removeChild(loader);
}, 2000);
}
}
break;
}
case 'found': {
found(msg);
break;
}
case 'not found': {
found(null);
break;
}
case 'markerInfos': {
marker = msg.marker;
}
}
track_update();
process();
};
};
var world;
var found = function (msg) {
if (!msg) {
world = null;
} else {
world = JSON.parse(msg.matrixGL_RH);
}
};
var lasttime = Date.now();
var time = 0;
var draw = function () {
render_update();
var now = Date.now();
var dt = now - lasttime;
time += dt;
lasttime = now;
if (!world) {
sphere.visible = false;
} else {
sphere.visible = true;
sphere.position.y = ((marker.height / marker.dpi) * 2.54 * 10) / 2.0;
sphere.position.x = ((marker.width / marker.dpi) * 2.54 * 10) / 2.0;
// set matrix of 'root' by detected 'world' matrix
setMatrix(root.matrix, world);
}
renderer.render(scene, camera);
};
var process = function () {
context_process.fillStyle = 'black';
context_process.fillRect(0, 0, pw, ph);
context_process.drawImage(video, 0, 0, vw, vh, ox, oy, w, h);
var imageData = context_process.getImageData(0, 0, pw, ph);
worker.postMessage({ type: 'process', imagedata: imageData }, [imageData.data.buffer]);
}
var tick = function () {
draw();
requestAnimationFrame(tick);
};
load();
tick();
process();
}