HLSL Camera Shake

Started by
8 comments, last by Roadkill247 16 years, 1 month ago
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 ?
Advertisement
why not shake the camera instead of making it as a shader?
I could do that, but I want to see the possibilities of a camera shake being done in the pixel shader.
I would suggest to shake camera in vertex shader (not in pixel shader) by tweaking view/projection matrix.

Paool
ok, so to do it in the vertex shader, I would manipulate the projection matrices ?
yes, in vertex shader you can change view matrix (i.e. camera position)
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 );
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 :)
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;
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]

This topic is closed to new replies.

Advertisement