Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating one Texture2D from multiple Texture2Ds

Tags:

c#

xna

My problem is the I need to represent a length-changing floor using a Texture2D, which means a floor that the sides has the image of the sides, and in the middle it repeats the same 'middle' image, like so: enter image description here

To achieve this, I get the 'left edge', the 'middle' and the 'right edge' textures, problem is I don't know how to merge them into one single texture2D,

It is important to do that at run-time because the floor length is changing (horizontally), I read you can do that using SetData but I have no idea how...

It is very important for me that it will act as one texture and not multiple texture parts because I am using Farseer Physics Engine to move the floor and use it.

I am using C# and XNA with Visual Studio 2010, I am an almost-experienced C# programmer,

Thank you!

like image 737
Itamar Avatar asked Oct 01 '11 08:10

Itamar


2 Answers

This answer may help you. Either you should use HLSL for repeating your floor or you should draw your floor on a RenderTarget and save it as single Texture. Enjoy.

like image 100
icaptan Avatar answered Nov 03 '22 16:11

icaptan


First, create a new Texture2D to serve as your floor texture, specifying the appropriate width and height. Then, get the data of the three textures you want to merge, using the GetData method. Finally, use the SetData method to set the data of the new texture as appropriate (check the link, you can specify the start index).

Warning: GetData and SetData methods are slow. If you need to create this texture only once per game (at the initialization for example), it's not a problem, though.

like image 42
Maja Remic Avatar answered Nov 03 '22 16:11

Maja Remic