Welcome to mirror list, hosted at ThFree Co, Russian Federation.

north.py « sun_position - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c343df05f502971022144fe51f3a7198a993c168 (plain)
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
# SPDX-License-Identifier: GPL-2.0-or-later

import bpy
import math
import gpu
from gpu_extras.batch import batch_for_shader
from mathutils import Vector


if bpy.app.background:  # ignore north line in background mode
    def north_update(self, context):
        pass
else:
    vertex_shader = '''
        uniform mat4 u_ViewProjectionMatrix;

        in vec3 position;

        flat out vec2 v_StartPos;
        out vec4 v_VertPos;

        void main()
        {
            vec4 pos    = u_ViewProjectionMatrix * vec4(position, 1.0f);
            gl_Position = pos;
            v_StartPos    = (pos / pos.w).xy;
            v_VertPos     = pos;
        }
    '''

    fragment_shader = '''
        uniform vec4 u_Color;

        flat in vec2 v_StartPos;
        in vec4 v_VertPos;
        out vec4 FragColor;

        uniform vec2 u_Resolution;

        void main()
        {
            vec4 vertPos_2d = v_VertPos / v_VertPos.w;
            vec2 dir  = (vertPos_2d.xy - v_StartPos.xy) * u_Resolution;
            float dist = length(dir);

            if (step(sin(dist / 5.0f), 0.0) == 1) discard;

            FragColor = u_Color;
        }
    '''

    shader = gpu.types.GPUShader(vertex_shader, fragment_shader)

    def draw_north_callback():
        # ------------------------------------------------------------------
        # Set up the compass needle using the current north offset angle
        # less 90 degrees.  This forces the unit circle to begin at the
        # 12 O'clock instead of 3 O'clock position.
        # ------------------------------------------------------------------
        sun_props = bpy.context.scene.sun_pos_properties

        color = (0.2, 0.6, 1.0, 0.7)
        radius = 100
        angle = -(sun_props.north_offset - math.pi / 2)
        x = math.cos(angle) * radius
        y = math.sin(angle) * radius

        coords = Vector((x, y, 0)), Vector((0, 0, 0))   # Start & end of needle

        batch = batch_for_shader(
            shader, 'LINE_STRIP',
            {"position": coords},
        )
        shader.bind()

        matrix = bpy.context.region_data.perspective_matrix
        shader.uniform_float("u_ViewProjectionMatrix", matrix)
        shader.uniform_float("u_Resolution", (bpy.context.region.width, bpy.context.region.height))
        shader.uniform_float("u_Color", color)
        gpu.state.line_width_set(2.0)
        batch.draw(shader)

    _handle = None


    def north_update(self, context):
        global _handle
        if self.show_north and _handle is None:
            _handle = bpy.types.SpaceView3D.draw_handler_add(draw_north_callback, (), 'WINDOW', 'POST_VIEW')
        elif _handle is not None:
            bpy.types.SpaceView3D.draw_handler_remove(_handle, 'WINDOW')
            _handle = None
        context.area.tag_redraw()