Is it possible to calculate the mouse velocity?

  • luluco250
  • Topic Author
More
8 years 1 week ago #1 by luluco250 Is it possible to calculate the mouse velocity? was created by luluco250
It seems I can't make changes to global variables, is there another way I can store the last mouse position to calculate it's velocity?

Please Log in or Create an account to join the conversation.

  • crosire
More
8 years 1 week ago #2 by crosire Replied by crosire on topic Is it possible to calculate the mouse velocity?
You can render to textures and use them to store data. A simple 1x1 R32F texture to store a single float value for instance.
The following user(s) said Thank You: luluco250

Please Log in or Create an account to join the conversation.

  • luluco250
  • Topic Author
More
8 years 1 week ago #3 by luluco250 Replied by luluco250 on topic Is it possible to calculate the mouse velocity?
Thanks, I was thinking of that, will try and see if it works.

Please Log in or Create an account to join the conversation.

  • crosire
More
8 years 1 week ago #4 by crosire Replied by crosire on topic Is it possible to calculate the mouse velocity?
Also, to fetch data from textures faster and without the interpolation "tex2D" does, use ReShade's "tex2Dfetch" intrinsic.
The following user(s) said Thank You: luluco250, OtisInf

Please Log in or Create an account to join the conversation.

  • OtisInf
More
8 years 1 week ago #5 by OtisInf Replied by OtisInf on topic Is it possible to calculate the mouse velocity?
Oh, this is precisely what I was after too! I need to store the last mouse coordinates if a key is toggled, so I can use the mouse coordinates to read a color from the framebuffer and use the last coordinates known if a key is toggled again (so you can keep using the mouse coordinates set and move the mouse).

As I've never used this before, the value written to the texture is preserved across frames, I recon?

Please Log in or Create an account to join the conversation.

  • crosire
More
8 years 1 week ago #6 by crosire Replied by crosire on topic Is it possible to calculate the mouse velocity?
Until you write to the texture again, yes.

Please Log in or Create an account to join the conversation.

  • luluco250
  • Topic Author
More
8 years 1 week ago - 8 years 1 week ago #7 by luluco250 Replied by luluco250 on topic Is it possible to calculate the mouse velocity?
I'm trying to do a mouse velocity based motion blur, but it doesn't seem to be working.

Everything works fine, the actual blur code also works it's just the whole velocity thing that doesn't seem to work. Maybe this is related to FrameTime?
I tested the velocity algorithm in Unity with Time.deltaTime and it worked, no idea why it's not working here.

Warning: Spoiler!


EDIT: The filename is called Bloom because I was messing around with bloom algorithms before trying motion blur, hence why the technique name is Bloom_tech.
Last edit: 8 years 1 week ago by luluco250.

Please Log in or Create an account to join the conversation.

  • OtisInf
More
8 years 1 week ago #8 by OtisInf Replied by OtisInf on topic Is it possible to calculate the mouse velocity?
You have to use 2 mouse coord variables/textures, not one :) So you store the coords in frame 1. In frame 2 you read the old ones from frame 1 and the real mouse coords and calculate the diff. Then store the real ones in the variable texture for the next frame.

Your store pass is right before the render pass, so your render pass will use the same mouse coords and not the ones from the last frame. (so you have to ping/pong between the texture variables).
At least that's what I think it is ;) (but it's late, so I could be wrong ;))
The following user(s) said Thank You: luluco250

Please Log in or Create an account to join the conversation.

  • crosire
More
8 years 1 week ago - 8 years 1 week ago #9 by crosire Replied by crosire on topic Is it possible to calculate the mouse velocity?
You should vectorize your code. GPUs calculate stuff in vector registers, so they can do multiple computations per data at once (SIMD).

Example. Replace:
srcCoord = coord - float2(i * ReShade::PixelSize.x * scale.x, i * ReShade::PixelSize.y * scale.y);
with the equivalent, but faster (the x and y components are calculated at the same time now):
srcCoord = coord - i * ReShade::PixelSize * scale;

Also. tex2Dfetch takes the texture coordinate as integers, because it has no sampling (that's the whole point):
float2 lastPos = tex2Dfetch(_sPos, int2(0, 0));
Last edit: 8 years 1 week ago by crosire.
The following user(s) said Thank You: luluco250

Please Log in or Create an account to join the conversation.

  • luluco250
  • Topic Author
More
8 years 6 days ago #10 by luluco250 Replied by luluco250 on topic Is it possible to calculate the mouse velocity?
Thanks OtisInf, the order of passes did the trick. Also crosire, thanks for the tips, but I'm now getting "implicit truncation of vector type" errors, I take it this is because of the tex2Dfetch using int coords? If so, is it possible to bypass this warning or is there a proper way for this?

It looks pretty neat, I'm thinking of including some depth buffer code in it so that more distant objects get more blur, shame ME1 clears the depth buffer but it could look great on GTA San Andreas for example.

This is what it looks like

I think I'll look into vignetting the motion blur as well, could work well to simulate a more curvy blur.
The following user(s) said Thank You: crosire

Please Log in or Create an account to join the conversation.

  • kingeric1992
More
8 years 6 days ago - 8 years 6 days ago #11 by kingeric1992 Replied by kingeric1992 on topic Is it possible to calculate the mouse velocity?
You can also use vertex texture fetch to calculate velocity, then pass along to pixel shader, to eliminate the additional velocity texture.
struct v2f {
	float4 pos  : SV_Position;
	float2 uv   : TEXCOORD0;
	float2 mpos : TEXCOORD1;
};

v2f VS_MotionBlur(uint id : SV_VertexID) {
    v2f o;
    o.uv.x = (id == 2) ? 2.0 : 0.0;
    o.uv.y = (id == 1) ? 2.0 : 0.0;
    o.pos  = float4(o.uv * float2(2, -2) + float2(-1, 1), 0, 1);
    o.mpos = float2(tex2Dlod(_sPos, float4(0.5, 0.5, 0, 0)).xy - ReShade::MouseCoords) / ReShade::FrameTime;
    return o;
}
Last edit: 8 years 6 days ago by kingeric1992.
The following user(s) said Thank You: luluco250

Please Log in or Create an account to join the conversation.

  • luluco250
  • Topic Author
More
8 years 6 days ago #12 by luluco250 Replied by luluco250 on topic Is it possible to calculate the mouse velocity?
That makes sense, I'll look into it, thanks!

Please Log in or Create an account to join the conversation.

We use cookies
We use cookies on our website. Some of them are essential for the operation of the forum. You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.