I want to load the model at runtime. I have done like instruction. When the first image target was detected, model 1 appeared but when the second image target was detected, both models appeared, please help me. I use Vuforia, Unity and Trilib. Thanks ❤

using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using Vuforia; using System; using System.IO; using System.Collections; using System.Collections.Generic; using TriLib; using TriLib.Samples; public class recoEventHandler : MonoBeh… Augmented Reality & /u/hienvic97

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.Networking;

using Vuforia;

using System;

using System.IO;

using System.Collections;

using System.Collections.Generic;

using TriLib;

using TriLib.Samples;

public class recoEventHandler : MonoBehaviour, IObjectRecoEventHandler

{

#region PRIVATE_MEMBERS

CloudRecoBehaviour m_CloudRecoBehaviour;

ObjectTracker m_ObjectTracker;

TargetFinder m_TargetFinder;

string itemName;

public GameObject menuContainer;

public GameObject menuItem;

public GameObject showAnimCloud;

private GameObject model;

private BlendShapeControl _blendShapeControlPrefab;

private Transform _blendShapesContainerTransform;

public GameObject sliderGO;

public Slider sliderComp;

public GameObject loading;

private GameObject _rootGameObject;

public bool mIsScanning =false;

#endregion

#region PUBLIC_MEMBERS

[Tooltip("Here you can set the ImageTargetBehaviour from the scene that will be used to " +

"augment new cloud reco search results.")]

public ImageTargetBehaviour m_ImageTargetBehaviour;

#endregion

#region MONOBEHAVIOUR_METHODS

void Start()

{

m_CloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();

if (m_CloudRecoBehaviour)

{

m_CloudRecoBehaviour.RegisterEventHandler(this);

}

}

#endregion

#region INTERFACE_IMPLEMENTATION_ICloudRecoEventHandler

public void OnInitialized()

{

Debug.Log("Cloud Reco initialized successfully.");

m_ObjectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();

m_TargetFinder = m_ObjectTracker.GetTargetFinder<ImageTargetFinder>();

}

public void OnInitialized(TargetFinder targetFinder)

{

Debug.Log("Cloud Reco initialized successfully.");

m_ObjectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();

m_TargetFinder = targetFinder;

}

public void OnInitError(TargetFinder.InitState initError) { }

public void OnUpdateError(TargetFinder.UpdateState updateError) { }

public void OnStateChanged(bool scanning)

{

mIsScanning = scanning;

if (scanning)

{

var tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();

tracker.GetTargetFinder<ImageTargetFinder>().ClearTrackables(false);

}

}

public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)

{

Debug.Log("<color=blue>OnNewSearchResult(): </color>" + targetSearchResult.TargetName);

TargetFinder.CloudRecoSearchResult cloudRecoResult = (TargetFinder.CloudRecoSearchResult)targetSearchResult;

if (cloudRecoResult.MetaData == null)

{

Debug.Log("Target metadata not available.");

}

else

{

string[] splitString = cloudRecoResult.MetaData.Split(new string[] { ",", "\n" }, StringSplitOptions.None);

itemName = splitString[0];

showAnimCloud.SetActive(false);

Show3DModel();

Debug.Log("MetaData: " + cloudRecoResult.MetaData);

Debug.Log("TargetName: " + cloudRecoResult.TargetName);

Debug.Log("Pointer: " + cloudRecoResult.TargetSearchResultPtr);

Debug.Log("TrackingRating: " + cloudRecoResult.TrackingRating);

Debug.Log("UniqueTargetId: " + cloudRecoResult.UniqueTargetId);

}

m_CloudRecoBehaviour.CloudRecoEnabled = true;

m_TargetFinder.ClearTrackables(false);

m_TargetFinder.EnableTracking(cloudRecoResult, m_ImageTargetBehaviour.gameObject);

m_ImageTargetBehaviour.gameObject.SendMessage("TargetCreated", cloudRecoResult, SendMessageOptions.DontRequireReceiver);

}

#endregion

void Show3DModel()

{

if (itemName != null)

{

if (File.Exists(Application.persistentDataPath + "/" + itemName))

{

sliderGO.SetActive(false);

LoadModel();

}

else

{

StartCoroutine(DownloadModel());

}

}

}

private void AssetLoader_OnMetadataProcessed(AssimpMetadataType metadataType, uint metadataIndex, string metadataKey, object metadataValue)

{

Debug.Log("Found metadata of type [" + metadataType + "] at index [" + metadataIndex + "] and key [" + metadataKey + "] with value [" + metadataValue + "]");

}

void LoadModel()

{

loading.SetActive(true);

model = m_ImageTargetBehaviour.gameObject.transform.Find("3dmodel").gameObject;

var assetLoaderOptions = GetAssetLoaderOptions();

using (var assetLoader = new AssetLoaderAsync())

{

assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed;

assetLoader.LoadFromFileWithTextures(Application.persistentDataPath + "/" + itemName, assetLoaderOptions, null, delegate (GameObject loadedGameObject)

{

CheckForValidModel(assetLoader);

_rootGameObject = loadedGameObject;

_rootGameObject.transform.parent = model.transform;

_rootGameObject.transform.localPosition = new Vector3(0, 0, 0);

_rootGameObject.transform.localEulerAngles = new Vector3(0, 0, 0);

_rootGameObject.transform.localScale = new Vector3(1, 1, 1);

loading.SetActive(false);

FullPostLoadSetup();

});

}

}

private void FullPostLoadSetup()

{

if (_rootGameObject != null)

{

PostLoadSetup();

}

}

private void PostLoadSetup()

{

var rootAnimation = _rootGameObject.GetComponent<Animation>();

var skinnedMeshRenderers = _rootGameObject.GetComponentsInChildren<SkinnedMeshRenderer>();

if (skinnedMeshRenderers != null)

{

var hasBlendShapes = false;

foreach (var skinnedMeshRenderer in skinnedMeshRenderers)

{

if (!hasBlendShapes && skinnedMeshRenderer.sharedMesh.blendShapeCount > 0)

{

hasBlendShapes = true;

}

for (var i = 0; i < skinnedMeshRenderer.sharedMesh.blendShapeCount; i++)

{

CreateBlendShapeItem(skinnedMeshRenderer, skinnedMeshRenderer.sharedMesh.GetBlendShapeName(i), i);

}

}

}

if (rootAnimation != null)

{

foreach (Transform child in menuContainer.transform)

{

GameObject.Destroy(child.gameObject);

}

showAnimCloud.SetActive(true);

foreach (AnimationState animationState in rootAnimation)

{

Debug.Log(animationState.name);

GameObject newItem = Instantiate(menuItem);

newItem.transform.SetParent(menuContainer.transform);

newItem.transform.localScale = new Vector3(1f, 1f, 1f);

newItem.GetComponentInChildren<Text>().text = animationState.name;

Button button = newItem.GetComponent<Button>();

button.onClick.AddListener(() => { _rootGameObject.GetComponent<Animation>().Play(animationState.name); });

}

}

}

private void CreateBlendShapeItem(SkinnedMeshRenderer skinnedMeshRenderer, string name, int index)

{

var instantiated = Instantiate(_blendShapeControlPrefab, _blendShapesContainerTransform);

instantiated.SkinnedMeshRenderer = skinnedMeshRenderer;

instantiated.Text = name;

instantiated.BlendShapeIndex = index;

}

private void CheckForValidModel(AssetLoaderBase assetLoader)

{

if (assetLoader.MeshData == null || assetLoader.MeshData.Length == 0)

{

throw new Exception("File contains no meshes");

}

}

private AssetLoaderOptions GetAssetLoaderOptions()

{

var assetLoaderOptions = AssetLoaderOptions.CreateInstance();

assetLoaderOptions.DontLoadCameras = false;

assetLoaderOptions.DontLoadLights = false;

assetLoaderOptions.UseOriginalPositionRotationAndScale = true;

assetLoaderOptions.AddAssetUnloader = true;

assetLoaderOptions.AutoPlayAnimations = true;

assetLoaderOptions.AdvancedConfigs.Add(AssetAdvancedConfig.CreateConfig(AssetAdvancedPropertyClassNames.FBXImportDisableDiffuseFactor, true));

return assetLoaderOptions;

}

private IEnumerator DownloadModel()

{

var _www = new WWW("https://github.com/hienvic/Models/blob/master/Model%203d/" + itemName + "?raw=true");

```_www = WWW.LoadFromCacheOrDownload(ModelURI, 5);```

while (!_www.isDone)

{

yield return null;

sliderGO.SetActive(true);

sliderComp.value = _www.progress;

}

sliderGO.SetActive(false);

var ModelLocalPath = "/" + itemName;

var fullPath = Application.persistentDataPath + ModelLocalPath;

File.WriteAllBytes(fullPath, _www.bytes);

StartCoroutine(LoadAfterDownload());

}

IEnumerator LoadAfterDownload()

{

yield return new WaitForSeconds(3f);

LoadModel();

}

}

submitted by /u/hienvic97
[link] [comments]