I'm trying to convert some C# code into a shader so that I can get better performance. What my code does is look at the final scene rendered and average out the colors every (for example) 16x8 block of pixels. I've tried for 3 days to get this code working in a shader but I cannot figure it out. Shaders are really beyond me and there doesn't seem to be any up-to-date examples that I can hack together (which is all I could really do)
I did try to use two rendertargets, render the full scene then render it to a smaller target to force a resize. But I ended up losing too much clarity and it doesn't look as good as hand calculating the averages. I think this is because my average code doesn't assume the block area is square, which a resize would.
Can someone help me? Here is my C# code:
private void TranslateImageToTextSurface2(Texture2D image, TextSurface surface)
{
surface.Clear();
image.GetData<Color>(pixels);
// surface.Font.Size represents the size of blocks in pixels.
for (int h = 0; h < image.Height / surface.Font.Size.Y; h++)
{
int startY = (h * surface.Font.Size.Y);
for (int w = 0; w < image.Width / surface.Font.Size.X; w++)
{
int startX = (w * surface.Font.Size.X);
float allR = 0;
float allG = 0;
float allB = 0;
for (int y = 0; y < surface.Font.Size.Y; y++)
{
for (int x = 0; x < surface.Font.Size.X; x++)
{
int cY = y + startY;
int cX = x + startX;
Color color = pixels[cY * image.Width + cX];
allR += color.R;
allG += color.G;
allB += color.B;
//allBri += color.GetBrightness();
}
}
// This only works for square fonts... ack??
byte sr = (byte)(allR / (surface.Font.Size.X * surface.Font.Size.Y));
byte sg = (byte)(allG / (surface.Font.Size.X * surface.Font.Size.Y));
byte sb = (byte)(allB / (surface.Font.Size.X * surface.Font.Size.Y));
var newColor = new Color(sr, sg, sb);
// HERE IS WHERE I WOULD ADD PROCESS THAT AVG COLOR IN MY GAME ENGINE
}
}
}
Here is an example of what I end up out putting just using my code above versus forced resized through two render targets.