-
-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathcontroller.worker.js
71 lines (57 loc) · 2.08 KB
/
controller.worker.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
import { Matcher } from './matching/matcher.js';
import { Estimator } from './estimation/estimator.js';
let projectionTransform = null;
let matchingDataList = null;
let debugMode = false;
let matcher = null;
let estimator = null;
onmessage = (msg) => {
const { data } = msg;
switch (data.type) {
case "setup":
projectionTransform = data.projectionTransform;
matchingDataList = data.matchingDataList;
debugMode = data.debugMode;
matcher = new Matcher(data.inputWidth, data.inputHeight, debugMode);
estimator = new Estimator(data.projectionTransform);
break;
case "match":
const interestedTargetIndexes = data.targetIndexes;
let matchedTargetIndex = -1;
let matchedModelViewTransform = null;
let matchedDebugExtra = null;
for (let i = 0; i < interestedTargetIndexes.length; i++) {
const matchingIndex = interestedTargetIndexes[i];
const { keyframeIndex, screenCoords, worldCoords, debugExtra } = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints);
matchedDebugExtra = debugExtra;
if (keyframeIndex !== -1) {
const modelViewTransform = estimator.estimate({ screenCoords, worldCoords });
if (modelViewTransform) {
matchedTargetIndex = matchingIndex;
matchedModelViewTransform = modelViewTransform;
}
break;
}
}
postMessage({
type: 'matchDone',
targetIndex: matchedTargetIndex,
modelViewTransform: matchedModelViewTransform,
debugExtra: matchedDebugExtra
});
break;
case 'trackUpdate':
const { modelViewTransform, worldCoords, screenCoords } = data;
const finalModelViewTransform = estimator.refineEstimate({ initialModelViewTransform: modelViewTransform, worldCoords, screenCoords });
postMessage({
type: 'trackUpdateDone',
modelViewTransform: finalModelViewTransform,
});
break;
case "dispose":
close();
break;
default:
throw new Error(`Invalid message type '${data.type}'`);
}
};