-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreejs_worker_ES6_jsfeatNext.js
More file actions
269 lines (225 loc) · 7.33 KB
/
threejs_worker_ES6_jsfeatNext.js
File metadata and controls
269 lines (225 loc) · 7.33 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var oWidth = window.innerWidth;
var oHeight = window.innerHeight;
//var oWidth = 640;
//var oHeight = 480;
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 color2gray(data, videoSize) {
let q = 0;
let gray = new Uint8Array(videoSize);
// Create luma from video data assuming Pixelformat AR_PIXEL_FORMAT_RGBA
// see (ARToolKitJS.cpp L: 43)
for (let p = 0; p < videoSize; p++) {
let r = data[q + 0],
g = data[q + 1],
b = data[q + 2];
// @see https://stackoverflow.com/a/596241/5843642
gray[p] = (r + r + r + b + g + g + g + g) >> 3;
q += 4;
}
return gray;
}
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 imageData;
var jsfeat = jsfeatNext.jsfeatNext;
var imgproc = new jsfeat.imgproc();
var img_u8 = new jsfeat.matrix_t(input_width, input_height, jsfeat.U8_t | jsfeat.C1_t);
//var img_u8 = new jsfeat.matrix_t(oWidth, oHeight, jsfeat.U8_t | jsfeat.C1_t);
//var grayV = new Uint8ClampedArray(input_width * input_height);
//var grayV = new Uint8ClampedArray(oWidth * oHeight);
//var canvas_process = document.getElementById('canvas_process');
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(1, 1, 1);
root.matrixAutoUpdate = false;
root.add(sphere);
var load = function () {
vw = input_width;
vh = input_height;
//vw = oWidth;
//vh = oHeight;
sscale = isMobile() ? window.outerWidth / input_width : 1;
//sscale = isMobile() ? window.outerWidth / oWidth : 1;
sw = vw * sscale;
sh = vh * sscale;
w = vw;
h = vh;
pw = vw;
ph = vh;
ox = 0;
oy = 0;
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('./worker_jsfeatNext.js')
//const refIm = document.getElementById("refIm");
const image_H = 2048;
const image_W = 1637;
var type = setTrackerType();
const loadImage = (URL) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, img.width, img.height);
let img_u8_tracker = new jsfeat.matrix_t(img.width, img.height, jsfeat.U8_t | jsfeat.C1_t);
imgproc.grayscale(imageData.data, img.width, img.height, img_u8_tracker);
worker.postMessage({
type: "initTracker",
trackerType: type,
imageData: img_u8_tracker.data,
imgWidth: img.width,
imgHeight: img.height,
videoWidth: vw,
videoHeight: vh,
}, [img_u8_tracker.data.buffer]);
resolve();
};
img.onerror = reject;
img.src = URL;
});
}
loadImage(markerUrl)
worker.onmessage = function (ev) {
var msg = ev.data;
switch (msg.type) {
case "loadedTracker": {
//console.log(msg)
var proj = JSON.parse(msg.cameraProjMat);
//var proj = [1.9102363924347978, 0, 0, 0, 0, 2.5377457054523322, 0, 0, -0.013943280545895442, -0.005830389685211879, -1.0000002000000199, -1, 0, 0, -0.00020000002000000202, 0];
//console.log("proj: ", 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);
//process();
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);
//process();
break;
}
case 'not found': {
found(null);
//process();
break;
}
}
track_update();
process();
};
};
var world;
var found = function (msg) {
if (!msg) {
world = null;
} else {
world = JSON.parse(msg.pose);
//world = JSON.parse(msg.matrixGL_RH);
//world = JSON.parse(msg.viewMatrix_GL);
}
};
//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 = 1;
//sphere.position.x = 1;
//sphere.position.z = 1;
// set matrix of 'root' by detected 'world' matrix
//console.log("world: ", world);
//var world2= [0.04984269657942322, 0.0011028004165823837, 0.0037468644060579515, 0, -0.00015674864315588379, 0.048456810395189856, -0.012054592420455989, 0, -0.003895003003705642, 0.012004841145274035, 0.04830878467734379, 0, -5.418834804971434, -3.6673568534354173, -10.857604385997499, 1];
setMatrix(root.matrix, world);
}
renderer.render(scene, camera);
};
const process = function () {
context_process.fillStyle = 'black';
//console.log("vw, vh, pw, ph, ox, oy: ",vw, vh, pw, ph, ox, oy);
//context_process.fillRect(0, 0, pw, ph);
context_process.fillRect(0, 0, vw, vh);
//context_process.drawImage(video, 0, 0, vw, vh, ox, oy, w, h);
context_process.drawImage(video, 0, 0, vw, vh);
const imageData = context_process.getImageData(0, 0, vw, vh);
imgproc.grayscale(imageData.data, vw, vh, img_u8);
//console.log(img_u8)
worker.postMessage({ type: 'process', imagedata: img_u8.data });
}
var tick = function () {
draw();
//update();
requestAnimationFrame(tick);
};
load();
tick();
//process();
}