forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaders.py
216 lines (182 loc) · 6.51 KB
/
shaders.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import gpu
from gpu.types import GPUShader
import sys
if sys.version_info >= (3, 9):
from functools import cache
else:
from functools import lru_cache
cache = lru_cache(maxsize=None)
class Shaders:
base_vertex_shader_3d = '''
uniform mat4 ModelViewProjectionMatrix;
in vec3 pos;
vec4 project = ModelViewProjectionMatrix * vec4(pos, 1.0);
void main() {
gl_Position = project;
}
'''
base_fragment_shader_3d = '''
uniform vec4 color;
uniform float dash_width = 10.0;
uniform float dash_factor = 0.40;
uniform vec2 Viewport;
uniform bool dashed = false;
noperspective in vec2 stipple_pos;
flat in vec2 stipple_start;
out vec4 fragColor;
void main() {
float aspect = Viewport.x/Viewport.y;
float distance_along_line = distance(stipple_pos, stipple_start);
float normalized_distance = fract(distance_along_line / dash_width);
if (dashed == true) {
if (normalized_distance <= dash_factor) {
discard;
}
else {
fragColor = color;
}
}
else {
fragColor = color;
}
}
'''
@staticmethod
@cache
def uniform_color_2d():
return gpu.shader.from_builtin("2D_UNIFORM_COLOR")
@staticmethod
@cache
def uniform_color_3d():
return gpu.shader.from_builtin("3D_UNIFORM_COLOR")
@classmethod
@cache
def id_line_3d(cls):
shader = cls.uniform_color_line_3d()
return shader
@classmethod
@cache
def uniform_color_line_3d(cls):
geometry_shader = '''
layout(lines) in;
layout(triangle_strip, max_vertices = 10) out;
uniform mat4 ModelViewProjectionMatrix;
uniform vec2 Viewport;
uniform float thickness = float(0.1);
/* We leverage hardware interpolation to compute distance along the line. */
noperspective out vec2 stipple_pos; /* In screen space */
flat out vec2 stipple_start; /* In screen space */
out vec2 mTexCoord;
out float alpha;
float aspect = Viewport.x/Viewport.y;
vec2 pxVec = vec2(1.0/Viewport.x,1.0/Viewport.y);
float minLength = length(pxVec);
vec2 get_line_width(vec2 normal, float width){
vec2 offsetvec = vec2(normal * width);
offsetvec.x /= Viewport.x;
offsetvec.y /= Viewport.y;
if (length(offsetvec) < minLength){
offsetvec = normalize(offsetvec);
offsetvec *= minLength;
}
return(offsetvec);
}
float get_line_alpha(vec2 normal, float width){
vec2 offsetvec = vec2(normal * width);
offsetvec.x /= Viewport.x;
offsetvec.y /= Viewport.y;
float alpha = 1.0;
if (length(offsetvec) < minLength){
alpha *= (length(offsetvec)/minLength);
}
return alpha;
}
void main() {
//calculate line normal
vec4 p1 = gl_in[0].gl_Position;
vec4 p2 = gl_in[1].gl_Position;
vec2 ssp1 = vec2(p1.xy / p1.w);
vec2 ssp2 = vec2(p2.xy / p2.w);
float width = thickness;
vec2 dir = normalize(ssp2 - ssp1);
vec2 normal = vec2(-dir[1], dir[0]);
normal = normalize(normal);
// get offset factor from normal and user input thickness
vec2 offset = get_line_width(normal,width);
float lineAlpha = get_line_alpha(normal,width);
vec4 coords[4];
vec2 texCoords[4];
coords[0] = vec4((ssp1 + offset)*p1.w,p1.z,p1.w);
texCoords[0] = vec2(0,1);
coords[1] = vec4((ssp1 - offset)*p1.w,p1.z,p1.w);
texCoords[1] = vec2(0,0);
coords[2] = vec4((ssp2 + offset)*p2.w,p2.z,p2.w);
texCoords[2] = vec2(0,1);
coords[3] = vec4((ssp2 - offset)*p2.w,p2.z,p2.w);
texCoords[3] = vec2(0,0);
for (int i = 0; i < 4; ++i) {
mTexCoord = texCoords[i];
gl_Position = coords[i];
alpha = lineAlpha;
vec4 stipple_base;
if (i < 2) {
stipple_base = vec4(ssp1*p1.w,p1.z,p1.w);
}
else {
stipple_base = vec4(ssp2*p2.w, p2.z, p2.w);
}
stipple_start = stipple_pos = Viewport * 0.5 * (stipple_base.xy / stipple_base.w);
EmitVertex();
}
EndPrimitive();
}
'''
return GPUShader(cls.base_vertex_shader_3d, cls.base_fragment_shader_3d, geocode=geometry_shader)
@staticmethod
@cache
def id_shader_3d():
vertex_shader = """
uniform mat4 ModelViewProjectionMatrix;
in vec3 pos;
void main()
{
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
}
"""
fragment_shader = """
uniform vec4 color;
out vec4 fragColor;
void main()
{
fragColor = color;
}
"""
return GPUShader(vertex_shader, fragment_shader)
@staticmethod
@cache
def dashed_uniform_color_3d():
vertex_shader = '''
uniform mat4 ModelViewProjectionMatrix;
in vec3 pos;
in float arcLength;
out float v_ArcLength;
vec4 project = ModelViewProjectionMatrix * vec4(pos, 1.0f);
vec4 offset = vec4(0,0,-0.001,0);
void main()
{
v_ArcLength = arcLength;
gl_Position = project + offset;
}
'''
fragment_shader = '''
uniform float u_Scale;
uniform vec4 color;
in float v_ArcLength;
out vec4 fragColor;
void main()
{
if (step(sin(v_ArcLength * u_Scale), 0.7) == 0) discard;
fragColor = color;
}
'''
return GPUShader(vertex_shader, fragment_shader)