- Home /
RenderTexture on RawImage does not render anything
I have a render texture defined like so:
Render = new RenderTexture(512, 512, 8);
Render.enableRandomWrite = true;
Render.filterMode = FilterMode.Point;
Render.Create();
And a RawImage defined:
Img = gameobject.GetComponent<RawImage>();
Img.rectTransform.sizeDelta = new Vector2(1, 1);
Img.texture = Render;
I then have a compute shader which is simply the base code that unity provides:
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;
[numthreads(1,1,1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// TODO: insert actual code here!
Result[id.xy] = float4(id.x & id.y, (id.x & 15) / 15.0, (id.y & 15) / 15.0, 0.0);
}
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.
Compute.SetTexture(ComputeKernel, "Result", Render);
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)
Answer by Cd12sd4 · Apr 14, 2021 at 02:39 PM
Apparently the transparency that unity sets by default (value of 0) means transparent, so changing the last value to 1 works:
Result[id.xy] = float4(id.x & id.y, (id.x & 15) / 15.0, (id.y & 15) / 15.0, 1.0);
Your answer
Follow this Question
Related Questions
What is "sampler_ScreenTextures_linear_clamp" and what other options do I have in shaders? 1 Answer
How does Material's "SetShaderPassEnabled" work? 1 Answer
Custom UI Shader Stencil Problem [willing to pay for fix] 1 Answer
Standard Shader Still Visible through Stencil Shader 0 Answers
CommandBuffer.SetViewProjectionMatrices() does not have any effect, regardless of input 0 Answers