在 WebGL 构建 Unity 上设置 WebcamTexture 的分辨率

问题描述 投票:0回答:1

我有这个 Windows 应用程序可以切换到 WebGL。

对于网络摄像头分辨率,我需要 16:9 分辨率。

我的代码可在编辑器、Windows 和 Android 版本中运行:

    public IEnumerator RefreshCamera()
    {
        if (camTexture != null)
        {
            Display.texture = null;
            camTexture.Stop();
            camTexture = null;
        }
        else
        {

            WebCamDevice device = WebCamTexture.devices[currentCamIndex];
            camTexture = new WebCamTexture(device.name, 576, 1024, 24);
            camTexture.requestedHeight = 576;
            camTexture.requestedWidth = 1024;
            camTexture.requestedFPS = 24;
            Display.texture = camTexture;
            camTexture.Play();
        }

        yield return new WaitForEndOfFrame();
    }

当我在设置 camTexture 变量时设置请求的分辨率时,这在任何平台上都不起作用,默认为 480 x 640。然而,接下来的几行似乎在 Windows、编辑器和 Android 上可以解决问题。

WebGL 始终默认为 480 x 640。

我在文档中看到IOS不支持requestedHeight/requestedWidth,但没有提到WebGL。

unity-game-engine webcam unity-webgl
1个回答
1
投票

这是 Unity WebGL 错误。 为了在浏览器中获取网络摄像头信息,Unity 使用以下代码:

navigator.mediaDevices.getUserMedia({
        audio: false,
        video: videoInputDevices[deviceId].deviceId ? {
            deviceId: { exact: videoInputDevices[deviceId].deviceId }
    } : true
}).then(function(stream) {...}

Unity 不请求相机功能,并且 getUserMedia() 返回最小可用分辨率 (640x480)。 我们应该为 getUserMedia() 设置自定义约束。例如:

navigator.mediaDevices.getUserMedia({
        audio: false,
        video: videoInputDevices[deviceId].deviceId ? {
            deviceId: {
                exact: videoInputDevices[deviceId].deviceId
            },
            width: {  min: 640, ideal: 1280, max: 1920 },
            height: { min: 480, ideal: 720, max: 1080 },
    } : true
}).then(function(stream) {...}

您可以将此代码添加到您的 [projectname].framework.js 文件中(每次构建后都需要重复),或者可能使用 mergeInto 函数覆盖 JS_WebCamVideo_Start 方法。

© www.soinside.com 2019 - 2024. All rights reserved.