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. Dismiss Notice

Question Flipped RenderTexture Y coordinate when reading it with Texture2D.ReadPixels

Discussion in 'General Graphics' started by h_david, Jan 3, 2023.

  1. h_david

    h_david

    Joined:
    May 11, 2017
    Posts:
    5
    I'm writing a simple color picker from a quad mesh that contains a RenderTexture.
    When I read just a single pixel from the render texture, it's Y coordinate is flipped (on Windows DirectX and some other platforms too).
    But when I read the whole Rect, the problem does not occur.

    Here's the code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ColorPicker : MonoBehaviour
    4. {
    5.     const int RTWidth = 3;
    6.     const int RTHeight = 3;
    7.  
    8.     [SerializeField] private Texture2D _debugColorsTexture;
    9.     [SerializeField] private Color _pickedColor;
    10.     [SerializeField] private Color _pickedColorIncorrect;
    11.  
    12.     private Renderer _quadRenderer;
    13.     private Texture2D _texture1x1;
    14.     private Texture2D _textureFULL;
    15.     private RenderTexture _renderTexture;
    16.  
    17.     private void Start()
    18.     {
    19.         _quadRenderer = GetComponent<Renderer>();
    20.         _texture1x1 = new Texture2D(1, 1, TextureFormat.RGB24, false);
    21.         _textureFULL = new Texture2D(RTWidth, RTHeight, TextureFormat.RGB24, false);
    22.  
    23.         _renderTexture = new RenderTexture(RTWidth, RTHeight, 0);
    24.         _renderTexture.filterMode = FilterMode.Point;
    25.  
    26.         RenderTexture.active = _renderTexture;
    27.         Graphics.Blit(_debugColorsTexture, _renderTexture);
    28.         RenderTexture.active = null;
    29.  
    30.         _quadRenderer.material.SetTexture("_MainTex", _renderTexture);
    31.     }
    32.  
    33.     private void Update()
    34.     {
    35.         Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    36.         if (Physics.Raycast(cameraRay, out RaycastHit hit, maxDistance: 100f))
    37.         {
    38.             Vector2 uv = hit.textureCoord;
    39.  
    40.             _pickedColor = PickColor(_renderTexture, uv);
    41.             _pickedColorIncorrect = PickColorIncorrect(_renderTexture, uv);
    42.         }
    43.     }
    44.  
    45.     private Color PickColor(RenderTexture rt, Vector2 uv)
    46.     {
    47.         int x = Mathf.Min((int)(Mathf.Clamp01(uv.x) * RTWidth), RTWidth - 1);
    48.         int y = Mathf.Min((int)(Mathf.Clamp01(uv.y) * RTHeight), RTHeight - 1);
    49.  
    50.         RenderTexture.active = rt;
    51.         Rect textureRect = new Rect(0, 0, RTWidth, RTHeight);
    52.         _textureFULL.ReadPixels(textureRect, 0, 0);
    53.         _textureFULL.Apply();
    54.         RenderTexture.active = null;
    55.  
    56.         return _textureFULL.GetPixel(x, y);
    57.     }
    58.  
    59.     private Color PickColorIncorrect(RenderTexture rt, Vector2 uv)
    60.     {
    61.         int x = Mathf.Min((int)(Mathf.Clamp01(uv.x) * RTWidth), RTWidth - 1);
    62.         int y = Mathf.Min((int)(Mathf.Clamp01(uv.y) * RTHeight), RTHeight - 1);
    63.  
    64.         RenderTexture.active = rt;
    65.         Rect textureRect = new Rect(x, y, 1, 1);
    66.         _texture1x1.ReadPixels(textureRect, 0, 0); // <--- Possible problem occurs here
    67.         _texture1x1.Apply();
    68.         RenderTexture.active = null;
    69.  
    70.         return _texture1x1.GetPixel(0, 0);
    71.     }
    72. }
    73.  
    At first I thought, that this problem is related to target Graphics API, because each API defines it's own texture coordinate space. But when I tested this project on 2 different Android devices (Huawei Honor 9, Huawei P20 Lite), they gave opposite Y values. My Unity version is 2021.3.16f1

    Does anyone know why is the texture Y coordinate flipped, when using the PickColorIncorrect method?

    Attaching .unitypackage if you want to take a look at this. Thanks in advance.
     

    Attached Files:

  2. georgerh

    georgerh

    Joined:
    Feb 28, 2020
    Posts:
    72
    h_david likes this.
  3. h_david

    h_david

    Joined:
    May 11, 2017
    Posts:
    5
    Thanks a lot! This solved my problem.