In short in the first example the texture passed was probably null.
I think whoever wrote this code is missing the fundamental difference between get and set data. I say this because of the way the code is structured.
Anyway i made you a example at the bottom, as for the question.
Easier to just explain it step by step… looking to the second method.
public Texture2D CreateModifiedTexture(Texture2D texture)
We pass in a texture to the method normally when you pass a texture you expect it to not be null which is probably why the first method didn’t work.
Then in the next line we make a color array but we haven’t filled it up yet with any actual colors. So the array is initialized but not the colors in it so that the array is defined but its data is only declared.
Color[] data = new Color[texture.Width * texture.Height];
Then we come to this line …
texture.GetData(data);
^^^ this would take the pixels in the texture provided its not null and put it in the data array basically filling it up with colors… right…
then he did this…
for (int i = 0; i < data.Length; i++)
data[i] *= 2;
Which does the exact same thing but overwrites whats in the arrays data that he just filled up ? That is from whatever pixel data was in the texture, provided it was not null ?
Ok then he makes a brand new texture so i guess he didn’t need the original one after all.
Texture2D result = new Texture2D(GraphicsDevice, texture.Width, texture.Height);
He takes the data array and sets all the pixels that is in it on to the newly created texture.
result.SetData(data);
then he returns the data. … ok now that you can see everything that was done here… i hope you can see how most of it doesn’t make much sense.
In short this entire method doesn’t actually use get data at all.
.
.
Here are a few practical use methods.
The first 3 are basically all you need for setting or getting data.
You mostly only set data its kinda dumb to load dumb little one pixel dot textures or checkerboard textures that you can just generate.
Unless this is some sort of editor were you are doing image processing and need complete control as sort of the case were you wish to grey scale or monochrome a image and need to get the data modify it then reset it.
//
public Color[] CreateColorArrayViaGetDataFromTexture(Texture2D texture)
{
Color[] data = new Color[texture.Width * texture.Height];
texture.GetData(data);
return data;
}
public Texture2D CreateTextureViaSetDataFromColorArray(int width, int height, Color[] data)
{
Texture2D result = new Texture2D(GraphicsDevice, width, height);
result.SetData(data);
return result;
}
public Texture2D ModifyTextureViaSetDataFromColorArray(Texture2D texture, Color[] data)
{
texture.SetData(data);
return texture;
}
// these are related to the topic i made them on the fly to show you just reuse the first 3 methods over and over.
public Texture2D CreateGreyScaleCopy(Texture2D texture)
{
var data = CreateColorArrayViaGetDataFromTexture(texture);
// gray scale the color pixels.
for (int i = 0; i < data.Length; i++)
{
var c = data[i];
int n = (c.R + c.G + c.B) / 3;
data[i] = new Color(n, n, n, 255);
}
// we said we would copy it so.
return CreateTextureViaSetDataFromColorArray(texture.Width, texture.Height, data);
}
public Texture2D ModifyTextureToTransparentBlack(Texture2D texture)
{
Color[] data = new Color[texture.Width * texture.Height];
for (int i = 0; i < data.Length; i++)
{
data[i] = new Color(0, 0, 0, 0);
}
// we said we would modify it so.
return ModifyTextureViaSetDataFromColorArray(texture, data);
}
// here is a couple i keep around and use that are helpful.
public Texture2D TextureDotCreate( Color c)
{
Color[] data = new Color[1];
data[0] = c;
return CreateTextureViaSetDataFromColorArray(1, 1, data);
}
public Texture2D CreateCheckerBoard( int w, int h, Color c0, Color c1)
{
Color[] data = new Color[w * h];
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int index = y * w + x;
Color c = c0;
if ((y % 2 == 0))
{
if ((x % 2 == 0))
c = c0;
else
c = c1;
}
else
{
if ((x % 2 == 0))
c = c1;
else
c = c0;
}
data[index] = c;
}
}
return CreateTextureViaSetDataFromColorArray(w, h, data);
}