Gradient troubles with motion blur using textureGrad() and anisotropic filtering

Started by
3 comments, last by Ohforf sake 10 years, 1 month ago

Hello.

I am trying to implement motion blur for my particles by abusing anisotropic filtering. The idea is to use a particle texture with premultiplied alpha, enable 16x anisotropic filtering and set the clamp mode to GL_CLAMP_TO_BORDER (fade to vec4(0, 0, 0, 0) on edges). Theoretically, I could then sample the texture using textureGrad() with custom gradients to sample 16 pixels at a time from the particle texture. I've computed correct motion vectors in pixels for my particles and increased their size to allow them to blur over their edges. However, I am having trouble with calculating correct gradients to achieve my blur. I have a very hard time wrapping my head around the gradients affect the sample pattern of anisotropic filtering. It's worth noting that my particle sprites are always screen aligned and unrotated.

Here are the relevant parts of my fragment shader so far:


uniform sampler2D particleTexture;

uniform float motionBlurMaxRadius;

in vec2 gTexCoord;
in vec2 gMotionVector;


void main(){
    float length = max(0.0001, sqrt(dot(gMotionVector, gMotionVector)));
    float clampedLength = min(motionBlurMaxRadius, length);
    
    vec2 dfdx = dFdx(gTexCoord);
    vec2 dfdy = dFdy(gTexCoord);
	
    vec2 gx = dfdx + gMotionVector * clampedLength / length;
    vec2 gy = dfdy + gMotionVector * clampedLength / length;
	
    vec4 textureResult = textureGrad(particleTexture, gTexCoord, gx, gy);

    //soft depth test, write to framebuffer, etc.
}

Results:

On:

WFYqPJG.jpg

Off:

vgcf9c4.jpg

The super small sparks work pretty well, but the fire and smoke is blurred beyond recognition... I just don't know how I should modify the gradient to achieve a certain amount of blur in screen space! Any help is appreciated!

Advertisement
Side question: Is 16x anisotropic filtering really faster then elongated particles?

Maybe the following approach helps you. It should also work for rotated particles:

dfdx contains the partial derivatives for the x coordinate:
\[dfdx = \begin{pmatrix} \frac{\delta u}{\delta x} & \frac{\delta v}{\delta x} \end{pmatrix}\]
Similarly dfdy is the partial derivative for the y coordinate:
\[dfdy =\begin{pmatrix}\frac{\delta u}{\delta y} & \frac{\delta v}{\delta y} \end{pmatrix}\]

Now lets arrange dfdx and dfdy into a matrix A such that:

\[
A = \begin{pmatrix}
\frac{\delta u}{\delta x} & \frac{\delta u}{\delta y} \\
\frac{\delta v}{\delta x} & \frac{\delta v}{\delta y} \\
\end{pmatrix}
\]

This way, \[A \cdot v\] gives the uv stretch along the screen space displacement v.

Now you want to create new gradients (also encoded in a matrix named B) such that the texture stretch is increased (by a factor f >= 1) along the motion vector but stays the same perpendicular to it. Assuming v is said motion vector this gives you 2 equations:

\[
B \cdot v = f \cdot A \cdot v
\]
\[
B \cdot \begin{pmatrix}v.y \\ -v.x\end{pmatrix} = A \cdot \begin{pmatrix}v.y \\ -v.x\end{pmatrix}
\]

This yields a system of linear equations with 4 equations and 4 unknowns:

\[
\begin{pmatrix}
v.x & v.y & 0 & 0 \\
0 & 0 & v.x & v.y \\
v.y & -v.x & 0 & 0 \\
0 & 0 & v.y & -v.x
\end{pmatrix}
\cdot
\begin{pmatrix}
b11 \\
b12 \\
b21 \\
b22
\end{pmatrix}
=
\begin{pmatrix}
f \cdot A \cdot v \\
A \cdot \begin{pmatrix} v.y \\ -v.x \end{pmatrix}
\end{pmatrix}
\]


The system can be analytically solved (for example in maxima). I won't tex it here but there is a closed solution with lots of potential for hand optimization by the looks of it.
With that you can compute B given A, f, and v and B just contains the gradients that you seek.

Edit: Finally found the correct tag for latex. Turns out it isn't [ eqn ] but \ [ and \ ] (without spaces). The matrix within a matrix rendering is still somewhat broken.

I am very confused by this post. Is there something wrong with the formatting?

Double posting, but I've managed to get reasonable results:

Off:

FN79NlY.jpg

On:

i6VMy0n.jpg

The problem seemed to be that I needed to take the values from dFdx() and dFdy() into account when calculating my gradients:


	float length = max(0.0001, sqrt(dot(gMotionVector, gMotionVector)));
	float clampedLength = min(motionBlurMaxRadius, length);
	
	vec2 dfdx = dFdx(gTexCoord);
	vec2 dfdy = dFdy(gTexCoord);
	
	float mvMul = 0.3 * clampedLength/length;
	
	vec2 gx = dfdx + gMotionVector * mvMul * sqrt(dot(dfdx, dfdx));
	vec2 gy = dfdy + gMotionVector * mvMul * sqrt(dot(dfdy, dfdy));
	
	vec4 base = textureGrad(particleTexture, gTexCoord, gx, gy) * gColor;

This will obviously completely break down if the sprite is rotated, so I'm still very interested in the post above!!!

Is there something wrong with the formatting?


Yes, sorry about that, I kind of messed that up. It should be readable now, although for some matrices the brackets are too small. Apperently even outside any tags, the forum software scans for code-ish text and tries to interpret it, which created a colorful mix of tex code interspersed with rendered formula fragments.

This topic is closed to new replies.

Advertisement