Unity3D WebGL Headless 未渲染

Api*_*oud 5 rendering headless unity-game-engine chromium unity-webgl

我已经在 Unity 的论坛上发布了同样的问题,但没有任何答案,因此也将其发布在这里。

我一直在尝试在无头模式下运行 Unity WebGL构建(通过 puppeteer),同时保存游戏的“屏幕截图”,但相机渲染似乎不起作用。生成的图像都是黑色的

当不处于无头模式(但仍然是 WebGL )时,它会按预期工作。它也可以通过-batchMode在独立构建(例如,windows、mac)中正常工作

这是有问题的代码:

// Problem seems to be in the following 2 lines
RenderTexture.active = camera.targetTexture;
camera.Render();

// same dimensions, both in headless and not headless
Debug.Log("CAMERA TARGET TEXTURE WIDTH: " + camera.targetTexture.width);
Debug.Log("CAMERA TARGET TEXTURE HEIGHT: " + camera.targetTexture.height);

tempTexture2D = new Texture2D(camera.targetTexture.width, camera.targetTexture.height, TextureFormat.RGB24, false);
tempTexture2D.ReadPixels(new Rect(0, 0, camera.targetTexture.width, camera.targetTexture.height), 0, 0);
tempTexture2D.Apply();

// RGBA(0.000, 0.000, 0.000, 1.000): totally black, when in WebGL headless mode. Works fine otherwise.
Debug.Log(tempTexture2D.GetPixels(100, 100, 1, 1)[0].ToString());

// Encode texture into JPG
byte[] bytes = tempTexture2D.EncodeToJPG();

// byte count is almost half when in headless mode
Debug.Log("IMG " + frameNumber + " byte count: " + bytes.Length);
// save to persistentData (indexedDB in WebGL)
// that data is then read on client side and encoded again
Run Code Online (Sandbox Code Playgroud)

我发现 Webgl headfull 和 headless 版本之间存在一些差异(各自的图片如下)。 全面的 GPU 统计数据 无头 GPU 统计

我还尝试设置--use-gl=swiftshader哪个具有更好的 GPU 统计数据,但仍然以黑色显示所有内容: 无头 Swift 着色器 GPU 统计数据

需要明确的是,我传递给 chromium 的参数如下:

args:[
 '--headless',
 '--hide-scrollbars',
 '--mute-audio',
 '--no-sandbox',
 '--use-gl=swiftshader' // tested with and without
]
Run Code Online (Sandbox Code Playgroud)

来自 unity 的无头日志输出如下:

PAGE LOG: Loading player data from data.unity3d
PAGE LOG: Initialize engine version: 2018.4.10f1 (a0470569e97b)
PAGE LOG: Creating WebGL 2.0 context.
PAGE LOG: Renderer: WebKit WebGL
PAGE LOG: Vendor: WebKit
PAGE LOG: Version: OpenGL ES 3.0 (WebGL 2.0 (OpenGL ES 3.0 Chromium))
PAGE LOG: GLES: 3
PAGE LOG: EXT_color_buffer_float GL_EXT_color_buffer_float EXT_float_blend GL_EXT_float_blend EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic OES_texture_float_linear GL_OES_texture_float_linear WEBGL_compressed_texture_etc GL_WEBGL_compressed_texture_etc WEBGL_compressed_texture_etc1 GL_WEBGL_compressed_texture_etc1 WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_lose_context GL_WEBGL_lose_context
PAGE LOG: OPENGL LOG: Creating OpenGL ES 3.0 graphics device ; Context level <OpenGL ES 3.0> ; Context handle 1
PAGE LOG: UnloadTime: 0.340000 ms
PAGE LOG: [.WebGL-0x7fcdb69e1600]GL ERROR :GL_INVALID_OPERATION : glFramebufferTexture2D: <- error from previous GL command
PAGE LOG: [.WebGL-0x7fcdb69e1600]GL ERROR :GL_INVALID_OPERATION : GetShaderiv: <- error from previous GL command
PAGE LOG: WebGL: INVALID_OPERATION: renderbufferStorageMultisample: samples out of range
PAGE LOG: [.WebGL-0x7fcdb69e1600]GL ERROR :GL_INVALID_FRAMEBUFFER_OPERATION : glClear: framebuffer incomplete
PAGE LOG: [.WebGL-0x7fcdb69e1600]GL ERROR :GL_INVALID_FRAMEBUFFER_OPERATION : glDrawElements: framebuffer incomplete
PAGE LOG: [.WebGL-0x7fcdb69e1600]GL ERROR :GL_INVALID_FRAMEBUFFER_OPERATION : glDrawArrays: framebuffer incomplete
PAGE LOG: [.WebGL-0x7fcdb69e1600]GL ERROR :GL_INVALID_FRAMEBUFFER_OPERATION : glBlitFramebufferCHROMIUM: framebuffer incomplete
Run Code Online (Sandbox Code Playgroud)

问题是否完全出在 WebGL 硬件加速上?我可以从我的 WebGL 构建中禁用它吗?

这个问题似乎与以下内容有关:Rendering WebGL image in headless chrome without a GPU

但这似乎工作正常,至少在 MacOS 下: https: //github.com/Apidcloud/WebGLHeadlessRendering

所以我仍然假设它与Unity3D有关。即使头满了,使用时也会变黑swift shader唯一的 GPU 统计数据差异是视频解码——禁用硬件加速Headfull Swift 着色器 GPU 统计数据

谢谢!

编辑答案(有关下面实际答案的更多详细信息):

它最终通过禁用unity中的抗锯齿功能起作用,这似乎会引发一些 OpenGL 错误。有或没有都可以工作。swift shader

Api*_*oud 0

经过 2 或 3 天的努力,并试图找到确切的问题,它最终通过禁用统一的抗锯齿功能起作用,这似乎会引发一些 OpenGL 错误。

也就是说,即使没有 ,Unity3D WebGL 渲染也可以无头工作(通过 chromium 和 puppeteer)swift shader,尽管这可能正是您想要的no-gpu场景(例如,某些服务器)。

对于两种情况(有和没有),日志输出虽然仍然很奇怪,swift shader如下(请注意,帧缓冲区不完整错误消失了):

PAGE LOG: [.WebGL-0x7f842a0f6400]GL ERROR :GL_INVALID_OPERATION : glFramebufferTexture2D: <- error from previous GL command
PAGE LOG: [.WebGL-0x7f842a0f6400]GL ERROR :GL_INVALID_OPERATION : GetShaderiv: <- error from previous GL command
Run Code Online (Sandbox Code Playgroud)

谢谢!