1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
8 from gpu_extras
.batch
import batch_for_shader
9 from mathutils
import Vector
11 from .sun_calc
import calc_surface
, calc_analemma
14 if bpy
.app
.background
: # ignore drawing in background mode
15 def north_update(self
, context
):
17 def surface_update(self
, context
):
19 def analemmas_update(self
, context
):
24 shader_interface
= gpu
.types
.GPUStageInterfaceInfo("my_interface")
25 shader_interface
.flat('VEC2', "v_StartPos")
26 shader_interface
.smooth('VEC4', "v_VertPos")
28 shader_info
= gpu
.types
.GPUShaderCreateInfo()
29 shader_info
.push_constant('MAT4', "u_ViewProjectionMatrix")
30 shader_info
.push_constant('VEC4', "u_Color")
31 shader_info
.push_constant('VEC2', "u_Resolution")
32 shader_info
.vertex_in(0, 'VEC3', "position")
33 shader_info
.vertex_out(shader_interface
)
35 shader_info
.vertex_source(
39 vec4 pos = u_ViewProjectionMatrix * vec4(position, 1.0f);
41 v_StartPos = (pos / pos.w).xy;
47 shader_info
.fragment_out(0, 'VEC4', "FragColor")
48 shader_info
.fragment_source(
52 vec4 vertPos_2d = v_VertPos / v_VertPos.w;
53 vec2 dir = (vertPos_2d.xy - v_StartPos.xy) * u_Resolution;
54 float dist = length(dir);
56 if (step(sin(dist / 5.0f), 0.0) == 1) discard;
63 shader
= gpu
.shader
.create_from_info(shader_info
)
69 Set up the compass needle using the current north offset angle
70 less 90 degrees. This forces the unit circle to begin at the
71 12 O'clock instead of 3 O'clock position.
73 sun_props
= bpy
.context
.scene
.sun_pos_properties
75 color
= (0.2, 0.6, 1.0, 0.7)
77 angle
= -(sun_props
.north_offset
- math
.pi
/ 2)
78 x
= math
.cos(angle
) * radius
79 y
= math
.sin(angle
) * radius
80 coords
= Vector((x
, y
, 0)), Vector((0, 0, 0))
81 batch
= batch_for_shader(shader
, 'LINE_STRIP', {"position": coords
})
83 matrix
= bpy
.context
.region_data
.perspective_matrix
84 shader
.uniform_float("u_ViewProjectionMatrix", matrix
)
85 shader
.uniform_float("u_Resolution", (bpy
.context
.region
.width
,
86 bpy
.context
.region
.height
))
87 shader
.uniform_float("u_Color", color
)
88 width
= gpu
.state
.line_width_get()
89 gpu
.state
.line_width_set(2.0)
91 gpu
.state
.line_width_set(width
)
95 def north_update(self
, context
):
97 sun_props
= context
.scene
.sun_pos_properties
98 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
100 if addon_prefs
.show_overlays
and sun_props
.show_north
:
101 _north_handle
= bpy
.types
.SpaceView3D
.draw_handler_add(north_draw
, (), 'WINDOW', 'POST_VIEW')
102 elif _north_handle
is not None:
103 bpy
.types
.SpaceView3D
.draw_handler_remove(_north_handle
, 'WINDOW')
108 def analemmas_draw(batch
, shader
):
109 shader
.uniform_float("color", (1, 0, 0, 1))
112 _analemmas_handle
= None
114 def analemmas_update(self
, context
):
115 global _analemmas_handle
116 sun_props
= context
.scene
.sun_pos_properties
117 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
119 if addon_prefs
.show_overlays
and sun_props
.show_analemmas
:
124 analemma_verts
= calc_analemma(context
, h
)
125 coords
.extend(analemma_verts
)
126 for i
in range(len(analemma_verts
) - 1):
127 indices
.append((coord_offset
+ i
,
129 coord_offset
+= len(analemma_verts
)
131 shader
= gpu
.shader
.from_builtin('UNIFORM_COLOR')
132 batch
= batch_for_shader(shader
, 'LINES',
133 {"pos": coords
}, indices
=indices
)
135 if _analemmas_handle
is not None:
136 bpy
.types
.SpaceView3D
.draw_handler_remove(_analemmas_handle
, 'WINDOW')
137 _analemmas_handle
= bpy
.types
.SpaceView3D
.draw_handler_add(
138 analemmas_draw
, (batch
, shader
), 'WINDOW', 'POST_VIEW')
139 elif _analemmas_handle
is not None:
140 bpy
.types
.SpaceView3D
.draw_handler_remove(_analemmas_handle
, 'WINDOW')
141 _analemmas_handle
= None
145 def surface_draw(batch
, shader
):
146 blend
= gpu
.state
.blend_get()
147 gpu
.state
.blend_set("ALPHA")
148 shader
.uniform_float("color", (.8, .6, 0, 0.2))
150 gpu
.state
.blend_set(blend
)
152 _surface_handle
= None
154 def surface_update(self
, context
):
155 global _surface_handle
156 sun_props
= context
.scene
.sun_pos_properties
157 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
159 if addon_prefs
.show_overlays
and sun_props
.show_surface
:
160 coords
= calc_surface(context
)
161 shader
= gpu
.shader
.from_builtin('UNIFORM_COLOR')
162 batch
= batch_for_shader(shader
, 'TRIS', {"pos": coords
})
164 if _surface_handle
is not None:
165 bpy
.types
.SpaceView3D
.draw_handler_remove(_surface_handle
, 'WINDOW')
166 _surface_handle
= bpy
.types
.SpaceView3D
.draw_handler_add(
167 surface_draw
, (batch
, shader
), 'WINDOW', 'POST_VIEW')
168 elif _surface_handle
is not None:
169 bpy
.types
.SpaceView3D
.draw_handler_remove(_surface_handle
, 'WINDOW')
170 _surface_handle
= None