DirectX Shader Effects

So I got distracted by vertex and pixel shaders, and started playing with Microsoft’s HLSL. MS’s Effects framework turned out to be really handy, once I got my head around what the hell it was actually supposed to do. It manages your 3D card’s state for you – no more fiddling with FVFs, blending, or vertex shaders directly – instead, you just specify a pass, render an object, specify another pass, and so on.

I’ve had a couple of requests for code – I’m busy cleaning up the shader files, so you should see them soon. If anyone wants the C# code, email me. I’ll hopefully put it up eventually, but I’d like to clean it up first…

Distortion Shader

Distortion Shader Here you can see my HLSL playground. The white frame can be dragged around and resized – it gives you a before-and-after effect of how the shader you’re working on compares to the default rendering. The head morphs randomly between different expressions.

This is a pretty trivial example – the surface is being offset by a sine wave moving through the head. This is pretty badly optimized – I have a few sines & cosines going on in there for each vertex every frame, which are extremely expensive in HLSL. I probably ought to be using a lower precision sin/cos. Code


Glow Shader

Glow Shader This took me all weekend to get working, and I’m still not very happy with it. The glow effect is rendered to a texture, blurred with pixel shaders, then overlaid on top of the original rendering.

It’s not very well behaved – depending the light conditions, in can easily get totally overblown and swamp the image with white, but can just as easily get so faint as to be invisible. Code


Natural Light Shader

Natural Light Shader This is one of my favorites – a 30 second hack produced with blatant disregard to the nice mathematical lighting models that so many clever people have put so much work into :)

It’s my attempt at natural light rendering – the sort of soft, global lighting conditions you get when outside in diffuse light. A glow is overlaid to soften the image – you can’t even see it, but it’s noticable when it’s missing…


Watercolor Shader

Watercolor Shader Alright – now we’re getting somewhere. These sort of sketchy effects are basically impossible to do on regular hardware, which is where vertex/pixel shaders come in.

Edges are detected by comparing the viewing direction with the surface normal, which we use to produce a texture coordinate. Custom-made mipmapped textures are used to control line width & give that rough handdrawn appearance to the lines.

I’m still working on the ‘watercolor’ shading – at the moment it’s just a blur effect on the original image.


Whole new world

Quake3 Map Viewer So this is all well and good, but there’s only so much you can do with a single model. I started to look into means of loading entire scenes, and eventually came to the conclusion I’d have to come up with some sort of map file format and scene management. I could do my own format, but then I’d need to write map tools, come up with maps, and so on. So I hijacked the Quake3 format, which is reasonably well described here. Currently, my engine handles loading everything, calculating visibility sets, and curved surfaces. It doesn’t handle some of Quake3’s more complex texture effects, but I’m planning on writing my own shader tools anyway. That’s next on the list.

· 2004-10-19 ·

  1. Wow. I’ve never seen that watercolor effect before… I’d love to see it in motion. Too bad my graphics card sucks too much to use of pixel shaders anyway. Phil    Oct 20, 01:04 AM    #
  2. Yeah, the watercolor effect looks pretty sweet in motion. Maybe I’ll try and get a video of it up over christmas. Jonathan del Strother    Dec 21, 04:14 AM    #
  3. I love the watercolor shader that you have created. I am interested in knowing how to use it in the unreal engine. I have no clue of programing and I would appreciate it if someone could help me out. — martian    Jan 3, 07:41 PM    #
  4. diretc x — stoy    Mar 19, 10:55 AM    #
  5. Does somebody of you know, how I can write a shader(vertex + pixel) with bump mapping and reflection, maybe including shadow lighting? I have seen something at Microsoooofts “shadermark v2.0”! — Jub Hekot    Apr 29, 02:46 PM    #
  6. For the glow shader do a stretch rect it is a very fast operation and does not require another render. Just a tip. None    May 11, 01:19 PM    #
  7. I’d like DirectX Shader Effects C# code. I’m trying to get someplace with this on my own and just need to see a decent sample. — Troy    May 24, 02:56 PM    #
  8. It’s good to see somebody makin’ good stuff man.. I’m from Argentina.. in this place, nobody do anithing good, anithing at all..
    These shaders are very impressive, you’re going for the good path..
    You’re makin’ some 3d engine or something? Well, i’m keep myself goin’, greetings! bye! Pablo Manuel Ordoñez    Jun 7, 10:43 AM    #
  9. hello my friend
    please send me your sorce code of http://www.steelskies.com/article/4/directx-shader-effects

    thanx — mahdi    Sep 14, 06:47 PM    #
  10. Hi!
    I like your works so much.
    I am beginner on shader programming. can I ask you to send me your Source codes? — Ehsan    Sep 29, 08:30 PM    #
  11. I am beginner on shader programming too. and can I ask you to send me your Source codes? — luo    Oct 9, 07:35 AM    #
  12. Hey, I’m a programmer, but I’m new to game programming. Do you have any good references for researching? (i’m kind of partial to DirectX)

    Anythings helpful, and thanks

    — Terry    Feb 1, 03:23 PM    #
  13. Hi!
    I’d like the Shader Effects C# code, I’m trying to learn and just need to see a decent sample.
    Thanks in advance and keep the good work ;)

    — Hastur    Mar 19, 06:14 AM    #
  14. Hi Friend

    You have create such an amazing effects with DirectX Shader.I am also working in HLSL but as i am a beginner and I find great difficulty in understanding and everything working fine.Can u plz send me the C# code.I really really need this.Plz help me.I have a project pending.

    thanks and regards.

    — syed baber    Mar 21, 12:19 PM    #
  15. hi, your C# code will be very useful for me :) :)
    mostly for fx file manager :)
    could you send me please….

    is there some improvment to applies shader for image base post processing or post process video ?
    best regards :)

    — habs    May 2, 01:31 PM    #
  16. Did not see this page until after i had emailed you… excellent feature…

    — michael Joseph Graves    May 21, 02:13 PM    #
  17. Hi

    Can you send me the code please ? — Jasper    Jul 9, 06:40 AM    #

Commenting is closed for this article.