File: picking.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (60 lines) | stat: -rw-r--r-- 1,537 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

import struct

from .base_filter import Filter


class PickingFilter(Filter):
    """Filter used to color visuals by a picking ID.

    Note that the ID color uses the alpha channel, so this may not be used
    with blending enabled.
    """

    FRAG_SHADER = """
        void picking_filter() {
            if( $enabled == 0 )
                return;
            if( gl_FragColor.a == 0.0 )
                discard;
            gl_FragColor = $id_color;
        }
    """

    def __init__(self, id_=None):
        super(PickingFilter, self).__init__(fcode=self.FRAG_SHADER, fpos=10)

        self.id = id_
        self.enabled = False

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, id):
        if id < 1:
            raise ValueError('Picking ID must be integer > 0.')
        id_color = struct.unpack('<4B', struct.pack('<I', id))
        self.fshader['id_color'] = [x/255. for x in id_color]
        self._id = id
        self._id_color = id_color

    @property
    def enabled(self):
        return self._enabled

    @enabled.setter
    def enabled(self, e):
        self._enabled = e
        self.fshader['enabled'] = 1 if e is True else 0

    @property
    def color(self):
        """The RGBA color that will be drawn to the framebuffer for visuals
        that use this filter.
        """
        return self._id_color