How to properly compare colors from a render texture

Hi Everyone,

I’m having an issue reading and comparing colors from a render texture.

I’m trying to implement the technique described in this blog post

Basically, I am trying to determine which mesh renderers are visible from a given view point in my scene. I have assigned every mesh a unique color using on an unlit material. I then render a spherical panorama of the scene to a render texture (using the techniques described here)

I then try to look at each pixel in the render texture and compare it to the list of unique colors I’ve stored in a look up table… my problem is that none of the colors in the render texture seem to match the colors in the look up table… here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomCull : MonoBehaviour {

    public Material cullMaterial;
    public PanoramaRenderer panoramaRenderer;

    private Dictionary<MeshRenderer, Color> colorLookup;
    private List<GameObject> allObjects;
    private RenderTexture panoramaRT;
    private List<Color> validColors = new List<Color>();

    void Start ()
    {
        colorLookup = new Dictionary<MeshRenderer, Color>();

        allObjects = new List<GameObject>(FindObjectsOfType<GameObject>());
        List<Color> colors = new List<Color>();
        foreach(GameObject g in allObjects)
        {
            MeshRenderer mr = g.GetComponent<MeshRenderer>();
            if(mr != null)
            {
                Color c = CreateUniqueColor();
                mr.material = cullMaterial;
                mr.material.color = c;
                colorLookup.Add(mr, c);                
            }
        }

        if(panoramaRenderer != null)
        {
            RenderTexture rt = panoramaRenderer.RenderPanorama();
            SamplePanorama(rt);
        }
    }

    

	private void SamplePanorama(RenderTexture rt)
    {
        var tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
        RenderTexture.active = rt;
        tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        Color[] colors = tex.GetPixels();
        validColors = new List<Color>();
        Debug.Log(colors.Length);

        foreach(Color c in colors)
        {
            if(!validColors.Contains(c))
            {
                validColors.Add(c);
            }
        }
        foreach(KeyValuePair<MeshRenderer, Color> item in colorLookup)
        {
            if(validColors.Contains(item.Value))
            {
                Debug.LogFormat("{0} is visible", item.Key.name);
            }
        }
    }

    private Color CreateUniqueColor()
    {
        bool uniqueColor = false;
        Color c = Color.white;
        while (!uniqueColor)
        {
            c = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
            if (!colorLookup.ContainsValue(c))
            {
                uniqueColor = true;
            }
        }

        return c;
    }
}

Any ideas where I’m going wrong??

Ok I figured out what was going wrong…

  1. Render Textures needed sRGB checked
  2. Need to store colors as Color32, and to read pixels as Color32 values using GetPixels32