Welcome, Guest.
Username: Password: Remember me

TOPIC: Is it possible to calculate the mouse velocity?

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #1

  • luluco250
  • luluco250's Avatar
  • Offline
  • Posts: 80
  • Thank you received: 51
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?
The administrator has disabled public write access.

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #2

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2446
  • Thank you received: 1388
You can render to textures and use them to store data. A simple 1x1 R32F texture to store a single float value for instance.
Cheers, crosire =)
The administrator has disabled public write access.
The following user(s) said Thank You: luluco250

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #3

  • luluco250
  • luluco250's Avatar
  • Offline
  • Posts: 80
  • Thank you received: 51
Thanks, I was thinking of that, will try and see if it works.
The administrator has disabled public write access.

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #4

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2446
  • Thank you received: 1388
Also, to fetch data from textures faster and without the interpolation "tex2D" does, use ReShade's "tex2Dfetch" intrinsic.
Cheers, crosire =)
The administrator has disabled public write access.
The following user(s) said Thank You: luluco250, OtisInf

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #5

  • OtisInf
  • OtisInf's Avatar
  • Offline
  • Posts: 123
  • Thank you received: 51
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?
The administrator has disabled public write access.

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #6

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2446
  • Thank you received: 1388
Until you write to the texture again, yes.
Cheers, crosire =)
The administrator has disabled public write access.

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #7

  • luluco250
  • luluco250's Avatar
  • Offline
  • Posts: 80
  • Thank you received: 51
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! [ Click to expand ]


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: 7 months 2 weeks ago by luluco250.
The administrator has disabled public write access.

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #8

  • OtisInf
  • OtisInf's Avatar
  • Offline
  • Posts: 123
  • Thank you received: 51
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 administrator has disabled public write access.
The following user(s) said Thank You: luluco250

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #9

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2446
  • Thank you received: 1388
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));
Cheers, crosire =)
Last Edit: 7 months 2 weeks ago by crosire.
The administrator has disabled public write access.
The following user(s) said Thank You: luluco250

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #10

  • luluco250
  • luluco250's Avatar
  • Offline
  • Posts: 80
  • Thank you received: 51
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 administrator has disabled public write access.
The following user(s) said Thank You: crosire

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #11

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: 7 months 2 weeks ago by kingeric1992.
The administrator has disabled public write access.
The following user(s) said Thank You: luluco250

Is it possible to calculate the mouse velocity? 7 months 2 weeks ago #12

  • luluco250
  • luluco250's Avatar
  • Offline
  • Posts: 80
  • Thank you received: 51
That makes sense, I'll look into it, thanks!
The administrator has disabled public write access.