private async Task LoadModel() { // create a root object to parent a loaded model to Entity modelEntity = arrService.CurrentActiveSession.Connection.CreateEntity(); // get the game object representation of this entity modelEntityGO = modelEntity.GetOrCreateGameObject(UnityCreationMode.DoNotCreateUnityComponents); // ensure the entity will sync translations with the server var sync = modelEntityGO.GetComponent <RemoteEntitySyncObject>(); sync.SyncEveryFrame = true; // set position to an arbitrary distance from the parent PlaceModel(); modelEntityGO.transform.localScale = Vector3.one; // load a model that will be parented to the entity var loadModelParams = new LoadModelFromSasOptions(ModelName, modelEntity); var async = arrService.CurrentActiveSession.Connection.LoadModelFromSasAsync(loadModelParams, (float progress) => { LogMessage($"Loading Model: {progress.ToString("P2", CultureInfo.InvariantCulture)}"); }); await async; }
private async Task LoadModel() { // Create a root object to parent a loaded model to. Entity modelEntity = arrService.CurrentActiveSession.Connection.CreateEntity(); // Get the game object representation of this entity. modelEntityGO = modelEntity.GetOrCreateGameObject(UnityCreationMode.DoNotCreateUnityComponents); // Ensure the entity will sync translations with the server. var sync = modelEntityGO.GetComponent <RemoteEntitySyncObject>(); sync.SyncEveryFrame = true; // Hide the scene tree until the model is loaded and we had time to get the AABB and recenter the model. var stateOverride = modelEntityGO.CreateArrComponent <ARRHierarchicalStateOverrideComponent>(arrService.CurrentActiveSession); stateOverride.RemoteComponent.HiddenState = HierarchicalEnableState.ForceOn; // Set position to an arbitrary distance from the parent. PlaceModel(); modelEntityGO.transform.localScale = Vector3.one; // Load a model that will be parented to the entity. var loadModelParams = new LoadModelFromSasOptions(ModelName, modelEntity); var loadModelResult = await arrService.CurrentActiveSession.Connection.LoadModelFromSasAsync(loadModelParams, (float progress) => { LogMessage($"Loading Model: {progress.ToString("P2", CultureInfo.InvariantCulture)}"); }); // Recenter / scale model. var rootGO = loadModelResult.Root.GetOrCreateGameObject(UnityCreationMode.DoNotCreateUnityComponents); rootGO.GetComponent <RemoteEntitySyncObject>().SyncEveryFrame = true; var aabb = (await loadModelResult.Root.QueryLocalBoundsAsync()).toUnity(); bool tooBig = aabb.extents.magnitude > Camera.main.farClipPlane; bool tooFar = aabb.center.magnitude > Camera.main.farClipPlane; float scaleFactor = 1.0f; String modelMessage = "Model loaded"; if (tooBig) { scaleFactor = (2.0f / aabb.extents.magnitude); rootGO.transform.localScale = (rootGO.transform.localScale * scaleFactor); modelMessage += $", too big (scaled to {(scaleFactor).ToString("P2", CultureInfo.InvariantCulture)})"; } rootGO.transform.localPosition = (rootGO.transform.localPosition - aabb.center * scaleFactor); if (tooFar) { modelMessage += $", center too far (moved by {-aabb.center})"; } LogMessage(modelMessage); // Model is loaded and recentered. We can show the model now. stateOverride.RemoteComponent.HiddenState = HierarchicalEnableState.InheritFromParent; }