|
1 | 1 | import gpu
|
2 | 2 | from gpu.types import GPUShader, GPUShaderCreateInfo, GPUStageInterfaceInfo
|
3 | 3 | from gpu.shader import create_from_info
|
4 |
| -from bpy import app |
5 | 4 |
|
6 | 5 | import sys
|
7 | 6 |
|
|
16 | 15 |
|
17 | 16 | class Shaders:
|
18 | 17 |
|
19 |
| - base_vertex_shader_3d = """ |
20 |
| - void main() { |
21 |
| - gl_Position = ModelViewProjectionMatrix * vec4(pos.xyz, 1.0f); |
22 |
| -
|
23 |
| - vec2 ssPos = vec2(gl_Position.xy / gl_Position.w); |
24 |
| - segment_start = stipple_pos = ssPos; |
25 |
| - } |
26 |
| - """ |
27 |
| - base_fragment_shader_3d = """ |
28 |
| - void main() { |
29 |
| -
|
30 |
| - vec2 delta = stipple_pos - segment_start; |
31 |
| - vec2 stipple_start; |
32 |
| - if (abs(delta.x) > abs(delta.y)) { |
33 |
| - stipple_start.x = 0; |
34 |
| - float t = -segment_start.x / delta.x; |
35 |
| - stipple_start.y = segment_start.y + t * delta.y; |
36 |
| - } |
37 |
| - else { |
38 |
| - stipple_start.y = 0; |
39 |
| - float t = -segment_start.y / delta.y; |
40 |
| - stipple_start.x = segment_start.x + t * delta.x; |
41 |
| - } |
42 |
| - float distance_along_line = distance(stipple_pos, stipple_start); |
43 |
| - float normalized_distance = fract(distance_along_line / dash_width); |
44 |
| -
|
45 |
| - if (dashed == true) { |
46 |
| - if (normalized_distance <= dash_factor) { |
47 |
| - discard; |
48 |
| - } |
49 |
| - else { |
50 |
| - fragColor = color; |
51 |
| - } |
52 |
| - } |
53 |
| - else { |
54 |
| - fragColor = color; |
55 |
| - } |
56 |
| -
|
57 |
| - } |
58 |
| - """ |
59 |
| - |
60 |
| - base_vertex_shader_2d = """ |
61 |
| - void main() { |
62 |
| - gl_Position = ModelViewProjectionMatrix * vec4(pos.xy, 0.0, 1.0f); |
63 |
| - } |
64 |
| - """ |
65 |
| - base_fragment_shader_2d = """ |
66 |
| - void main() { |
67 |
| - fragColor = color; |
68 |
| - } |
69 |
| - """ |
70 |
| - |
71 |
| - @classmethod |
72 |
| - def get_base_shader_3d_info(cls): |
73 |
| - |
74 |
| - vert_out = GPUStageInterfaceInfo("stipple_pos_interface") |
75 |
| - vert_out.no_perspective("VEC2", "stipple_pos") |
76 |
| - vert_out.flat("VEC2", "segment_start") |
77 |
| - |
78 |
| - # NOTE: How to set default values? |
79 |
| - |
80 |
| - shader_info = GPUShaderCreateInfo() |
81 |
| - shader_info.push_constant("MAT4", "ModelViewProjectionMatrix") |
82 |
| - shader_info.push_constant("VEC4", "color") |
83 |
| - shader_info.push_constant("FLOAT", "dash_width") |
84 |
| - shader_info.push_constant("FLOAT", "dash_factor") |
85 |
| - # shader_info.push_constant("VEC2", "Viewport") |
86 |
| - shader_info.push_constant("BOOL", "dashed") |
87 |
| - shader_info.vertex_in(0, "VEC3", "pos") |
88 |
| - shader_info.vertex_out(vert_out) |
89 |
| - shader_info.fragment_out(0, "VEC4", "fragColor") |
90 |
| - |
91 |
| - shader_info.vertex_source(cls.base_vertex_shader_3d) |
92 |
| - shader_info.fragment_source(cls.base_fragment_shader_3d) |
93 |
| - |
94 |
| - return shader_info |
95 |
| - |
96 |
| - @classmethod |
97 |
| - def get_base_shader_2d_info(cls): |
98 |
| - |
99 |
| - shader_info = GPUShaderCreateInfo() |
100 |
| - shader_info.push_constant("MAT4", "ModelViewProjectionMatrix") |
101 |
| - shader_info.push_constant("VEC4", "color") |
102 |
| - shader_info.push_constant("FLOAT", "lineWidth") |
103 |
| - shader_info.vertex_in(0, "VEC2", "pos") |
104 |
| - shader_info.fragment_out(0, "VEC4", "fragColor") |
105 |
| - |
106 |
| - shader_info.vertex_source(cls.base_vertex_shader_2d) |
107 |
| - shader_info.fragment_source(cls.base_fragment_shader_2d) |
108 |
| - |
109 |
| - return shader_info |
110 |
| - |
111 |
| - @staticmethod |
112 |
| - @cache |
113 |
| - def uniform_color_3d(): |
114 |
| - if app.version < (3, 5): |
115 |
| - return gpu.shader.from_builtin("3D_UNIFORM_COLOR") |
116 |
| - return gpu.shader.from_builtin("UNIFORM_COLOR") |
117 |
| - |
118 |
| - @staticmethod |
119 |
| - @cache |
120 |
| - def point_color_3d(): |
121 |
| - """Get uniform color shader for points. Compatible with all GPU backends.""" |
122 |
| - return gpu.shader.from_builtin("UNIFORM_COLOR") |
123 |
| - |
124 |
| - @staticmethod |
125 |
| - @cache |
126 |
| - def polyline_color_3d(): |
127 |
| - """Get polyline shader for thick lines on all backends.""" |
128 |
| - return gpu.shader.from_builtin("POLYLINE_UNIFORM_COLOR") |
129 |
| - |
130 | 18 | @classmethod
|
131 | 19 | @cache
|
132 | 20 | def uniform_color_image_2d(cls):
|
@@ -170,24 +58,10 @@ def uniform_color_image_2d(cls):
|
170 | 58 | del shader_info
|
171 | 59 | return shader
|
172 | 60 |
|
173 |
| - @classmethod |
174 |
| - @cache |
175 |
| - def id_line_3d(cls): |
176 |
| - shader = cls.uniform_color_line_3d() |
177 |
| - return shader |
178 |
| - |
179 |
| - @classmethod |
180 |
| - @cache |
181 |
| - def uniform_color_line_3d(cls): |
182 |
| - |
183 |
| - shader_info = cls.get_base_shader_3d_info() |
184 |
| - shader = create_from_info(shader_info) |
185 |
| - del shader_info |
186 |
| - return shader |
187 |
| - |
188 | 61 | @staticmethod
|
189 | 62 | @cache
|
190 | 63 | def id_shader_3d():
|
| 64 | + """Simple ID shader for selection rendering (both points and lines).""" |
191 | 65 | shader_info = GPUShaderCreateInfo()
|
192 | 66 | shader_info.push_constant("MAT4", "ModelViewProjectionMatrix")
|
193 | 67 | shader_info.push_constant("VEC4", "color")
|
@@ -215,44 +89,3 @@ def id_shader_3d():
|
215 | 89 | shader = create_from_info(shader_info)
|
216 | 90 | del shader_info
|
217 | 91 | return shader
|
218 |
| - |
219 |
| - @staticmethod |
220 |
| - @cache |
221 |
| - def dashed_uniform_color_3d(): |
222 |
| - vertex_shader = """ |
223 |
| - uniform mat4 ModelViewProjectionMatrix; |
224 |
| - in vec3 pos; |
225 |
| - in float arcLength; |
226 |
| -
|
227 |
| - out float v_ArcLength; |
228 |
| - vec4 project = ModelViewProjectionMatrix * vec4(pos, 1.0f); |
229 |
| - vec4 offset = vec4(0,0,-0.001,0); |
230 |
| - void main() |
231 |
| - { |
232 |
| - v_ArcLength = arcLength; |
233 |
| - gl_Position = project + offset; |
234 |
| - } |
235 |
| - """ |
236 |
| - |
237 |
| - fragment_shader = """ |
238 |
| - uniform float u_Scale; |
239 |
| - uniform vec4 color; |
240 |
| -
|
241 |
| - in float v_ArcLength; |
242 |
| - out vec4 fragColor; |
243 |
| -
|
244 |
| - void main() |
245 |
| - { |
246 |
| - if (step(sin(v_ArcLength * u_Scale), 0.7) == 0) discard; |
247 |
| - fragColor = color; |
248 |
| - } |
249 |
| - """ |
250 |
| - return GPUShader(vertex_shader, fragment_shader) |
251 |
| - |
252 |
| - @classmethod |
253 |
| - @cache |
254 |
| - def uniform_color_line_2d(cls): |
255 |
| - shader_info = cls.get_base_shader_2d_info() |
256 |
| - shader = create_from_info(shader_info) |
257 |
| - del shader_info |
258 |
| - return shader |
0 commit comments