I have 2 issues when using PhotoCapture on the Hololens 2, which are probably connected. They did not occur on the Hololens 1.
- The only obtainable resolution is 3904x2196
- Getting the CameraToWorldMatrix always fails
As can be seen in the docs, this resolution has no framerate associated with it. My presumption is that the CameraToWorldMatrix is only obtainable for camera profiles with a lower resolution.
How can i change the resolution and get the matrix within Unity?
Minimal Reproducible Example
I am using Unity 2019.2.19f1 and Visual Studio 2019 Community (16.4.5)
- Create a new Unity Project following the steps here, except using IL2CPP as the scripting backend instead of .NET
- In Player Settings: Capabilities enable InternetClient, InternetClientServer, PrivateNetworkClientServer, Webcam, Microphone and SpatialPerception, in Player Settings: Supported Device Families select Holographic
Create a new empty gameobject and add the following script:
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Windows.WebCam; public class PhotoCaptureController : MonoBehaviour { PhotoCapture photoCaptureObject = null; bool isRunning = false; void Start() { StartCoroutine(StartCameraCapture()); } private IEnumerator StartCameraCapture() { if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) { yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); } if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { Debug.Log("Creating PhotoCapture"); PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated); } else { Debug.Log("Webcam Permission not granted"); } } private void Update() { if (isRunning) { photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory); } } void OnPhotoCaptureCreated(PhotoCapture captureObject) { photoCaptureObject = captureObject; IEnumerable<Resolution> availableResolutions = PhotoCapture.SupportedResolutions; foreach (var res in availableResolutions) { Debug.Log("PhotoCapture Resolution: " + res.width + "x" + res.height); } Resolution cameraResolution = availableResolutions.OrderByDescending((res) => res.width * res.height).First(); CameraParameters c = new CameraParameters(); c.hologramOpacity = 0.0f; c.cameraResolutionWidth = cameraResolution.width; c.cameraResolutionHeight = cameraResolution.height; c.pixelFormat = CapturePixelFormat.BGRA32; captureObject.StartPhotoModeAsync(c, OnPhotoModeStarted); } private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result) { if (result.success) { isRunning = true; photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory); } } void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame frame) { if (result.success) { if (frame.TryGetCameraToWorldMatrix(out Matrix4x4 cameraToWorldMatrix)) { Debug.Log("Successfully obtained CameraToWorldMatrix: " + cameraToWorldMatrix.ToString()); } else { Debug.Log("Failed to obtain CameraToWorldMatrix"); } } frame.Dispose(); } }
- Change Build Settings:
- Build, open VS solution, set build target to ARM64, Debug and deploy to Device