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.

So here's the background:

I'm writing a game, just for my own amusement and education really. I've already come to the conclusion that XNA was the way to go for graphics, I've bought a couple of books, I've gotten basic game graphics going, and that's great.

Now I'm starting to get a little in-depth and I'm starting to need to do stuff not covered in my (beginner) books. In particular, I need to display a sprite using a mask. Actually, what I need to do is display a generic sprite with a different color for each player. After banging around on the web, it seems the way to go is to have a color texture (one for each player) which I display using the mask, then display the generic part of the sprite. This has to be done dynamically, i.e. at runtime because there are too many sprites to keep in memory if I try to generate all the permutations at startup.

So, I need to use a shader. Fine. I've downloaded a sample shader program, and managed to hit it with a hammer until it does something close enough to what I want so that I know I'm on the right track. And here, we come to my problem... I have no friggin' clue what I'm doing. While there are a lot of samples and such about shaders, no one ever actually explains what's going on. For instance, I can't find any real docs on Tex2D. I feel like the guys in Zoolander poking at the computer. So, my question (yes, I have a question) -- where is a good URL or what is a good book to take me from dumskie to reasonably competent to write a basic shader?

share|improve this question
    
The tutorials are on the internet. –  Byte56 Jun 26 '12 at 19:40
    
But really, the best way to learn is doing tutorials and doing what you're already doing, pulling them apart and fiddling. Give it some time and keep doing what you're doing. –  Byte56 Jun 26 '12 at 19:42
2  
Yes they are, and there are 12,234,293,291 of them, of which 6,233,390,100 repeat the same thing, 2,919,192,092 are either wrong or omit entire (necessary) sections, and 12 are actually good complete tutorials. I'm hoping someone can point me at one or more of the 12. –  Donutz Jun 26 '12 at 20:10
    
I have learned a lot from this blog: digitalerr0r.wordpress.com/category/xna-shader-tutorial –  Vodáček Jun 27 '12 at 4:38

1 Answer 1

up vote 1 down vote accepted

If you want to learn about XNA shaders, this resource might help you out:

http://forums.create.msdn.com/forums/t/27849.aspx

However, there may be an easier way to do what you described. The SpriteBatch.Draw method takes in a Color parameter which can be used to tint the color of your sprite's Texture2D. If you pass Color.White, there will be no tinting. If you pass Color.Blue, your sprite will be tinted blue.

Keeping this in mind, if you need multiple sprites rendered with different colors depending on the player, you could assign a color to each player, and when rendering the player's sprite, tint it with the player's color. Something like this:

SpriteBatch.Draw( Player.Texture,   // a Texture2D object depicting the player
                  Player.Location,  // where the Texture should be drawn
                  Player.Color );   // the color the tint the player's sprite

If you use a texture that looks like this:

enter image description here

All the white parts would be tinted blue, while black would be left alone. The various shades of gray would be tinted a darker blue.

share|improve this answer
    
Thanks for that microsoft pointer. A brief examination shows it at least defines things like float4, which is a good omen :) . I've already dismissed the tinting option as it doesn't give enough control over the overall look of the sprite. The output tends to look like you've placed some of that colored cellophane over a drawing. –  Donutz Jun 26 '12 at 20:14
    
Yeah, it can be a bit limited if you want different pieces to be different colors. –  Cypher Jun 26 '12 at 21:21

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.