// It is recommended to deserialize a scene from scene fragments // consider all scenes as made up of scene fragments, even if only one. private SceneFragment GetLatestSceneSerialization() { SceneFragment fragmentToReturn = null; lock (SUDataLock) { if (LatestSUSceneData != null) { byte[] sceneBytes = null; int sceneLength = LatestSUSceneData.Length; sceneBytes = new byte[sceneLength]; Array.Copy(LatestSUSceneData, sceneBytes, sceneLength); // Deserialize the scene into a Scene Fragment fragmentToReturn = SceneFragment.Deserialize(sceneBytes); } } return(fragmentToReturn); }
/// <summary> /// This coroutine will deserialize the latest SU data, either queried from the device /// or from disk and use it to create Unity Objects that represent that geometry /// </summary> /// <param name="completionSource"> /// The <see cref="TaskCompletionSource{TResult}"/> that can be used to signal the coroutine is complete. /// </param> private IEnumerator DisplayDataRoutine(TaskCompletionSource <bool> completionSource) { Debug.Log("SceneUnderstandingManager.DisplayData: About to display the latest set of Scene Objects"); Scene suScene = null; // Get Latest Scene and Deserialize it // Scenes Queried from a device are Scenes composed of one Scene Fragment SceneFragment sceneFragment = GetLatestSceneSerialization(); SceneFragment[] sceneFragmentsArray = new SceneFragment[1] { sceneFragment }; suScene = Scene.FromFragments(sceneFragmentsArray); // Get Latest Scene GUID Guid latestGuidSnapShot = GetLatestSUSceneId(); LastDisplayedSceneGuid = latestGuidSnapShot; if (suScene != null) { // Retrieve a transformation matrix that will allow us orient the Scene Understanding Objects into // their correct corresponding position in the unity world System.Numerics.Matrix4x4?sceneToUnityTransformAsMatrix4x4 = GetSceneToUnityTransformAsMatrix4x4(suScene); if (sceneToUnityTransformAsMatrix4x4 != null) { // If there was previously a scene displayed in the game world, destroy it // to avoid overlap with the new scene about to be displayed DestroyAllGameObjectsUnderParent(SceneRoot.transform); // Allow from one frame to yield the coroutine back to the main thread yield return(null); // Using the transformation matrix generated above, port its values into the tranform of the scene root (Numerics.matrix -> GameObject.Transform) SetUnityTransformFromMatrix4x4(SceneRoot.transform, sceneToUnityTransformAsMatrix4x4.Value, true); // After the scene has been oriented, loop through all the scene objects and // generate their corresponding Unity Object IEnumerable <SceneObject> sceneObjects = suScene.SceneObjects; int i = 0; foreach (SceneObject sceneObject in sceneObjects) { if (DisplaySceneObject(sceneObject)) { if (++i % NumberOfSceneObjectsToLoadPerFrame == 0) { // Allow a certain number of objects to load before yielding back to main thread yield return(null); } } } } // When all objects have been loaded, finish. Debug.Log("SceneUnderStandingManager.DisplayData: Display Completed"); // Run CallBacks for Onload Finished OnLoadFinished.Invoke(); // Let the task complete completionSource.SetResult(true); } }