Tuesday, June 12, 2012

Taking my stuff and going....into UDK!


Hey, so I know I was gonna put up a video of the game I made but I don't know if I'm really proud of what it is right now to put it up (I was proud before I added some of the things to it that were required for my exam, but those things destroyed a lot of my work...). I am proud of my game but it's not what it was, I previously had normal mapping in it but for some reason, adding ShadowMapping has really messed it all up. It not only killed my Normal Mapping, like utterly and completely, but it also lit up everything on my models a ton AND didn't actually add shadows (that  I ever saw, but I think I do know why).

So I think I'm going to actually re do my models (make the rock/bricks and textures my self) and then start exporting them to UDK. I'm currently learning the syntax's for UScript (since UDK itself can't directly read c++ file without messing around) and since I just found out that Scaleform is in UDK as well, I'm learning Flash/actionscript to make my HUDs for this game now. So I'll start using trying to post some updates as they come (see how much work I can get done in the summer with working and such)

I'm gonna try to start porting my C++ code into UScript (once I figure out how to write UScript in UDK itself ...or wherever I have to put it...) and then hopefully I can get back up to speed pretty fast. Keep your eyes peeled for it cause I really hope to keep the motivation up to make this game.


Saturday, June 9, 2012

Explosion Shader V2.


So, from my last post with my "explosion" shader, I have made it a bit more controllable and "realistic". 

Here's a video of 3 explosions with varying "weights" of the object.
The shader isn't really that complex in it's nature and has some easily tweaked values to make it so you can determine a few things about the explosion.

As far as how it's written, I'm going to assume that if you were searching for this kind of shader that you have either been taught or know the basic pipline for Shaders (Vertex -> Optional Geometry -> Pixel) and would also like to say that I'm by no means an expert in shader writing either so if you happen to see something I could improve, feel free to leave a comment, it's always appreciated.  On to the code Snippets!

K, so this shader uses a few things that really help to determine the explosion.
1 - float SpeedVariation; This will help to create differences in how much speed the exploded material will gain at explode time with 1 being a very small explosion and 5 being the kind that I used in the video.

2 - float MaterialWeight; This is what determines how fast your exploded pieces will fall after they have been propelled out.

3 - float ExplosionViolence; This is an extra parameter to help make the explosion either big or small. Smaller the EV, the smaller the explosion. (It also gives some weird explosions if you use numbers closer to 0 like 1 or 2. Greater than 3 is usually a safe bet for a decent looking explosion.

I have 3 random float2 's that I use to get some more randomness in the texture coord look up and my speed calculation as well. rand1 = 0.09, 0.05 rand2 = 0.075, 0.05 rand3 = 0.09, 0.04

Those variables coupled with a Time variable help to make it propel and fall with some realism.
//Sampler States
I use 2 sampler states for this as well, one that is used for the diffuse and the other for sampling the gradient/noise.

SamplerState samGradient
 Address U and V are both Clamp
 and the diffuse one is just AddressU and V with Wrap;

There are also 3 texture2D's that I use for this which are as follows.

1 - Diffuse - just whatever you want your object to have for it's regular texture

2- Noise - This you put in a noise texture to get some randomness for the distance the exploded pieces will travel

3 - Gradient  - I use a gradient of 1 X 256 that is white to black.

We sample the Noise and Gradient in the Geometry shader to help give just a touch more randomness.

I use triangle as my Geometry input as we only need the 3 verts per tri and the [maxvertexcount] is 3 as well since we only do the calculations for movement per tri.

//Geometry Shader

So the way I did this shader was to make it so the pieces fly in a somewhat up and out movement (Outwards explosion from the center) the way I do this is by grabbing all the normals of the tri and adding them then taking the average (divide by 3) and saving this in a float3.

I then take the average of the texture coordinates and use them to do a SampleLevel on the noise and gradient textures.

float Noise = 2.0f * m_TextureNoise.SampleLevel(samGradient, (aveTextureCoord +  m_random),0).r - 1.0f;

and same but a different randomvalue for the gradient one.
One of my math teachers loved the word Lerp and Slerp, now I never really bothered to use them that often but I figured this would be a good time to use a lerp go get a bit of randomness.

for my "speed" calculation I do a float3 (0, lerp( Noise, Gradient, m_SpeedVariation), 0);

Gravity is the negative of the "MaterialWeight" * time (only in the Y direction obviously).

Now the first bit that I was trying to get the variations to work I didn't have this calculation, this came from a friend of mine Jay Heggerud that said if I wanted to get some more variation on the speeds/distances the pieces travel to maybe try making my initial speed based on the min speed I wanted + the speed variation * random value for differences. 

So this puts my vi calculation at something like this (to get a random minimum speed on the initial we use the Noise and put it like this.

float3 vInitial = float3(Noise * m_SpeedVar , Noise * m_SpeedVar , Noise * m_SpeedVar);

Now we're pretty much at the end of the shader. The only part left is to plug things into some equations from Frank Luna's Intro to DirectX book (where I also got the idea to make an explosion shader)

So it's a somewhat basic Physics equation we put into make the triangles travel and fall kinda realistically.

 0.5 t^2 a + t vi + p0;  so this more or less translates into 0.5f * accel * time squared + vinital * time + the inital position;

for the initial Position we use another formula to represent the explosion, also from Frank Luna

p(i) = p(i) + tn where i = each vertex 1,2,3

so if we shove that in there as well we get

 0.5 t^2 a + t vi +(p + tn);

Now I deviate from the equation in the name of randomness and threw some other calculations into it as well.

The full calculation for the position of one vertex  is

pos[0] = 0.5f * gravity * (m_Time * m_Time) + (vInitial * m_Time) + ((vertices[0].Position -vertices[2].Normal * m_Time)+(normals * (m_Time*(m_ExplosionViolence * sin(cos((m_SpeedVariation * speed )))))));

Output each vertex with it's respective normals and texture coords and bam, you're done really.

Hope this has possibly helped or at least been somewhat of an interesting read, I'm gonna go write my paper for this now haha.


Thursday, June 7, 2012

Explosion Geometry Shader


So for my exams I also have to make and write a paper on a Geometry Shader written in ShaderModel4.0 for HLSL. My original idea was to do a grass shader because...they seem kinda fun.  Well it didn't work out so well and I had technical problems that I still don't know what was causing them.

So, instead of grass rendering I decided to make things explode!  So I wrote this shader (with some help from my teacher to try and get some more velocity on the pieces when the explosion starts) in the last little bit and I think after playing with the equations a bit I finally have a decent start on this. I am still going to try and make the pieces have some more variance in speed but it's a work in progress.


Above, is the link to the video I took inside FXComposer2 where I wrote the shader. As I said, it's the first (good) iteration and I'm gonna try to tweak to be even a bit better than current but, that being said, there it is.

I'll also be posting the paper on it later (going to write it tonight/now-ish) and some snippets from the shader itself as there really isn't that much to go on when searching for Geometry shader help/tutorials (that I found atleast for DirectX)


Tuesday, June 5, 2012


So, as I think I've mentioned in the previous posts, I'm making a game for my exams that is "tentatively" called Dungeon Explorer. Yes, it's one of those dungeon games (if I had the time before the Exam I'd love to get enemies in and different weapons too but it's not gonna happen..) where you try to find loot and avoid dying. This one I decided to make "Procedurally Computer Generated".

For those that don't know what PCG is, in short it's basically the game makes itself. The only thing the player/user would have to do is tell it how big of a map you want to play in and how many rooms you want and the game will make a level based on those. Now that doesn't sound sooo fancy but the thing is, everytime you play, it will be different.

Right now I have the level generation working quite well, with A* (A-Star) pathing between the rooms as well as "treasure" tiles" and I just added Stairs Up/Down (and am working on the trigger so it will make a new level if you go Up or reload the last one if you go Down...) I still need to add locked doors/keys but that's hopefully something I'll have time for.

In the next few days I'll get a short video about it with me just running around in the level and showing new levels as well as Saving/Loading (which was kind of a pain in the butt to make.) Now that said, the movie will also be with the current textures which...aren't amazing but I wouldn't say are terrible...just don't laugh to hard at them haha ;).

Anyways, back to programming and Const Correctness Checking for the first presentation it's due for tomorrow.