I am trying to manipulate a Texture2D using GetRawTextureData<Color32>()
as this does not require extra memory to be allocated like GetPixels32()
does.
However, I can't make sense of the data it is providing or how to use it.
GetRawTextureData<Color32>()
is directly compatible with RGBA32 Textures as per: "RGBA32 texture format data layout exactly matches Color32 struct" at this page.
But if a Texture2D is loaded from a JPG byte Array and it has an RBG24 format, the output it provides is nonsense:
var texelsReal = texture.GetPixels32();
var texelsColor32 = texture.GetRawTextureData<Color32>();
var texelsColor = texture.GetRawTextureData<Color>();
for (int i = 0; i < 200; i++) {
Debug.Log("TEXEL REAL: " + texelsReal[i] + " || TEXEL COLOR32: " + texelsColor32[i] + " TEXEL COLOR: " + texelsColor[i] + " || texture format " + texture.format);
}
This will output something like:
TEXEL REAL: RGBA(120, 146, 233, 255) || TEXEL COLOR32: RGBA(225, 146, 190, 174) || TEXEL COLOR: RGBA(0.003, 0.000, 0.000, 183.106) ||Texture Format RGB24
Only the GetPixels32()
version is actually legitimate and the others are total nonsense.
I need GetRawTextureData<T>
to contain real data, as I want to then reallocate pixels by shifting them around using it as per this thread.
But when I try that currently, I get garbled nonsense because I'm moving around nonsense from one place to another and it then seems to apply that nonsense at the end and ruin the photo.
I tried converting the texture like this:
Texture2D copyTex = new Texture2D(photoThumbT2D.width, photoThumbT2D.height, TextureFormat.RGBA32, false);
bool succeeded = Graphics.ConvertTexture(photoThumbT2D, copyTex);
Debug.Log(" CONVERT SUCCESS " + succeeded + " FORMAT " + copyTex.format + " ORIG " + photoThumbT2D.format);
copyTex.Apply(true); //breaks it for unknown reasons...
photoThumbT2D = copyTex;
But this just creates a grey texture. I was instructed in the other thread to use a struct like:
public struct RGB24 { public byte r, g, b }
Then presumably:
var texels = texture.GetRawTextureData<RBG24>();
But this gives me an error saying no implicit conversion is possible. I don't know how to solve that if I can.
Any solutions? Thanks.
RGB24
data into aColor32
collection, or vice versa? You need to use the same type consistently in both collections you want to pass data between. \$\endgroup\$