简体   繁体   English

有没有办法在用三角形条绘制的网格上实现诸如行进方块之类的东西?

[英]Is there a way to implement something like marching squares on a grid drawn with triangle strips?

I am currently drawing a grid using a series of triangle strips.我目前正在使用一系列三角形条绘制网格。 I am using this to render a height field, and generating the vertex data completely in the vertex shader without any input buffers just using the vertex and instance indexes.我使用它来渲染高度字段,并完全在顶点着色器中生成顶点数据,而无需任何输入缓冲区,仅使用顶点和实例索引。 This is all working fine and is very efficient.这一切都很好,而且非常有效。

However, I now find myself also needing to implement border lines on this grid.然而,我现在发现自己也需要在这个网格上实现边界线。 The obvious solution here would be something like marching squares.这里显而易见的解决方案是行进广场之类的东西。 Basically, what I want to achieve is something like this:基本上,我想要实现的是这样的:

我想要的图

The black dots represent the vertices in the grid that are part of some set, and I want to shade the area inside the red line differently than that outside it.黑点代表网格中属于某个集合的顶点,我想对红线内的区域与红线外的区域进行不同的着色。

Naïvely, this seems like it would be easy: Add a value to the vertices that is 1 for vertices in the set and 0 for those outside it, and render differently depending on if the interpolated value is above or below 0.5, for instance.天真地,这似乎很容易:例如,向顶点添加一个值,该值对于集合中的顶点为 1,对于集合外的顶点为 0,并根据插值是高于还是低于 0.5 进行不同的渲染。

However, because this is rendered as a triangle strip, this does not quite work.但是,因为这是呈现为三角形条,所以这并不完全有效。 In pracitce, since this is rendered as a triangle strip, this ends up looking like this:实际上,由于它呈现为三角形条,因此最终看起来像这样:

在此处输入图片说明

So, half the edges work and half end up with ugly square staircases.所以,一半的边缘可以工作,一半的结果是丑陋的方形楼梯。

I've now been trying to wrack my brain for days whether there is some trick that could be used to generate the vertex values differently or making a more complicated test than >0.5 to get closer to the intended shape without giving up on the nice and simple triangle strips and having to actually generate geometry ahead of time, but I can not think of one.我现在一直在努力思考是否有一些技巧可以用来以不同的方式生成顶点值,或者进行比 >0.5 更复杂的测试以更接近预期的形状而不放弃好的和简单的三角形条带,不得不提前实际生成几何图形,但我想不出一个。

Has anyone ever dealt with a similar problem?有没有人处理过类似的问题? Is there some clever solution I am missing?我缺少一些聪明的解决方案吗?

I am doing this in Metal, but I don't expect this to depend much on the specific API used.我在 Metal 中执行此操作,但我不希望这在很大程度上取决于所使用的特定 API。

It sounds like you're trying to calculate the colors in the fragment shader independently of the mesh underneath.听起来您正在尝试独立于下面的网格来计算片段着色器中的颜色。 If so, you should decouple the color calculation from the mesh.如果是这样,您应该将颜色计算与网格分离。

Assuming your occupancy is stored in a texture, use textureGather to get the four nearby occupancy values;假设你的占用存储在一个纹理中,使用textureGather来获取四个附近的占用值; determine the equation of the boundary;确定边界方程; then use the fractional part of the texture coordinates to determine its position relative to the boundary.然后使用纹理坐标的小数部分来确定其相对于边界的位置。 (The devil here is in the details -- in particularly the ambiguous checker-board pattern case.) (这里的问题在于细节——尤其是模糊的棋盘图案案例。)

Once you implement the above approach, it's very likely you won't even need the triangle strip mesh anymore -- simply fill your entire drawing area with a single large quad and let the fragment shader to do the rest.一旦你实现了上面的方法,很可能你甚至不再需要三角形带网格——只需用一个大的四边形填充整个绘图区域,让片段着色器完成剩下的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM