Search Unity

Question WebGL clears frame after disabling camera

Discussion in 'Web' started by aer0ace, May 31, 2023.

  1. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I'm looking to have WebGL keep the last rendered frame around (i.e. not get cleared) after the camera is disabled. I'm looking to save some rendering time, since for my application, there will be more idle time than dynamic camera time.

    The thing is, in the Unity Editor and in a Windows Standalone build, it does exactly as I'd expect. The frame does not get cleared. It just hangs around. However, in WebGL, the frame is cleared.

    Does anyone have any ideas for what I can do to prevent the last rendered frame from getting cleared?
     
  2. KamilCSPS

    KamilCSPS

    Joined:
    May 21, 2020
    Posts:
    448
    How about saving the last frame into a texture and just keeping it on instead of RT rendering after it's disabled?
     
    CodeSmile likes this.
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    This is definitely my fallback option, but I was really hoping to not clear the last frame buffer, considering that would be the least expensive solution, especially since it already works for desktop/Unity editor.

    This window is also resizable, so having to deal with a render texture can sometimes be a bit more work.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,997
    You haven't mentioned how you achieve that. I assume it's via Camera Clear Flags?
    And URP or Legacy? You could try a test project with the other renderer to see if that allows not clearing the buffer.

    And does the behaviour change in fullscreen?

    I have a hunch that this is something OpenGL specific. One thing you can try is to go into Player Settings for Windows and disable Auto Graphics API and pick one of the OpenGL renderers as the topmost respectively only supported Graphics API to see if it won't work with desktop OpenGL either. At least it would confirm that this is something OpenGL specific.
     
  5. KamilCSPS

    KamilCSPS

    Joined:
    May 21, 2020
    Posts:
    448
    Good point. My hunch is that it has to do with how WebGL constantly redraws within the canvas, thus why there are so many issues around window resize, scaling when building for it. But just a hunch at this point.
     
  6. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I didn't do anything. I just disabled the camera in LateUpdate(), and the image stays, I was just assuming at that point, the back and front buffer just contain the same image, and it's not getting cleared. The Camera Clear flags wouldn't be used in this scenario since the camera is disabled at that point.

    I've since moved onto the RenderTexture solution, which I was expecting I'd have to do. So far, it's working okay. The 1920x1080 rendertexture is being written to fine, but since the background is transparent, I'm getting some hall of mirrors due to the backbuffer not getting cleared. Is there a way to clear the backbuffer independent of the camera doing it?


    EDIT:
    Oh yeah, for the Desktop standalone target, I switched from DX11 to OpenGL ES3, and I get the same results as DX11. Which is to say, NOT like WebGL.