I'm having trouble with texture.GetData, cause of AccessViolationExcpetions.
I'm trying to do something like the following:
Texture2D texture;
foreach (var mesh in model.Meshes)
foreach (BasicEffect effect in mesh.Effects)
texture = effect.Texture;
or
texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture;
and then:
Color[] data = new Color[texture.Width * texture.Height];
texture.GetData(data);
for (int i = 0; i < data.Length - 1; i++)
{
if (IsSimilar(data[i], new Color(58, 59, 248), 40, 40, 40))
data[i] = Color.Red;
}
texture.SetData(data);
foreach (var mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
effect.Texture = texture;
}
But at texture.GetData(data) it throws a AccessViolationException, apparantly I am not allowed the read the texture on the model?? Does someone know how to fix this?
If I just try it with a loaded texture everything works fine, but because we are dealing with a lot of different models. It would be to much of a hassle to find out which texture the model needs, then recolour that texture and apply it.
What is the format of the texture? Does it have mimaps?
I would expect a normal model to have compressed textures with mipmaps after processing. So the array you are passing into GetData is probably the wrong type and the wrong length.
If you can disable mipmap generation and texture compression on your model processor, do that to immediately get past this issue. BUT the solution for making this work if you need compression and mipmaps gets a lot more complex.