yta-editor-nodes-gpu 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,231 @@
1
+ from yta_editor_nodes_gpu.processor import _NodeProcessorGPU
2
+ from typing import Union
3
+
4
+ import moderngl
5
+
6
+
7
+ class _VideoNodeProcessorGPU(_NodeProcessorGPU):
8
+ """
9
+ *Abstract class*
10
+
11
+ *Singleton class*
12
+
13
+ *For internal use only*
14
+
15
+ Class to represent a node processor that uses GPU
16
+ to transform the input but it is meant to video
17
+ processing, so it implements a 'time' parameter to
18
+ manipulate the frames according to that time
19
+ moment.
20
+
21
+ This class must be implemented by any processor
22
+ that uses GPU to modify an input.
23
+ """
24
+
25
+ def __init__(
26
+ self,
27
+ opengl_context: Union[moderngl.Context, None],
28
+ output_size: tuple[int, int],
29
+ **kwargs
30
+ ):
31
+ """
32
+ Provide all the variables you want to be initialized
33
+ as uniforms at the begining for the global OpenGL
34
+ animation in the `**kwargs`.
35
+
36
+ The `output_size` is the size (width, height) of the
37
+ texture that will be obtained as result. This size
38
+ can be modified when processing a specific input, but
39
+ be consider the cost of resources of modifying the
40
+ size, that will regenerate the output texture.
41
+ """
42
+ super().__init__(
43
+ opengl_context = opengl_context,
44
+ output_size = output_size,
45
+ **kwargs
46
+ )
47
+
48
+ def __reinit__(
49
+ self,
50
+ opengl_context: Union[moderngl.Context, None] = None,
51
+ output_size: Union[tuple[int, int], None] = None,
52
+ **kwargs
53
+ ):
54
+ super().__reinit__(
55
+ opengl_context = opengl_context,
56
+ output_size = output_size,
57
+ **kwargs
58
+ )
59
+
60
+ def process(
61
+ self,
62
+ input: Union[moderngl.Texture, 'np.ndarray'],
63
+ # TODO: What about this output size (?)
64
+ output_size: Union[tuple[int, int], None] = None,
65
+ t: float = 0.0,
66
+ **kwargs
67
+ # TODO: What about the output type (?)
68
+ ) -> moderngl.Texture:
69
+ """
70
+ Process the provided `input` and transform it by
71
+ using the code that is defined here according to
72
+ the `t` time moment provided.
73
+
74
+ Apply the shader to the 'input', that
75
+ must be a frame or a texture, and return
76
+ the new resulting texture.
77
+
78
+ You can provide any additional parameter
79
+ in the **kwargs, but be careful because
80
+ this could overwrite other uniforms that
81
+ were previously set.
82
+
83
+ We use and return textures to maintain
84
+ the process in GPU and optimize it.
85
+ """
86
+ return super().process(
87
+ input = input,
88
+ output_size = output_size,
89
+ time = t,
90
+ **kwargs
91
+ )
92
+
93
+ # Specific implementations here below:
94
+ class BreathingFrameVideoNodeProcessorGPU(_VideoNodeProcessorGPU):
95
+ """
96
+ The frame but as if it was breathing.
97
+ """
98
+
99
+ @property
100
+ def fragment_shader(
101
+ self
102
+ ) -> str:
103
+ return (
104
+ '''
105
+ #version 330
106
+ uniform sampler2D base_texture;
107
+ uniform float time;
108
+ uniform float zoom;
109
+ in vec2 v_uv;
110
+ out vec4 output_color;
111
+ // Use uniforms to be customizable
112
+
113
+ void main() {
114
+ // Dynamic zoom scaled with t
115
+ float scale = 1.0 + zoom * sin(time * 2.0);
116
+ vec2 center = vec2(0.5, 0.5);
117
+
118
+ // Recalculate coords according to center
119
+ vec2 uv = (v_uv - center) / scale + center;
120
+
121
+ // Clamp to avoid artifacts
122
+ uv = clamp(uv, 0.0, 1.0);
123
+
124
+ output_color = texture(base_texture, uv);
125
+ }
126
+ '''
127
+ )
128
+
129
+ def __init__(
130
+ self,
131
+ opengl_context: Union[moderngl.Context, None],
132
+ output_size: tuple[int, int],
133
+ zoom: float = 0.05
134
+ # TODO: Handle dynamic parameters with None
135
+ ):
136
+ """
137
+ Provide all the variables you want to be initialized
138
+ as uniforms at the begining for the global OpenGL
139
+ animation in the `**kwargs`.
140
+
141
+ The `output_size` is the size (width, height) of the
142
+ texture that will be obtained as result. This size
143
+ can be modified when processing a specific input, but
144
+ be consider the cost of resources of modifying the
145
+ size, that will regenerate the output texture.
146
+ """
147
+ super().__init__(
148
+ opengl_context = opengl_context,
149
+ output_size = output_size,
150
+ # Uniforms
151
+ zoom = zoom
152
+ )
153
+
154
+ def __reinit__(
155
+ self,
156
+ opengl_context: Union[moderngl.Context, None] = None,
157
+ output_size: Union[tuple[int, int], None] = None,
158
+ zoom: Union[float, None] = None
159
+ # TODO: Handle dynamic parameters with None
160
+ ):
161
+ super().__reinit__(
162
+ opengl_context = opengl_context,
163
+ output_size = output_size,
164
+ zoom = zoom
165
+ )
166
+
167
+ class WavingFramesVideoNodeProcessorGPU(_VideoNodeProcessorGPU):
168
+ """
169
+ The GPU processor of the video frames that are
170
+ transformed into a wave.
171
+
172
+ TODO: Explain this better.
173
+ """
174
+
175
+ @property
176
+ def fragment_shader(
177
+ self
178
+ ) -> str:
179
+ return (
180
+ '''
181
+ #version 330
182
+ uniform sampler2D base_texture;
183
+ uniform float time;
184
+ uniform float amplitude;
185
+ uniform float frequency;
186
+ uniform float speed;
187
+ in vec2 v_uv;
188
+ out vec4 output_color;
189
+
190
+ void main() {
191
+ float wave = sin(v_uv.x * frequency + time * speed) * amplitude;
192
+ vec2 uv = vec2(v_uv.x, v_uv.y + wave);
193
+ output_color = texture(base_texture, uv);
194
+ }
195
+ '''
196
+ )
197
+
198
+ def __init__(
199
+ self,
200
+ opengl_context: Union[moderngl.Context, None],
201
+ output_size: tuple[int, int],
202
+ # Uniforms
203
+ amplitude: float = 0.05,
204
+ frequency: float = 10.0,
205
+ speed: float = 2.0
206
+ ):
207
+ super().__init__(
208
+ opengl_context = opengl_context,
209
+ output_size = output_size,
210
+ # Uniforms
211
+ amplitude = amplitude,
212
+ frequency = frequency,
213
+ speed = speed
214
+ )
215
+
216
+ def __reinit__(
217
+ self,
218
+ opengl_context: Union[moderngl.Context, None] = None,
219
+ output_size: Union[tuple[int, int], None] = None,
220
+ # Uniforms
221
+ amplitude: Union[float, None] = None,
222
+ frequency: Union[float, None] = None,
223
+ speed: Union[float, None] = None
224
+ ):
225
+ super().__reinit__(
226
+ opengl_context = opengl_context,
227
+ output_size = output_size,
228
+ amplitude = amplitude,
229
+ frequency = frequency,
230
+ speed = speed
231
+ )