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 if _north_handle
is None:
102 _north_handle
= bpy
.types
.SpaceView3D
.draw_handler_add(north_draw
, (), 'WINDOW', 'POST_VIEW')
103 elif _north_handle
is not None:
104 bpy
.types
.SpaceView3D
.draw_handler_remove(_north_handle
, 'WINDOW')
109 def analemmas_draw(batch
, shader
):
110 shader
.uniform_float("color", (1, 0, 0, 1))
113 _analemmas_handle
= None
115 def analemmas_update(self
, context
):
116 global _analemmas_handle
117 sun_props
= context
.scene
.sun_pos_properties
118 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
120 if addon_prefs
.show_overlays
and sun_props
.show_analemmas
:
125 analemma_verts
= calc_analemma(context
, h
)
126 coords
.extend(analemma_verts
)
127 for i
in range(len(analemma_verts
) - 1):
128 indices
.append((coord_offset
+ i
,
130 coord_offset
+= len(analemma_verts
)
132 shader
= gpu
.shader
.from_builtin('UNIFORM_COLOR')
133 batch
= batch_for_shader(shader
, 'LINES',
134 {"pos": coords
}, indices
=indices
)
136 if _analemmas_handle
is not None:
137 bpy
.types
.SpaceView3D
.draw_handler_remove(_analemmas_handle
, 'WINDOW')
138 _analemmas_handle
= bpy
.types
.SpaceView3D
.draw_handler_add(
139 analemmas_draw
, (batch
, shader
), 'WINDOW', 'POST_VIEW')
140 elif _analemmas_handle
is not None:
141 bpy
.types
.SpaceView3D
.draw_handler_remove(_analemmas_handle
, 'WINDOW')
142 _analemmas_handle
= None
146 def surface_draw(batch
, shader
):
147 blend
= gpu
.state
.blend_get()
148 gpu
.state
.blend_set("ALPHA")
149 shader
.uniform_float("color", (.8, .6, 0, 0.2))
151 gpu
.state
.blend_set(blend
)
153 _surface_handle
= None
155 def surface_update(self
, context
):
156 global _surface_handle
157 sun_props
= context
.scene
.sun_pos_properties
158 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
160 if addon_prefs
.show_overlays
and sun_props
.show_surface
:
161 coords
= calc_surface(context
)
162 shader
= gpu
.shader
.from_builtin('UNIFORM_COLOR')
163 batch
= batch_for_shader(shader
, 'TRIS', {"pos": coords
})
165 if _surface_handle
is not None:
166 bpy
.types
.SpaceView3D
.draw_handler_remove(_surface_handle
, 'WINDOW')
167 _surface_handle
= bpy
.types
.SpaceView3D
.draw_handler_add(
168 surface_draw
, (batch
, shader
), 'WINDOW', 'POST_VIEW')
169 elif _surface_handle
is not None:
170 bpy
.types
.SpaceView3D
.draw_handler_remove(_surface_handle
, 'WINDOW')
171 _surface_handle
= None