如何检测何时在 AR.js 中找到标记 [英] How to detect when a marker is found in AR.js
问题描述
我正在尝试检测标记是否在 ar.js 中找到/丢失,同时使用 a-frame.
I'm trying to detect when a marker if found/lost in ar.js, while using a-frame.
从我在 源中看到的代码,当找到标记时,应该触发'getMarker'事件,而且artoolkit似乎调度了一个markerFound事件.
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.
我试图在
或
上收听这些事件,但似乎我要么错了,或者我需要更深入地了解 arController
或 arToolkit
对象.
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.
当我记录场景或标记时,我只获得对属性的引用,这些属性似乎没有附加上述对象.(如 marker.arController
或 marker.getAttribute('artoolkitmarker').arController
)
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 ?
推荐答案
PR303 在标记被发现和丢失时引入事件
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}
简单的设置如下:
<a-marker id="anchor">
<a-entity>
</a-marker>
示例 此处.请注意,从 9 月 18 日开始,您需要使用 dev
分支才能使用上述内容.<小时>原始ANWSER - 如果您想手动进行
通过检查标记在需要时是否可见(其他事件或勾选时)可以检测是否找到标记:
if(document.querySelector("a-marker").object3D.visible == true)代码>
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)
例如:
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
}
}
}
<小时>正如阿德里安李指出的那样,它不适用于 a-marker-camera
,仅适用于 a-markers
这篇关于如何检测何时在 AR.js 中找到标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!