在点之间绘制中点线

时间:2019-06-25 19:01:55

标签: c math opengl shader

我正在用片段着色器制作生成的形状,其中一部分涉及绘制对象之间的边界,即对象之间的分界线。

目前我有:
img

所需的效果是:
img

(请注意,绘制线条的形状无关紧要)

但是问题是中间有些突出。

uniform int objects = 0;
uniform vec2 positions[50];
uniform float radii[50];
uniform float meter = 64;
varying vec2 texPos;
uniform vec2 cameraPosition;
uniform float k1 = sqrt(2*50*50); //smoothness of cells
uniform float k2 = sqrt(2*50*50)/2; //smoothness of cell walls
uniform float lineWidth = 6;

#ifdef VERTEX
vec4 position(mat4 transform_projection, vec4 vertex_position)
{
    texPos = (cameraPosition + vertex_position.xy);
    return transform_projection * vertex_position;
}
#endif

#ifdef PIXEL
float smin( float a, float b, float k )
{
    float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
    return mix( b, a, h ) - k*h*(1.0-h);
}

vec4 effect( vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords ) {
  float finalmin = 0;
  float bordermin = 99999;
  for (int i = 0; i < objects; i++) {
    for (int j = 0; j < i; j++) {
      float di = distance(positions[i], texPos) - radii[i];
      float dj = distance(positions[j], texPos) - radii[j];
      float sm = smin(di, dj, k1);

      if (sm < finalmin) {
        finalmin = sm;
        bordermin = min(abs(di-dj),bordermin);
      }
    }
  }
  if (finalmin < 0) {
    // if (smin(borderdist, abs(finalmin), k2) < lineWidth) {
      if (bordermin < lineWidth) {
      color.a = 1;
    }

    return color;
  }
  return vec4(0.1,0,0,1);
}
#endif

请注意,这是love2d glsl,这就是为什么它与众不同的原因

编辑:我解决了自己的问题

1 个答案:

答案 0 :(得分:0)

uniform int objects = 0;
uniform vec2 positions[50];
uniform float radii[50];
uniform float meter = 64;
varying vec2 texPos;
uniform vec2 cameraPosition;
uniform float k1 = sqrt(2*50*50); //smoothness of cells
uniform float k2 = sqrt(2*50*50)/4; //smoothness of cell walls
uniform float lineWidth = 6;

#ifdef VERTEX
vec4 position(mat4 transform_projection, vec4 vertex_position)
{
    texPos = (cameraPosition + vertex_position.xy);
    return transform_projection * vertex_position;
}
#endif

#ifdef PIXEL
float smin( float a, float b, float k )
{
    float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
    return mix( b, a, h ) - k*h*(1.0-h);
}

vec4 effect( vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords ) {
  float finalmin = 0;
  float bordermin = 99999;
  int closestobj = -1;


  float mindist = 99999;
  for (int i = 0; i < objects; i++) {
    float dist = distance(positions[i], texPos) - radii[i];
    if (closestobj == -1 || dist < mindist) {
      mindist = dist;
      closestobj = i;
    }
  }

  for (int i = 0; i < objects; i++) {
    for (int j = 0; j < i; j++) {
      float di = distance(positions[i], texPos) - radii[i];
      float dj = distance(positions[j], texPos) - radii[j];
      float sm = smin(di, dj, k1);
      float m = min(di, dj);
      if (sm < finalmin) {
        finalmin = sm;
        if (closestobj == i || closestobj == j) {
          bordermin = min(abs(di-dj), bordermin);
        }
      }
    }
  }
  if (finalmin < 0) {
    if (smin(bordermin, abs(finalmin), k2) < lineWidth) {
    // if (bordermin < lineWidth) {
      color.a = 1;
    }

    return color;
  }
  return vec4(0);
}
#endif

基本上,如果两点之一是离片段最近的点,我只会渲染两点之间的中点线。