Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question UI elements appearing in output RenderTexture of Graphics.Blit.

Discussion in 'General Graphics' started by materie, Jul 22, 2022.

  1. materie

    materie

    Joined:
    Aug 3, 2013
    Posts:
    3
    Hi everyone,

    I’m working on a terrain generation system which will utilize shaders . I want to be able to combinate layers of different noise types so I created a scriptable object that stores some noise settings. The scriptable object also has a RenderTexture that shows a preview of the noise generated with the given settings. The RenderTexture gets refreshed in OnValidate (the code is quite simple), sometimes I can see parts of the Unity Editor UI (see attached image) in the generated RenderTexture. It’s quite annoying does anyone have any idea what causes this?

    upload_2022-7-22_15-38-29.png

    Code (CSharp):
    1.  
    2.         private void GeneratePreview()
    3.         {
    4.             Material mat = NoiseMapUtils.SetupNoiseMaterial(noiseMapData);
    5.             NoisePreview = new RenderTexture((int)previewSize, (int)previewSize, 1);
    6.             RenderTexture tmp = new RenderTexture(NoisePreview);
    7.             Graphics.Blit(tmp,NoisePreview,mat );
    8.         }
    9.         private void OnValidate()
    10.         {
    11.             SyncSettings();
    12.             GeneratePreview();
    13.         }
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    559
    Very strange. Maybe you can find out with a RenderDoc capture.

    Actually, looking at your code, it's no surprise. You are copying an uninitialized render texture with random contents to NoisePreview.
     
    Last edited: Jul 23, 2022
    materie likes this.
  3. materie

    materie

    Joined:
    Aug 3, 2013
    Posts:
    3
    Thank you, your input helped me fix the issue. I didn't know that RenderTextures are created with random context, haven't used them in this way before and didn't even think about that. Everything is working correctly now (also addressed the memory leaks which I was causing by not releasing the previously created RenderTextures).
     
    c0d3_m0nk3y likes this.
  4. Atlas-terrain

    Atlas-terrain

    Joined:
    Jul 11, 2015
    Posts:
    113
    Hi, im having the same issue ^^, how did you fix this?
     
  5. materie

    materie

    Joined:
    Aug 3, 2013
    Posts:
    3
    Hi,


    Turns out the problem was still there, I didn't test long enough. Even after I made sure all render textures are created and had a black texture copied to them the same thing kept happening.


    In the end I fixed it by applying a slight delay between OnValidate and the function that creates the preview. I think this is a bug.

    Code (CSharp):
    1.         private void OnValidate()
    2.         {
    3.             NoiseSource.noiseSettings = noiseSettings;
    4.             GeneratePreview();
    5.         }
    6.  
    7.         async void GeneratePreview()
    8.         {
    9.             #arbitrary delay
    10.             await Task.Delay(5);
    11.             if (noisePreview != null)
    12.             {
    13.                 noisePreview.DiscardContents();
    14.                 RenderTexture.ReleaseTemporary(noisePreview);
    15.             }
    16.  
    17.             noisePreview = NoiseSource?.Generate((int) textureSize,noiseStrength,textureOffset,textureTiling);
    18.         }
     
    Last edited: Aug 10, 2022