Donnerstag, 1. Oktober 2009

Shader Explanations - Introduction (Part One of probably Five)








Hello first,
I'll introduce you to a mysterium within modders. You probably haven't seen many modders who can create shaders. The only ones are some of Stargate Modding, and the Steiner Modding Group. Me not included, of course. I don't want to show off. Would be bad if I only talk about myself in a post for the public. I mean, it's for you guys out there, not me...
In this tutorial series I will show you how to edit shaders from the all-time-successful game Star Wars - Empire at War™ and explain (most of) them. After you've read this tutorial you'll be able to understand and create shaders of awesome quality, not just the lame original ones. I will only handle the shaders used on maximum details, on less details the explained shaders won't work. That's due to different shader versions, only try this tutorial if your computer supports Pixel Shader 2.0 or higher. Don't trust dxdiag, it always says you have DirectX 10 or higher (I'm saying this because your PC has to support Pixel Shader 4.0 to be able to use DirectX 10).
But hey, if you know what you do, you can achieve this in the end:

Should we begin now? I think so. So, what exactly is a shader?

What is a shader?
A shader is the part of the game that makes the shades. Shadows for the easy people. Yes, you've read it right. They just make the shadows. But why would we need them, I mean shadows aren't something special. A good question, right?
The answer is only  that every game does the shadows on it's own way. And you also calculate all the stuff regarding lights and colors. Lights and shadows aren't computed automatically, you know. You have to make all that in the shaders. And guess what a shader is for: It brings all the calculations being made to the graphics card. But why the graphics card?
Many possibilities:
  1. On the graphics card are special processors only designed to process shaders. And ONLY shaders. It cannot do anything different. Because of that there is something like a Shader Model. You've probably heard about it. The Shader Model defines the ways a Shader can be used. The better the Shader Model, the better things you can achieve with it, or achieve the same results in a faster way.
  2. The calculations being are so intensive for the CPU, you need to call WETA just to play a game from 2004. They have some of the best computers, for all the stuff they put in Peter Jackson's movies (Lord of the rings, King Kong, District 9..., just to name a few). You have to process all the textures (the pictures being projected onto the surface) and geometry, and these are many, REALLY MANY!!! And then the CPU also has to calculate physics (so your bombs do damage), user input (so you can control your units), animations (so your units move and you bombs don't stay at home) and all the other stuff your CPU does.
With all these calculations on the GPU(s) your CPU has more time to make the other thingy things needed. But how does it work?

How does a shader work?
There are different types of shaders. Three, exactly, but we only need the following two to understand most of enough of shaders to move further:
  • Vertex Shaders
  • and Pixel Shaders
The third one, Geometry Shaders, are useless for us, we don't need them except you want to have displacement.
You have probably already heard of the pixel shaders (behind the game package for example, where the system requirements are). But I'm sure that you don't know what a pixel shader exactly is. Go on, and you'll find the explanation. Only those who go on will find, me says. And me says much. Or write.

Vertex Shaders
The vertex shaders are responsible for all the lighting stuff and more. Means it calculates the lighting and the position within the world, or the screen. It also calculates (a faked variant of) the light directly being directly reflected into the eye/screen. You can see that effect for example on non-rough metal pieces, or glass. The lights are being reflected directly into you eye by it. The same thing makes the vertex shader. Calculating wheter the light can be reflected in your eye depending on position of the vertex (imagine it as the pixely object in the monitor when you're playing a game for now, it's only partially true), it's rotation (where does it face) and it's roughness. Rougher things do not reflect the light, they diffuse it, like the wall over there (look around you, there should be a wall), or paper. They still reflect, but not enough to be highlighted in contrast to the areas around it. See the figure below.













 And here's a render of the holy 3D software 3dsmax, showing a diffuse sphere.















Pixel Shader
Pixel Shaders do every step after lighting until output to screen. They put all the textures on the 3D object and possibly modify them. They merge the lighting data with the textures and calculate additional data like bumps. I'll explain the bump technique in another part. The picture below shows the same render from above, but they all are bumped now.














I only explained roughly the functions of shader yet. To fully understand what a shader does, we need to know what a 3D model is, how they are built up, the different matrices, textures, implementation of the shaders from the game etc. I will write about them in the next parts of this tutorial series. A little picture to end this part of the tutorial.