How to increase the speed of the procedure OnRenderImage as a script for the main camera

I used an own script instead of a precompiled shader because I wanted to modify it for my purposes. Another problem occured during the use of a shader, was to get access to static classes. It lags during playing and the execution of the sub.

void OnRenderImage (RenderTexture source, RenderTexture destination)
{
Texture2D texture = new Texture2D (source.width, source.height);
texture.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0);
//Using a texture2d for manipulation

for (int y = 0; y <= texture.height - 1; y++) //Iterating through all pixels
{
for (int x = 0; x <= texture.width - 1; x++)
{

Color C = texture.GetPixel (x, y);

var hue = ToHue (C); //Using a hue value to identity the color range

if (Content.Green == true)
{
if (hue >= 69 && hue <= 174)
{
continue;
}
}
else if (Content.Brown == true)
{
if (hue >= 0 && hue <= 60)
{
continue;
}
else if (hue == 359)
{
continue;
}
}
else if (Content.Red == true)
{
if (hue >= 0 && hue <= 16)
{
continue;
}
else if (hue >= 337 && hue <= 358)
{
continue;
}
}
else if (Content.Blue == true)
{
if (hue >= 187 && hue <= 254.8)
{
continue;
}
}
else if (Content.Violet == true)
{
if (hue >= 266 && hue <= 333)
{
continue;
}
}
else if (Content.Orange == true)
{
if (hue >= 5 && hue <= 39)
{
continue;
}
else if (hue == 359)
{
continue;
}

}

float gray = (C.r + C.g + C.b) / 3;
Color ColGray = new Color (gray, gray, gray);
texture.SetPixel (x, y, ColGray);

}
}

texture.Apply ();

Graphics.Blit(texture, destination); //Apply output change
}

public float ToHue(Color color)
{
float min = Math.Min(Math.Min(color.r, color.g), color.b);
float max = Math.Max(Math.Max(color.r, color.g), color.b);

float hue = 0f;

if (max == color.r)
{
hue = (color.g - color.b) / (max - min);
}
else if (max == color.g)
{
hue = 2f + (color.b - color.r) / (max - min);
}
else
{
hue = 4f + (color.r - color.g) / (max - min);
}

hue = hue * 60;

if (hue < 0)
{
hue = hue + 360;
}

return float.Parse(Double.Parse(hue.ToString()).ToString());

}

If anyone has the same problem, here is a bit of optimized code but it’s still slow. I used the fastest quality setting and decreased the resolution down to 1024 x 768.

  • It is recommended to give up math functions
  • to use an iteration through one array, a color array instead of interleaved x and y value iterations
  • declaring objects once and overwrite the values instead of instancing them.
Texture2D texture;
    Color C;
    float hue;
    Color[] Col;
    float min = 0f;
    float max = 0f;
    float hue2 = 0f;

    void Awake()
    {

    }


    void Start()
    {
        Application.targetFrameRate = 100;
        QualitySettings.vSyncCount = 0;
    }



    void OnRenderImage (RenderTexture source, RenderTexture destination)
    {

        texture = new Texture2D(source.width, source.height);

        texture.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0);

        Col = texture.GetPixels();

        for (int k = 0; k <= Col.Length - 1; k++)
        {
            hue = ToHue (Col[k]);

           // [if Content.color == true]
           // if (hue >= ... && <= ...) continue;

            float gray = (Col[k].r + Col[k].g + Col[k].b) / 3;
            C = new Color(gray,gray,gray);
            Col [k] = C;

        }

        texture.SetPixels (Col);

        texture.Apply (false);

        Graphics.Blit(texture, destination);

    }




    public float ToHue(Color color)
    {

      
    max = 0;
        min = 0;


        if (color.r >= color.g && color.r >= color.b)
        {
            max = color.r;
        }
        else if (color.g >= color.b && color.g >= color.r)
        {
            max = color.g;
        }
        else if (color.b >= color.r && color.b >= color.g)
        {
            max = color.b;
        }


        if (color.r <= color.g && color.r <= color.b)
        {
            min = color.r;
        }
        else if (color.g <= color.b && color.g <= color.r)
        {
            min = color.g;
        }
        else if (color.b <= color.r && color.b <= color.g)
        {
            min = color.b;
        }
          


        hue2 = 0f;

        if (max == color.r)
        {
            hue2 = (color.g - color.b) / (max - min);
        }
        else if (max == color.g)
        {
            hue2 = 2f + (color.b - color.r) / (max - min);
        }
        else
        {
            hue2 = 4f + (color.r - color.g) / (max - min);
        }

        hue2 = hue2 * 60;

        if (hue2 < 0)
        {
            hue2 = hue2 + 360;
        }

        return hue2;


    }