Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shader tutorial
06-12-2012, 05:00 PM,
Post: #1
Shader tutorial
First, you MUST download the latest patch for this to work(12-27 or newer). Attempting to do this otherwise will result in either the new shaders not working, or worse yet, crashing the game.

Just what are shaders? Shaders are scripts that allow you to overlay textures and skins with other textures, effects, and even replace things. Quake III was the first game to make extensive use of them, and to this day, it was the game that probably used them the most. Shaders can give a game a great deal of visual appeal.

With the new patch, you will notice that nearly every item model now has a shader that makes it a little prettier, more dynamic, and more visible. Two player models, the Brainlet and Martian Enforcer also have shaders. You'll also notice a slight difference in the way glass is drawn, as now the reflection moves on the glass as your view angle does, making it more realistic.

You'll also probably notice a slight drop in framerate now when shaders are on. It's not much, but with all eyecandy, there is always a cost. These shaders that I have included in the patch are designed with efficiency in mind, and I spent many hours tweaking them to get the best performance.

The shader script code was originally built by Mr G, an excellent programmer at Quakesrc.org, and I integrated the code in about a year ago, but was only able to get it working for map textures, and only to get it hardcoded. I've been playing with it ever since then to get it working in CRX, and now have it so that it works for both textures and models, and the hardcoded things have been replaced by the script language. I found a few bugs along the way, and now it appears that the code is very stable. However, there are still a few things to work out in the future.

So on to making shaders for your custom textures and maps.

The basics:

Scripts are located in the folder data1/scripts. Every shader in this folder gets loaded for each map, and these shaders include both models, and some textures. These are called global shaders. If you wish to create a new texture, and you want a shader on it no matter what map you are in, you can make a file called myshaders.rscript and place it there. If you want a shader to appear only in a certain map, you can create a folder in data1/rscripts called "maps", and place it in there and call it mapname.rscript(mapname being the name of your map). This will only be loaded when that map is loaded, and will be flushed out on the next map change.

Shader graphic effects are located in the folder data1/gfx. There are already a number of them you can choose from, and you can place new ones here, or even to make it simpler for you to package them up, you can put your new ones in a folder such as data1/gfx/myeffects.

Now, onto writing a script!

Below is a sample script, for the red martian enforcer skin:
Code:
players/martianenforcer/red
{
                {
                               map players/martianenforcer/red_lite.tga
                }
               {
                                map gfx/martianfx.tga
                                blendFunc GL_ONE GL_ONE_MINUS_SRC_ALPHA
                                scale static 1 static 1
                                scroll static 0 static 0
                                alphashift 5 0.05 0.7
                                nolightmap
               }
}
There are two sections enclosed within the main shader with parens, called "stages". The first stage, we draw the base skin. This is neccesary only for models, not map textures. In the second stage, we draw our effect, which is glowing eyes, backpack and lights. BlendFunc is set to allow "blending of the texture". I don't recommend using other blendfuncs than the one I've shown, as I've seen some weird crap mixing them. Scale(x,y) and Scroll(x,y) are shown there, and those are the default settings. "Static" can be replaced with "sine" or "cosine" for different effects. Scale changes the size of the image, and scroll moves it around on the model. In our purpose here, we don't want the texture moving or changing size. Next is Alphashift. We want to fade the alpha in and out, to create a throbbing effect. 5 is the speed of the effect, and the other numbers are the min and max values. Last we have "nolightmap". These means our effect texture will show up even in dark areas, creating a glowing effect.

Now lets do another script, this for a simple texture:
Code:
textures/arena6/rimlight
{
    {
        map gfx/flash.tga
        blendFunc GL_ONE GL_ONE_MINUS_SRC_ALPHA
        scale static 4 static 1
        scroll static 10 static 0
        alphashift 20 0.05 0.5
    }
    
}
This places an effect on the texture "rimlight, used in dm-obsidian, the pulsing blue neon lights. Here we simply need one stage, as the base texture is automatically drawn with map textures.

Now onto a more complex shader, the armor:
Code:
models/items/armor/jacket/skin
{
    {
        map gfx/armorbase.tga
    }
    {
        map gfx/greenlightning.tga
        blendFunc GL_ONE GL_ONE_MINUS_SRC_ALPHA
        scroll static 0 static 3
        alphashift 5 0.1 0.7
        nolightmap
    }
    {
        map gfx/chrome.tga
        blendFunc GL_ONE GL_ONE_MINUS_SRC_ALPHA
        scale static .5 static .5
        alphashift 0 .2 .2
        envmap
    }
    {
        map gfx/armormask.tga
        alphamask
    }
    
}
Here we have 4 stages. First is our base texture. For efficiency's sake, I've created a base texture that is very tiny, because it gets overlayed later on. This needs to be here, even though it's overlayed later, because if you don't, the model will appear transparent. Our next stage, we put a electric type effect that scrolls and pulses. In stage three, we place a shiny relective effect, using "envmap". In stage four, we place the final overlay, a texture that has been created from the original armor skin, and using an alphamask(both here in the code and in the mask texture itself) allows only a portion of the earlier stage effects to show through, ie, on the tube and the little holes and vents of the armor.

Last, let's talk about making shaders efficient:

First, you don't want to go overboard with shaders. It's easy to get carried away, but if you do so, you'll wind up with a map that is painfully slow.

Also, it is important to realize, when you are adding a shader to a texture, the renderer is now drawing multiple textures where only one used to be. So try not to add *too* many stages to one texture, and try not to make effects that are larger than 128x128 pixels.

So that is shaders! Enjoy and have fun tweaking things and creating new effects!
Reply
06-12-2012, 05:34 PM,
Post: #2
Conditionals in Rscripts.
Rscripts can now be conditionally controlled by cvars.

As a simple test case, here is an rscript which could be used to direct you back to your own team's base if you are in possession of the flag. This would require having the server set the appropriate cvars using stuffed commands.
Code:
textures/arena10/neonsign11
{
    { if ( $ rs_hasflag && ( $ rs_team == blue ) )
        map textures/arena10/neonsign11b
        scale static -1 static -1
        blendFunc GL_ONE GL_ZERO
    }
}
textures/arena10/neonsign11b
{
    { if ( $ rs_hasflag && ( $ rs_team == red ) )
        map textures/arena10/neonsign11
        scale static -1 static -1
        blendFunc GL_ONE GL_ZERO
    }
}
[Image: alienarena178.jpg]
[Image: alienarena179.jpg]
The most immediate use of this would be adding an "rs_highvis" cvar to reskin the map with brighter colored textures.
Reply
07-02-2012, 04:05 AM,
Post: #3
RE: Shader tutorial
the patches had helped me a lot...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)