/// <inheritdoc /> public override void Initialize() { base.Initialize(); sceneEventData = new MixedRealitySpatialAwarenessEventData <SpatialAwarenessSceneObject>(EventSystem.current); CreateQuadFromExtents(normalizedQuadMesh, 1, 1); var accessStatus = SceneObserver.RequestAccessAsync().GetAwaiter().GetResult(); if (accessStatus == SceneObserverAccessStatus.Allowed) { IsRunning = true; observerState = ObserverState.Idle; StartUpdateTimers(); } else { Debug.LogError("Something went terribly wrong getting scene observer access!"); } if (UpdateOnceOnLoad) { observerState = ObserverState.GetScene; //doUpdateOnceOnLoad = true; } }
/// <summary> /// Saves the <see cref="SceneUnderstanding.SceneProcessor"/>'s data stream as a file for later use /// </summary> /// <param name="sceneBuffer">the <see cref="byte[]"/></param> private async void SaveToFile(string prefix) { SceneQuerySettings sceneQuerySettings = new SceneQuerySettings() { EnableSceneObjectQuads = true, EnableSceneObjectMeshes = true, EnableOnlyObservedSceneObjects = false, EnableWorldMesh = true, RequestedMeshLevelOfDetail = LevelOfDetailToMeshLOD(WorldMeshLevelOfDetail) }; var serializedScene = SceneObserver.ComputeSerializedAsync(sceneQuerySettings, QueryRadius).GetAwaiter().GetResult(); var bytes = new byte[serializedScene.Size]; serializedScene.GetData(bytes); var timestamp = DateTime.Now.ToString("yyyyMMdd_hhmmss"); var filename = $"SceneUnderStanding_{timestamp}.bytes"; if (prefix != "") { filename = $"{prefix}_{timestamp}.bytes"; } StorageFolder folderLocation = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await folderLocation.CreateFileAsync(filename); await FileIO.WriteBytesAsync(storageFile, bytes); }
/// <summary> /// /// </summary> /// <param name="sceneObserver">The observer this was build with.</param> public void Initialize(SceneObserver sceneObserver) { thisIsMe = (T)this; instanceID = GetInstanceID(); this.sceneObserver = sceneObserver; main = transform.root.GetComponent <IInitializeMain>(); }
// Start is called before the first frame update private void Start() { player = GetComponent <Rigidbody2D>(); playerAnimations = GetComponent <Animator>(); observer = new SceneObserver(); DieManager manager = new DieManager(observer, ui); }
// Start is called before the first frame update private async void Start() { SceneRoot = SceneRoot == null ? new GameObject("Scene Root") : SceneRoot; if (!SceneObserver.IsSupported()) { Debug.LogError("SceneUnderstandingManager.Start: Scene Understanding not supported."); return; } SceneObserverAccessStatus access = await SceneObserver.RequestAccessAsync(); if (access != SceneObserverAccessStatus.Allowed) { Debug.LogError("SceneUnderstandingManager.Start: Access to Scene Understanding has been denied.\n" + "Reason: " + access); return; } try { #pragma warning disable CS4014 Task.Run(() => RetrieveDataContinuously()); #pragma warning restore CS4014 } catch (Exception e) { Debug.LogException(e); } }
private Scene GetSceneAsync(Scene previousScene) { Scene scene = null; if (Application.isEditor && ShouldLoadFromFile) { if (sceneBytes == null) { Debug.LogError("sceneBytes is null!"); } // Move onto a background thread for the expensive scene loading stuff if (UsePersistentObjects && previousScene != null) { scene = Scene.Deserialize(sceneBytes, previousScene); } else { // This happens first time through as we have no history yet scene = Scene.Deserialize(sceneBytes); } } else { SceneQuerySettings sceneQuerySettings = new SceneQuerySettings() { EnableSceneObjectQuads = RequestPlaneData, EnableSceneObjectMeshes = RequestMeshData, EnableOnlyObservedSceneObjects = !InferRegions, EnableWorldMesh = SurfaceTypes.HasFlag(SpatialAwarenessSurfaceTypes.World), RequestedMeshLevelOfDetail = LevelOfDetailToMeshLOD(WorldMeshLevelOfDetail) }; // Ideally you'd call SceneObserver.ComputeAsync() like this: // scene = await SceneObserver.ComputeAsync(...); // however this has has been problematic (buggy?) // For the time being we force it to be synchronous with the ...GetAwaiter().GetResult() pattern if (UsePersistentObjects) { if (previousScene != null) { scene = SceneObserver.ComputeAsync(sceneQuerySettings, QueryRadius, previousScene).GetAwaiter().GetResult(); } else { // first time through, we have no history scene = SceneObserver.ComputeAsync(sceneQuerySettings, QueryRadius).GetAwaiter().GetResult(); } } else { scene = SceneObserver.ComputeAsync(sceneQuerySettings, QueryRadius).GetAwaiter().GetResult(); } } return(scene); }
private void OnEnable() { var sceneFetcher = SceneObserver.IsSupported() ? (ISceneFetcher) new SceneFetcher() : new BuiltInSceneFetcher(() => fallbackScene.sceneSnapshot); sceneManager = new SceneManager(sceneFetcher); sceneManager.onScene.AddListener(() => drawer.DrawScene(SceneSnapshot.Create(sceneManager.Scene))); }
public override void OnInspectorGUI() { DrawDefaultInspector(); SceneObserver sceneObserver = (SceneObserver)target; if (GUILayout.Button("GatherMains")) { sceneObserver.GatherInitializeMains(); } }
public Scene FetchScene() { var querySettings = Prepare(); SceneUnderstandingState.UpdateState("Querying scene"); var fetchSceneTask = lastScene == null ? SceneObserver.ComputeAsync(querySettings, SearchRadius) : SceneObserver.ComputeAsync(querySettings, SearchRadius, lastScene); var scene = fetchSceneTask.GetAwaiter().GetResult(); SceneUnderstandingState.UpdateState("Scene bytes queried"); return(scene); }
public byte[] FetchSceneBytes() { var querySettings = Prepare(); SceneUnderstandingState.UpdateState("Querying scene bytes..."); var fetchSceneTask = SceneObserver.ComputeSerializedAsync(querySettings, SearchRadius); var sceneBuffer = fetchSceneTask.GetAwaiter().GetResult(); SceneUnderstandingState.UpdateState("Scene bytes queried"); var data = new byte[sceneBuffer.Size]; sceneBuffer.GetData(data); return(data); }
/// <summary> /// Calls into the Scene Understanding APIs, to retrieve the latest scene as a byte array. /// </summary> /// <param name="enableQuads">When enabled, quad representation of scene objects is retrieved.</param> /// <param name="enableMeshes">When enabled, mesh representation of scene objects is retrieved.</param> /// <param name="enableInference">When enabled, both observed and inferred scene objects are retrieved. Otherwise, only observed scene objects are retrieved.</param> /// <param name="enableWorldMesh">When enabled, retrieves the world mesh.</param> /// <param name="lod">If world mesh is enabled, lod controls the resolution of the mesh returned.</param> private void RetrieveData(float boundingSphereRadiusInMeters, bool enableQuads, bool enableMeshes, bool enableInference, bool enableWorldMesh, SceneMeshLevelOfDetail lod) { Debug.Log("SceneUnderstandingManager.RetrieveData: Started."); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); try { SceneQuerySettings querySettings; querySettings.EnableSceneObjectQuads = enableQuads; querySettings.EnableSceneObjectMeshes = enableMeshes; querySettings.EnableOnlyObservedSceneObjects = !enableInference; querySettings.EnableWorldMesh = enableWorldMesh; querySettings.RequestedMeshLevelOfDetail = lod; // Ensure that the bounding radius is within the min/max range. boundingSphereRadiusInMeters = Mathf.Clamp(boundingSphereRadiusInMeters, MinBoundingSphereRadiusInMeters, MaxBoundingSphereRadiusInMeters); // Make sure the scene query has completed swap with latestSUSceneData under lock to ensure the application is always pointing to a valid scene. SceneBuffer serializedScene = SceneObserver.ComputeSerializedAsync(querySettings, boundingSphereRadiusInMeters).GetAwaiter().GetResult(); lock (SUDataLock) { // The latest data queried from the device is stored in these variables LatestSUSceneData = new byte[serializedScene.Size]; serializedScene.GetData(LatestSUSceneData); LatestSceneGuid = Guid.NewGuid(); } } catch (Exception e) { Debug.LogException(e); } stopwatch.Stop(); Debug.Log(string.Format("SceneUnderstandingManager.RetrieveData: Completed. Radius: {0}; Quads: {1}; Meshes: {2}; Inference: {3}; WorldMesh: {4}; LOD: {5}; Bytes: {6}; Time (secs): {7};", boundingSphereRadiusInMeters, enableQuads, enableMeshes, enableInference, enableWorldMesh, lod, (LatestSUSceneData == null ? 0 : LatestSUSceneData.Length), stopwatch.Elapsed.TotalSeconds)); }
/// <summary> /// This method is called right before Init and is implemented through the IInitialize interface, only let the system call this method /// </summary> /// <param name="sceneObserver">The observer this was build with.</param> public void Initialize(SceneObserver sceneObserver) { thisIsMe = (T)this; scene = sceneObserver.Scene; instanceID = GetInstanceID(); this.sceneObserver = sceneObserver; subsToAdd = new List <IInitializeSub>(); subsToRemove = new List <IInitializeSub>(); initializeSubs = new List <IInitializeSub>(); for (int i = 0; i < startingSubs.Count; i++) { IInitializeSub sub = (IInitializeSub)startingSubs[i]; sub.Initialize(sceneObserver); sub.Init(this); initializeSubs.Add(sub); } startingSubs.Clear(); }
/// <summary> /// Use this method to instantiate IInitializeMain Objects /// </summary> /// <typeparam name="T">Reference to the asset you want instantiated.</typeparam> /// <typeparam name="T1">The type of the value to give to the main.</typeparam> /// <param name="asset">Reference to the prefab or ScriptableObject to be instantiated.</param> /// <param name="args">The value given to the instantiated main.</param> /// <param name="observer">Reference to the sceneobserver</param> /// <returns></returns> internal T Create <T, T1>(T asset, T1 args, SceneObserver observer) where T : UnityEngine.Object { IInitializeMain mainI = (IInitializeMain)asset; if (mainI.Scene == SceneMarker.All || mainI.Scene == observer.Scene) { T instance = Instantiate(asset); var main = (IInitializeMain)instance; main.Initialize(observer); main.Init(args); observer.AddMain(main); return(instance); } else { Debug.LogError("The asset " + asset + " cant be instantiated in this scene because it is Scenebound"); return(null); } }
private SceneQuerySettings Prepare() { SceneUnderstandingState.UpdateState("Requesting access"); var status = SceneObserver.RequestAccessAsync().GetAwaiter().GetResult(); if (status != SceneObserverAccessStatus.Allowed) { SceneUnderstandingState.UpdateState($"FAILED - no access: {status}"); throw new Exception($"Expected to get access. Actually got: " + status); } var querySettings = new SceneQuerySettings() { EnableWorldMesh = false, EnableSceneObjectMeshes = false, EnableSceneObjectQuads = true, RequestedMeshLevelOfDetail = SceneMeshLevelOfDetail.Coarse, EnableOnlyObservedSceneObjects = false }; return(querySettings); }