<pre><code>strength = clamp(lineWidth - distance, 0.0, 1.0);
</code></pre>
<p>This method isn't linear interpolation, it is clamped linear falloff.</p>
<p>Linear interpolation:<span class="math-container">$L(a,b,t)=a+t⋅(b−a)$</span> would give slightly better result, is easier/more intuitive to tweak and may produces results that are good enough, after some tweaking. Though it is typically only slightly better.</p>
<pre><code>float t = clamp((distance - (lineWidth - 1)) / 1.0, 0.0, 1.0); // Normalize within range
strength = mix(1.0, 0.0, t); // Linear interpolation
</code></pre>
<p>It is computationally similar to clamped linear falloff so has about the same performance. If the output is plotted it gives a straight line. Because it is linear, only interpolating values, it is more sensitive to harsh transitions especially at the end points. You can often compute <span class="math-container">$t^2$</span> then feed that into lerp and get slightly better results for very little extra computation. Using <span class="math-container">$t^2$</span> introduces an entire host of ease in, ease out algorithms that can be used with lerp to get different effects.</p>
<p>Smoothstep, aka:Cubic Hermite interpolation: <span class="math-container">$H_3(t)=t^2(3−2t)$</span> also written as <span class="math-container">$H_3(t)=3t^2-2t^3$</span> is first order smooth. Meaning that it interpolates both the values and the slopes. Where slopes refer to the first derivative of the function being interpolated. If it's output is plotted the result will give a curve instead of a straight line. This makes it much less sensitive to transitions especially at the end points. Computationally it is more expensive then basic lerp but still fast enough that it can be used in many situations, and gives good results which makes it popular.</p>
<p>Because GPU's have gotten faster there is another version of Hermite interpolation that has become popular, and,  which is second order smooth called Quintic Hermite interpolation. <span class="math-container">$H_5(t)=6t^5-15t^4+10t^3$</span> this produces very smooth transitions at the end points and controls curvature which produces better interpolation across the entire curve. It is useful when other methods start to break down or very fine controlled results are desired. Quintic interpolation is the doorway to an entire world of interpolation algorithms which are largely only used in very specific situations.</p>

CC BY-SA 4.0
2024-11-25T21:45:47.987Z
Thanks for the very detailed answer! I understand my error with linear interpolation and that it's easier to tweak than linear falloff. But I don't get why it would give slightly better results than falloff? Wouldn't it give the exact same results (at least with the parameters you provided: `a = 1; b = 0; t = distance - (lineWidth - 1)`)?  Or do you mean it gives better results after tweaking?
Mos
CC BY-SA 4.0
2024-11-25T23:04:49.833Z
The method listed should be slightly better then clamped linear falloff because it normalizes the range creating a consistent gradient. Where as clamped linear falloff depends directly on the distances. The best way to see the difference is compute both, then subtract one from the other and return abs(diff)*scale as the color. For straight lines the will be little or no difference, but for curved lines and abrupt direction changes I would expect a small difference.
pmw1234
CC BY-SA 4.0
2024-11-24T12:40:12.260Z
2024-11-24T13:37:38.450Z
2024-11-24T13:37:38.450Z
pmw1234
<p>I am currently writing a fragment shader where I want to draw an antialiased line. My first instinct was to calculate the color of each pixel based on the distance to the line. Something like: (using pixel coordinates)</p>
<pre><code>strength = clamp(lineWidth - distance, 0.0, 1.0);
</code></pre>
<p>After doing some research on how to do antialiasing, I found that smoothstep is used a lot for this:</p>
<pre><code>strength = 1 - smoothstep(lineWidth - 1, lineWidth, distance);
</code></pre>
<p>But I also found examples using linear interpolation like I was doing in the first place. I couldn't find an explanation on why one is used over the other.</p>
<p>There is only a slight difference between the two but I want to know if there is an obvious/correct choice and want to unterstand why?</p>

CC BY-SA 4.0
2024-11-24T02:24:52.377Z
Linear interpolation can result in obvious stepping patterns, since the derivative of linear interation at 0 and 1 is non-zero. Smooth step applies Hermite interpolation (`t*t*(3.0 - 2.0*t` is applied after normalized linear interpolation). The derivative of this interpolation function has two zero points: t=0 and t=1, indicating smooth trasition at the boundary. So `smoothstep` can eliminate the harsh transition effects of linear interpolation.
Enigmatisms
<glsl><antialiasing><fragment-shader>
What's the difference of using smoothstep and linear interpolation for antialiasing?
Mos
# What's the difference of using smoothstep and linear interpolation for antialiasing?

(Asked by: username_0 on 2024-11-23)

I am currently writing a fragment shader where I want to draw an antialiased line. My first instinct was to calculate the color of each pixel based on the distance to the line. Something like: (using pixel coordinates)

```
strength = clamp(lineWidth - distance, 0.0, 1.0);

```

After doing some research on how to do antialiasing, I found that smoothstep is used a lot for this:

```
strength = 1 - smoothstep(lineWidth - 1, lineWidth, distance);

```

But I also found examples using linear interpolation like I was doing in the first place. I couldn't find an explanation on why one is used over the other.

There is only a slight difference between the two but I want to know if there is an obvious/correct choice and want to unterstand why?

**Tags:** glsl, antialiasing, fragment-shader

> **Comments:**
> **username_1**: Linear interpolation can result in obvious stepping patterns, since the derivative of linear interation at 0 and 1 is non-zero. Smooth step applies Hermite interpolation (`t*t*(3.0 - 2.0*t` is applied after normalized linear interpolation). The derivative of this interpolation function has two zero points: t=0 and t=1, indicating smooth trasition at the boundary. So `smoothstep` can eliminate the harsh transition effects of linear interpolation.  

---

## 1 answers

### Answer by username_2 on 2024-11-24. Score: 4 | Accepted answer

```
strength = clamp(lineWidth - distance, 0.0, 1.0);

```

This method isn't linear interpolation, it is clamped linear falloff.

Linear interpolation:$L(a,b,t)=a+t⋅(b−a)$ would give slightly better result, is easier/more intuitive to tweak and may produces results that are good enough, after some tweaking. Though it is typically only slightly better.

```
float t = clamp((distance - (lineWidth - 1)) / 1.0, 0.0, 1.0); // Normalize within range
strength = mix(1.0, 0.0, t); // Linear interpolation

```

It is computationally similar to clamped linear falloff so has about the same performance. If the output is plotted it gives a straight line. Because it is linear, only interpolating values, it is more sensitive to harsh transitions especially at the end points. You can often compute $t^2$ then feed that into lerp and get slightly better results for very little extra computation. Using $t^2$ introduces an entire host of ease in, ease out algorithms that can be used with lerp to get different effects.

Smoothstep, aka:Cubic Hermite interpolation: $H\_3(t)=t^2(3−2t)$ also written as $H\_3(t)=3t^2-2t^3$ is first order smooth. Meaning that it interpolates both the values and the slopes. Where slopes refer to the first derivative of the function being interpolated. If it's output is plotted the result will give a curve instead of a straight line. This makes it much less sensitive to transitions especially at the end points. Computationally it is more expensive then basic lerp but still fast enough that it can be used in many situations, and gives good results which makes it popular.

Because GPU's have gotten faster there is another version of Hermite interpolation that has become popular, and, which is second order smooth called Quintic Hermite interpolation. $H\_5(t)=6t^5-15t^4+10t^3$ this produces very smooth transitions at the end points and controls curvature which produces better interpolation across the entire curve. It is useful when other methods start to break down or very fine controlled results are desired. Quintic interpolation is the doorway to an entire world of interpolation algorithms which are largely only used in very specific situations.

> **Comments:**
> **username_0**: Thanks for the very detailed answer! I understand my error with linear interpolation and that it's easier to tweak than linear falloff. But I don't get why it would give slightly better results than falloff? Wouldn't it give the exact same results (at least with the parameters you provided: `a = 1; b = 0; t = distance - (lineWidth - 1)`)?  Or do you mean it gives better results after tweaking?  
> **username_2**: The method listed should be slightly better then clamped linear falloff because it normalizes the range creating a consistent gradient. Where as clamped linear falloff depends directly on the distances. The best way to see the difference is compute both, then subtract one from the other and return abs(diff)*scale as the color. For straight lines the will be little or no difference, but for curved lines and abrupt direction changes I would expect a small difference.  

---

