Skip to content

Instantly share code, notes, and snippets.

@ratozumbi
Created September 5, 2022 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratozumbi/722141a0b1cbd7d71fa989be761f7d57 to your computer and use it in GitHub Desktop.
Save ratozumbi/722141a0b1cbd7d71fa989be761f7d57 to your computer and use it in GitHub Desktop.
Alpha blend, gray scale and sepia
public Texture2D ToGrayScale(Color[] cArr)
{
var bottomPixelData = cArr;
int count = bottomPixelData.Length;
var resultData = new Color[count];
for(int i = 0; i < count; i++)
{
Color bottomPixels = bottomPixelData[i];
Color temp = new Color(
(1f-bottomPixels.r),
(1f-bottomPixels.g),
(1f-bottomPixels.b),
(1f-bottomPixels.a));
resultData[i] = bottomPixels * temp.grayscale
* bottomPixels.linear
* bottomPixels.linear
* bottomPixels.linear;
}
var res = new Texture2D(1280, 720);
res.SetPixels(resultData);
res.Apply();
return res;
}
public Texture2D ToSepia(Color[] cArr)
{
var bottomPixelData = cArr;
int count = bottomPixelData.Length;
var resultData = new Color[count];
for(int i = 0; i < count; i++)
{
Color bottomPixels = bottomPixelData[i];
float outputRed = (bottomPixels.r * 0.393f) + (bottomPixels.g * .769f) + (bottomPixels.b * .189f);
float outputGreen = (bottomPixels.r * .349f) + (bottomPixels.g * .686f) + (bottomPixels.b * .168f);
float outputBlue = (bottomPixels.r * .272f) + (bottomPixels.g * .534f) + (bottomPixels.b * .131f);
Color temp = new Color(
(outputRed),
(outputGreen),
(outputBlue),
(1f));
resultData[i] = temp;
}
var res = new Texture2D(1280, 720);
res.SetPixels(resultData);
res.Apply();
return res;
}
public Texture2D AlphaBlend(Texture2D textureBottom, Texture2D textureTop)
{
if (textureBottom.width != textureTop.width || textureBottom.height != textureTop.height)
throw new System.InvalidOperationException("AlphaBlend only works with two equal sized images");
var bottomPixelData = textureBottom.GetPixels();
var topPixelData = textureTop.GetPixels();
int count = bottomPixelData.Length;
var resultData = new Color[count];
for(int i = 0; i < count; i++)
{
Color bottomPixels = bottomPixelData[i];
Color topPixels = topPixelData[i];
float srcF = topPixels.a;
float destF = 1f - topPixels.a;
float alpha = srcF + destF * bottomPixels.a;
Color resultColor = (topPixels * srcF + bottomPixels * bottomPixels.a * destF)/alpha;
resultColor.a = alpha;
resultData[i] = resultColor;
}
var res = new Texture2D(textureBottom.width, textureBottom.height);
res.SetPixels(resultData);
res.Apply();
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment