Created
September 5, 2022 19:05
-
-
Save ratozumbi/722141a0b1cbd7d71fa989be761f7d57 to your computer and use it in GitHub Desktop.
Alpha blend, gray scale and sepia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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