Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Render texture not being cleared

Discussion in 'General Graphics' started by CoughE, Aug 13, 2016.

  1. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    Hello all. I am creating a sort of canvas, which utilizes a render texture. You can "draw" objects in front of a camera, which captures what you are drawing into the camera and then into a render texture. I have this working great.

    My problem arises in that the render texture does not clear, that is get rid of all of the pixels when I tell it to. Even if the camera has nothing at all in it's viewport, I can go onto the render texture and see there is still something written to the texture.

    I tried saving my render texture with the changes made into a texture, and then I clear the objects. I then try to set the render texture again, this time with an empty camera, which I assumed would then make the texture empty. This does not seem to be the case though. Does anyone know why this is happening? RenderTexture's DiscardContents() method along with GL's Clear() method does not work.
     
  2. Lost-in-the-Garden

    Lost-in-the-Garden

    Joined:
    Nov 18, 2015
    Posts:
    176
    If nothing is in the camera's view port then nothing is drawn onto the target texture. If you wanted to clear the camera that way, you would have to render for example a view filling quad in the color you want to clear with.

    My guess is that discardContents() only frees the memory and does not clear the texture per se.

    GL.clear cleans the active render target, which is not the camera's target texture.

    A quick google search didn't dig up an answer about how to clear the texture though. The brute force way would be to simply set every pixel to the desired color, but there might be more efficient ways to do that.