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

public class GetSumRed : MonoBehaviour
{
    public RenderTexture renderTexture;
    public float TotalRed = 0f; //0 = black, 1 = white

    void FixedUpdate()
    {
        TotalRed = GetRedPixels(renderTexture);
    }

    static public float GetRedPixels(RenderTexture rt)
    {
        // Remember currently active render texture
        RenderTexture currentActiveRT = RenderTexture.active;
        RenderTexture.active = rt;

        float TotalRed = 0f;

        // Create a new Texture2D and read the RenderTexture image into it
        Texture2D tex = new Texture2D(rt.width, rt.height);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);

        Color[] colorvalues = tex.GetPixels();
        for (int i=0; i < colorvalues.Length; i++)
        {
            TotalRed += colorvalues[i].r;
        }
        TotalRed = TotalRed / (tex.width * tex.height);

        // Restore previously active render texture
        RenderTexture.active = currentActiveRT;
        return TotalRed;
    }
}
