MonoGame Tiled – Extract Texture2D

NEZ, an open source MonoGame framework extension, provides the functionality to import and render a tiled map. (.tmx) If MonoGame and NEZ isn’t a term for you, consider reading an introduction article first.

Recap: Following code does allow us to render a tiledmap with the NEZ’s ECS system

But how do we only render one specific layer? And what if we would like to manipulate the Texture2D of that layer. E.g.: Applying a material, shader, matrix manipulation, …

Rendering only a specific layer is already supported by NEZ:

Manipulating the Texture2D however, isn’t. The Texture2D behind each and every layer equals the whole Texture2D of the tileset. Hence, we need to implement our own logic.

Option 1 – RenderTarget2D

Create a new Texture2D, limit the TiledMapComponent’s render layers and call the spriteBatch.Draw within an active RenderTarget2D. Done

Option 2 – Texture2D.GetData

A RenderTarget2D requires a spriteBatch which we maybe won’t have access to. In that case, we can make use of the Texture2D’s .GetData method.

Call the extension method with

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.