Hello everyone,
I have developed a small application to test the PhotoCapture API of Hololens to capture a screenshot with location matrix. The screenshot is correctly taken and adding as texture on a gameobject, but the methods TryGetProjectionMatrix and TryGetCameraToWorldMatrix return always false. Despite hours of research on the Web, I do not find why the location data are not accessible. I have tried on the play mode of unity (computer) and on the Hololens directly, but the result is the same.
I need them because I must extracted the pixels of the real environment behind the virtual elements.
Unity version: 2019.4.27f1
Hololens version: 1
Hololens SDK: 10.0.17763.0
Capabilities activated: InternetClient, WebCam, Microphone, SpatialPerception, GazeInput
You can find below the script associated to the main camera under MixedRealityPlayscape.
I hope that someone can help me. Best regards, Maxime
using UnityEngine;
using System.Collections;
using System.Linq;
using UnityEngine.Windows.WebCam;
public class AutomaticColorAdaptation : MonoBehaviour
{
PhotoCapture photoCaptureObject = null;
Texture2D targetTexture = null;
// Start is called before the first frame update
void Start()
{
Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
// Create a PhotoCapture object
PhotoCapture.CreateAsync(false, delegate(PhotoCapture captureObject) {
photoCaptureObject = captureObject;
CameraParameters cameraParameters = new CameraParameters(WebCamMode.PhotoMode);
cameraParameters.hologramOpacity = 0.0f;
cameraParameters.cameraResolutionWidth = cameraResolution.width;
cameraParameters.cameraResolutionHeight = cameraResolution.height;
cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
// Activate the camera
photoCaptureObject.StartPhotoModeAsync(cameraParameters, delegate(PhotoCapture.PhotoCaptureResult result) {
// Take a picture
photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
});
});
}
// Update is called once per frame
void Update()
{
}
void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
Debug.Log(result.success); // Display True
Debug.Log(photoCaptureFrame.hasLocationData); // Display False
Matrix4x4 m1 = new Matrix4x4();
Matrix4x4 m2 = new Matrix4x4();
Debug.Log(photoCaptureFrame.TryGetCameraToWorldMatrix(out m1)); // Display False
Debug.Log(photoCaptureFrame.TryGetProjectionMatrix(out m2)); // Display False
Debug.Log(m1); // Display Identity matrix
Debug.Log(m2); // Display Identity matrix
// Copy the raw image data into our target texture
photoCaptureFrame.UploadImageDataToTexture(targetTexture);
// Create a gameobject that we can apply our texture to
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer;
quadRenderer.material = new Material(Shader.Find("Unlit/Texture"));
quad.transform.parent = this.transform;
quad.transform.localPosition = new Vector3(0.0f, 0.0f, 3.0f);
quadRenderer.material.SetTexture("_MainTex", targetTexture);
// Deactivate our camera
photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
// Shutdown our photo capture resource
photoCaptureObject.Dispose();
photoCaptureObject = null;
}
}