<h2>Sigma and kernel size of Gaussian filter</h2>

<p>Regarding how to choose the sigma and the kernel size (pixels) of the Gaussian: you set the sigma based on how wide of a blur you want (judging it visually) and then choose the kernel size based on the sigma. It's a game of finding a kernel size that captures enough of the (mathematically infinite) bell curve to look good, while not being too expensive.</p>

<p>As a rule of thumb, the kernel size should be at least 6 times the sigma. Then the kernel extends out to 3 sigmas in each direction, which is enough to <a href="https://en.wikipedia.org/wiki/Normal_distribution#/media/File:Empirical_Rule.PNG" rel="noreferrer">cover almost all the weight</a> of the mathematical bell curve. It's also nice to round up the pixel size to the next odd number so the filter will be symmetrical. So for example, with sigma = 1.5 px, you'd use a 9-pixel filter; with sigma = 2.0 px, use a 13-pixel filter, etc.</p>

<p>Alternatively, if you have performance limitations that control how big a kernel size you can use, then divide by 6 to get the maximum sigma you can get away with. (Note that sigma can be fractional, as it's just an input to the bell curve equation.)</p>

<p>By the way, a trick to get better performance on large Gaussian blurs is to downsample the image, perform a smaller blur, then upsample. For instance, to get an effective 13-pixel blur, you could downsample the image by 2x (using bilinear filtering), then perform a 7-pixel blur, and then upsample (using bilinear filtering). It will look almost as good as the 13-pixel blur, but will be considerably faster.</p>

<h2>Creating a good-looking glow filter</h2>

<p>A common trick with these is to use multiple Gaussians of different radius, added together. It makes a better glow effect than any single Gaussian by itself. For example, here's a mockup in Photoshop created with three layers additively blended together. The three Gaussians are of size 3px, 5px and 11px (corresponding to sigma = 0.5px, 0.83px, 1.83px respectively).</p>

<p><a href="https://i.sstatic.net/9QCiw.jpg" rel="noreferrer"><img src="https://i.sstatic.net/9QCiw.jpg" alt="mocked-up bloom filter"></a></p>

<p>You can also add even larger Gaussians to further increase the apparent radius of the glow. This is how the standard "bloom" effect is implemented in 3D game engines.</p>

<p>If performance is a concern, the smallest Gaussian can be replaced by a copy of the unblurred original image; the resulting effect will look sharper, not so soft, but will still have a glow surrounding it. As mentioned previously, larger Gaussians can be done by downsampling, blurring, and upsampling; and the upsampling can be done using hardware bilinear filtering, folded into the pass that blends the layers together.</p>

<h2>Jaggies</h2>

<p>You'll notice that jaggies are still visible in some areas of the above image. Unfortunately, blurring is not very effective at hiding jaggies unless the blur radius is quite large. There's no easy way to use a post-process get rid of jaggies: the best way to avoid them is not to render them in the first place. :)</p>

<p>I hear you that you can't use MSAA for performance reasons, but may be another option.  Depending on how you render the glow trails, you might be able to slightly increase their size, and write a shader that applies a gradient to fade out the color within a couple pixels of the edge. That should help them look smoother. <a href="https://www.shadertoy.com/view/MdtXWX" rel="noreferrer">Here's a shadertoy</a> that demonstrates a 1px-wide edge falloff used for antialiasing a procedural shape.</p>

CC BY-SA 3.0
2016-07-12T07:31:10.410Z
@Nathan_Reed I applied most of your tips, except instead of doing MSAA which oddly enough did the trick with not to much of a hit at 2x. If I had a question about adding more highlights to to the current effect should I edit the question or make a new one?
J.Doe
CC BY-SA 3.0
2016-07-12T07:36:54.207Z
@J.Doe Great, glad to hear it's working out! I'd say it's better to ask a new question about tweaking the effect if you have a specific look you're going for.
Nathan Reed
CC BY-SA 3.0
2016-07-03T23:42:12.383Z
2016-07-04T00:34:46.920Z
2016-07-04T00:34:46.920Z
Nathan Reed
<p>I have a simple game and and subjects of it are these little lines. It is targeted for iOS and android so it has a HUGE range of processors that it could run on, right now.</p>

<p><a href="https://i.sstatic.net/6CojT.jpg" rel="noreferrer"><img src="https://i.sstatic.net/6CojT.jpg" alt="enter image description here"></a></p>

<p>I am trying to add realtime glow to them for two reasons</p>

<ol>
<li>I am trying to hide the fact that I don't have the rendering time to process anti-aliasing on most devices.</li>
<li>The subject of the game is supposed to be pure light, ergo that picture should look like those things are pure light.</li>
</ol>

<p>I have been tweaking a separable gaussian blur shader for quite awhile and I have came up to a point of frustration, I just can't get it to look right, perhaps the issue is I am trying to futilely hide the jagged edges of the light meanwhile not making the light look blurry.</p>

<p>My biggest problem is all the variables involved in making it look its best.</p>

<p>I am very new to graphics/rendering and I am in no way an artist. Perhaps the most frustrating thing about rending to me is all the variables that seem to be involved. With glow I have seen so many possible changes.</p>

<ul>
<li>A. Add blend mode, screen blend mode, or some other concoction  </li>
<li>B.Weighting the blur, and normal differently before combination </li>
<li>C. The
Sigma of the gaussian bell function (I have been using <a href="http://dev.theomader.com/gaussian-kernel-calculator/" rel="noreferrer">this</a>
confusing calculator but it doesn't seem to give the same values I
see other people have in) </li>
<li>D. Scalars on the "x" values sent to the
sigma function </li>
<li>E. Sample scale (making the blur radius smaller or
larger) </li>
<li>F. Changing the resolution of the glow buffer</li>
</ul>

<p>How does one find the "best looking" constants when working with so many variables like this?</p>

<p>I am also having issues because the time between me making a tweak and seeing it is a long that its hard to see changes, I would do it in shader toy however I can't load up this image or procedurally generate one like it.</p>

<p>Right now I am really stuck on the sigma of the gaussian bell curve kernel, especially because I am coding in the numbers rather then the formula because I need the processor speed. <a href="http://dev.theomader.com/gaussian-kernel-calculator/" rel="noreferrer">Can you suggest a good sigma to use</a>?</p>

<lighting><pixel-shader><blur>
Tweaking a glow shader to make it look better
J.Doe
# Tweaking a glow shader to make it look better

(Asked by: username_0 on 2016-06-30)

I have a simple game and and subjects of it are these little lines. It is targeted for iOS and android so it has a HUGE range of processors that it could run on, right now.

[![enter image description here](https://i.sstatic.net/6CojT.jpg)](https://i.sstatic.net/6CojT.jpg)

I am trying to add realtime glow to them for two reasons

1. I am trying to hide the fact that I don't have the rendering time to process anti-aliasing on most devices.
2. The subject of the game is supposed to be pure light, ergo that picture should look like those things are pure light.

I have been tweaking a separable gaussian blur shader for quite awhile and I have came up to a point of frustration, I just can't get it to look right, perhaps the issue is I am trying to futilely hide the jagged edges of the light meanwhile not making the light look blurry.

My biggest problem is all the variables involved in making it look its best.

I am very new to graphics/rendering and I am in no way an artist. Perhaps the most frustrating thing about rending to me is all the variables that seem to be involved. With glow I have seen so many possible changes.

* A. Add blend mode, screen blend mode, or some other concoction
* B.Weighting the blur, and normal differently before combination
* C. The
  Sigma of the gaussian bell function (I have been using [this](http://dev.theomader.com/gaussian-kernel-calculator/)
  confusing calculator but it doesn't seem to give the same values I
  see other people have in)
* D. Scalars on the "x" values sent to the
  sigma function
* E. Sample scale (making the blur radius smaller or
  larger)
* F. Changing the resolution of the glow buffer

How does one find the "best looking" constants when working with so many variables like this?

I am also having issues because the time between me making a tweak and seeing it is a long that its hard to see changes, I would do it in shader toy however I can't load up this image or procedurally generate one like it.

Right now I am really stuck on the sigma of the gaussian bell curve kernel, especially because I am coding in the numbers rather then the formula because I need the processor speed. [Can you suggest a good sigma to use](http://dev.theomader.com/gaussian-kernel-calculator/)?

**Tags:** lighting, pixel-shader, blur


---

## 1 answers

### Answer by username_1 on 2016-07-03. Score: 9 | Accepted answer

## Sigma and kernel size of Gaussian filter

Regarding how to choose the sigma and the kernel size (pixels) of the Gaussian: you set the sigma based on how wide of a blur you want (judging it visually) and then choose the kernel size based on the sigma. It's a game of finding a kernel size that captures enough of the (mathematically infinite) bell curve to look good, while not being too expensive.

As a rule of thumb, the kernel size should be at least 6 times the sigma. Then the kernel extends out to 3 sigmas in each direction, which is enough to [cover almost all the weight](https://en.wikipedia.org/wiki/Normal_distribution#/media/File:Empirical_Rule.PNG) of the mathematical bell curve. It's also nice to round up the pixel size to the next odd number so the filter will be symmetrical. So for example, with sigma = 1.5 px, you'd use a 9-pixel filter; with sigma = 2.0 px, use a 13-pixel filter, etc.

Alternatively, if you have performance limitations that control how big a kernel size you can use, then divide by 6 to get the maximum sigma you can get away with. (Note that sigma can be fractional, as it's just an input to the bell curve equation.)

By the way, a trick to get better performance on large Gaussian blurs is to downsample the image, perform a smaller blur, then upsample. For instance, to get an effective 13-pixel blur, you could downsample the image by 2x (using bilinear filtering), then perform a 7-pixel blur, and then upsample (using bilinear filtering). It will look almost as good as the 13-pixel blur, but will be considerably faster.

## Creating a good-looking glow filter

A common trick with these is to use multiple Gaussians of different radius, added together. It makes a better glow effect than any single Gaussian by itself. For example, here's a mockup in Photoshop created with three layers additively blended together. The three Gaussians are of size 3px, 5px and 11px (corresponding to sigma = 0.5px, 0.83px, 1.83px respectively).

[![mocked-up bloom filter](https://i.sstatic.net/9QCiw.jpg)](https://i.sstatic.net/9QCiw.jpg)

You can also add even larger Gaussians to further increase the apparent radius of the glow. This is how the standard "bloom" effect is implemented in 3D game engines.

If performance is a concern, the smallest Gaussian can be replaced by a copy of the unblurred original image; the resulting effect will look sharper, not so soft, but will still have a glow surrounding it. As mentioned previously, larger Gaussians can be done by downsampling, blurring, and upsampling; and the upsampling can be done using hardware bilinear filtering, folded into the pass that blends the layers together.

## Jaggies

You'll notice that jaggies are still visible in some areas of the above image. Unfortunately, blurring is not very effective at hiding jaggies unless the blur radius is quite large. There's no easy way to use a post-process get rid of jaggies: the best way to avoid them is not to render them in the first place. :)

I hear you that you can't use MSAA for performance reasons, but may be another option. Depending on how you render the glow trails, you might be able to slightly increase their size, and write a shader that applies a gradient to fade out the color within a couple pixels of the edge. That should help them look smoother. [Here's a shadertoy](https://www.shadertoy.com/view/MdtXWX) that demonstrates a 1px-wide edge falloff used for antialiasing a procedural shape.

> **Comments:**
> **username_0**: @username_1 I applied most of your tips, except instead of doing MSAA which oddly enough did the trick with not to much of a hit at 2x. If I had a question about adding more highlights to to the current effect should I edit the question or make a new one?  
> **username_1**: @username_0 Great, glad to hear it's working out! I'd say it's better to ask a new question about tweaking the effect if you have a specific look you're going for.  

---

