Jump to content
  • Advertisement
Sign in to follow this  
Roadkill247

HLSL Camera Shake

This topic is 3864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to implement a bunch of shaders effects onto a scene, I've got a disco effect working with HSL and then also a gaussian blur, now I am trying to implement a camera shake, as if its been hit by something, but can't seem to find anything that would even give me a basis for one. Does anyone know of a tutorial or somewhere that would point me in the right direction ?

Share this post


Link to post
Share on other sites
Advertisement
I would suggest to shake camera in vertex shader (not in pixel shader) by tweaking view/projection matrix.

Paool

Share this post


Link to post
Share on other sites
In fact, you *have* to do it in the vertex shader unless you're doing this as a post-process. Pixel shaders can't modify where the pixel they're "creating" ends up on the render target, that's already been determined through rasterization. Slightly moving everything back and forth in view space or world space should do the trick, though. In this example fPeriod and fShakeAmount would be the parameters of the special effect, and fTime would be the time (duh):


float4 vPositionVS = mul( matWorldView, IN.vPositionOS );
vPositionVS.x += cos( ( fTime / fPeriod ) * 2 * 3.14159 ) * fShakeAmount;
OUT.vPositionCS = mul( matProjection, vPositionVS );


or alternately just alter the view matrix itself:


matView[3][0] += cos( ( fTime / fPeriod ) * 2 * 3.14159 ) * fShakeAmount;


As a post-process, you could do it like this in the pixel shader:


float2 vTexCoord = IN.vTexCoord;
vTexCoord.x += cos( ( fTime / fPeriod ) * 2 * 3.14159 ) * fShakeAmount;
float4 vSample = tex2D( textureSampler, vTexCoord );

Share this post


Link to post
Share on other sites
ok, got that working, was curious as well how to do it in the pixel shader as a post process, for future reference.

much appreciated with the help so far as well :)

Share this post


Link to post
Share on other sites
Of course you should keep in mind that the post-process method is somewhat less "accurate" by performing the shake in screen-space. Pixels in the distance should be shook less than those up close due to perspective projection. I suppose if you had access to a depth buffer you could do it more "correctly", but it's probably not worth it.

Also probably a natural thing to do with this technique would be to have the fMoveAmount parameter decay over time, by doing something like this (in your app code, not in HLSL):


fMoveAmount *= 0.9f;

Share this post


Link to post
Share on other sites
ok, managed to implement the post processing one without any hiccups, only slight flaw is the image moves to the top left, and then moves back, is there anyway to get it to stay still but still shake ?

edit: Nevermind my mistake, just my process in which to make it diminishing isn't workng

[Edited by - Roadkill247 on March 18, 2008 11:59:24 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!