Log in or register to post comments

Dynamically swap samples problem

June 16, 2013 - 9:25am #1

Hello I was trying to use the dynamic swap code in image targets sample as below:

https://developer.vuforia.com/forum/faq/unity-how-can-i-dynamically-swap-3d-model-target

When I try to drag the image target from the scene view the inspector changes to the imagetarget and MyModelSwapper is out of inspector

And if I try to pull it from the hirerachy, its not getting onto  "The Trackable" in  inspector that is highleted

 

Dynamically swap samples problem

June 22, 2013 - 12:04pm #14

You can use Virtual Buttons, yes.

Have a look at the Virtual Buttons sample; in particular, look at the sample implementation of the IVirtualButtonEventHandler interface; 

you will need to implement your own VB event handler and trigger the model swapping when a virtual button event is triggered.

Dynamically swap samples problem

June 22, 2013 - 10:42am #13

Is there a way where I can port the same technique by substituting on-screen buttons with virtual buttons.

If yes, pls provide the outlines. I'' try and implement.

 

Dynamically swap samples problem

June 21, 2013 - 12:06am #12

I have updated the name MyModelSwapper to ModelSwapper, thanks for the suggestion!

 

Dynamically swap samples problem

June 20, 2013 - 10:55pm #11

Fine worked out perfectly.

One doubt though, I was able to pull the 3d model onto the Mymodel in inspector view of ModelSwapper.cs.

But why is not possible to pull an image target in the same way?

 

Last: If possible in this link https://developer.vuforia.com/forum/faq/unity-how-can-i-dynamically-swap-3d-model-target where you have defined the complete procedure, pls do change this part of the second script: "create a C# script, call it MyModelSwapper for instance;" to "create a C# script, call it ModelSwapper for instance;" as the name of the class and file dont match thereby raising an error when attaching to the ARCamera.

Thought will be helpful for beginners not to get psyched.

Thank you.

 

Dynamically swap samples problem

June 20, 2013 - 2:39am #10

I will use the teapot model as example, as you have it already in your project.

So, basically we are going to swap the original teapot, with a new instance of the teapot (so, you will se the teapot being replaced by another teapot, but you will notice the difference nevertheless, as the new model will gets scaled and positioned in a slightly  different way)

So, in your script, add a public variable called:

public Transform MyPrefab;

 

The replace the code in your SwapModel() method with this:

private void SwapModel() 
	{     
        GameObject trackableGameObject = theTrackable.gameObject;
         
        //disable any pre-existing augmentation
        for (int i = 0; i < trackableGameObject.transform.GetChildCount(); i++) 
        {
            Transform child = trackableGameObject.transform.GetChild(i);
            child.gameObject.active = false;            
        }
         
        // Create a simple cube object 
        //GameObject model = GameObject.CreatePrimitive(PrimitiveType.Cube);
		
		if (MyPrefab != null)
		{
			Transform model = GameObject.Instantiate(MyPrefab) as Transform;
			 
	        // Re-parent the model as child of the trackable gameObject
	        model.parent = theTrackable.transform;
	     
	        // Adjust the position and scale 
	        // so that it fits nicely on the target
	        model.transform.localPosition = new Vector3(0,0.2f,0);
	        model.transform.localRotation = Quaternion.identity;//you might need to adjust the rotation
	        model.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
	         
	        // Make sure it is active
	        model.active = true;
		}
    }

Note: don't forget , you need to drag the teapot model into the "My Prefab" element in the inspector !!!!

 

Dynamically swap samples problem

June 19, 2013 - 11:02pm #9

Or for instance lets make use of the bowl in the multi targets example.

Can you suggest me a code/modification with proper scale to replace teapot with the bowl

 

Dynamically swap samples problem

June 19, 2013 - 11:37am #8

I've no specific 3DS model to suggest, but one thing you could try is to first create a Prefab consisting of a simple Cube (or other game object) via the Unity editor, then you can verify if yyou can at least instantiate and visualize that Prefab

 

Dynamically swap samples problem

June 19, 2013 - 9:35am #7

OR would you suggest a 3ds model and the scale,orientation and positions. I'll use these in image targets sample & check out if the problem occurs

Dynamically swap samples problem

June 19, 2013 - 8:04am #6

I'm wondering if the mesh of your 3D model is not centered around the origin of the prefab local reference frame...

have you tried with a different mesh / object ?

 

Dynamically swap samples problem

June 19, 2013 - 7:44am #5

Yes I have scaled it and made it a child. Does show up properly when tried of making it a child of some imagetarget.

But the problem's only with this swap

 

Dynamically swap samples problem

June 17, 2013 - 10:31pm #4

Have you tried changing the scale of the "Dear" object ? 

Dynamically swap samples problem

June 17, 2013 - 10:56am #3

Hello

The workaround has solved the issue.

Now,

I added a new 3d model(.obj) in the scene(the model is not a child of any image target).

Here's the modified code(With Dear being the new 3d object):

using UnityEngine;
using System.Collections;
 
public class ModelSwapper : MonoBehaviour {
     
    private GameObject theTrackable;
    private GameObject cube;
     
    private bool mSwapModel = false;
     
     
    // Use this for initialization
    void Start () {
        theTrackable=GameObject.Find("Dog");
          cube = GameObject.Find("Dear");

        if (theTrackable == null)
        {
            Debug.Log ("Warning: Trackable not set !!");
        }
    }
     
    // Update is called once per frame
    void Update () {
        if (mSwapModel && theTrackable != null) {
            SwapModel();
            mSwapModel = false;
        }
    }
     
    void OnGUI() {
        if (GUI.Button (new Rect(50,50,120,40), "Swap Model")) {
            mSwapModel = true;
        }
    }
     
    private void SwapModel() {
         
        GameObject trackableGameObject = theTrackable.gameObject;
         
        //disable any pre-existing augmentation
        for (int i = 0; i < trackableGameObject.transform.GetChildCount(); i++)
        {
            Transform child = trackableGameObject.transform.GetChild(i);
            child.gameObject.active = false;           
        }
         
     
         
       //  Re-parent the cube as child of the trackable gameObject
       cube.transform.parent = theTrackable.transform;
     
        // Adjust the position and scale
        // so that it fits nicely on the target
        cube.transform.localPosition = new Vector3(0,0.2f,0);
        cube.transform.localRotation = Quaternion.identity;
        cube.transform.localScale = new Vector3(34.66113f, 34.66113f, 34.66113f);
         
        // Make sure it is active
        cube.active = true;
    }

 

When I press the Swap Model button,my teapot model is dissapearing but the dear model is not popping up on the imagetarget.

What should I change

Dynamically swap samples problem

June 17, 2013 - 1:22am #2

If that does not show up in the inspector, one easy workaround would be to make "theTrackable" private and just assign it porogrammatically inside the Start() method of your script (you could use GameObject.Find() for instance, and search the trackable by name, for example...)

Log in or register to post comments