GLSL自定义/不对具有顶点颜色的三角形进行插值 [英] GLSL custom/no interpolation of triangles with vertex colors
问题描述
我要实现的效果是具有清晰轮廓的顶点颜色.因此,在三角形内部,片段着色器应使用最接近该片段的任何顶点的颜色.
The effect I want to achieve is vertex color with sharp contours. So inside the triangle the fragment shader should use the color of whatever vertex is closest to that fragment.
现在考虑这个问题时,我能想到的唯一解决方案是将tex坐标1,0,0 0,1,0和0,0,1分配给这三个顶点,并具有2个(重新排序的)顶点顶点颜色阵列,然后从其对应的tex坐标最高的颜色中选择.此方法将至少为每个顶点添加9个以上的浮点数.由于我的网格物体经常退出,这会减慢应用程序的速度,并显着增加内存占用.
Now when thinking about it, the only solution I can come up with is assigning tex coords 1,0,0 0,1,0 and 0,0,1 to the three vertices and have 2 (reoordered) duplicates of the vertex color array and then choose from the color aray of which the corresponding tex coord is highest. This method would at least add 9 more floats to each vertex. Which will slow down the application as my meshes are changing quit often and increase the memory footprint significantly.
是否有更好/更简便的方法来实现这一目标?
Is there a better/easier way to achieve this?
推荐答案
[替换了不正确的原始图片]
这应该实际上有效...
//vert
out vec3 colour;
...
//geom
//simple passthrough but dupe colours to separate and include barycentric coord
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec3 colour[];
flat out vec3 colours[3];
out vec3 coord;
...
for (int i = 0; i < 3; ++i)
colours[i] = colour[i];
for (int i = 0; i < 3; ++i)
{
coord = vec3(0.0);
coord[i] = 1.0;
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
//frag
flat in vec3 colours[3]; //triangle's 3 vertex colours
in vec3 coord; //barycentric weights as a result of interp
...
//get index of biggest coord
int i = (coord.x > cood.y && coord.x > coord.z) ? 0 :
((coord.y > coord.z) ? 1 : 2);
//choose that colour
vec3 fragColour = colours[i];
这篇关于GLSL自定义/不对具有顶点颜色的三角形进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!