Search Unity

Graphics.Blit produces transparent texture when passing a Material (bug?)

Discussion in 'Web' started by uwdlg, Jul 23, 2021.

  1. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    (Using Unity 2021.1.7 and URP 11):
    For a background blur effect, I render the current camera view minus UI to a RenderTexture, pass the contents to a Texture2D via Texture2D.ReadPixels and Blit that Texture2D to another RenderTexture using a material with a blur shader. All this works in the Editor but not in the WebGL build, where it seems that the resulting RenderTexture is fully transparent (I can't see it when applied to a RawImage but can see another image placed behind it). Testing the steps individually, I found that the problem is tied to calling Graphics.Blit with a Material as third parameter. The shader doesn't seem to be the root of the problem as I also tried Unlit/Texture, UI/Unlit/Transparent, Universal Render Pipeline/Unlit, Universal Render Pipeline/2D/Sprite-Lit-Default with the same result. Omitting the Material parameter produces a fully opaque texture as expected (minus the desired blur effect of course).
    I don't know how to proceed, is this due to some platform-specific limitation I'm unaware of, should I turn to the issue tracker or is there some other way to get a blurred version of my RenderTexture?
     
  2. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    46
    Hello,

    Are there any errors in the browser console when you use a material when calling
    Graphics.Blit
    ?
    On a side note, I think you can get rid of the
    Texture2D.ReadPixels
    step and directly use the
    RenderTexture
    as the input of the
    Graphics.Blit
    . Depending on the platform calling
    Texture2D.ReadPixels
    can be inefficient as it copies data from graphics memory to CPU memory and back to graphics memory to update the texture.

    Best wishes

    Marcel
     
  3. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Hi, thanks for replying!
    There aren't any error messages in the console, but I'll leave the OpenGL log stuff below in case that helps.
    Regarding the extra
    Texture2D.ReadPixels
    step: I could have sworn I looked into that and found that
    Graphics.Blit
    doesn't accept a RenderTexture, but of course RenderTexture subclasses Texture, so thanks, don't know how I missed that.
    Initialize engine version: 2021.1.7f1 (d91830b65d9b)
    test.loader.js:1 Creating WebGL 2.0 context.
    test.framework.js:2 Renderer: WebKit WebGL
    test.framework.js:2 Vendor: WebKit
    test.framework.js:2 Version: OpenGL ES 3.0 (WebGL 2.0 (OpenGL ES 3.0 Chromium))
    test.framework.js:2 GLES: 3
    test.framework.js:2 EXT_color_buffer_float GL_EXT_color_buffer_float EXT_color_buffer_half_float GL_EXT_color_buffer_half_float EXT_disjoint_timer_query_webgl2 GL_EXT_disjoint_timer_query_webgl2 EXT_float_blend GL_EXT_float_blend EXT_texture_compression_bptc GL_EXT_texture_compression_bptc EXT_texture_compression_rgtc GL_EXT_texture_compression_rgtc EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic EXT_texture_norm16 GL_EXT_texture_norm16 KHR_parallel_shader_compile GL_KHR_parallel_shader_compile OES_texture_float_linear GL_OES_texture_float_linear WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_compressed_texture_s3tc_srgb GL_WEBGL_compressed_texture_s3tc_srgb WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_lose_context GL_WEBGL_lose_context WEBGL_multi_draw GL_WEBGL_multi_draw OVR_multiview2 GL_OVR_multiview2
     
    Last edited: Aug 11, 2021
  4. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    68
    Is there any update regarding this issue? I have the exact same problem that webgl produces a transparent rendertexture when providing a material in the Graphics.Blit statement.
    I tried many things but had no success so far
     
    MrDizzle26 likes this.
  5. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    68
    Okay this is definatly a bug ... i just found out that if i build the exact same project on MacEditor for WebGL everything works as expected ... same Project state built on WindowsEditor for WebGL its like described renders only Transparent.
    I submitted this bug some days ago ... had no feedback yet ... but i did not include the mac windows difference since i just learned about it.
     
  6. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Good to hear I'm not alone with this problem and that it does in fact seem to be a bug. I don't have any news, but could you post a link to the bugtracker entry? Might help getting more traction.
     
    MrDizzle26 likes this.
  7. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    68
    I will share the bugtracker url as soon as i get one form unity. currently i only have a ticket number and its still open
     
  8. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Any news?
     
  9. Steamc0re

    Steamc0re

    Joined:
    Nov 24, 2014
    Posts:
    144
    I am having trouble with this suddenly after upgrading to 2021.2 and later. Graphics.Blit with 3 arguments (material) produces a white texture. Submitted a bug
     
  10. Cactuar18

    Cactuar18

    Joined:
    Jun 29, 2013
    Posts:
    3
    Getting the same error, glad to see it's not just me at least. Makes passing through Render Textures with alpha impossible.
     
  11. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    So it's been a few months, has anyone gotten feedback on the bug reports? Or found that the problem has been fixed in some newer version? Still looking for some kind of fix and I can't find related bug reports on the issue tracker.
     
    MrDizzle26 likes this.
  12. MrDizzle26

    MrDizzle26

    Joined:
    Feb 8, 2015
    Posts:
    36
    I am also having this issue!
     
  13. infrared511

    infrared511

    Joined:
    Apr 11, 2020
    Posts:
    6
    I am also having this issue. Surprised I haven't seen a fix yet, I hope I just missed it.
     
  14. Jakub_Machowski

    Jakub_Machowski

    Joined:
    Mar 19, 2013
    Posts:
    647
    Still waiting for fix.
     
  15. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    Can anyone link me to a bug report on this issue? I see a couple of people who said they submitted them, are there links to them?
     
  16. Jakub_Machowski

    Jakub_Machowski

    Joined:
    Mar 19, 2013
    Posts:
    647
  17. Smitzo

    Smitzo

    Joined:
    Mar 23, 2019
    Posts:
    11
    In the shader of the material passed in Graphics.Blit(src, dst, mat), set ZTest to Always.
    If your material for some reason needs ZTest LEqual, then make sure the textures being passed to the material have no depth data.
    One way is to get a temporary RenderTexture with RenderTexture.GetTemporary(width, height, 0) where 0 = depth.
    Then Blit your source texture to the temporary RT with Graphics.Blit(src, tempRT).
    Now you can pass it to the material.
    Do this to the src tex or RT being passed to the material, as well as to tex or RT being passed to with mat.SetTexture();

    I also have seen some people posting that changing ZTest to Always has not solved the issue until they passed a texture with no depth data. So you might want to do both to be sure.
     
    SpikeDevelops likes this.
  18. SpikeDevelops

    SpikeDevelops

    Joined:
    Mar 12, 2018
    Posts:
    8
    This solved things for me on 2021.3.16f
    I was Blitting to and from a temp texture back to the camera and webgl would output a black screen until this fix