1
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ The tutorials are on the internet. \$\endgroup\$
    – House
    Jun 26, 2012 at 19:40
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Jun 26, 2012 at 19:42
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Donutz
    Jun 26, 2012 at 20:10
  • \$\begingroup\$ I have learned a lot from this blog: digitalerr0r.wordpress.com/category/xna-shader-tutorial \$\endgroup\$
    – Vodáček
    Jun 27, 2012 at 4:38

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Donutz
    Jun 26, 2012 at 20:14
  • \$\begingroup\$ Yeah, it can be a bit limited if you want different pieces to be different colors. \$\endgroup\$
    – Cypher
    Jun 26, 2012 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .