Welcome to mirror list, hosted at ThFree Co, Russian Federation.

edit_uv_edges_frag.glsl « shaders « overlay « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f90c1acbbbf89fd9dd3902a37c13c915b5b5191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma BLENDER_REQUIRE(common_globals_lib.glsl)
#pragma BLENDER_REQUIRE(common_overlay_lib.glsl)

uniform int lineStyle;
uniform bool doSmoothWire;
uniform float alpha;
uniform float dashLength;

in float selectionFac_f;
noperspective in float edgeCoord_f;
noperspective in vec2 stipplePos_f;
flat in vec2 stippleStart_f;

layout(location = 0) out vec4 fragColor;

#define M_1_SQRTPI 0.5641895835477563 /* 1/sqrt(pi) */

/**
 * We want to know how much a pixel is covered by a line.
 * We replace the square pixel with acircle of the same area and try to find the intersection area.
 * The area we search is the circular segment. https://en.wikipedia.org/wiki/Circular_segment
 * The formula for the area uses inverse trig function and is quite complexe. Instead,
 * we approximate it by using the smoothstep function and a 1.05 factor to the disc radius.
 */
#define DISC_RADIUS (M_1_SQRTPI * 1.05)
#define GRID_LINE_SMOOTH_START (0.5 - DISC_RADIUS)
#define GRID_LINE_SMOOTH_END (0.5 + DISC_RADIUS)

void main()
{
  vec4 inner_color = vec4(vec3(0.0), 1.0);
  vec4 outer_color = vec4(0.0);

  vec2 dd = fwidth(stipplePos_f);
  float line_distance = distance(stipplePos_f, stippleStart_f) / max(dd.x, dd.y);

  if (lineStyle == OVERLAY_UV_LINE_STYLE_OUTLINE) {
    inner_color = mix(colorWireEdit, colorEdgeSelect, selectionFac_f);
    outer_color = vec4(vec3(0.0), 1.0);
  }
  else if (lineStyle == OVERLAY_UV_LINE_STYLE_DASH) {
    if (fract(line_distance / dashLength) < 0.5) {
      inner_color = mix(vec4(vec3(0.35), 1.0), colorEdgeSelect, selectionFac_f);
    }
  }
  else if (lineStyle == OVERLAY_UV_LINE_STYLE_BLACK) {
    vec4 base_color = vec4(vec3(0.0), 1.0);
    inner_color = mix(base_color, colorEdgeSelect, selectionFac_f);
  }
  else if (lineStyle == OVERLAY_UV_LINE_STYLE_WHITE) {
    vec4 base_color = vec4(1.0);
    inner_color = mix(base_color, colorEdgeSelect, selectionFac_f);
  }
  else if (lineStyle == OVERLAY_UV_LINE_STYLE_SHADOW) {
    inner_color = colorUVShadow;
  }

  float dist = abs(edgeCoord_f) - max(sizeEdge - 0.5, 0.0);
  float dist_outer = dist - max(sizeEdge, 1.0);
  float mix_w;
  float mix_w_outer;

  if (doSmoothWire) {
    mix_w = smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist);
    mix_w_outer = smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist_outer);
  }
  else {
    mix_w = step(0.5, dist);
    mix_w_outer = step(0.5, dist_outer);
  }

  vec4 final_color = mix(outer_color, inner_color, 1.0 - mix_w * outer_color.a);
  final_color.a *= 1.0 - (outer_color.a > 0.0 ? mix_w_outer : mix_w);
  final_color.a *= alpha;

  fragColor = final_color;
}