private IEnumerator GetTextureOnlyAlphaByRT(RenderTexture tex)
{
yield return new WaitForEndOfFrame();
// Alpha8 -- 只拿a值,但是会产生全黑图
_myTex = new Texture2D(render.width,render.height, TextureFormat.ARGB32, true);
//激活为当前renderTex
RenderTexture.active = tex;
// 读取像素(可进行裁剪)
_myTex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
for (int i = 0; i < tex.height; i++)
{
for (int j = 0; j < tex.width; j++)
{
Color newColor = new Color();
// 产生灰度值
newColor.r = newColor.g = newColor.b = _myTex.GetPixel(i, j).a;
if (_myTex.GetPixel(i, j).a == 1)
{
newColor.a = 0;
}
else
{
newColor.a = 1;
}
_myTex.SetPixel(i, j, newColor);
}
}
_myTex.Apply();
_maskImg.texture = new NTexture(_myTex, new Rect(0, 0, tex.width, tex.height));
//saveTexture2D(_myTex, yourFile);
}