diff --git a/src/image-target/aframe.js b/src/image-target/aframe.js index bbfbbb13..3ade3f91 100644 --- a/src/image-target/aframe.js +++ b/src/image-target/aframe.js @@ -53,6 +53,7 @@ AFRAME.registerSystem('mindar-image-system', { track.stop(); }); this.video.remove(); + this.controller.dispose(); }, pause: function(keepVideo=false) { @@ -229,6 +230,10 @@ AFRAME.registerComponent('mindar-image', { arSystem.start(); }); } + }, + remove: function () { + const arSystem = this.el.sceneEl.systems['mindar-image-system']; + arSystem.stop(); } }); diff --git a/src/image-target/controller.js b/src/image-target/controller.js index 91156cc9..b6bec947 100644 --- a/src/image-target/controller.js +++ b/src/image-target/controller.js @@ -110,6 +110,13 @@ class Controller { return {dimensions: dimensions, matchingDataList, trackingDataList}; } + dispose() { + this.stopProcessVideo(); + this.worker.postMessage({ + type: "dispose" + }); + } + // warm up gpu - build kernels is slow dummyRun(input) { const inputT = this.inputLoader.loadInput(input); diff --git a/src/image-target/controller.worker.js b/src/image-target/controller.worker.js index eeadd63f..12d4ea8c 100644 --- a/src/image-target/controller.worker.js +++ b/src/image-target/controller.worker.js @@ -1,5 +1,5 @@ -import {Matcher} from './matching/matcher.js'; -import {Estimator} from './estimation/estimator.js'; +import { Matcher } from './matching/matcher.js'; +import { Estimator } from './estimation/estimator.js'; let projectionTransform = null; let matchingDataList = null; @@ -8,53 +8,64 @@ let matcher = null; let estimator = null; onmessage = (msg) => { - const {data} = msg; - - if (data.type === 'setup') { - projectionTransform = data.projectionTransform; - matchingDataList = data.matchingDataList; - debugMode = data.debugMode; - matcher = new Matcher(data.inputWidth, data.inputHeight, debugMode); - estimator = new Estimator(data.projectionTransform); - } - else if (data.type === 'match') { - const interestedTargetIndexes = data.targetIndexes; + 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; - let matchedTargetIndex = -1; - let matchedModelViewTransform = null; - let matchedDebugExtra = null; + case "match": + const interestedTargetIndexes = data.targetIndexes; - for (let i = 0; i < interestedTargetIndexes.length; i++) { - const matchingIndex = interestedTargetIndexes[i]; + let matchedTargetIndex = -1; + let matchedModelViewTransform = null; + let matchedDebugExtra = null; - const {keyframeIndex, screenCoords, worldCoords, debugExtra} = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints); - matchedDebugExtra = debugExtra; + for (let i = 0; i < interestedTargetIndexes.length; i++) { + const matchingIndex = interestedTargetIndexes[i]; - if (keyframeIndex !== -1) { - const modelViewTransform = estimator.estimate({screenCoords, worldCoords}); + const { keyframeIndex, screenCoords, worldCoords, debugExtra } = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints); + matchedDebugExtra = debugExtra; - if (modelViewTransform) { - matchedTargetIndex = matchingIndex; - matchedModelViewTransform = modelViewTransform; - } - break; + 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 - }); - } - else if (data.type === 'trackUpdate') { - const {modelViewTransform, worldCoords, screenCoords} = data; - const finalModelViewTransform = estimator.refineEstimate({initialModelViewTransform: modelViewTransform, worldCoords, screenCoords}); - postMessage({ - type: 'trackUpdateDone', - modelViewTransform: finalModelViewTransform, - }); + + 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}'`); } };