hiukim / mind-ar-js

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

Get distance from from a-camera to tracked image? #461

Open Phillip-Thomas opened 9 months ago

Phillip-Thomas commented 9 months ago

Hey Hiukim! (or anyone else who may be able to answer),

I am attempting to get the relative position of the a-frame camera at the moment an image-target is found. Both the aframe components and the associated object3D's position and rotation are not reflected in their respective objects.

I have begun digging into ...['mindar-image-system'].controller in hopes of finding the correct matrix I can then decompose into the appropriate vector and quaternion... Currently I am attempting to decompose the tracking matrix, and while these matrices do offer decent change over time, the initial values seem arbitrary. A marker only inches away from the physical camera will have values such as:

Position: {x: -64.45630244163725, y: -122.58675738715118, z: -1129.497714154405}

Rotation: {w:0.9809891589263874, x: 0.17096930036715458, y: 0.009986823354502981 ,z: -0.0902177739996236}

As you can see within the Position vector, we have rather large numbers relative to A-frames default scaling. So I hope that I am able to do one of two things... Figure out an appropriate scaling factor to couple the trackingMatrix positional data to my scene entities, or use a different matrix (perhaps projectionMatrix or worldMatrix) that will provide the necessary data out of the box.

Any help would be greatly appreciated!

Thanks

Phillip-Thomas commented 9 months ago

PS: if I am able to accomplish my goals, I would be willing to contribute my efforts towards a world-tracking system to this project!

Phillip-Thomas commented 9 months ago

I see now that currentModelViewTransform was what I was seeking and just needed to be adjusted by a factor of .001.

ffd8 commented 8 months ago

@Phillip-Thomas - This sounds awesome and just came up in a workshop with MindAR... could you share an insight to how you're grabbing those values.. maybe a tiny example of what one needs to listen to? thanks!

Phillip-Thomas commented 8 months ago

Certainly!

In my current project, I am using A-frames internal tick mechanism to check the calculated pose of the camera in reference to the marker at the appropriate rate. In order to expose the relevant matrices, we access the global 'mindar-image-system' that is available after the mindAR CDN is loaded. Then it's just a game of discerning what's what. I can't be certain my approach is the greatest approach and I'm sure @hiukim could offer far greater insight but here is a glance of the code in question:


init() {
         this.mindarSystem = document.querySelector('a-scene').systems['mindar-image-system'];
         this.initCamPosition = null
    }

tick: function(time, timeDelta) {
            this.getProjection(); // Call your frame update logic here
    }

getProjection() {
        if (this.mindarSystem && this.mindarSystem.controller) {
            const trackingStates = this.mindarSystem.controller.trackingStates;
            trackingStates.forEach(state => {
                if (state.isTracking && state.showing) {

                    const position = {
                        x: (state.currentModelViewTransform[0][3] * .001).toFixed(2),
                        y: (state.currentModelViewTransform[1][3] * .001).toFixed(2),
                        z: (state.currentModelViewTransform[2][3] * .001).toFixed(2),
                    };

                    this.initCamPos = position;
                    console.log("Position:", `${position.x}, ${position.y}, ${position.z}`);
                }
            });
        }
    }
Bobbianss commented 8 months ago

HI @Phillip-Thomas Thank you for pointing out the aspect related to the currentModelViewTransform. I noticed that you've identified it, but I'm seeking some additional clarification regarding the rotations within the transformation matrix. Could you elucidate how the rotations are represented and function within it?