在 Unity 的顶点着色器中丢弃顶点

Discard vertex in Unity's vertex shader

在我的情况下,我在 Unity 中有一个网格,我想在顶点着色器中丢弃一些顶点。 'Discard vertex' 表示顶点和与其相连的线都不会被绘制。

我无意中实现了它,但我认为这不是一种优雅或安全的方式。我的方法是把position的y部分设置为无效数字,即1.0 / 0

这是我的部分代码:

    // count may be 0, but no crash, and the point will not be drawn, seems all good.
    sum /= count;
    pos.y = sum;
    o.pos = UnityObjectToClipPos(pos);
    return o;

这是我的结果:

说明绿框内的一些顶点和与之相连的线没有画出来。

请问有没有其他优雅安全的方法。

来自 opengl 规范:

Similarly, treatment of conditions such as divide by 0 may lead to an unspecified result, but in no case should such a condition lead to the interruption or termination of processing.

您不能在顶点着色器中丢弃顶点,顶点仍然存在。

您可以在称为 "geometry shader" 的阶段中修改顶点流,在顶点着色器中,您只能修改提供给片段着色器的插值器的值