【问题标题】:LocatableCamera (PhotoCapture) on the Hololens 2Hololens 2 上的 LocatableCamera (PhotoCapture)
【发布时间】:2020-02-26 14:58:03
【问题描述】:

在 Hololens 2 上使用 PhotoCapture 时有 2 个问题,它们可能已连接。它们没有出现在 Hololens 1 上。

  • 唯一可获得的分辨率是 3904x2196
  • 获取 CameraToWorldMatrix 总是失败

docs 中可以看出,此分辨率没有与之关联的帧率。 我的假设是 CameraToWorldMatrix 仅适用于分辨率较低的相机配置文件。

如何在 Unity 中更改分辨率并获取矩阵?


最小可重现示例

我正在使用 Unity 2019.2.19f1 和 Visual Studio 2019 Community (16.4.5)

  1. 按照here 的步骤创建一个新的 Unity 项目,但使用 IL2CPP 而不是 .NET 作为脚本后端
  2. Player Settings: Capabilities 中启用 InternetClientInternetClientServerPrivateNetworkClientServerWebcam麦克风空间感知,在播放器设置:支持的设备系列中选择全息
  3. 创建一个新的空游戏对象并添加以下脚本:

    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();
        }
    }
    
  4. 更改构建设置:
  5. 构建,打开 VS 解决方案,将构建目标设置为 ARM64Debug 并部署到 Device

【问题讨论】:

  • 你能添加你正在使用的代码吗?
  • @derHugo 基本上是从 Unity DocsMS Docs 复制而来的,并稍作调整
  • 您使用的是哪个版本的 unity?是否有任何步骤发生错误?目前,我们很难从您当前的帖子中找到问题的原因。建议您提供 MVCE(stackoverflow.com/help/minimal-reproducible-example) 和重现问题的步骤,以便我们定位问题或找到解决方案。
  • @Hernando-MSFT 你认识没有遇到这两个问题的人吗?我可以提供代码,但我真的怀疑问题是否在我这边,因为相同的代码在 Hololens 1 上按预期工作
  • @Hernando-MSFT 问题出现在 Unity 2019.2.19 和 2018.4.17 (LTS) 中

标签: unity3d hololens windows-mixed-reality


【解决方案1】:

这个CameraCapture 插件可能对你有用。它是 Unity 的原生插件。 readme.txt 包含有关构建它的信息。基本上,在尝试打开 Unity 示例之前,您需要构建原生插件变体。如果您尚未构建 C++/WinRT 组件,您可能需要完成设置步骤 here。 SpatialCameraTracker.cs 文件显示了如何接收相机矩阵。

对此的支持很少,但所有代码都在 repo 中

【讨论】:

  • 谢谢,看起来很有用 - 不幸的是我没有时间去看看这个知道的
猜你喜欢
  • 2021-12-12
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
相关资源
最近更新 更多