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

How can I render negative values into a RenderTexture?

Discussion in 'General Graphics' started by Linvst, May 4, 2020.

  1. Linvst

    Linvst

    Joined:
    Apr 4, 2020
    Posts:
    1
    I used Graphics.DrawMeshInstanced with my custom shader to draw meshes into a camera with a rendertexture attached to it. The renderTexture is in linear space and RFloat format.
    I then use texture2d.ReadPixels and GetRawTextureData to get values form the rendertexture. And negative values are clamped to 0 while positive values keep unchanged.

    I've tried method in this:
    https://forum.unity.com/threads/sol...ing-negative-values-to-render-texture.700868/
    but it doesn't work.

    here is my code reading values from the
    Code (CSharp):
    1.      void Update()
    2.     {
    3.         var texReadback = new Texture2D(rt.width, rt.height, TextureFormat.RFloat, false, true);
    4.         Graphics.SetRenderTarget(rt);
    5.         texReadback.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0, false);
    6.         Graphics.SetRenderTarget(null);
    7.         texReadback.Apply();
    8.  
    9.         var data = texReadback.GetRawTextureData<float>();
    10.         for (int i = 0; i < data.Length; i++)
    11.         {
    12.             float t = data[i];
    13.         }
    14.     }
    Where is the problem?
     
    Last edited: May 5, 2020
  2. samarth-robo

    samarth-robo

    Joined:
    Aug 13, 2020
    Posts:
    4