Search Unity

RenderTexture on RawImage not displaying anything

Discussion in 'Editor & General Support' started by Cd12sd4, Nov 11, 2020.

  1. Cd12sd4

    Cd12sd4

    Joined:
    Apr 15, 2020
    Posts:
    3
    I have a RenderTexture defined like so:

    Code (CSharp):
    1.  Render = new RenderTexture(512, 512, 8);
    2. Render.enableRandomWrite = true;
    3. Render.filterMode = FilterMode.Point;
    4. Render.Create();
    And a RawImage defined like so:

    Code (CSharp):
    1.  Img = gameobject.GetComponent<RawImage>();
    2. Img.rectTransform.sizeDelta = new Vector2(1, 1);
    3. Img.texture = Render;
    I then have a compute shader which is simply the base code that unity provides:

    Code (CSharp):
    1.  // Each #kernel tells which function to compile; you can have many kernels
    2. #pragma kernel CSMain
    3. // Create a RenderTexture with enableRandomWrite flag and set it
    4. // with cs.SetTexture
    5. RWTexture2D<float4> Result;
    6. [numthreads(1,1,1)]
    7. void CSMain(uint3 id : SV_DispatchThreadID)
    8. {
    9.      // TODO: insert actual code here!
    10.      Result[id.xy] = float4(id.x & id.y, (id.x & 15) / 15.0, (id.y & 15) / 15.0, 0.0);
    11. }
    12.  
    I then call the compute shader such that it will render the sierpiński triangle that the unity base code generates onto my raw image.

    Code (CSharp):
    1. Compute.SetTexture(ComputeKernel, "Result", Render);
    2. Compute.Dispatch(ComputeKernel, 512, 512, 1);
    The problem is that the raw image remains blank. Using the inspect I can see that the render texture has generated the correct image, but for somereason raw image doesn't show it.

    If I use a normal sprite image, or convert the render texture to a Texture2D using ReadPixels it works fine, however I should not need to do this with a render texture and raw image right?

    Image of the raw image not working, but the render texture is successful as shown in the inspector:


    For some reason, when I set the material of the raw image to Default-Line it works for some reason, but when the material is blank or Sprites-Default it does not work:



    I have a thought that this is due to the texture format as unity uses DXT5 compression for Texture2D and Sprites, but I am not sure how to get this compression from the compute shader. I do not want to have to convert my render texture to a texture 2D either.

    If someone could help me solve this issue that would be great :D

    Additional information:
    • Unity version 2020.1.4f1

    • I am using Universal RP in my project (I have tested this in a separate project without Universal RP and the same problem occurs)
     
  2. Cd12sd4

    Cd12sd4

    Joined:
    Apr 15, 2020
    Posts:
    3
    The images I sent in the previous post seemed to have not worked so I have attached them here again:

    For some reason, when I set the material of the raw image to Default-Line it works for some reason, but when the material is blank or Sprites-Default it does not work:

    Capture.PNG
    Image of the raw image not working, but the render texture is successful as shown in the inspector:

    Capture1.PNG
     
    ArtKuper, Havokki and Vlad0sZ like this.
  3. ing_unity

    ing_unity

    Joined:
    Nov 30, 2020
    Posts:
    12
    Because the alpha channel value is zero.
    Just need to change the code like below:
    Result[id.xy] = float4((id.x / 255.0), (id.y / 255.0), 0.0, 1.0);
     
    TiiLeaf, Narkdagas and macka1080 like this.
  4. JonathanCel

    JonathanCel

    Joined:
    Feb 17, 2021
    Posts:
    22
    Might also be worth noting that RawImage needs to be part of a canvas, while a SpriteRenderer does not.