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:
Off:
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!