|
1 |
| -importScripts('../dist/ARToolkitNFT.js') |
| 1 | +var browser = (function () { |
| 2 | + var test = function (regexp) { |
| 3 | + return regexp.test(navigator.userAgent); |
| 4 | + }; |
| 5 | + switch (true) { |
| 6 | + case test(/edg/i): |
| 7 | + return "Microsoft Edge"; |
| 8 | + case test(/trident/i): |
| 9 | + return "Microsoft Internet Explorer"; |
| 10 | + case test(/firefox|fxios/i): |
| 11 | + return "Mozilla Firefox"; |
| 12 | + case test(/opr\//i): |
| 13 | + return "Opera"; |
| 14 | + case test(/ucbrowser/i): |
| 15 | + return "UC Browser"; |
| 16 | + case test(/samsungbrowser/i): |
| 17 | + return "Samsung Browser"; |
| 18 | + case test(/chrome|chromium|crios/i): |
| 19 | + return "Google Chrome"; |
| 20 | + case test(/safari/i): |
| 21 | + return "Apple Safari"; |
| 22 | + default: |
| 23 | + return "Other"; |
| 24 | + } |
| 25 | +})(); |
| 26 | + |
| 27 | +if (browser == "Apple Safari") { |
| 28 | + importScripts("../dist/ARToolkitNFT.js"); |
| 29 | +} else { |
| 30 | + importScripts("../dist/ARToolkitNFT_simd.js"); |
| 31 | +} |
| 32 | +// Import OneEuroFilter class into the worker. |
| 33 | +importScripts("./one-euro-filter.js"); |
2 | 34 |
|
3 | 35 | self.onmessage = function (e) {
|
4 |
| - var msg = e.data |
| 36 | + var msg = e.data; |
5 | 37 | switch (msg.type) {
|
6 |
| - case 'load': { |
7 |
| - load(msg) |
8 |
| - return |
| 38 | + case "load": { |
| 39 | + load(msg); |
| 40 | + return; |
9 | 41 | }
|
10 |
| - case 'process': { |
11 |
| - next = msg.imagedata |
12 |
| - process() |
| 42 | + case "process": { |
| 43 | + next = msg.imagedata; |
| 44 | + process(); |
13 | 45 | }
|
14 | 46 | }
|
15 |
| -} |
| 47 | +}; |
16 | 48 |
|
17 |
| -var next = null |
18 |
| -var ar = null |
19 |
| -var markerResult = null |
| 49 | +var next = null; |
| 50 | +var ar = null; |
| 51 | +var markerResult = null; |
| 52 | +var marker; |
20 | 53 |
|
21 |
| -function load (msg) { |
22 |
| - console.debug('Loading marker at: ', msg.marker) |
| 54 | +const WARM_UP_TOLERANCE = 5; |
| 55 | +let tickCount = 0; |
| 56 | + |
| 57 | +// initialize the OneEuroFilter |
| 58 | +var oef = true; |
| 59 | +let filterMinCF = 0.0001; |
| 60 | +let filterBeta = 0.01; |
| 61 | +const filter = new OneEuroFilter({ |
| 62 | + minCutOff: filterMinCF, |
| 63 | + beta: filterBeta, |
| 64 | +}); |
| 65 | + |
| 66 | +function oefFilter(matrixGL_RH) { |
| 67 | + tickCount += 1; |
| 68 | + var mat; |
| 69 | + if (tickCount > WARM_UP_TOLERANCE) { |
| 70 | + mat = filter.filter(Date.now(), matrixGL_RH); |
| 71 | + } else { |
| 72 | + mat = matrixGL_RH; |
| 73 | + } |
| 74 | + return mat; |
| 75 | +} |
| 76 | + |
| 77 | +function load(msg) { |
| 78 | + console.debug("Loading marker at: ", msg.marker); |
23 | 79 |
|
24 | 80 | var onLoad = function (arController) {
|
25 |
| - ar = arController |
26 |
| - var cameraMatrix = ar.getCameraMatrix() |
27 |
| - |
28 |
| - ar.addEventListener('getNFTMarker', function (ev) { |
29 |
| - markerResult = { type: 'found', matrixGL_RH: JSON.stringify(ev.data.matrixGL_RH)} |
30 |
| - }) |
31 |
| - |
32 |
| - ar.loadNFTMarker(msg.marker).then(function (nft) { |
33 |
| - ar.trackNFTMarkerId(nft.id) |
34 |
| - console.log('loadNFTMarker -> ', nft.id) |
35 |
| - console.log('nftMarker struct: ', nft) |
36 |
| - postMessage({ type: 'endLoading', end: true }) |
| 81 | + ar = arController; |
| 82 | + var cameraMatrix = ar.getCameraMatrix(); |
| 83 | + |
| 84 | + ar.addEventListener("getNFTMarker", function (ev) { |
| 85 | + var mat; |
| 86 | + if (oef == true) { |
| 87 | + mat = oefFilter(ev.data.matrixGL_RH); |
| 88 | + } else { |
| 89 | + mat = ev.data.matrixGL_RH; |
| 90 | + } |
| 91 | + markerResult = { |
| 92 | + type: "found", |
| 93 | + matrixGL_RH: JSON.stringify(mat), |
| 94 | + }; |
| 95 | + }); |
| 96 | + |
| 97 | + ar.addEventListener("lostNFTMarker", function (ev) { |
| 98 | + filter.reset(); |
| 99 | + }); |
| 100 | + |
| 101 | + ar.loadNFTMarker(msg.marker, function (id) { |
| 102 | + ar.trackNFTMarkerId(id); |
| 103 | + let marker = ar.getNFTData(0); |
| 104 | + console.log("nftMarker data: ", marker); |
| 105 | + postMessage({ |
| 106 | + type: "markerInfos", |
| 107 | + marker: marker, |
| 108 | + }); |
| 109 | + console.log("loadNFTMarker -> ", id); |
| 110 | + postMessage({ |
| 111 | + type: "endLoading", |
| 112 | + end: true, |
| 113 | + }); |
37 | 114 | }).catch(function (err) {
|
38 |
| - console.log('Error in loading marker on Worker', err) |
39 |
| - }) |
| 115 | + console.log("Error in loading marker on Worker", err); |
| 116 | + }); |
40 | 117 |
|
41 |
| - postMessage({ type: 'loaded', proj: JSON.stringify(cameraMatrix) }) |
42 |
| - } |
| 118 | + postMessage({ |
| 119 | + type: "loaded", |
| 120 | + proj: JSON.stringify(cameraMatrix), |
| 121 | + }); |
| 122 | + }; |
43 | 123 |
|
44 | 124 | var onError = function (error) {
|
45 |
| - console.error(error) |
46 |
| - } |
| 125 | + console.error(error); |
| 126 | + }; |
47 | 127 |
|
48 |
| - console.debug('Loading camera at:', msg.camera_para) |
| 128 | + console.debug("Loading camera at:", msg.camera_para); |
49 | 129 |
|
50 | 130 | // we cannot pass the entire ARControllerNFT, so we re-create one inside the Worker, starting from camera_param
|
51 |
| - ARToolkitNFT.ARControllerNFT.initWithDimensions(msg.pw, msg.ph, msg.camera_para).then(onLoad).catch(onError) |
| 131 | + ARControllerNFT.initWithDimensions(msg.pw, msg.ph, msg.camera_para) |
| 132 | + .then(onLoad) |
| 133 | + .catch(onError); |
52 | 134 | }
|
53 | 135 |
|
54 |
| -function process () { |
55 |
| - markerResult = null |
| 136 | +function process() { |
| 137 | + markerResult = null; |
56 | 138 |
|
57 | 139 | if (ar && ar.process) {
|
58 |
| - ar.process(next) |
| 140 | + ar.process(next); |
59 | 141 | }
|
60 | 142 |
|
61 | 143 | if (markerResult) {
|
62 |
| - postMessage(markerResult) |
| 144 | + postMessage(markerResult); |
63 | 145 | } else {
|
64 |
| - postMessage({ type: 'not found' }) |
| 146 | + postMessage({ |
| 147 | + type: "not found", |
| 148 | + }); |
65 | 149 | }
|
66 | 150 |
|
67 |
| - next = null |
| 151 | + next = null; |
68 | 152 | }
|
0 commit comments