<blockquote>
<p>I somehow understand that it should not work, as quadratic bending and linear fragment interpolation between two vertices is different,</p>
</blockquote>
<p>Indeed. If you want to non-linearly distort a mesh object, then you have to <em>sample the nonlinear function frequently</em> to get a reasonable linear approximation to it — that is, you must not have very large or long triangles.</p>
<p>If that were the only problem, then your mesh would wobble weirdly but not be full of gaps. The gaps are a different, but related problem:</p>
<blockquote>
<p>but I implemented a greedy mesh optimization, that same faces are merged to one huge mesh.</p>
</blockquote>
<p>The greedy meshing algorithm is known to produce “T-junctions” — points where an edge of one quad (pair of triangles) meets the vertices of two (or more) others.</p>
<pre><code>|         Quad 1         |
|                        |
|V1         V2           |V3
+-----------+------------+
|           |            |
|   Quad 2  |   Quad 3   |
</code></pre>
<p>Under ordinary circumstances, this merely produces a few pixel-sized specks in the rendering, because the rasterization of the edge of quad 1 doesn't quite line up with the rasterizations of the other two.</p>
<p>However, when you introduce a non-linear transformation, then those specks open up into big gaps, because the non-linear transformation moved vertex V2 so that <strong>it is not on the line between V1 and V2 at all</strong>. Those are the gaps you are seeing.</p>
<p>To avoid these gaps, you must generate a mesh which <strong>contains no T-junctions</strong> — every edge of every triangle must exactly meet (share two vertices with) a neighboring triangle. (This is true of most meshes created by 3D modeling software, except where an object is intentionally incomplete, such as leaving off the bottom of a tree's trunk.)</p>
<p>The simplest way to achieve this is to remove the greedy mesh optimization, and generate 2 triangles for every block face. Conveniently, this will also ensure that the visual effect of the distortion is consistently sampled.</p>
<p>If you still want to optimize the mesh, you will have to use a different algorithm, which avoids T-junctions — it cannot simply generate one quad for a large area and must instead take neighboring triangles into account. Once the algorithm has decided to put a vertex at a certain corner, all other surfaces meeting that corner must also have a vertex there too rather than an edge passing by. This is a nontrivial problem and I'm not aware of any articles describing a complete algorithm for it, nor do I have such an algorithm in code form. (Perhaps one day I'll write one.)</p>

CC BY-SA 4.0
2023-06-03T06:54:11.840Z
Thanks for sharing! But, how can a greedy mesh optimization not produce T-junctions? As soon as two quads have the same edge it could be optimized as well, if they have the same material. I was also thinking about producing meta data for the distorted vertices and add correctly bended vertices in the tesselation stage? At current I have quite good results for not bend at all for near vertices and use a small curve factor.
Aitch
CC BY-SA 4.0
2023-06-03T16:14:22.560Z
@Aitch "how can a greedy mesh optimization not produce T-junctions" — The greedy mesh algorithm by definition _does_ produce T-junctions. I'm saying you would have to write some _different_ algorithm, and the algorithm must know that once you have decided to put a vertex at a certain corner, all surfaces meeting that corner **must also have a vertex there too,** which means it will produce more vertices, but still not as many as quad-per-block-face. As I said, this is a tricky problem and I don't have a solution handy — I just know that the _result_ is possible.
Kevin Reid
CC BY-SA 4.0
2023-06-03T03:23:55.067Z
2023-06-03T16:16:04.623Z
2023-06-03T16:16:04.623Z
Kevin Reid
<p>I have a voxel based game, similar to Minecraft and want to apply the &quot;curved world effect&quot; like <a href="https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader/" rel="nofollow noreferrer">described here</a>.</p>
<p>As I was using a per 1x1 face quad renderer, applying the effect went well, but I implemented a greedy mesh optimization, that same faces are merged to one huge mesh.</p>
<p>Now the curved world shader seems to not work anymore, but most of the sources describe, that it should just work for all meshes (whatever &quot;all&quot; means).</p>
<p><a href="https://i.sstatic.net/Uawcn.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Uawcn.png" alt="greedy mesh optimization artifacts" /></a></p>
<p>I somehow understand that it should not work, as quadratic bending and linear fragment interpolation between two vertices is different, but I don't find any hint when it does not work.</p>
<p>That's the part of my vertex shader.</p>
<pre><code>// offscreen.vert
// ..
vec3 diff = ubo.pos - inPosition;
diff.y = 0;

float dist = length(diff);
vec3 d = vec3(0.0, 0.005 * -dist * dist, 0.0);

vec3 p = inPosition + d;
gl_Position = ubo.proj * ubo.view * vec4(p, 1.0);
// ..
</code></pre>
<p>Do have any idea?
Thanks for helping</p>

<shader><glsl>
Curved world effect shader for heterogeneous mesh faces
Aitch
# Curved world effect shader for heterogeneous mesh faces

(Asked by: username_0 on 2023-06-02)

I have a voxel based game, similar to Minecraft and want to apply the "curved world effect" like [described here](https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader/).

As I was using a per 1x1 face quad renderer, applying the effect went well, but I implemented a greedy mesh optimization, that same faces are merged to one huge mesh.

Now the curved world shader seems to not work anymore, but most of the sources describe, that it should just work for all meshes (whatever "all" means).

[![greedy mesh optimization artifacts](https://i.sstatic.net/Uawcn.png)](https://i.sstatic.net/Uawcn.png)

I somehow understand that it should not work, as quadratic bending and linear fragment interpolation between two vertices is different, but I don't find any hint when it does not work.

That's the part of my vertex shader.

```
// offscreen.vert
// ..
vec3 diff = ubo.pos - inPosition;
diff.y = 0;

float dist = length(diff);
vec3 d = vec3(0.0, 0.005 * -dist * dist, 0.0);

vec3 p = inPosition + d;
gl_Position = ubo.proj * ubo.view * vec4(p, 1.0);
// ..

```

Do have any idea?
Thanks for helping

**Tags:** shader, glsl


---

## 1 answers

### Answer by username_1 on 2023-06-03. Score: 1 | Accepted answer

> I somehow understand that it should not work, as quadratic bending and linear fragment interpolation between two vertices is different,

Indeed. If you want to non-linearly distort a mesh object, then you have to *sample the nonlinear function frequently* to get a reasonable linear approximation to it — that is, you must not have very large or long triangles.

If that were the only problem, then your mesh would wobble weirdly but not be full of gaps. The gaps are a different, but related problem:

> but I implemented a greedy mesh optimization, that same faces are merged to one huge mesh.

The greedy meshing algorithm is known to produce “T-junctions” — points where an edge of one quad (pair of triangles) meets the vertices of two (or more) others.

```
|         Quad 1         |
|                        |
|V1         V2           |V3
+-----------+------------+
|           |            |
|   Quad 2  |   Quad 3   |

```

Under ordinary circumstances, this merely produces a few pixel-sized specks in the rendering, because the rasterization of the edge of quad 1 doesn't quite line up with the rasterizations of the other two.

However, when you introduce a non-linear transformation, then those specks open up into big gaps, because the non-linear transformation moved vertex V2 so that **it is not on the line between V1 and V2 at all**. Those are the gaps you are seeing.

To avoid these gaps, you must generate a mesh which **contains no T-junctions** — every edge of every triangle must exactly meet (share two vertices with) a neighboring triangle. (This is true of most meshes created by 3D modeling software, except where an object is intentionally incomplete, such as leaving off the bottom of a tree's trunk.)

The simplest way to achieve this is to remove the greedy mesh optimization, and generate 2 triangles for every block face. Conveniently, this will also ensure that the visual effect of the distortion is consistently sampled.

If you still want to optimize the mesh, you will have to use a different algorithm, which avoids T-junctions — it cannot simply generate one quad for a large area and must instead take neighboring triangles into account. Once the algorithm has decided to put a vertex at a certain corner, all other surfaces meeting that corner must also have a vertex there too rather than an edge passing by. This is a nontrivial problem and I'm not aware of any articles describing a complete algorithm for it, nor do I have such an algorithm in code form. (Perhaps one day I'll write one.)

> **Comments:**
> **username_0**: Thanks for sharing! But, how can a greedy mesh optimization not produce T-junctions? As soon as two quads have the same edge it could be optimized as well, if they have the same material. I was also thinking about producing meta data for the distorted vertices and add correctly bended vertices in the tesselation stage? At current I have quite good results for not bend at all for near vertices and use a small curve factor.  
> **username_1**: @username_0 "how can a greedy mesh optimization not produce T-junctions" — The greedy mesh algorithm by definition _does_ produce T-junctions. I'm saying you would have to write some _different_ algorithm, and the algorithm must know that once you have decided to put a vertex at a certain corner, all surfaces meeting that corner **must also have a vertex there too,** which means it will produce more vertices, but still not as many as quad-per-block-face. As I said, this is a tricky problem and I don't have a solution handy — I just know that the _result_ is possible.  

---

