こちらのドキュメントのPhotocaptureを利用して、HoloLens2のカメラで撮影した写真をQuad等のゲームオブジェクトに張り付けるスクリプトを作成しようとしています。
https://learn.microsoft.com/en-us/windows/mixed-reality/develop/unity/locatable-camera-in-unity
以下のコードを試したところ、Unityエディタ上ではWebカメラを使用して問題なく動作したのですが、HoloLens2で動作をさせようとしても、Quadに写真が表示されません。
調べてみたところ、適切な写真のサイズなどがPCとHoloLens2で違うことが原因であるという記事を見つけたのですが、解決方法がわかりませんでした。
解決方法をご存知の方、具体的な解決方法やソースコードを教えていただきたいです。
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Windows.WebCam;
public class PhotoCaptureTest : MonoBehaviour
{
private PhotoCapture photoCaptureObject = null;
private Resolution cameraResolution;
public GameObject quad;
Texture2D texture;
public void Start()
{
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
}
private void OnPhotoCaptureCreated(PhotoCapture captureObject)
{
photoCaptureObject = captureObject;
cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
CameraParameters cameraParameters = new CameraParameters(WebCamMode.PhotoMode)
{
hologramOpacity = 0.0f,
cameraResolutionWidth = cameraResolution.width,
cameraResolutionHeight = cameraResolution.height,
pixelFormat = CapturePixelFormat.BGRA32
};
captureObject.StartPhotoModeAsync(cameraParameters, OnPhotoModeStarted);
}
private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
photoCaptureObject.TakePhotoAsync(OnPhotoCaptured);
}
else
{
Debug.Log("Photo is not ready");
}
}
private void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
if (result.success)
{
texture = new Texture2D(cameraResolution.width, cameraResolution.height);
//カメラで撮影した画像データをテクスチャにコピー
photoCaptureFrame.UploadImageDataToTexture(texture);
byte[] bodyData = texture.EncodeToJPG();
Hyouji();
}
photoCaptureObject.StopPhotoModeAsync(OnPhotoCaptureStopped);
}
private void OnPhotoCaptureStopped(PhotoCapture.PhotoCaptureResult result)
{
photoCaptureObject.Dispose();
photoCaptureObject = null;
}
private void Hyouji()
{
Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer;
quadRenderer.material = new Material(Shader.Find("Unlit/Texture"));
quadRenderer.material.SetTexture("_MainTex", texture);
}
}