hiukim / mind-ar-js

Web Augmented Reality. Image Tracking, Face Tracking. Tensorflow.js
MIT License
2.13k stars 394 forks source link

Anchor identification when emits onTargetFound #408

Closed adilsonv77 closed 1 year ago

adilsonv77 commented 1 year ago

In image-target/three.js, line 161, I identified that any information is send to onTargetFound.

With multiple-targets, I´m interesting to identifiy which target emitted this event. How can I do this? Suggestions...

hiukim commented 1 year ago

onTargetFound is bind to specific anchor already

adilsonv77 commented 1 year ago

Ok. How can access It?

hiukim commented 1 year ago
anchor.onTargetFound = () => {
}
adilsonv77 commented 1 year ago

OK... I understood your point. However, I´m working with multi targets, dinamically loaded from a database.

I have this piece of code, to adding an anchor for each target. When the target is detected, then show a button (in thruth is an hyperlink) in the page. Each target must change the value of the parameter from the hyperlink.

for (var i = 0; .....) {

    ...
    const glb = await loadGLTF(<dinamically from a database>);
    const glbScene = glb.scene;
     ...
   const anchor = mindarThree.addAnchor(i);
  anchor.someAttribute = take the value from somewhere
  anchor.group.add(glbScene);

    anchor.onTargetFound = () => {
      buttonAR.href = buttonAR._href + "?id=" + <I wanna get someAttribute from the current anchor>
      buttonAR.style.display = 'block';
    }
    ....

} Can you see? Anchor is dinamically created in multitarget problem.

hiukim commented 1 year ago

Yes, buttonAR.href = buttonAR._href + "?id=" + anchor.someAttribute should work.

adilsonv77 commented 1 year ago

I tried now, and it gave the error message "Uncaught ReferenceError: anchor is not defined"

adilsonv77 commented 1 year ago

I changed three.js with this code

if (this.anchors[i].onTargetFound) { this.anchors[i].onTargetFound(this.anchors[i]); }

It worked for me.

hiukim commented 1 year ago

That's weird. I really don't think you need to do that. But whatever works for you.

adilsonv77 commented 1 year ago

However, I would like to use your suggestion. After compile the code with my modification and use mindar-image-three.js, seems the model is shivering.

adilsonv77 commented 1 year ago

Everything is ok. I tried your suggestion.