Hi
I’m trying to make a small viewer/picker for HDRP material.
so using HDRP/Lit shader, materials have a “maskmap” texture that I want to split into 4 texture : one for each channel (because R channel is metallic, G is occlusion, etc… ). I want to show them in the UI using some RawImage.
mat.GetTexture(“_MaskMap”) gives me a Texture.
In order to read its pixels I need to convert it to a Texture2D, but conversion fail.
Is there a way to convert a Texture to Texture2D ?
OR
Is there a way to access pixels color on a Texture ?
Thanks for the help
if (mat.HasProperty("_MaskMap"))
{
Texture2D maskMap = (Texture2D)mat.GetTexture("_MaskMap"); //This doesn't work : InvalidCastException: Specified cast is not valid.
int w = maskMap.width;
int h = maskMap.height;
Texture2D R_tex = new Texture2D(w, h);
Texture2D G_tex = new Texture2D(w, h);
Texture2D B_tex = new Texture2D(w, h);
Texture2D A_tex = new Texture2D(w, h);
Color tempColor;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
tempColor = maskMap.GetPixel(x, y); //this is why I need a texture2D
R_tex.SetPixel(x, y, new Color(tempColor.r, tempColor.r, tempColor.r, 1));
G_tex.SetPixel(x, y, new Color(tempColor.g, tempColor.g, tempColor.g, 1));
B_tex.SetPixel(x, y, new Color(tempColor.b, tempColor.b, tempColor.b, 1));
A_tex.SetPixel(x, y, new Color(tempColor.a, tempColor.a, tempColor.a, 1));
}
}
R_tex.Apply();
G_tex.Apply();
B_tex.Apply();
A_tex.Apply();
m_RawImage_MaskMap_R.texture = R_tex;
m_RawImage_MaskMap_G.texture = G_tex;
m_RawImage_MaskMap_B.texture = B_tex;
m_RawImage_MaskMap_A.texture = A_tex;
}