Log in or register to post comments

Swap the model

December 19, 2013 - 9:18am #1

I have 2 images with separate 3d model. I drag the one 3d model to scene for one imagetarget and another for another image. How to disable the first 3d model while tracking on second image. I used code from here: https://developer.vuforia.com/forum/faq/unity-how-can-i-dynamically-swap-3d-model-target. then drag the script to ARCamera. See the attached image. But i didn't understand this line:

  • drag one of the Image Targets from your scene view into the "The trackable" field of your script 
AttachmentSize
Image icon Capture.PNG157.55 KB

Swap the model

December 20, 2013 - 3:31am #9

I tried the below code what you given from the link. But i can't swap the model..When i point stones target i need to display cow model, when i point tarmac i need to display elephant. But 2 models displaying for both target. Please help this. What should i change in code? I tried from this link also : https://developer.vuforia.com/forum/faq/unity-how-can-i-dynamically-attach-my-3d-model-image-target. This is only for chips target. How can i do it for another target. Please give some tips..

Swap the model

December 20, 2013 - 3:22am #8

If you read this:

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

this provides an excellent starting point for what you are trying to achieve; you then need to build a bit your app logic on top of that (or taking inpiration from the sample logic shown in that article);  the building blocks are all there, you can build your specific custom application logic on top of that.

 

Swap the model

December 20, 2013 - 2:48am #7

AlessandroB Help this? It's annoying problem. I tried but i didn't get proper solution. 

Swap the model

December 19, 2013 - 9:16pm #6

Below code is working. See the attached screenshot. In chips target cow & elephant models are showing. I drag the ModelSwapper script to ARCamera and in Inspector view i choosed cow. When i run the app click the button the cow is gone. It's works fine. Second screenshot tarmac showing cow & elephant model. In inspector what should i do? I need to remove elephant while click the button. Is i need to create another script for tarmac?

 

using UnityEngine;

using System.Collections;

 

public class ModelSwapper : MonoBehaviour {

 

// public TrackableBehaviour theTrackable;

 

 

 

private GameObject theTrackable;

 

private GameObject cube;

     

    private bool mSwapModel = false;

 

public Transform MyPrefab;

 

// Use this for initialization

void Start () {

 

 

theTrackable=GameObject.Find("Cow");

 

          cube = GameObject.Find("Cow");

 

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;            

        }

          

        // 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;

        }

    }

}

 

Swap the model

December 19, 2013 - 7:16pm #5

I'm new to coding. I have 3 images Stones, Chips, tarmac. I need to display the cow model for stones, elephant model for chips, dog model for tarmac. If i point stones image, i need to display only cow, other model should be deactivate. In that code which line shall i change. Also what is this error means 

 

Cannot implicitly convert type `UnityEngine.GameObject' to `TrackableBehaviour' "

Swap the model

December 19, 2013 - 11:46am #4

Hi, the error that you get is due to the fact that in the Start() method you have this line:

 cube = GameObject.Find("Dear");

but you have not defined the cube variable; you should define a private variable GameObject cube;

also, if you do that, then you won't need to create a cube in the code inside SwapModel()...

this is a simple programming error.

 

Swap the model

December 19, 2013 - 9:56am #3

I'm getting below error while using below code:

Assets/ModelSwapper.cs(13,17): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `TrackableBehaviour'

Assets/ModelSwapper.cs(14,11): error CS0103: The name `cube' does not exist in the current context

 

using UnityEngine;
using System.Collections;
 
public class ModelSwapper : MonoBehaviour {
 
public TrackableBehaviour theTrackable;
     
    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;            
        }
         
        // Create a simple cube object 
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         
        // 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(0.1f, 0.1f, 0.1f);
         
        // Make sure it is active
        cube.active = true;
    }
 
 
}
 

Swap the model

December 19, 2013 - 9:48am #2

Within the trackable event handler, you can implement a behavior in OnTrackingFound that will selectively deactivate the model on the other target. The simplest way is to reference the object to be deactiveated using GameObject.Find( name ) and then define .active = false on the model's GameObject, but a more precise way is to use .transform.Find( name ) on the other target as this will isolate the search to children of that target. 

 

 

Log in or register to post comments