Try MAXST AR Fusion Tracker Now ✨
List
How to add more than one 3D objects(that can control separately) in instant tracker?
Posted Date: 2020-07-07 9:13     Edited Date: 2020-07-13 8:54     Writer: venolig

Development details:

1. SDK Version: 5.0.2
2. Development Environment: (eg. Unity-Android, Unity-iOS, Native Android, Native iOS): Unity-Android, Unity-iOS
3. Tracker/Scanner: Instant tracker
4. License Type(Free / Pro-One Time Fee / Pro-Subscription / Enterprise): Free
5. Target Device(Optional): Any

Hi,

How can I add more than one objects (InstantTrackables) in the instant tracker? Or how can I add more than one 3d objects(that can control separately by touching) in the InstantTrackable?

 

Thank you!

Posted Date: 2020-07-08 4:36     Edited Date: 2020-07-08 4:36     Writer: sjkim

Thanks for your interest to our MAXST AR SDK.

 

Do you wanna control to separately 3d objects(model)?

 

Currently, You can't control multiple content individually in Instant Tracker Sample script.

But, You can add the following API to transform screen coordinates into place multiple 3D content in Instant Tracker.

-> TrackerManager.GetInstance().GetWorldPositionFromScreenCoordinate(ScreenPosition)

 

Please reference to bottom code.

 

    void Update()

    {

            for (int i = 0; i < instantTrackables.Count; i++)
            {
                Trackable track = trackingResult.GetTrackable(0);
                Matrix4x4 poseMatrix = track.GetPose() * Matrix4x4.Translate(touchSumPositions[i]);
                instantTrackables[i].OnTrackSuccess(track.GetId(), track.GetName(), poseMatrix);
            }
    }

 

    void UpdateTouchPositionDelta(int id)
    {
        switch (Input.GetTouch(0).phase)
        {
            case TouchPhase.Began:
                touchToWorldPositions[id] = TrackerManager.GetInstance().GetWorldPositionFromScreenCoordinate(Input.GetTouch(0).position);
                break;

            case TouchPhase.Moved:
                Vector3 currentWorldPosition = TrackerManager.GetInstance().GetWorldPositionFromScreenCoordinate(Input.GetTouch(0).position);
                touchSumPositions[id] += (currentWorldPosition - touchToWorldPositions[id]);
                touchToWorldPositions[id] = currentWorldPosition;
                break;
        }
    }

 

If you have other questions, feel free to questions us.

 

 

Best regards

Francisco

MAXST Support Team

Posted Date: 2020-07-09 6:09     Edited Date: 2020-07-09 6:27     Writer: venolig

Thank you. It works. It saved a lot of time of me. But I make a few changes to your code when keeping the values. Can you please check and say is my way will badly affect the performance of the program?

I didn't keep three arrays. I create two member variables in InstantTrackableBehaviour class to keep touchSumPosition and touchToWorldPosition. Then I keep one array of InstantTrackableBehaviour objects.  Also, I have replaced other places that used instantTrackable with foreach. Can these badly affects for performance of the program? (I have bold my changes here)

 public class InstantTrackableBehaviour : AbstractInstantTrackableBehaviour
    {
            public Vector3 touchToWorldPosition = Vector3.zero;
            public Vector3 touchSumPosition = Vector3.zero;

            

        public override void OnTrackSuccess(string id, string name, Matrix4x4 poseMatrix){

        ....

}}

 

//InstantTrackerSample.cs

   List<InstantTrackableBehaviour> instantTrackables = new List<InstantTrackableBehaviour>();
 

void Update()
    {
               TrackingState state = TrackerManager.GetInstance().UpdateTrackingState();

        if (state == null)
        {
            return;
        }

        cameraBackgroundBehaviour.UpdateCameraBackgroundImage(state);

        TrackingResult trackingResult = state.GetTrackingResult();

        if (trackingResult.GetCount() == 0)
        {
            foreach (InstantTrackableBehaviour instantTrackable in instantTrackables)
            {
                instantTrackable.OnTrackFail();
            }

            return;
        }        

        if (Input.touchCount > 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100))
            {
                UpdateTouchPositionDelta(hit.transform.gameObject.GetComponentInParent(typeof(InstantTrackableBehaviour)) as InstantTrackableBehaviour);

            }

        }

        foreach (InstantTrackableBehaviour instantTrackable in instantTrackables)
        {
            Trackable track = trackingResult.GetTrackable(0);
            Matrix4x4 poseMatrix = track.GetPose() * Matrix4x4.Translate(instantTrackable.touchSumPosition);
            instantTrackable.OnTrackSuccess(track.GetId(), track.GetName(), poseMatrix);
        }

    }
 

void UpdateTouchPositionDelta(InstantTrackableBehaviour instantTrackable)
    {
        switch (Input.GetTouch(0).phase)
        {
            case TouchPhase.Began:
                instantTrackable.touchToWorldPosition = TrackerManager.GetInstance().GetWorldPositionFromScreenCoordinate(Input.GetTouch(0).position);
                break;

            case TouchPhase.Moved:
                Vector3 currentWorldPosition = TrackerManager.GetInstance().GetWorldPositionFromScreenCoordinate(Input.GetTouch(0).position);
                instantTrackable.touchSumPosition += (currentWorldPosition - instantTrackable.touchToWorldPosition);
                instantTrackable.touchToWorldPosition= currentWorldPosition;
                break;

        }
    }

 

 

 

 void Start()
    {
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = 60;

        foreach (InstantTrackableBehaviour instantTrackable in Resources.FindObjectsOfTypeAll(typeof(InstantTrackableBehaviour)) as InstantTrackableBehaviour[])
        {
            instantTrackables.Add(instantTrackable);
        }

        
        foreach (InstantTrackableBehaviour instantTrackable in instantTrackables)
        {
            instantTrackable.OnTrackFail();
        }

        StartCamera();

        .....
    }

 

public void OnClickStart()
    {
        if (!findSurfaceDone)
        {
            TrackerManager.GetInstance().FindSurface();
            if (startBtnText != null)
            {
                startBtnText.text = "Stop Tracking";
            }
            findSurfaceDone = true;
            foreach (InstantTrackableBehaviour instantTrackable in instantTrackables)
            {
                instantTrackable.touchSumPosition = Vector3.zero;
            }

            
        }
        else
        {
            TrackerManager.GetInstance().QuitFindingSurface();
            if (startBtnText != null)
            {
                startBtnText.text = "Start Tracking";
            }
            findSurfaceDone = false;
        }
    }

 

Sorry to bother with my code. But I can't guess with my knowledge, whether it affects the performance by adding member variables to the InstantTrackableBehaviour class. Your help is highly appreciated.

Thank You!

Posted Date: 2020-07-13 8:53     Edited Date: 2020-07-13 8:54     Writer: sjkim

MAXST Forum only answers program bug and questions about MAXST AR SDK.

But, I write some tips.

 

In general, foreach statements perform better than for statements in List<> containers.

Also, the for statement in an array is better than the foreach statement in List<> that uses foreach.

 

If you have related MAXST AR SDK questions, feel free to questions us.

 

 

Best regards

Francisco

MAXST Support Team