4

I'm trying to detect when a marker if found/lost in ar.js, while using a-frame.

From what I see in the source code, when the marker is found, a 'getMarker' event should be fired, moreover artoolkit seems to dispatch a markerFound event.

I tried to listen to those events on the <a-scene>, or on the <a-marker>, but it seems I'm either mistaken, or i need to get deeper to the arController, or arToolkit objects.

When i log the scene, or the marker, i only get references to the attributes, which don't seem to have the above objects attached.(like marker.arController, or marker.getAttribute('artoolkitmarker').arController)

Did anybody tried this and have any tips how to do this ?

Piotr Adam Milewski
  • 14,150
  • 3
  • 21
  • 42

2 Answers2

12

PR303 introduces events when a marker is found and lost

  • markerFound
  • markerLost

You can use them by simply adding an event listener:

anchorRef.addEventListener("markerFound", (e)=>{ // your code here}

with a simple setup like this:

<a-marker id="anchor">
  <a-entity>
</a-marker>

example here. Please note, that as of sep 18', you need to use the dev branch to use the above.


ORIGINAL ANWSER - in case you want to do it manually

Detecting if the marker is found is possible by checking if the marker is visible when needed (other event, or on tick): if(document.querySelector("a-marker").object3D.visible == true)

For example:

init: function() {
   this.marker = document.querySelector("a-marker")
   this.markerVisible = false
},
tick: function() {
   if (!this.marker) return
   if (this.marker.object3D.visible) {
      if (!this.markerVisible) {
         // marker detected
         this.markerVisible = true
      }
   } else {
      if (this.markerVisbile) {
         // lost sight of the marker
         this.markerVisible = false
      }
   }
}


As adrian li noted, it doesn't work with a-marker-camera, only with a-markers
Piotr Adam Milewski
  • 14,150
  • 3
  • 21
  • 42
  • 1
    Also related, https://github.com/jeromeetienne/AR.js/issues/217 and https://github.com/jeromeetienne/AR.js/pull/303. This needs to be fixed within the AR.js library; until then it isn't possible without checking visibility aggressively. – Don McCurdy May 01 '18 at 17:37
  • @DonMcCurdy thanks for the information, i've subscribed to the pull request (#303), if it gets pulled, i'll modify the anwser :) – Piotr Adam Milewski May 01 '18 at 19:18
  • 1
    @DonMcCurdy thanks for the tip, i've modified the anwser as the branch was merged with Jerome's `dev` branch. – Piotr Adam Milewski Sep 24 '18 at 17:52
  • is there this also for three.js version? – Suisse May 27 '21 at 18:17
0

I went with a dirty hack diving into the internals, bear in mind that what I've provided might not suffice because the event get's called every time the marker is found, sadly I couldn't find an event to hook into for markers being lost.

const arController = document.querySelector("a-scene").systems.arjs._arSession.arContext.arController;

arController.addEventListener("getMarker", (evt) => {
    const markerType = evt.data.type;
    const patternType = 0;

    //console.log("onMarkerFound!!");

    if (markerType == patternType) {
        //console.log("onMarkerFound out pattern!!");

        //Do stuff...
    }
});
janpio
  • 10,645
  • 16
  • 64
  • 107
Steakeye
  • 71
  • 1
  • 8