TorqueX/CombiningTextures
From TDN
Contents |
Introduction
This is a tutorial for combining textures and applying them to torque objects that have materials
Get some Textures!!!
Given that textures are usually managed in TorqueX as Materials, if you want to manipulate them using XNA functions, you need them in the XNA texture form, which is Texture2D (At least for 2D content, not sure about 3D) So there's the way you can load a texture from the content pipeline
Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\controller\xboxControllerButtonA")
Or you can just get the texture from a material that is already loaded
((piece.Material as SimpleMaterial).Texture.Instance as Texture2D)
Once you get a texture, in whatever unseemly way you want, we can start to manipulate it.
Make the combine texture function
I figured out how to render stuff from one Texture2D(XNA) onto another. So I made a function that encapsulated that
public static Texture2D CombineTextures(Texture2D destTexture, Texture2D sourceTexture, Vector2 destSize, Vector2 position, Vector2 sourceSize)
{
RenderTarget2D myTarget = new RenderTarget2D(Game.Instance.GraphicsDevice, ((int)destSize.X), ((int)destSize.Y), 1, destTexture.Format);
TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, myTarget);
TorqueEngineComponent.Instance.Game.GraphicsDevice.Clear(ClearOptions.Target, new Vector4(0f, 0f, 0f, 0f), 0.0f, 0);
// Blit to the other texture, or so I thought :(
SpriteBatch spriteBatch = new SpriteBatch(TorqueEngineComponent.Instance.Game.GraphicsDevice);
spriteBatch.Begin();
spriteBatch.Draw(destTexture, new Rectangle(0, 0, ((int)destSize.X), ((int)destSize.Y)), Color.White);
spriteBatch.Draw(sourceTexture, new Rectangle(((int)position.X), ((int)position.Y), ((int)sourceSize.X), ((int)sourceSize.Y)), Color.White);
spriteBatch.End();
TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, null);
return myTarget.GetTexture();
}
You have to set the render target and then you can you clear the background of the target so that it doesn't show up as a black background.
Then you can really do any XNA drawing on the texture that you want, but here we just use sprite batch to draw the source texture onto the dest texture. You then return the texture from the render target.
Use the function
Here's how I used this function with torque.
tempBackgroundTexture = MaterialHelper.CombineTextures(tempBackgroundTexture, MaterialHelper.Instance.GetButtonTexture((ButtonEnum)cardIndex), cardBackground.Size, buttonOffset, buttonSize);
(cardBackground.Material as SimpleMaterial).SetTexture(tempBackgroundTexture);
cardBackground is a sprite representing a card in our game. Here we draw the appropriate xbox button on the card background, and then what we do is set the texture of cardBackground.Material with this Texture2D that we've been manipulating.
Conclusion
I hope this helps!



