Archive for the ‘HLSL’ Category

Sunday, January 21st, 2007

Howdy all.

Kukyona at CodeAnxiety has posted another update on the Terrain Component for XNA. It’s looking very cool indeed.  Check it out here

XNA Terrain component,  now with Shadow maps

p.s. I’m still working on Part 2 of my XNA Collision Detection tutorial.

Average Rating: 4.9 out of 5 based on 235 user reviews.

Friday, December 8th, 2006

Shawn Hargreaves seems to be blogging with a vengeance today.  Possibly going for some kind of record.  ;)

Anyway, among other things, he alerts us to another XNA Team member’s blog:  Eli’s Cornflower Blue blog opens with part 1 of an article explaining Alpha Blending, how it works, and it’s relationship to the Depth Buffer.

I’ve only skim read it so far, but it looks like some very good info.  Exactly what the doctor ordered actually, because when I was converting from XNA Beta 1 to 2 I had all sorts of problems with weird Alpha Blending symptoms.  All fixed now, but I never really understood the problem.

I’m looking forward to studying this one ASAP.

Average Rating: 5 out of 5 based on 180 user reviews.

Tuesday, October 31st, 2006

Once again, I’ve stumbled on some cool HLSL (High Level Shader Language), but haven’t had time to seriously read it!!!

Rather than bookmarking it selfishly, I’ll continue my trend of blogging about it.  Fire and forget blogging – ROCK ON!!!

So here we go…

“XNA – Parallax Mapping” tutorial

Over at Ziggyware I found an XNA based tutorial for a Parallax Mapping Shader.  Advanced stuff by my reckoning!

Check out the “XNA – Parallax Mapping” tutorial here

“Human Skin shader” sample

I can’t remember how, but I also stumbled on what looks like some seriously hard-core shader action at “J.I. Styles | Online portfolio”.  It’s not XNA specific, but the HLSL (*.fx) file is there to download.  The sample is an awesome Human Skin shader!  Impressive!  Out of my league, but impressive!

Check out ”Adventures in HLSL pixel shaders” here

Average Rating: 4.8 out of 5 based on 281 user reviews.

Tuesday, October 24th, 2006


I promised I’d start sharing some snippets of source, so here’s the HLSL Shader code (an *.fx file) I use for rendering my Aircraft.  (as requested by Omegaman)

Those familiar with the XNA “Spacewars” sample will see an uncanny the resemblance to the sample’s Ship.fx file.  In fact it’s probably barely changed. Perhaps some trimming here and there, but I really can’t remember.

It’s pretty scary HLSL for a beginner like me.  I’d love to be able to explain it to folk, but frankly I barely understand it myself!  ;)

(next time hopefully I’ll be posting some HLSL I can at least reasonably explain)

I will try and describe its characteristics though:

  • It lights & shades the model with a provided Ambient color, a Directional light and a Point light.  Directional light is a color of light cast in a particular direction, from no particular point in space.  In other words, its brightness is the same no matter where the model is in space.  Point light however is a color radiating out from a point in space, so the closer the model is to it, the stronger the effect.
  • SkinTexture is the painted skin of the model…duh!
  • ReflectionTexture is special kind of texture – a TextureCube.  I haven’t really experimented with it but I believe the texture passed would be a mock up of the typical world around the model, and it would be rendered as ‘good enough’ reflections on the model.  I say ‘good enough’ because you’d probably have a static TextureCube of a typical sky, clouds and ground, rather than dynamically create one with the exact rendered background and moving clouds.  To do so, might be possible, but I imagine a performance killer if you had to do it on every frame.
  • I think the NormalMap stuff might be redundant actually.
//Input variables
float4x4 world;
float4x4 inverseWorld;
float4x4 worldViewProjection;
float4 viewPosition;
float4 Ambient;
float4 DirectionalDirection;
float4 DirectionalColor;
float4 PointPosition;
float4 PointColor;
float PointFactor;
float4 Material;
texture SkinTexture;
sampler Skin = sampler_state
Texture = (SkinTexture);
texture NormalMapTexture;
sampler NormalMap = sampler_state
Texture = (NormalMapTexture);
texture ReflectionTexture;
sampler Reflection = sampler_state
Texture = (ReflectionTexture);
struct VS_INPUT
    float4 ObjectPos: POSITION;
    float2 Tex : TEXCOORD0;
    float3 ObjectNormal : NORMAL;
struct VS_OUTPUT
float4 ScreenPos: POSITION;
float2 Tex: TEXCOORD0;
float3 WorldNormal: TEXCOORD1;
float4 PointDirection: TEXCOORD2;
    //Move to screen space
    Out.ScreenPos = mul(In.ObjectPos, worldViewProjection);
    float4 WorldPos = mul(In.ObjectPos, world);
    float4 WorldNormal = mul(In.ObjectNormal, world);
    //Pass on texture coordinates and normal
    Out.Tex = In.Tex;
    Out.WorldNormal = WorldNormal;
    //Direction of point light to this vertex
    float4 lightDir;
    lightDir= PointPosition - WorldPos;
    float dist = length(lightDir);
    lightDir = lightDir/dist;
    //Store attenuation in w
    lightDir.w = saturate(1/(PointFactor * dist));
    Out.PointDirection = lightDir;
return Out;
float4 DirectionalLight(float3 WorldNormal)
    return DirectionalColor * saturate(dot(WorldNormal, normalize(DirectionalDirection)));
float4 PointLight(float3 Normal, float4 PointDirection)
    return PointColor * saturate(PointDirection.w * dot(Normal, normalize(;
float4 AircraftPS( float2 tex: TEXCOORD0, float3 WorldNormal : TEXCOORD1, float4 PointDirection : TEXCOORD2) : COLOR
    WorldNormal = normalize(WorldNormal);
    float4 Directional = DirectionalLight(WorldNormal);
    float4 Point = PointLight(WorldNormal, PointDirection);
    //Specular map is in alpha of diffusemap and material
    float MattFactor = Material.a * tex2D(Skin, tex).a;
    //Add it all up
    float4 retval;
    retval= saturate(
                Material *
                (                                        //Remove alpha channel from texture
                    ((Ambient + Directional + Point) * float4(tex2D(Skin, tex).rgb, 1)) +
                    (1-MattFactor) * texCUBE(Reflection, WorldNormal)
    return retval;
// Technique Section for Aircraft Effects
technique Aircraft
pass Single_Pass
        ZENABLE = TRUE;
        CULLMODE = CW;
        VertexShader = compile vs_1_1 AircraftVS();
        PixelShader = compile ps_2_0 AircraftPS();

Average Rating: 4.9 out of 5 based on 297 user reviews.

Wednesday, October 11th, 2006

I’ve been asked in a blog comment whether I’m giving out the source.

I’m afraid,   no, sorry.   :(

Y’see, this is kind of a personal project and learning exercise for me. I first coded this exact game idea when I was 13 (that’s 23 years ago) in BASIC, but performance got rotten on those ancient “Home Computers”. And it certainly wasn’t 3D.

Now I’m much more capable and having another go…for fun.    ;)

I’m am certainly happy to share snippets and attempt to answer questions though!!!

Any special requests?

Average Rating: 4.4 out of 5 based on 286 user reviews.

Friday, October 6th, 2006

Many people discover my blog while searching for XNA & HLSL Tutorials.

To be honest, I’m probably as much a beginner as most of you.  I do know your pain though – it’s damn hard to find an HLSL tutorial pitched at a beginner’s level.  That is why I blogged when I finally discovered some promising resources.

I’m planning to update this blog occasionally with whatever HLSL learning’s I uncover.  They will likely fall short of what you’d call a tutorial.  More likely they’ll be sporadic little journals sharing my HLSL findings as my own understanding grows.

I’ve also been in contact with the operator of the Riemers site. I’ve linked to his HLSL tutorial before and no doubt a few of you are finding it an excellent resource.

He plans to update or include more beginner’s level HLSL tutorials and has welcomed any feedback or ideas from me.  

I’ll be giving him my own wish list at some stage.

If you have any particular ideas or wishes, please leave a comment here, and I’ll be sure to pass them his way.

So what would you like to see in a beginner’s level HLSL tutorial?  Go ahead and comment!

Average Rating: 4.9 out of 5 based on 257 user reviews.

Friday, September 29th, 2006

Just a quick update tonight.

Things are progressing very well. I’m nearly back to the point I was at before rewriting for XNA.

Just the shooting to add and some minor glitches to fix.

However, along the way, I couldn’t help but throw in something new. So here’s a quick peak.

Thanks to the joys of Refactoring, XNA, HLSL (shaders) I now have I nice wee system for changing the look of the game for different environments. Click for a comparison screenshot showing the Spitty in bright daylight vs moody dusk. In particular, check out the way the lighting of the model itself is different.


Also note the glossy Cockpit. Difficult to appreciate with a still image, but in game you can clearly see it’s of a different material to the rest of the plane. Not transparent, but I don’t think it really needs to be for this game.

I’ve designed things such that I can switch between the two environments with one line of code!!!

Average Rating: 4.8 out of 5 based on 198 user reviews.

Monday, September 25th, 2006

Just a quick update on the Game dev progress.

Rewriting for XNA has been keeping me busy. Up till a couple of nights ago, it had felt very much like back to square one.

I’ve finally got past the initial slowness, and am starting to make forward progress again.


  • The seriously stripped down XNA sample code is starting to feel like my own at last. I actually know where to look for stuff now without needing to search unfamiliar code.
  • I’ve integrated my Spitfire model.
  • So far, I’ve got by with the sample HLSL shader. By looking at the sample code, I’ve also worked out how to make the Cockpit look glassy. Not exactly transparent, but more reflective and probably good enough.
  • I’ve converted over my code for the movement, so the Spitty now flys and turns as it did before.

What’s next?

  • Incorporate or rewrite my Skybox code for the background.
  • Ditto for the CloudManager.
  • Then it’ll be time to incorporate some SHOOTING to keep the punters happy.

That’s about all I can think of for now.

Oh, and I have this week off work, so between holiday activities I’m hoping to get some good progress done!

Average Rating: 5 out of 5 based on 268 user reviews.

Monday, September 18th, 2006

I haven’t even studied the last ones yet, but this one looks like a good starter for Pixel Shaders specifically.

Average Rating: 4.6 out of 5 based on 300 user reviews.

Friday, September 15th, 2006

I mentioned earlier that the new XNA framework requires you to do things with Shaders, where before you didn’t have to.

This is all good, but yet another new thing to learn.

So I’ve been googling quite a lot lately. I can’t believe what a struggle it has been to find a decent beginner level tutorial out there!!!

Last night, I think I may have finally stumbled on a couple. I’ll look into them in the weekend, but they did at least start like a tutorial for beginners like me.

They are not XNA specific, but I don’t think that should matter. HLSL is not new, and it is a DirectX language – not C#.

One of them appears to be an excerpt from a book, and I don’t know how old or how relevant it is to DirectX 9’s HLSL.

Here are the links, on the off chance they prove useful…

There is also this one, but I find it goes too deep too quickly…

Average Rating: 5 out of 5 based on 284 user reviews.