Showing posts with label Instancing. Show all posts
Showing posts with label Instancing. Show all posts

Wednesday, 11 July 2012

Volumetric Clouds in XNA 4.0

As mentioned in the last post and the comments before that, I got asked to port my old XNA 3.x volumetric clouds over to XNA 4.0, and that’s just what I have done, but with a few changes.
The first is I am using instancing, if you don’t know what instancing is, it’s a way we can pass one set of geometry to the graphic card, but have it render lots and lots of instances of that geometry. So, in the case of our clouds, we render wisps' that are render on a quad or billboard, this billboard is just verts, so as far as the vertex buffer goes, it just holds these 4 verts, on another channel we send the graphics card a list of transforms, these transforms (or world matrix’s) represent and instance to be rendered, so, if we send ten transforms, then 10 billboards will be rendered.
You can find my archived post on the original code base here.
So, that’s quite a change from the old implementation, in the earlier version I had Point Sprites (never a good idea) and billboards, and I would chain them up into a single draw call, so the vertex buffer would be the number of wisps multiplied by four, one for each vert in the quad. It gave a good result, and looked like this
You can see in the clip, it’s a bit laggy, but to be fair it was running on a very old and crappy laptop.
I also changed the shader a little, as I had to anyway to get the instancing to work, but also to get a better fade as the camera got close to the cloud, before as you approached the cloud, it would fade out, but as I was doing this in the vertex shader, if it was a big billboard you would see the billboard rotate and  then start to fade. I am still doing this check in the vertex shader, but I am also now doing it in the pixel shader, it now fades the cloud out much better and reduces the visibility of the twisting billboard artifact on large billboards.
This is what it now looks like
So, here is the code for the clouds in XNA 4.0
Next post ill expand on instancing a bit more and show how I used it to render 1 million particles :)
REMEMBER: DO NOT COMERCIALY USE THE CLOUD TEXTURE IN THE ZIP AS IT IS PROPERTY OF MS!
Just so you know and don’t get your self stung, I have put this comment in the code where ever it is referenced.
            //////////////////////////////////////////////////////////////////
            // REMEMBER THE CloudSpriteSheetOrg texture is property of MS   //
            // Do not comercialy use this texture!                          //
            //////////////////////////////////////////////////////////////////