private void HandleAnchorLocated(object sender, AnchorLocatedEventArgs args) { Debug.Log($"Anchor recognized as a possible Azure anchor"); if (args.Status == LocateAnchorStatus.Located || args.Status == LocateAnchorStatus.AlreadyTracked) { currentCloudAnchor = args.Anchor; AppDispatcher.Instance().Enqueue(() => { Debug.Log($"Azure anchor located successfully"); var indicator = Instantiate(anchorPositionPrefab); #if WINDOWS_UWP || UNITY_WSA indicator.gameObject.CreateNativeAnchor(); if (currentCloudAnchor == null) { return; } Debug.Log("Local anchor position successfully set to Azure anchor position"); indicator.GetComponent <UnityEngine.XR.WSA.WorldAnchor>().SetNativeSpatialAnchorPtr(currentCloudAnchor.LocalAnchor); #elif UNITY_ANDROID || UNITY_IOS Pose anchorPose = Pose.identity; anchorPose = currentCloudAnchor.GetPose(); Debug.Log($"Setting object to anchor pose with position '{anchorPose.position}' and rotation '{anchorPose.rotation}'"); indicator.transform.position = anchorPose.position; indicator.transform.rotation = anchorPose.rotation; // Create a native anchor at the location of the object in question indicator.gameObject.CreateNativeAnchor(); #endif indicator.Init(currentTrackedObject); anchorArrowGuide.SetTargetObject(indicator.transform); activeAnchors.Add(currentTrackedObject.SpatialAnchorId, indicator); // Notify subscribers OnFindAnchorSucceeded?.Invoke(this, EventArgs.Empty); currentWatcher?.Stop(); currentTrackedObject = null; }); } else { Debug.Log($"Attempt to locate Anchor with ID '{args.Identifier}' failed, locate anchor status was not 'Located' but '{args.Status}'"); } StopAzureSession(); }
/// <summary> /// Take a photo from the WebCam. Make sure the camera is active. /// </summary> /// <returns>Image data with a Texture for thumbnail.</returns> public Task <ImageThumbnail> TakePhotoWithThumbnail() { if (!IsCameraActive) { throw new Exception("Can't take photo when camera is not ready."); } return(Task.Run(() => { var completionSource = new TaskCompletionSource <ImageThumbnail>(); AppDispatcher.Instance().Enqueue(() => { Debug.Log("Starting photo capture."); #if UNITY_WSA photoCapture.TakePhotoAsync((photoCaptureResult, frame) => { Debug.Log("Photo capture done."); var buffer = new List <byte>(); frame.CopyRawImageDataIntoBuffer(buffer); var texture = new Texture2D(2, 2); var imageData = buffer.ToArray(); texture.LoadImage(imageData); var imageThumbnail = new ImageThumbnail { ImageData = imageData, Texture = texture }; completionSource.TrySetResult(imageThumbnail); }); #else var tex = new Texture2D(webCamTexture.width, webCamTexture.height); tex.SetPixels(webCamTexture.GetPixels()); tex.Apply(); var data = tex.EncodeToPNG(); var imageThumbnail = new ImageThumbnail { ImageData = data, Texture = tex }; completionSource.TrySetResult(imageThumbnail); #endif }); return completionSource.Task; })); }
private async void CreateAsaAnchorEditor(Transform indicatorTransform) { var indicator = Instantiate(anchorPositionPrefab, indicatorTransform.position, indicatorTransform.rotation); anchorCreationController.StartProgressIndicatorSession(); await Task.Delay(2500); var mockAnchorId = Guid.NewGuid().ToString(); currentTrackedObject.SpatialAnchorId = mockAnchorId; activeAnchors.Add(currentTrackedObject.SpatialAnchorId, indicator); AppDispatcher.Instance().Enqueue(() => { indicator.Init(currentTrackedObject); OnCreateAnchorSucceeded?.Invoke(this, mockAnchorId); currentTrackedObject = null; }); }
private async void FindAsaAnchorEditor() { anchorCreationController.StartProgressIndicatorSession(); await Task.Delay(3000); var targetPosition = Camera.main.transform.position; targetPosition.z += 0.5f; var indicator = Instantiate(anchorPositionPrefab); indicator.transform.position = targetPosition; indicator.Init(currentTrackedObject); anchorArrowGuide.SetTargetObject(indicator.transform); // Notify subscribers activeAnchors.Add(currentTrackedObject.SpatialAnchorId, indicator); AppDispatcher.Instance().Enqueue(() => OnFindAnchorSucceeded?.Invoke(this, EventArgs.Empty)); currentTrackedObject = null; }
private async void CreateAsaAnchor(Transform indicatorTransform) { Debug.Log("\nAnchorManager.CreateAsaAnchor()"); anchorCreationController.StartProgressIndicatorSession(); if (cloudManager.Session == null) { // Creates a new session if one does not exist Debug.Log("await cloudManager.CreateSessionAsync()"); await cloudManager.CreateSessionAsync(); } // Starts the session if not already started Debug.Log("await cloudManager.StartSessionAsync()"); await cloudManager.StartSessionAsync(); var anchorPositionIndicator = Instantiate(anchorPositionPrefab, indicatorTransform.position, indicatorTransform.rotation); // Create native XR anchor at the location of the object anchorPositionIndicator.gameObject.CreateNativeAnchor(); Debug.Log("anchorPosition.gameObject.CreateNativeAnchor()"); // Create local cloud anchor var localCloudAnchor = new CloudSpatialAnchor(); // Set the local cloud anchor's position to the native XR anchor's position localCloudAnchor.LocalAnchor = anchorPositionIndicator.gameObject.FindNativeAnchor().GetPointer(); Debug.Log("anchorPosition.gameObject.FindNativeAnchor().GetPointer()"); // Check to see if we got the local XR anchor pointer if (localCloudAnchor.LocalAnchor == IntPtr.Zero) { Debug.Log("Didn't get the local anchor..."); return; } else { Debug.Log("Local anchor created"); } // Set expiration (when anchor will be deleted from Azure) localCloudAnchor.Expiration = DateTimeOffset.Now.AddDays(7); // Save anchor to cloud while (!cloudManager.IsReadyForCreate) { await Task.Delay(330); var createProgress = cloudManager.SessionStatus.RecommendedForCreateProgress; UnityDispatcher.InvokeOnAppThread(() => Debug.Log($"Move your device to capture more environment data: {createProgress:0%}")); } Debug.Log("cloudManager is ready."); try { // Actually save Debug.Log("await cloudManager.CreateAnchorAsync(localCloudAnchor)"); await cloudManager.CreateAnchorAsync(localCloudAnchor); Debug.Log("Anchor created!"); // Store currentCloudAnchor = localCloudAnchor; // Success? var success = currentCloudAnchor != null; if (success) { Debug.Log($"Azure anchor with ID '{currentCloudAnchor.Identifier}' created successfully"); // Update the current Azure anchor ID Debug.Log($"Current Azure anchor ID updated to '{currentCloudAnchor.Identifier}'"); currentTrackedObject.SpatialAnchorId = currentCloudAnchor.Identifier; activeAnchors.Add(currentTrackedObject.SpatialAnchorId, anchorPositionIndicator); // Notify subscribers Debug.Log("OnCreateAnchorSucceeded?.Invoke(this, currentCloudAnchor.Identifier)"); AppDispatcher.Instance().Enqueue(() => { anchorPositionIndicator.Init(currentTrackedObject); currentTrackedObject = null; OnCreateAnchorSucceeded?.Invoke(this, currentCloudAnchor.Identifier); }); } else { Debug.Log($"Failed to save cloud anchor with ID '{currentCloudAnchor.Identifier}' to Azure"); // Notify subscribers AppDispatcher.Instance().Enqueue(() => { currentTrackedObject = null; Destroy(anchorPositionIndicator.gameObject); OnCreateAnchorFailed?.Invoke(this, EventArgs.Empty); }); } } catch (Exception ex) { Debug.Log(ex.ToString()); } StopAzureSession(); }