Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Vuforia + Unity (oncloud recognition + oncloud model3D)

Discussion in 'Editor & General Support' started by eduGigigo, Apr 2, 2018.

  1. eduGigigo

    eduGigigo

    Joined:
    Apr 2, 2018
    Posts:
    2
    Good morning,

    actually I have a problem, I want to use a image recognize with Vuforia on cloud. When I recognize it, I track the position and load a 3D model on it.

    In many examples I have found out how to do this, only when the 3D model is inside the scene. But I want use the 3D model recovered from the Internet and include in imtageTarget, I dont know.

    Code:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using Vuforia;
    4. using UnityEngine.UI;
    5.  
    6. using System.Collections;
    7. using UnityEditor;
    8. using UnityEngine.Networking;
    9.  
    10.  
    11. public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler
    12. {
    13.     #region PRIVATE_MEMBER_VARIABLES
    14.  
    15.     private CloudRecoBehaviour mCloudRecoBehaviour;
    16.     private ObjectTracker mImageTracker;
    17.     private bool mIsScanning = false;
    18.     private string mTargetMetadata = "";
    19.     private bool elementFound = false;
    20.     private ImageTargetAbstractBehaviour imageTargetBehaviour;
    21.  
    22.     #endregion // PRIVATE_MEMBER_VARIABLES
    23.  
    24.  
    25.  
    26.     #region EXPOSED_PUBLIC_VARIABLES
    27.  
    28.     public ImageTargetBehaviour ImageTargetTemplate;
    29.     public GameObject myModel;
    30.  
    31.     #endregion
    32.  
    33.  
    34.     #region UNTIY_MONOBEHAVIOUR_METHODS
    35.  
    36.  
    37.     [MenuItem ("Assets/Build AssetBundles")]
    38.     static void BuildAllAssetBundles ()
    39.     {
    40.  
    41.     }
    42.  
    43.  
    44.     void Start()
    45.     {
    46.         // register this event handler at the cloud reco behaviour
    47.         CloudRecoBehaviour cloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
    48.         if (cloudRecoBehaviour)
    49.         {
    50.             cloudRecoBehaviour.RegisterEventHandler(this);
    51.         }
    52.  
    53.         // remember cloudRecoBehaviour for later
    54.         mCloudRecoBehaviour = cloudRecoBehaviour;
    55.     }
    56.  
    57.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    58.  
    59.  
    60.     #region ICloudRecoEventHandler_IMPLEMENTATION
    61.  
    62.  
    63.     public void OnInitialized()
    64.     {
    65.         mImageTracker = (ObjectTracker)TrackerManager.Instance.GetTracker<ObjectTracker>();
    66.     }
    67.  
    68.     public void OnInitError(TargetFinder.InitState initError)
    69.     {
    70.     }
    71.  
    72.     public void OnUpdateError(TargetFinder.UpdateState updateError)
    73.     {
    74.     }
    75.  
    76.     public void OnStateChanged(bool scanning) {
    77.         mIsScanning = scanning;
    78.         if (scanning) {
    79.             // clear all known trackables
    80.             ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker> ();
    81.             tracker.TargetFinder.ClearTrackables (false);
    82.         }
    83.     }
    84.  
    85.  
    86.     public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    87.     {
    88.         // HERE Vuforia return a key of image recognized.
    89.  
    90.         GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;
    91.         imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);
    92.  
    93.        // HERE get a oncloud model
    94.         StartCoroutine(GetAssetBundle("BSD", modelName, targetSearchResult));
    95.  
    96.         if (!mIsScanning)
    97.         {
    98.             mCloudRecoBehaviour.CloudRecoEnabled = true;
    99.         }
    100.     }
    101.  
    102.     IEnumerator GetAssetBundle(String assetBundleName, String modelName, TargetFinder.TargetSearchResult targetSearchResult)
    103.     {
    104.         UnityWebRequest www = UnityWebRequest.GetAssetBundle("https://nuborisar.github.io/"+assetBundleName);   // data base
    105.         yield return www.SendWebRequest();
    106.  
    107.         if (www.isNetworkError || www.isHttpError)
    108.         {
    109.             Debug.Log(www.error);
    110.         }
    111.         else
    112.         {
    113.             elementFound = true;
    114.  
    115.             AssetBundle assetbundle = DownloadHandlerAssetBundle.GetContent(www);
    116.             GameObject mybb8 = (GameObject)assetbundle.LoadAsset(modelName);  // Modelo ID key de vuforia
    117.  
    118.  
    119.             //imageTargetBehaviour.gameObject.transform.Find ("myModelScene").gameObject = mybb8;
    120.            //Here I cant include my onCloud model into ImageTarget
    121.      
    122.         }
    123.     }
    124.  
    125.  
    126.     #endregion // ICloudRecoEventHandler_IMPLEMENTATION
    127.  
    128.     void OnGUI() {
    129.         GUI.Box (new Rect(100,200,200,50), "Metadata: " + mTargetMetadata);
    130.     }
    131. }
    132.  
    133.  

    Thx very much
     
    Last edited: Apr 2, 2018
unityunity