简体   繁体   中英

Edit texture as 2D array in Xna/MonoGame

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:

  • use GetData to get colors from source tile
  • use SetData to set a region rectangle the size of source tile in the target texture

To 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM