public void loadMeshAsync(string filename) { if (currentLoadedARObject != null) { unloadARObject(currentLoadedARObject); } Debug.Log("!!! ATTEMPTING TO LOAD MESH"); using (var assetLoader = new AssetLoaderAsync()) { try { var assetLoaderOptions = AssetLoaderOptions.CreateInstance(); // assetLoaderOptions.RotationAngles = new Vector3(90f, 180f, 0f); // assetLoaderOptions.AutoPlayAnimations = true; assetLoaderOptions.Use32BitsIndexFormat = true; string filetoload = Application.persistentDataPath + "/" + filename; assetLoader.LoadFromFile(filetoload, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { //recalculateNormals(loadedGameObject); loadedGameObject.transform.position = Vector3.zero; loadedGameObject.transform.rotation = Quaternion.identity; currentLoadedARObject = loadedGameObject; Debug.Log("!!!!!!!!!**** game object loaded"); }); } catch (Exception e) { Debug.LogError(e.ToString()); } } }
private IEnumerator LoadSettingConfig() { using (AssetLoaderAsync assetLoader = new AssetLoaderAsync("assets_config", 0, "EngineSetting", typeof(EngineSetting), false, AssetBundleManager.AssetBundleLoadMethod.FromFile)) { if (assetLoader.asset == null) { yield return(StartCoroutine(assetLoader)); } engineSetting.CopyFrom(assetLoader.asset as EngineSetting); } yield break; }
IEnumerator Start() { using (AssetLoaderAsync assetLoader = new AssetLoaderAsync(AssetBundleName, Version, AssetName, typeof(GameObject))) { if (assetLoader.asset == null) { yield return(StartCoroutine(assetLoader)); } if (assetLoader.asset != null) { ObjInstance = (GameObject)Instantiate(assetLoader.asset); } } }
protected override IEnumerator PrepareInterludeAnim() { using (AssetLoaderAsync assetLoader = new AssetLoaderAsync("assets_prefabs", 0, "TeleportLoadingUI", typeof(GameObject))) { if (assetLoader.asset == null) { yield return(StartCoroutine(assetLoader)); } GameObject pref = assetLoader.asset as GameObject; if (pref != null) { Instantiate(pref); } } }
public override IEnumerator DoWork() { using (AssetLoaderAsync assetLoader = new AssetLoaderAsync(_assetBundleName, _version, _assetName, typeof(GameObject))) { if (assetLoader.asset == null) { yield return(AssetManager.instance.StartCoroutine(assetLoader)); } if (_instantiateNow) { GameObject pref = assetLoader.asset == null ? null : assetLoader.asset as GameObject; if (pref != null) { UnityEngine.Object.Instantiate(pref); } } } }
void testasync() { using (var assetLoaderAsync = new AssetLoaderAsync()) { var assetLoaderOptions = AssetLoaderOptions.CreateInstance(); //Creates the AssetLoaderOptions instance. //AssetLoaderOptions let you specify options to load your model. //(Optional) You can skip this object creation and it's parameter or pass null. //You can modify assetLoaderOptions before passing it to LoadFromFile method. You can check the AssetLoaderOptions API reference at: //https://ricardoreis.net/trilib/manual/html/class_tri_lib_1_1_asset_loader_options.html var wrapperGameObject = gameObject; //Sets the game object where your model will be loaded into. //(Optional) You can skip this object creation and it's parameter or pass null. var thread = assetLoaderAsync.LoadFromFile("PATH TO MY FILE.FBX", assetLoaderOptions, wrapperGameObject, delegate(GameObject myGameObject) { //Here you can get the reference to the loaded model using myGameObject. }); //Loads the model asynchronously and returns the reference to the created Task/Thread. } }
//Generates the progress bar textures and starts to load the asset private void Start() { //Generates progress bar textures GenerateProgressBarTextures(); //Creates our AssetLoaderAsync instance using (var assetLoaderAsync = new AssetLoaderAsync()) { try { //Loads the given asset and set the loading progress and loading completed callback assetLoaderAsync.LoadFromFile(Application.dataPath + "/TriLib/TriLib/Samples/Models/BigModel.obj", null, gameObject, OnAssetLoaded, null, OnAssetLoadingProgress); } catch (Exception e) { //Stores the error message to show on GUI _error = e.ToString(); } } }
//Generates the progress bar textures and starts to load the asset private void Start() { //Generates progress bar textures GenerateProgressBarTextures(); //Creates our AssetLoaderAsync instance using (var assetLoaderAsync = new AssetLoaderAsync()) { try { //Loads the given asset and set the loading progress and loading completed callback assetLoaderAsync.LoadFromFile(string.Format("{0}/BigModel.obj", TriLibProjectUtils.FindPathRelativeToProject("Models", "t:Model BigModel")), null, gameObject, OnAssetLoaded, null, OnAssetLoadingProgress); } catch (Exception e) { //Stores the error message to show on GUI _error = e.ToString(); } } }
/// <summary> /// Tries to load "Bouncing.fbx" model /// </summary> protected void Start() { using (var assetLoader = new AssetLoaderAsync()) { try { var assetLoaderOptions = AssetLoaderOptions.CreateInstance(); assetLoaderOptions.RotationAngles = new Vector3(90f, 180f, 0f); assetLoaderOptions.AutoPlayAnimations = true; assetLoader.LoadFromFile(Application.dataPath + "/TriLib/TriLib/Samples/Models/Bouncing.fbx", assetLoaderOptions, null, delegate(GameObject loadedGameObject) { loadedGameObject.transform.position = new Vector3(128f, 0f, 0f); }); } catch (Exception e) { Debug.LogError(e.ToString()); } } }
/// <summary> /// Tries to load "Bouncing.fbx" model /// </summary> protected void Start() { using (var assetLoader = new AssetLoaderAsync()) { try { var assetLoaderOptions = AssetLoaderOptions.CreateInstance(); //assetLoaderOptions.RotationAngles = new Vector3(90f, 180f, 0f); assetLoaderOptions.AutoPlayAnimations = true; //assetLoaderOptions.UseOriginalPositionRotationAndScale = true; assetLoader.LoadFromFile(string.Format("{0}/Bouncing.fbx", TriLibProjectUtils.FindPathRelativeToProject("Models", "t:Model Bouncing")), assetLoaderOptions, null, delegate(GameObject loadedGameObject) { // loadedGameObject.transform.position = new Vector3(128f, 0f, 0f); }); } catch (Exception e) { Debug.LogError(e.ToString()); } } }
/// <summary> /// Loads a model from the given filename or given file bytes. /// </summary> /// <param name="filename">Model filename.</param> /// <param name="fileBytes">Model file bytes.</param> private void LoadInternal(string filename, byte[] fileBytes = null) { PreLoadSetup(); var assetLoaderOptions = GetAssetLoaderOptions(); if (!Async) { using (var assetLoader = new AssetLoader()) { assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; try { #if (UNITY_WINRT && !UNITY_EDITOR_WIN) var extension = FileUtils.GetFileExtension(filename); _rootGameObject = assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, _rootGameObject); #else if (fileBytes != null && fileBytes.Length > 0) { var extension = FileUtils.GetFileExtension(filename); _rootGameObject = assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, _rootGameObject); } else if (!string.IsNullOrEmpty(filename)) { _rootGameObject = assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions); } else { throw new System.Exception("File not selected"); } #endif } catch (System.Exception exception) { ErrorDialog.Instance.ShowDialog(exception.ToString()); } } if (_rootGameObject != null) { PostLoadSetup(); } } else { using (var assetLoader = new AssetLoaderAsync()) { assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; try { if (fileBytes != null && fileBytes.Length > 0) { var extension = FileUtils.GetFileExtension(filename); assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { _rootGameObject = loadedGameObject; if (_rootGameObject != null) { PostLoadSetup(); } }); } else if (!string.IsNullOrEmpty(filename)) { assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { _rootGameObject = loadedGameObject; if (_rootGameObject != null) { PostLoadSetup(); } }); } else { throw new System.Exception("File not selected"); } } catch (System.Exception exception) { ErrorDialog.Instance.ShowDialog(exception.ToString()); } } } }
private void ProcessPicture(List <byte> rawbytes) { MessageParser parser = new MessageParser(rawbytes); switch (parser.MessageType) { case (int)BinaryMessageType.VideoStreamWithCameraTransform: { GameObject tempCam = new GameObject(); tempCam.transform.position = new Vector3(parser.CameraPosition.X, parser.CameraPosition.Y, parser.CameraPosition.Z); tempCam.transform.rotation = new Quaternion(parser.CameraRotation.X, parser.CameraRotation.Y, parser.CameraRotation.Z, parser.CameraRotation.W); float pictureDistance = Camera.main.nearClipPlane + 0.01f; RaycastHit spatialMapHit; if (SpatialMappingRaycast(tempCam.transform.position, tempCam.transform.forward, out spatialMapHit)) { pictureDistance = spatialMapHit.distance; //0.05 is a magic number to adjust the real distance } ShowDrawing(parser.Payload, tempCam.transform, pictureDistance); } break; case (int)BinaryMessageType.Picture: { ShowPicture(parser.Payload); } break; case (int)BinaryMessageType.SpatialIndicator: { Vector3 CamPosition = new Vector3(parser.CameraPosition.X, parser.CameraPosition.Y, parser.CameraPosition.Z); Quaternion CamRotation = new Quaternion(parser.CameraRotation.X, parser.CameraRotation.Y, parser.CameraRotation.Z, parser.CameraRotation.W); int width = parser.ImageWidth; int height = parser.ImageHeight; Debug.Log("width: " + width + " height: " + height); GameObject temp = new GameObject(); temp.transform.position = CamPosition; temp.transform.rotation = CamRotation; float scaleFactor = 0.001f; //float realDistance = height / (2.0f * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad)) * scaleFactor; //0.001 is the scale of the //Debug.Log("realDistance: " + realDistance); //realDistance += 0.4f; //magic number to adjust the positon float realDistance = 1.50f; temp.transform.Translate(Vector3.forward * realDistance, Space.Self); Vector3 central = temp.transform.position; foreach (RemoteAssistControl.Numerics.Point point in parser.IndicatorPostions) { Debug.Log("point.x: " + point.x + " point.y: " + point.y); temp.transform.position = central; //reset to central //need to compensate indicator's position, 40 is the size of the indicator RemoteAssistControl.Numerics.Point realPoint = new RemoteAssistControl.Numerics.Point(point.x, point.y); //the origin of the picture if at topleft Vector3 translation = new Vector3(((float)realPoint.x + 103 - width / 2) * scaleFactor, (height / 2 - 118 - (float)realPoint.y) * scaleFactor, 0); temp.transform.Translate(translation, Space.Self); //Debug.Log(" originalCamPosition " + originalCamPosition + " central: " + central + " realpoint: " + realPoint + " translation: " + translation + " new position: " + temp.transform.position); //Debug.Log(" cast direction " + (temp.transform.position - originalCamPosition).normalized + " camera forward: " + temp.transform.forward); RaycastHit spatialMapHit; if (SpatialMappingRaycast(CamPosition, (temp.transform.position - CamPosition).normalized, out spatialMapHit)) { GameObject indicator = Instantiate(Resources.Load <GameObject>("Prefabs/Arrow")); indicator.transform.position = spatialMapHit.point; indicator.transform.up = spatialMapHit.normal; indicator.AddComponent <WorldAnchor>(); Debug.Log("hitPostioin: " + spatialMapHit.point + " hitNormal: " + spatialMapHit.normal); } } } break; case (int)BinaryMessageType.Model: { string fileExtension = parser.FileExtension; using (var assetLoaderAsync = new AssetLoaderAsync()) { assetLoaderAsync.LoadFromMemory(parser.Payload, fileExtension, null, null, (loadedGameObject) => { GameObject BBParent = Instantiate(Resources.Load <GameObject>("Prefabs/BBParent")); BBParent.transform.position = Vector3.zero; loadedGameObject.transform.position = Vector3.zero; loadedGameObject.transform.SetParent(BBParent.transform); BoundingBoxRig bbr = BBParent.GetComponent <BoundingBoxRig>(); BoundingBox bb = bbr.BoundingBoxPrefab; bb.Target = BBParent; //force BoundingBox to call RefreshTargetBounds, ideally the function should be public bb.RefreshTargetBounds(); Vector3 position = bb.TargetBoundsCenter; Vector3 size = bb.TargetBoundsLocalScale; Debug.Log("TargetBoundsCenter: " + position + "TargetBoundsScale: " + size); //this is tricky, we get the box collider center, and then we need move the bbparent to the position. //in order not to affect the model, we disconnect the bbparent temporarily loadedGameObject.transform.parent = null; BBParent.transform.position = position; BoxCollider bc = BBParent.EnsureComponent <BoxCollider>(); bc.center = Vector3.zero; bc.size = size; loadedGameObject.transform.SetParent(BBParent.transform); //scale the model to fit in the fov at the 2 meter position float desiredHeight = 1.5f / (2.0f * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad)); //divide another 2 is because that the RGB camer FOV is 2 times bigger than the real fov float scaleFactor = desiredHeight / Mathf.Max(new float[] { size.x, size.y, size.z }) / 2 / 4; BBParent.transform.position = Camera.main.transform.position; BBParent.transform.rotation = Camera.main.transform.rotation; BBParent.transform.Translate(Vector3.forward * 1.5f, Space.Self); Vector3 directionToTarget = Camera.main.transform.position - BBParent.transform.position; BBParent.transform.rotation = Quaternion.LookRotation(-directionToTarget); BBParent.transform.localScale = new Vector3(scaleFactor, scaleFactor, scaleFactor); }); } } break; default: break; } }
/// <summary> /// Loads a model from the given filename, file bytes or browser files. /// </summary> /// <param name="filename">Model filename.</param> /// <param name="fileBytes">Model file bytes.</param> /// <param name="browserFilesCount">Browser files count.</param> private void LoadInternal(string filename, byte[] fileBytes = null, int browserFilesCount = -1) { PreLoadSetup(); var assetLoaderOptions = GetAssetLoaderOptions(); if (!Async) { using (var assetLoader = new AssetLoader()) { assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; try { #if !UNITY_EDITOR && UNITY_WEBGL if (browserFilesCount >= 0) { _rootGameObject = assetLoader.LoadFromBrowserFilesWithTextures(browserFilesCount, assetLoaderOptions); } else #endif if (fileBytes != null && fileBytes.Length > 0) { _rootGameObject = assetLoader.LoadFromMemoryWithTextures(fileBytes, FileUtils.GetFileExtension(filename), assetLoaderOptions, _rootGameObject); } else if (!string.IsNullOrEmpty(filename)) { _rootGameObject = assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions); } else { throw new Exception("File not selected"); } CheckForValidModel(assetLoader); } catch (Exception exception) { HandleException(exception); } } FullPostLoadSetup(); } else { using (var assetLoader = new AssetLoaderAsync()) { assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; try { #if !UNITY_EDITOR && UNITY_WEBGL if (browserFilesCount >= 0) { assetLoader.LoadFromBrowserFilesWithTextures(browserFilesCount, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { CheckForValidModel(assetLoader); _rootGameObject = loadedGameObject; FullPostLoadSetup(); }); } else #endif if (fileBytes != null && fileBytes.Length > 0) { assetLoader.LoadFromMemoryWithTextures(fileBytes, FileUtils.GetFileExtension(filename), assetLoaderOptions, null, delegate(GameObject loadedGameObject) { CheckForValidModel(assetLoader); _rootGameObject = loadedGameObject; FullPostLoadSetup(); }); } else if (!string.IsNullOrEmpty(filename)) { assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { CheckForValidModel(assetLoader); _rootGameObject = loadedGameObject; FullPostLoadSetup(); }); } else { throw new Exception("File not selected"); } } catch (Exception exception) { HandleException(exception); } } } }
/// <summary> /// Loads a model from the given filename or given file bytes. /// </summary> /// <param name="filename">Model filename.</param> /// <param name="fileBytes">Model file bytes.</param> private void LoadInternal(string filename, byte[] fileBytes = null) { _loadingTimer.Reset(); _loadingTimer.Start(); PreLoadSetup(); var assetLoaderOptions = GetAssetLoaderOptions(); if (!Async) { using (var assetLoader = new AssetLoader()) { assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; try { #if !UNITY_EDITOR && UNITY_WINRT && (NET_4_6 || NETFX_CORE || NET_STANDARD_2_0) && !ENABLE_IL2CPP && !ENABLE_MONO var extension = FileUtils.GetFileExtension(filename); _rootGameObject = assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, _rootGameObject); #else if (fileBytes != null && fileBytes.Length > 0) { var extension = FileUtils.GetFileExtension(filename); _rootGameObject = assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, _rootGameObject); } else if (!string.IsNullOrEmpty(filename)) { _rootGameObject = assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions); } else { throw new System.Exception("File not selected"); } #endif } catch (Exception exception) { ErrorDialog.Instance.ShowDialog(exception.ToString()); } } if (_rootGameObject != null) { PostLoadSetup(); ShowLoadingTime(); } else { HideLoadingTime(); } } else { using (var assetLoader = new AssetLoaderAsync()) { assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; try { if (fileBytes != null && fileBytes.Length > 0) { var extension = FileUtils.GetFileExtension(filename); assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { _rootGameObject = loadedGameObject; if (_rootGameObject != null) { PostLoadSetup(); ShowLoadingTime(); } else { HideLoadingTime(); } }); } else if (!string.IsNullOrEmpty(filename)) { assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions, null, delegate(GameObject loadedGameObject) { _rootGameObject = loadedGameObject; if (_rootGameObject != null) { PostLoadSetup(); ShowLoadingTime(); } else { HideLoadingTime(); } }); } else { throw new Exception("File not selected"); } } catch (Exception exception) { HideLoadingTime(); ErrorDialog.Instance.ShowDialog(exception.ToString()); } } } }