【问题标题】:How to detect when a marker is found in AR.js如何检测何时在 AR.js 中找到标记
【发布时间】:2017-12-01 15:16:54
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: three.js aframe artoolkit ar.js


    【解决方案1】:

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

    • markerFound
    • markerLost

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

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

    通过这样的简单设置:

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

    例如here。 请注意,从 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

    【讨论】:

    • 也相关,github.com/jeromeetienne/AR.js/issues/217github.com/jeromeetienne/AR.js/pull/303。这需要在 AR.js 库中修复;在那之前,如果不积极检查可见性是不可能的。
    • @DonMcCurdy 感谢您提供的信息,我已经订阅了拉取请求(#303),如果它被拉取,我会修改答案 :)
    • @DonMcCurdy 感谢您的提示,因为分支与 Jerome 的 dev 分支合并,我已经修改了答案。
    • three.js 版本也有这个吗?
    【解决方案2】:

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

    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-10
      • 1970-01-01
      • 2018-10-06
      • 2020-09-30
      • 2020-01-19
      • 2018-05-18
      • 2021-08-29
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多