sdfSprite.glsl
2.8 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@export ecgl.sdfSprite.vertex
uniform mat4 worldViewProjection : WORLDVIEWPROJECTION;
uniform float elapsedTime : 0;
attribute vec3 position : POSITION;
#ifdef VERTEX_SIZE
attribute float size;
#else
uniform float u_Size;
#endif
#ifdef VERTEX_COLOR
attribute vec4 a_FillColor: COLOR;
varying vec4 v_Color;
#endif
#ifdef VERTEX_ANIMATION
attribute vec3 prevPosition;
attribute float prevSize;
uniform float percent : 1.0;
#endif
#ifdef POSITIONTEXTURE_ENABLED
uniform sampler2D positionTexture;
#endif
varying float v_Size;
void main()
{
#ifdef POSITIONTEXTURE_ENABLED
// Only 2d position texture supported
gl_Position = worldViewProjection * vec4(texture2D(positionTexture, position.xy).xy, -10.0, 1.0);
#else
#ifdef VERTEX_ANIMATION
vec3 pos = mix(prevPosition, position, percent);
#else
vec3 pos = position;
#endif
gl_Position = worldViewProjection * vec4(pos, 1.0);
#endif
#ifdef VERTEX_SIZE
#ifdef VERTEX_ANIMATION
v_Size = mix(prevSize, size, percent);
#else
v_Size = size;
#endif
#else
v_Size = u_Size;
#endif
#ifdef VERTEX_COLOR
v_Color = a_FillColor;
// v_StrokeColor = a_StrokeColor;
#endif
gl_PointSize = v_Size;
}
@end
@export ecgl.sdfSprite.fragment
uniform vec4 color: [1, 1, 1, 1];
uniform vec4 strokeColor: [1, 1, 1, 1];
uniform float smoothing: 0.07;
uniform float lineWidth: 0.0;
#ifdef VERTEX_COLOR
varying vec4 v_Color;
// varying vec4 v_StrokeColor;
#endif
varying float v_Size;
uniform sampler2D sprite;
@import clay.util.srgb
void main()
{
gl_FragColor = color;
vec4 _strokeColor = strokeColor;
#ifdef VERTEX_COLOR
gl_FragColor *= v_Color;
// TODO
// _strokeColor *= v_StrokeColor;
#endif
#ifdef SPRITE_ENABLED
float d = texture2D(sprite, gl_PointCoord).r;
// Antialias
gl_FragColor.a *= smoothstep(0.5 - smoothing, 0.5 + smoothing, d);
if (lineWidth > 0.0) {
// TODO SCREEN SPACE OUTLINE
float sLineWidth = lineWidth / 2.0;
float outlineMaxValue0 = 0.5 + sLineWidth;
float outlineMaxValue1 = 0.5 + sLineWidth + smoothing;
float outlineMinValue0 = 0.5 - sLineWidth - smoothing;
float outlineMinValue1 = 0.5 - sLineWidth;
// FIXME Aliasing
if (d <= outlineMaxValue1 && d >= outlineMinValue0) {
float a = _strokeColor.a;
if (d <= outlineMinValue1) {
a = a * smoothstep(outlineMinValue0, outlineMinValue1, d);
}
else {
a = a * smoothstep(outlineMaxValue1, outlineMaxValue0, d);
}
gl_FragColor.rgb = mix(gl_FragColor.rgb * gl_FragColor.a, _strokeColor.rgb, a);
gl_FragColor.a = gl_FragColor.a * (1.0 - a) + a;
}
}
#endif
#ifdef SRGB_DECODE
gl_FragColor = sRGBToLinear(gl_FragColor);
#endif
}
@end