• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
theagentd

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

4 posts in this topic

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!

 

0

Share this post


Link to post
Share on other sites
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. Edited by Ohforf sake
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0