HowTo:IntroToShaders

From VsWiki
Revision as of 06:22, 3 September 2007 by chuck_starchaser (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction to Shaders, and what they mean to the modelling and texturing pipeline

I'm going to expand on this and write a whole bunch of wiki how-to's; this is a first overview. As of this writing, the default shaders aren't finalized yet, and the changes that are coming affect the way the textures need to be packed.

However, you CAN start working on bumpmaps and normalmaps (and shininess maps) right now, if you want. There will be off-line tools provided to convert texture packs from the current set, namely...

* Diffuse texture
* Specular texture
* Glow texture
* Damage texture,  b]plus the new
* Normal map
* Shininess map (the current shader expecting it in the alpha channel of the specular texture)

...to the most likely new packing arrangement of:

* Texture 0: Diffuse texture (plus normalmap dU in alpha)
* Texture 1: Specular texture (plus shininess in alpha)
* Texture 2: Glow texture (plus ambient occlusion in alpha)
* Texture 3: Damage texture (plus normalmap dV in alpha)

In fact, there may be several such tools, for variations in types and numbers of input textures. And I have a prototype of one such tool, the "nodulizer": nodulizer.png

The nodulizer is really a http://www.blender.org/]Blender3D .blend file. You throw it into a folder where you want to convert a set of textures, rename the textures to what the nodulizer expects, or else edit the input nodes for them (on the left), adjust the scaling factor with that input node at the bottom left corner, and press the Use Nodes button in the menu bar below. The scaling factor is there in case you have large (2k by 2k or 4k x 4k originals, and want to produce 1k outputs; otherwise make the scaling factor 1.0. Needless to say, you need to have Blender installed, then you can just double- -click on nodulizer.blend.

You can download the nodulizer beta here: http://deeplayer.com/dan_w/nodulizer/beta/nodulizer.7z]nodulizer.7z Just don't use it yet; the shader that uses this packing is still in the works; but you can play with it, see what it does.

There may also be a command line tool to do the same thing; not decided yet, but the advantage of using Blender's "nodes" and "noodles" is that the tool can be easily customized, specialized, tweaked, expanded... So, IF there may be a command line version, it will probably only be provided for a default repacking, for those who can't use Blender due to their religion :D


But, getting back to basics, the question most often asked: what's the difference between bump- and normalmaps?


Bump map

A bump map is a gray scale image where the brightness represents height above the surface, and darkness represents depth. 50% gray means at surface level. The problem with bumpmaps is they take a lot of processing in the gpu (videocard) to be really good to use; and that they are quintessentially non-mipmap-able. So normalmaps soon took over.


Normalmaps:

Normal maps encode simply the angle of the surface. At each point on the surface, if you think of a normal vector to it, how much it is moved left to right from the straight UP position is called the dU. How much it is moved forwards or back from straight up is the dV. And how high it is assuming length of one, after being tilted, is the z. In a tangent space normal map, normals are specified relateive to the surface (as opposed to older normal maps where x y and z were specified in global coords). So, since, on average, a surface is flat, z is usually close to 1. z goes in the blue channel, so normal maps, when you open them in Gimp look very blue. Red gets the dU, but transposed, as the dU could be positive or negative. 50% red means zero. Same with green and dV. So where a surface is flat, the normal map color is red = 0.5, green = 0.5, blue = 1.0 And you produce them with http://www.xnormal.net/ xNormal, and/or with http://nifelheim.dyndns.org/%7ecocidius/normalmap/ the Gimp plugin from nvidia. Google it. The plugin takes a grayscale bumpmap and computes a normal map. xNormal takes a fine mesh and a coarse mesh, and makes a normal map that, when applied to the coarse mesh, makes it appear like the fine mesh. Dual Joe nowadays swears by http://epsylon.rptd.ch/denormgen.php]denormgen; --says it's easier to use than xnormal, runs on Linux, and can stack normalmaps. I haven't tried it yet.


What do they look like?

Bumpmap:

bump.jpg

Normal map:

final_norm.jpg

The above normal map is actually a combination of subtle normal corrections computed using xNormal, from a 100k+ polygon mesh, and the result of making a normal map out of the bumpmap above, using Gimp's normal map plugin from Nvidia. That plugin has a choice in the Conversion drop box, called "Normalize Only". Very useful when you're blending normal maps, because normal maps get corrupted by blending ops, and you need to renormalize them, afterwards. Also, if you want to increase the strength of a normal map, you can dim down the blue channel in Gimp, then renormalize.



Shininess Map

"Shininess" is a bad name for a good to model, fundamental property of materials, which can make a glass cockpit look glossy, and make metal look "shiny" without looking "glossy". Here's shininess at work in my current beta shader:

shinycockpit.jpg

shot.jpg

Hard to appreciate with a still picture... The metal does reflect the environment, but the reflections are so blurred you can barely notice them. Don't confuse "shininess" with "specular color". Totally different animals.

Shininess describes how "UN-blurred" reflections are. A high shininess (200 or so) reflects images sharp and clear. With a shininess of 100 you begin to notice blurriness. With a shininess of 20, the reflected environment is so blurred it looks almost like half way between specularity or plain diffuse color.

Unlike specular color, shininess is a monchrome (gray scale) parameter. For the ship in the shader screenshot above, this is what the specular texture looks like:

specular512.jpg

And this is what the shininess texture looks like:

shininess512.jpg

Notice that specular color of the cockpit is pinkish or purplish. Yes, it looks green, but that's because there's a bit of green light I put in the glow map, to make it look like someone lives there; but the specular reflections are actually tinted purple. But anyhow, what I wanted to call your attention to is that the specular strength of the cockpit

is not much greater than that of the metal, and in fact it should be less, or rather, the metal should be brighter in specular.

But what makes the cockpit look "glossy" is the white color in the shininess map, versus grey for the metal.

Texture packing

As of this writing, it goes like I wrote at the beginning of the file:

* Texture 0: Diffuse texture (plus normalmap dU in alpha)
* Texture 1: Specular texture (plus shininess in alpha)
* Texture 2: Glow texture (plus ambient occlusion in alpha)
* Texture 3: Damage texture (plus normalmap dV in alpha)

BTW, a new release of the vs engine may automatically initialize the ambient and specular color in the system xml's (take them by averaging the color of their background textures), and this subject is not complete without discussing radiosity baking and the glow map; but this is too experimental at the moment. Suffice it to say that if I succeed, we'll never use gl ambient light, but will use the glow map to emulate ambient illumination, with self-occlusion baked in, and modulated by the color and intensity of the background.

And I also left out the "detail textures". No need to complicate this post with it too much, though. Detail textures are basically tiling textures, typically 256x256, that map multiple times over the same UV map as the regular textures do, "adding detail" as you get closer to an object (ship or station). There will be two:

* Texture 4: Diffuse detail (color in rgb, detail normal map dU in alpha)
* Texture 5: Spec detail (luma in red and blue, shininess in green, normal dV in alpha)

The above is tentative and subject to change. The reason you don't need to worry about it is that there will be default detail textures that will apply generally to all ships and stations and planets, unless you supply your own detail textures and specify them in the bfxm. The default ones will simply be some kind of perlin noise, barely noticeable, just enough to hide pixelation artifacts; no more.