彩色图生成灰度图与分离alpha通道
彩色图片生成对应灰度图
using UnityEngine;
using System.Collections;
public class TestPicture : MonoBehaviour {
public Texture2D srcTex;
public float fadeLevel = 0;
//生成灰度图
[ContextMenu("GenerateGrayTex")]
void GenerateGrayTex() {
if (null == srcTex) {
return;
}
Texture2D destTex = new Texture2D(srcTex.width, srcTex.height);
Color32[] destColor = srcTex.GetPixels32();
for (int index = 0; index < destColor.Length; index++) {
float c = (destColor[index].g + destColor[index].b + destColor[index].r) / 3f;
destColor[index].r = System.BitConverter.GetBytes((int)(destColor[index].r * fadeLevel + c * (1 - fadeLevel)))[0];
destColor[index].g = System.BitConverter.GetBytes((int)(destColor[index].g * fadeLevel + c * (1 - fadeLevel)))[0];
destColor[index].b = System.BitConverter.GetBytes((int)(destColor[index].b * fadeLevel + c * (1 - fadeLevel)))[0];
}
destTex.SetPixels32(destColor);
string path = string.Format("{0}/_Test/Picture/dest.png", Application.dataPath);
System.IO.File.WriteAllBytes(path,destTex.EncodeToJPG());
UnityEditor.AssetDatabase.Refresh();
}
//提前alpha通道
[ContextMenu("GenerateGrayAlphaTex")]
void GenerateGrayAlphaTex()
{
if (null == srcTex)
{
return;
}
Texture2D destTex = new Texture2D(srcTex.width, srcTex.height);
Color32[] destColor = srcTex.GetPixels32();
for (int index = 0; index < destColor.Length; index++)
{
destColor[index].r = (destColor[index].a);
destColor[index].g = (destColor[index].a);
destColor[index].b = (destColor[index].a);
destColor[index].a = 0;
}
destTex.SetPixels32(destColor);
string path = string.Format("{0}/_Test/Picture/liuyifei.alpha.png", Application.dataPath);
System.IO.File.WriteAllBytes(path, destTex.EncodeToJPG());
UnityEditor.AssetDatabase.Refresh();
}
}
浙公网安备 33010602011771号