Search Unity

Camera Clear Flags "Don't Clear" and "Depth Only" don't work as expected on WebGL

Discussion in 'WebGL' started by momo_the_monster, Jan 25, 2017.

  1. momo_the_monster

    momo_the_monster

    Joined:
    Sep 3, 2014
    Posts:
    13
    I have a project with a Camera's Clear Flags set to "Depth Only".
    In the editor, this works as expected, with each frame drawing upon the last.
    When I export for WebGL, it clears to black each frame.

    Is it possible to keep WebGL from clearing each frame? I've tried several shader approaches as well, writing the frame to a RenderTexture and accessing that on the next frame. That also works in the editor but gives a blank (black) output in a WebGL build.
     

    Attached Files:

  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,653
    It's a bug and here is the public tracker for it:
    https://issuetracker.unity3d.com/issues/clear-flags-doesnt-work-properly-on-webgl

    I expect the fix to be in 5.6 and in a 5.5 patch in the coming weeks.

    Thanks for reporting the issue.
     
  3. Pham-Thanh-Binh

    Pham-Thanh-Binh

    Joined:
    Nov 9, 2016
    Posts:
    3
    oh It didn't fix in 5.6. I got it in 5.6
     
  4. Pham-Thanh-Binh

    Pham-Thanh-Binh

    Joined:
    Nov 9, 2016
    Posts:
    3
    upload_2017-6-12_19-54-12.png

    This is result when i try to set depth only for cube camera.
     
  5. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    451
    Disable the AA on your depth camera to solve the problem until we got a patch.
     
  6. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,653
    I should have provided an update on this issue. TLTR: We had to revert the fix before 5.6 release because of performance concerns, in fact if you look at the bug tracker, it will say that the bug will not be fixed. However, it's possible to workaround it.

    By default, the browser always clears the main render buffer. This is because it is more efficient to do so, than to preserve the image from the current frame to the next. This default behavior can be modified on webgl context creation via preserveDrawingBuffer attribute.

    At the moment, there is no way to change this attribute via scripting/ui, however, you can workaround it:
    - open PlaybackEngines/WebGLSupport/BuildTools/Emscripten/src/library_html5.js from Unity installation folder
    - search for preserveDrawingBuffer assignment in emscripten_webgl_init_context_attributes
    - change it's value from 0 to 1
    as follows:
    Code (csharp):
    1.  
    2. {{{ makeSetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer, 1, 'i32') }}};
    3.  
    A cleaner solution is to use an additional camera (which will not clear) to render your scene to a render texture, then blit the image to the main camera.
     
  7. Pham-Thanh-Binh

    Pham-Thanh-Binh

    Joined:
    Nov 9, 2016
    Posts:
    3
    Thanks you roka. Your suggest work fine.

    Thanks Marco-Trivellator : I not yet tried your suggest. Anyway, thanks you for your help.
     
  8. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    451
    On the version 5.6.1P4, my solution "Disable the AA on your depth camera to solve the problem until we got a patch." do not work anymore, if we enable the AA on the quality setting and if we have 2 camera , the Orthographic camera do not render in all clear flag.
     
    Last edited: Jun 19, 2017
  9. Chris_Entropy

    Chris_Entropy

    Joined:
    Apr 11, 2011
    Posts:
    166
    Are there any news on this problem? Will this be fixed in one of the 2017 Versions?
     
  10. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    35
    Will this be fixed in one of the 2019 versions?
     
  11. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    251
    @Marco-Trivellato I don't know if I'm doing this correctly, but the only way I can get a Mac OSX build to render as in the Editor is to create 2 cameras and a render texture. This seems like a complicated way to do what should be easier and fast. The Render texture is slow when I increase the resolution.

    Am I missing something? Is there a way to just have one camera in the scene and not have to use a rendertexture when the desired look is to have the camera clear flags set to depth only?