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
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
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
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
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
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.
Commenting is closed for this article.
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 #
please send me your sorce code of http://www.steelskies.com/article/4/directx-shader-effects
thanx — mahdi Sep 14, 06:47 PM #
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 #
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 #Hi!
— Hastur Mar 19, 06:14 AM #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 ;)
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 #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 ?
— habs May 2, 01:31 PM #best regards :)
Did not see this page until after i had emailed you… excellent feature…
— michael Joseph Graves May 21, 02:13 PM #Hi
Can you send me the code please ? — Jasper Jul 9, 06:40 AM #