Log in or register to post comments

Scale Image Target at runtime

March 8, 2016 - 5:55am #1

Hi, I'm kinda new to Vuforia / Unity.

I have a problem : I need my RA app to be able to be told what size is my marker.

For example, I uploaded my Target saying it's 50 units wide.
Wether I see this target printed as a 50mm wide image or a 100mm wide image, Unity will get this as being 50 units wide.

What I want is to be able to go through an input and say "now, this target is 100 units wide".
Then, wether I see this target printed as a 50mm wide image or a 100mm wide image, Unity should get this as being 100 units wide.

How can I do that?

Simple tool to see if it works properly : 
I've created a mesh to create a frame around my ImageTarget. This frame is a child of my Image Target Game Object.

Attempt 1 [Failed]: 
I've tried to simply scale the Image Target Game Object to have a width of 100 units, I figured that when the camera would detect the image target, it would recognize it as being 100 units wide and, as usual, draw my frame around it.
Instead of that, it drawed my frame AS IF my printed target was twice bigger.

From what I understood with that, I guessed that the initial value of 50 is stored somewhere else, but I don't know where, and I don't know if I can change it at runtime.
That would explain why, when the camera detects my printed image targets, it creates (without drawing) the Image Target Game Object twice bigger on top of it, with all it's children (that are drawn as opposed to their parent !)


Any help (or explanation if my deductions aren't on point) is welcome here :)

Scale Image Target at runtime

March 11, 2016 - 2:33am #3

Well, it seems the code I found needed some foolproofing, as I'm fool enough to fall in the only trap it had for me !

Long story short, I was only checking that I was getting a null on Vuforia's virtual preview. Wich is absolutely normal : it seems the Trackers are all null when using only a preview as there is no point trying to see real items when playing my app on a pure virtual context !

I eventually ended up with "Disable Vuforia Play" checkbox unchecked (on ARCamera -> Web Cam Behaviour) after switching through a couple versions of Unity (function that I didn't know even exist !) and realized I wasn't getting a null value when using an actual camera. Since my app is designed for Android (and getting Debug.Log outputs on Android is a mess...) and since I usually get stuff working better on Unity previews than on actual Android runtime, I figured that having null on Unity couldn't lead to better luck on Android...

Anyway, if other fools like me somehow ends up here, since I have the foolproof code, I might as well share it ;)

    private void resizeMarker(ImageTargetBehaviour imageTarget, Vector2 size)
        //No point trying to rescale if it's already scaled !
        if(imageTarget.transform.localScale.x == size.x && imageTarget.transform.localScale.z == size.y)
        //Retreiving the track
        ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        //foolproofing !
        if (tracker == null)
            Debug.LogWarning("You might be using a virtual camera (Vuforia Play). The scaling of the trackable "+imageTarget.name+" will only be virtual wich doesn't unsure your function will work in actual runtime");
            //Retreiving all datasets
            IEnumerable<DataSet> sets = tracker.GetActiveDataSets();
            bool foundAndReplaced = false;
            //browsing through the datasets
            using (var setsEnum = sets.GetEnumerator())
                while (!foundAndReplaced && setsEnum.MoveNext())
                    DataSet ds = setsEnum.Current;
                    //deactivating every datasets, one at a time
                    //retreiving all trackables
                    IEnumerable<Trackable> tracks = ds.GetTrackables();
                    //browsing through trackables
                    using (var tracksEnum = tracks.GetEnumerator())
                        while (!foundAndReplaced && tracksEnum.MoveNext())
                            Trackable trackable = tracksEnum.Current;
                            //Looking for Image Targets
                            if (trackable is ImageTarget)
                                ImageTarget it = trackable as ImageTarget;
                                //Looking for MY image target that I need to update
                                if (it.ID == imageTarget.ImageTarget.ID)
                                    //Now, Vuforia knows the real size of my target
                                    foundAndReplaced = true;
                    //Once I'm done with this dataset, I can reactivate it
        //Updates the virtual size of my imagetarget along with his child GameObjects. (Also does the scaling on Vuforia previews !)
        imageTarget.transform.localScale = new Vector3(size.x, 1, size.y);


Scale Image Target at runtime

March 10, 2016 - 6:15am #2

Well, seems like a new version of Vuforia (5.5.9) is out and was made compatible with the latest version of Unity : 5.3.3.
I'll try to see if eventually that can fix my problem...

(I was on Unity 5.1.1 and Vuforia 5.0.10)

I'll post an update if I can get this to work properly.

Log in or register to post comments