4

我正在尝试检测标记是否在 ar.js 中找到/丢失,同时使用 a-frame。

从我在源代码中看到的,当找到标记时,应该触发一个“getMarker”事件,而且artoolkit 似乎调度了一个markerFound 事件。

<a-scene>我试图在或 上收听这些事件<a-marker>,但似乎我错了,或者我需要更深入地了解arController、 或arToolkit对象。

当我记录场景或标记时,我只获得对属性的引用,这些属性似乎没有附加上述对象。(如marker.arController,或marker.getAttribute('artoolkitmarker').arController

有没有人试过这个并且有任何提示如何做到这一点?

4

2 回答 2

11

PR303在找到和丢失标记时引入事件

  • markerFound
  • markerLost

您可以通过简单地添加事件侦听器来使用它们:

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

通过这样的简单设置:

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

这里的例子。请注意,从 9 月 18 日开始,您需要使用dev分支才能使用上述内容。


ORIGINAL ANWSER - 如果您想手动操作

通过检查标记是否在需要时可见(其他事件或刻度线)来检测是否找到标记是可能的:if(document.querySelector("a-marker").object3D.visible == true)

例如:

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
      }
   }
}


正如 adrian li 指出的那样,它不适用于a-marker-camera,仅适用于a-markers

于 2017-06-28T17:49:46.230 回答
0

我对内部进行了肮脏的黑客攻击,请记住,我提供的内容可能还不够,因为每次找到标记时都会调用事件获取,遗憾的是我找不到一个事件来挂钩标记丢失.

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...
    }
});
于 2018-08-07T00:55:13.410 回答