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

gpencil_fx_shadow_prepare_frag.glsl « fx « shaders « gpencil « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2e20feae1825bc22047a5da1360475d81ecaf14 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;

/* ******************************************************************* */
/* create shadow                                                       */
/* ******************************************************************* */
uniform sampler2D strokeColor;
uniform sampler2D strokeDepth;
uniform vec2 Viewport;

uniform int offset[2];
uniform float scale[2];
uniform float rotation;
uniform vec4 shadow_color;

uniform float amplitude;
uniform float period;
uniform float phase;
uniform int orientation;

uniform vec3 loc;
uniform float pixsize; /* rv3d->pixsize */
uniform float pixfactor;

#define M_PI 3.1415926535897932384626433832795

#define HORIZONTAL 0
#define VERTICAL 1

float defaultpixsize = pixsize * (1000.0 / pixfactor);
vec2 noffset = vec2(offset[0], offset[1]);
float cosv = cos(rotation);
float sinv = sin(rotation);

out vec4 FragColor;

/* project 3d point to 2d on screen space */
vec2 toScreenSpace(vec4 vertex)
{
  /* need to calculate ndc because this is not done by vertex shader */
  vec3 ndc = vec3(vertex).xyz / vertex.w;

  vec2 sc;
  sc.x = ((ndc.x + 1.0) / 2.0) * Viewport.x;
  sc.y = ((ndc.y + 1.0) / 2.0) * Viewport.y;

  return sc;
}

void main()
{
  vec2 uv = vec2(gl_FragCoord.xy);
  vec4 nloc = ProjectionMatrix * ViewMatrix * vec4(loc.xyz, 1.0);
  vec2 loc2d = toScreenSpace(nloc);

  float dx = (ProjectionMatrix[3][3] == 0.0) ? (noffset[0] / (nloc.z * defaultpixsize)) :
                                               (noffset[0] / defaultpixsize);
  float dy = (ProjectionMatrix[3][3] == 0.0) ? (noffset[1] / (nloc.z * defaultpixsize)) :
                                               (noffset[1] / defaultpixsize);

  /* move point to new coords system */
  vec2 tpos = vec2(uv.x, uv.y) - loc2d;

  /* rotation */
  if (rotation != 0) {
    vec2 rotpoint = vec2((tpos.x * cosv) - (tpos.y * sinv), (tpos.x * sinv) + (tpos.y * cosv));
    tpos = rotpoint;
  }

  /* apply offset */
  tpos = vec2(tpos.x - dx, tpos.y - dy);

  /* apply scale */
  tpos.x *= 1.0 / scale[0];
  tpos.y *= 1.0 / scale[1];

  /* back to original coords system */
  vec2 texpos = tpos + loc2d;

  /* wave */
  if (orientation == HORIZONTAL) {
    float pval = (uv.x * M_PI) / Viewport[0];
    texpos.y += amplitude * sin((period * pval) + phase);
  }
  else if (orientation == VERTICAL) {
    float pval = (uv.y * M_PI) / Viewport[1];
    texpos.x += amplitude * sin((period * pval) + phase);
  }

  vec4 src_pixel = texelFetch(strokeColor, ivec2(texpos.x, texpos.y), 0);
  /* is transparent */
  if (src_pixel.a == 0.0f) {
    discard;
  }

  gl_FragDepth = texelFetch(strokeDepth, ivec2(texpos.x, texpos.y), 0).r;
  FragColor = shadow_color;
}