<p>You do not have a bug. This is not an artifact of your program, but of the human visual system — an optical illusion. The retina emphasizes the second spatial derivative of the image, a phenomenon known as <a href="https://en.wikipedia.org/wiki/Mach_bands" rel="nofollow noreferrer">Mach bands</a> (unfortunately, the Wikipedia article isn't very clear and I didn't find any definitely better one). The most common example of this phenomenon when working with gradients is a gradient which reverses direction:</p>
<p><a href="https://i.sstatic.net/6pkhU1BM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6pkhU1BM.png" alt="Gradient from white, to dark gray, to white" /></a></p>
<p>This image transitions smoothly from white, to dark gray, to white again. It has no abrupt changes in intensity (the first derivative is small everywhere). Despite this, there will be a strong perception of a vertical line in the center.</p>
<p>Your image is doing almost exactly the same thing, but on a diagonal. If I rotate your image 45° and crop it to a similar shape, then the only difference is that it <em>also</em> has a uniform vertical gradient component:</p>
<p><a href="https://i.sstatic.net/LI61gCdr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LI61gCdr.png" alt="Cropped and rotated image from question" /></a></p>
<p>This can be understood not as a bug, but as our vision trying to help us out — a gradient like these, if it were present in the image of a physical object, likely would be some kind of corner, so highlighting it helps us perceive the shape of the object.</p>
<hr />
<p>In order to avoid this perception of a diagonal line, you will have to do something <em>more complex</em>. (For example, if you made a rounded corner instead of a sharp rectangular corner, that would work.) There is no mathematical error. You must elaborate your algorithm until you find something that <em>looks</em> good for your purposes, in context with the rest of your rendered scene.</p>
<p>(Perhaps there is some sort of standard “correction” formula that cancels out Mach bands, but if there is, I haven't heard of it, and it would probably have secondary effects.)</p>

CC BY-SA 4.0
2024-05-23T20:28:32.917Z
Great response, thank you.
hungry91
CC BY-SA 4.0
2024-05-24T12:08:46.540Z
Adding noise can help with this issue, the noise can be as simple as random values within the range of the current dithered value, or sampling a noise texture.
pmw1234
CC BY-SA 4.0
2024-05-23T15:37:03.473Z
2024-05-23T15:37:03.473Z
Kevin Reid
<p>I am trying to achieve a border effect to a square tile on a xz plane. I am using max of coordinates x and z to achieve that. Here is my ShaderLab code:</p>
<pre><code>    void surf (Input IN, inout SurfaceOutputStandard o)
    {
        half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
        float max_xz = max(IN.worldPos.z , IN.worldPos.x);

        o.Albedo = c.rgb;
        o.Alpha = max_xz;
    }
</code></pre>
<p>This is how a corner of my tile looks like:</p>
<p><a href="https://i.sstatic.net/AJkoAjL8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AJkoAjL8.png" alt="max artifact" /></a></p>
<p>Why does the shady line towards the corner appear?
Is there a simple way to fix this?</p>

<shader><artifacts><coordinates>
How to achieve smooth max of two coordinates in a shader
hungry91
# How to achieve smooth max of two coordinates in a shader

(Asked by: username_0 on 2024-05-22)

I am trying to achieve a border effect to a square tile on a xz plane. I am using max of coordinates x and z to achieve that. Here is my ShaderLab code:

```
    void surf (Input IN, inout SurfaceOutputStandard o)
    {
        half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
        float max_xz = max(IN.worldPos.z , IN.worldPos.x);

        o.Albedo = c.rgb;
        o.Alpha = max_xz;
    }

```

This is how a corner of my tile looks like:

[![max artifact](https://i.sstatic.net/AJkoAjL8.png)](https://i.sstatic.net/AJkoAjL8.png)

Why does the shady line towards the corner appear?
Is there a simple way to fix this?

**Tags:** shader, artifacts, coordinates


---

## 1 answers

### Answer by username_1 on 2024-05-23. Score: 1 | Accepted answer

You do not have a bug. This is not an artifact of your program, but of the human visual system — an optical illusion. The retina emphasizes the second spatial derivative of the image, a phenomenon known as [Mach bands](https://en.wikipedia.org/wiki/Mach_bands) (unfortunately, the Wikipedia article isn't very clear and I didn't find any definitely better one). The most common example of this phenomenon when working with gradients is a gradient which reverses direction:

[![Gradient from white, to dark gray, to white](https://i.sstatic.net/6pkhU1BM.png)](https://i.sstatic.net/6pkhU1BM.png)

This image transitions smoothly from white, to dark gray, to white again. It has no abrupt changes in intensity (the first derivative is small everywhere). Despite this, there will be a strong perception of a vertical line in the center.

Your image is doing almost exactly the same thing, but on a diagonal. If I rotate your image 45° and crop it to a similar shape, then the only difference is that it *also* has a uniform vertical gradient component:

[![Cropped and rotated image from question](https://i.sstatic.net/LI61gCdr.png)](https://i.sstatic.net/LI61gCdr.png)

This can be understood not as a bug, but as our vision trying to help us out — a gradient like these, if it were present in the image of a physical object, likely would be some kind of corner, so highlighting it helps us perceive the shape of the object.

---

In order to avoid this perception of a diagonal line, you will have to do something *more complex*. (For example, if you made a rounded corner instead of a sharp rectangular corner, that would work.) There is no mathematical error. You must elaborate your algorithm until you find something that *looks* good for your purposes, in context with the rest of your rendered scene.

(Perhaps there is some sort of standard “correction” formula that cancels out Mach bands, but if there is, I haven't heard of it, and it would probably have secondary effects.)

> **Comments:**
> **username_0**: Great response, thank you.  
> **username_2**: Adding noise can help with this issue, the noise can be as simple as random values within the range of the current dithered value, or sampling a noise texture.  

---

