Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to put some text on a Model and I want it to be dynamic. Did some research and came up with drawing the text on the texture and then set it on the model. I use something like this:

public static Texture2D SpriteFontTextToTexture(SpriteFont font, string text, Color backgroundColor, Color textColor)
{
    Size = font.MeasureString(text);
    RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, (int)Size.X, (int)Size.Y);
    GraphicsDevice.SetRenderTarget(renderTarget);

    GraphicsDevice.Clear(Color.Transparent);

    Spritbatch.Begin();
    //have to redo the ColorTexture
    Spritbatch.Draw(ColorTexture.Create(GraphicsDevice, 1024, 1024, backgroundColor), Vector2.Zero, Color.White);
    Spritbatch.DrawString(font, text, Vector2.Zero, textColor);
    Spritbatch.End();

    GraphicsDevice.SetRenderTarget(null);

    return renderTarget;
}

When I was working with primitives and not models everything worked fine because I set the texture exactly where I wanted but with the model (RoundedRect 3D button). It now looks like that:

Text on a cube

Is there a way to have the text centered only on one side?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

You need to modify your model so that the UV (texture) coordinates place the texture at the correct location.


It's possible that setting the texture address mode to clamp may (sort-of) solve your issue. But this also depends on your model having the correct UV coordinates to make it work.

GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

(The default mode is wrap, which will cause texture coordinates outside the range of 0..1 to wrap around when addressing the texture. Clamp will duplicate the edge pixels outwards, outside that range.)

share|improve this answer
    
I don't really want to change my model in blender. If I use a cube without rounded-rect it works fine since the texture fits in the UV. I just need a rounded rect cube and a text-texture on it. Just like having a sticker on the cube. I'll try the linearClamp and get back to you. –  alecnash Oct 16 '12 at 12:04
3  
A rather wild alternative would be to calculate texture coordinates in a custom shader, based on model-space vertex positions (ignoring the model's own UVs). That would save you modifying the model. But it is kind of crazy. –  Andrew Russell Oct 16 '12 at 12:25
    
So you mean that my only alternative is that. I thought there would be an easier way. –  alecnash Oct 16 '12 at 12:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.