I want to procedurally generate a texture2D
by pasting smaller textures as tiles, I've done similar things in Python and MatLab, but do not know how to do that in MonoGame.
This question is similar to what I need to do, but it generates the texture in the Draw()
method, which in my case could be a performance hit.
Like this question, I want to have something that generates the texture and returns it, so I can initialize it as a field instead of re-generating the texture again every time in Draw()
.
But I do not know how to edit a texture2D
, the SetData()
method for Texture2D
is rather confusing, the new data is of type T[]
, how do I pass in another Texture2D
as T[]
? And what are the Int32
parameters doing?
The T
parameter is a convenience in which say, you may pass raw bytes instead of colors.
In short, just stick to Color[]
for T[]
, the framework will handle all the details such as computing the stride/pitch and so on when using raw bytes.
In your case you could just do the following:
GetData
to get colors from source tileSetData
to set a region rectangle the size of source tile in the target textureTo initalize it outside Draw
, I suppose LoadContent
could be a good place since GraphicsDevice
should be not be null at this point.
And if somehow you have to do it in Draw
, use a simple bool NeedsRefresh
as a guard, check if it's true, if yes then generate your stuff and set it to false; the code block would run only once.