是否可以在片段着色器中绘制线条粗细? [英] Is it possible to draw line thickness in a fragment shader?

查看:137
本文介绍了是否可以在片段着色器中绘制线条粗细?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我使用GL_LINES绘制线条,是否可以在片段着色器中添加线条粗细?我看到的大多数示例似乎仅访问片段着色器中图元内的纹理元素,而线粗细着色器将需要写入线图元外部的texel以获得厚度.但是,如果可能的话,一个很小的基本示例将是很好的选择.

Is it possible for me to add line thickness in the fragment shader considering that I draw the line with GL_LINES? Most of the examples I saw seem to access only the texels within the primitive in the fragment shader and a line thickness shader would need to write to texels outside the line primitive to obtain the thickness. If it is possible however, a very small, basic, example, would be great.

推荐答案

片段着色器有很多可能.只需一些人在做什么.我本人距离该级别还很遥远,但是这段代码可以给您一个想法:

Quite a lot is possible with fragment shaders. Just look what some guys are doing. I'm far away from that level myself but this code can give you an idea:

#define resolution vec2(500.0, 500.0)
#define Thickness 0.003

float drawLine(vec2 p1, vec2 p2) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;

  float a = abs(distance(p1, uv));
  float b = abs(distance(p2, uv));
  float c = abs(distance(p1, p2));

  if ( a >= c || b >=  c ) return 0.0;

  float p = (a + b + c) * 0.5;

  // median to (p1, p2) vector
  float h = 2 / c * sqrt( p * ( p - a) * ( p - b) * ( p - c));

  return mix(1.0, 0.0, smoothstep(0.5 * Thickness, 1.5 * Thickness, h));
}

void main()
{
  gl_FragColor = vec4(
      max(
        max(
          drawLine(vec2(0.1, 0.1), vec2(0.1, 0.9)),
          drawLine(vec2(0.1, 0.9), vec2(0.7, 0.5))),
        drawLine(vec2(0.1, 0.1), vec2(0.7, 0.5))));
}

另一种选择是用texture2D检查附近像素的颜色-这样,您可以使图像发光或变粗(例如,如果任何调整像素为白色-如果当前像素附近,则使当前像素为白色)是白色-将当前像素设为灰色).

Another alternative is to check with texture2D for the color of nearby pixel - that way you can make you image glow or thicken (e.g. if any of the adjustment pixels are white - make current pixel white, if next to nearby pixel is white - make current pixel grey).

这篇关于是否可以在片段着色器中绘制线条粗细?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆