-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Scene.py
836 lines (684 loc) · 31.4 KB
/
Scene.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
import ShaderFlow
from . import *
class ShaderBackend(BrokenEnum):
Headless = "headless"
GLFW = "glfw"
@define
class ShaderScene(ShaderModule):
__name__ = "Scene"
modules: Deque[ShaderModule] = Factory(deque)
"""Deque of all Modules on the Scene, not a set for order preservation"""
"""
Implementing Fractional SSAA is a bit tricky:
• A Window's FBO always match its real resolution (can't final render in other resolution)
• We need a final shader to sample from some other SSAA-ed texture to the window
For that, a internal self._final is used to sample from the user's main self.engine
• _final: Uses the FBO of the Window, simply samples from a `final` texture to the screen
• engine: Scene's main engine, where the user's final shader is rendered to
"""
_final: Shader = None
shader: Shader = None
camera: ShaderCamera = None
keyboard: ShaderKeyboard = None
def build(self):
# Init Imgui
imgui.create_context()
self.imguio = imgui.get_io()
self.imguio.font_global_scale = SHADERFLOW.CONFIG.imgui.default("font_scale", 1.0)
self.imguio.fonts.add_font_from_file_ttf(
str(BROKEN.RESOURCES.FONTS/"DejaVuSans.ttf"),
16*self.imguio.font_global_scale,
)
# Default modules
self.init_window()
log.info(f"{self.who} Adding default base Scene modules")
ShaderFrametimer(self)
self.keyboard = ShaderKeyboard(scene=self)
self.camera = ShaderCamera(scene=self)
# Create the SSAA Workaround engines
log.info(f"{self.who} Creating SSAA Implementation")
self.shader = Shader(self)
self.shader.texture.name = "iScreen"
self.shader.texture.track = True
self._final = Shader(self)
self._final.texture.components = 3
self._final.texture.final = True
self._final.texture.dtype = "f1"
self._final.texture.track = True
self._final.fragment = (SHADERFLOW.RESOURCES.FRAGMENT/"Final.glsl")
# ---------------------------------------------------------------------------------------------|
# Basic information
time: Seconds = 0.0
duration: Seconds = 10.0
time_scale: float = Factory(lambda: DynamicNumber(value=1, frequency=5))
frame: int = 0
fps: Hertz = 60.0
dt: Seconds = 0.0
rdt: Seconds = 0.0
# Base classes and utils for a Scene
eloop: BrokenEventLoop = Factory(BrokenEventLoop)
vsync: BrokenEventClient = None
ffmpeg: BrokenFFmpeg = None
@property
def frametime(self) -> Seconds:
return 1/self.fps
# # Title
_title: str = "ShaderFlow"
@property
def title(self) -> str:
return self._title
@title.setter
def title(self, value: str) -> None:
log.info(f"{self.who} Changing Window Title to ({value})")
self.window.title = value
self._title = value
# # Resizable
_resizable: bool = True
@property
def resizable(self) -> bool:
return self._resizable
@resizable.setter
def resizable(self, value: bool) -> None:
log.info(f"{self.who} Changing Window Resizable to ({value})")
self.window.resizable = value
self._resizable = value
if (self._backend == ShaderBackend.GLFW):
glfw.set_window_attrib(self.window._window, glfw.RESIZABLE, value)
# # Visible
_visible: bool = False
@property
def visible(self) -> bool:
return self._visible
@visible.setter
def visible(self, value: bool) -> None:
log.info(f"{self.who} Changing Window Visibility to ({value})")
self.window.visible = value
self._visible = value
# # Resolution
quality: float = Field(default=75, converter=lambda x: clamp(x, 0, 100))
_width: Pixel = 1920
_height: Pixel = 1080
_ssaa: float = 1.0
def resize(self, width: Pixel=Unchanged, height: Pixel=Unchanged) -> None:
self._width, self._height = BrokenUtils.round_resolution(width, height)
log.info(f"{self.who} Resizing window to resolution {self.resolution}")
self.opengl.screen.viewport = (0, 0, self.width, self.height)
self.window.size = self.resolution
def read_screen(self) -> bytes:
return self.opengl.screen.read(viewport=(0, 0, self.width, self.height), components=3)
# # Resolution related
@property
def width(self) -> Pixel:
return self._width
@width.setter
def width(self, value: Pixel) -> None:
self.resize(width=value)
@property
def height(self) -> Pixel:
return self._height
@height.setter
def height(self, value: Pixel) -> None:
self.resize(height=value)
@property
def resolution(self) -> Tuple[Pixel, Pixel]:
return self.width, self.height
@resolution.setter
def resolution(self, value: Tuple[Pixel, Pixel]) -> None:
self.resize(*value)
@property
def ssaa(self) -> float:
return self._ssaa
@ssaa.setter
def ssaa(self, value: float) -> None:
log.info(f"{self.who} Changing Fractional SSAA to {value}")
self._ssaa = value
self.relay(Message.Shader.RecreateTextures)
@property
def render_resolution(self) -> Tuple[Pixel, Pixel]:
return BrokenUtils.round_resolution(self.width*self.ssaa, self.height*self.ssaa)
@property
def aspect_ratio(self) -> float:
return self.width / self.height
# # Window Fullscreen
_fullscreen: bool = False
@property
def fullscreen(self) -> bool:
return self._fullscreen
@fullscreen.setter
def fullscreen(self, value: bool) -> None:
log.info(f"{self.who} Changing Window Fullscreen to ({value})")
self._fullscreen = value
try:
self.window.fullscreen = value
except AttributeError:
pass
# # Window Vsync
_window_vsync: bool = False
@property
def window_vsync(self) -> bool:
return self._window_vsync
@window_vsync.setter
def window_vsync(self, value: bool) -> None:
log.info(f"{self.who} Changing Window Native Vsync to ({value})")
self._window_vsync = value
self.window.vsync = value
# # Window Exclusive
_exclusive: bool = False
@property
def exclusive(self) -> bool:
return self._exclusive
@exclusive.setter
def exclusive(self, value: bool) -> None:
log.info(f"{self.who} Changing Window Exclusive to ({value})")
self.window.mouse_exclusivity = value
self._exclusive = value
# # Focused
@property
def focused(self) -> bool:
return glfw.get_window_attrib(self.window._window, glfw.FOCUSED)
@focused.setter
def focused(self, value: bool) -> None:
log.info(f"{self.who} Changing Window Focused to ({value})")
if value: glfw.focus_window(self.window._window)
# # Backend
_backend: ShaderBackend = ShaderBackend.GLFW
@property
def backend(self) -> ShaderBackend:
return self._backend
# Window attributes
icon: PathLike = Broken.PROJECT.RESOURCES.ICON
opengl: moderngl.Context = None
window: ModernglWindow = None
render_ui: bool = False
imgui: ModernglImgui = None
imguio: Any = None
def init_window(self) -> None:
"""Create the window and the OpenGL context"""
log.info(f"{self.who} Creating Window and OpenGL Context")
log.info(f"{self.who} • Backend: {self.backend}")
log.info(f"{self.who} • Resolution: {self.resolution}")
module = f"moderngl_window.context.{self.backend.value.lower()}"
self.window = importlib.import_module(module).Window(
size=self.resolution,
title=self.title,
resizable=self.resizable,
visible=self.visible,
fullscreen=self.fullscreen,
vsync=False if self.rendering else self.window_vsync,
)
ShaderKeyboard.set_keymap(self.window.keys)
self.imgui = ModernglImgui(self.window)
self.opengl = self.window.ctx
# Bind window events to relay
self.window.resize_func = self.__window_resize__
self.window.close_func = self.__window_close__
self.window.iconify_func = self.__window_iconify__
self.window.key_event_func = self.__window_key_event__
self.window.mouse_position_event_func = self.__window_mouse_position_event__
self.window.mouse_press_event_func = self.__window_mouse_press_event__
self.window.mouse_release_event_func = self.__window_mouse_release_event__
self.window.mouse_drag_event_func = self.__window_mouse_drag_event__
self.window.mouse_scroll_event_func = self.__window_mouse_scroll_event__
self.window.mouse_enter_event_func = self.__window_mouse_enter_event__
self.window.unicode_char_entered_func = self.__window_unicode_char_entered__
self.window.files_dropped_event_func = self.__window_files_dropped_event__
# Workaround: Implement file dropping for GLFW and Keys, parallel icon setting
if (self._backend == ShaderBackend.GLFW):
glfw.set_drop_callback(self.window._window, self.__window_files_dropped_event__)
BrokenThread.new(target=self.window.set_icon, icon_path=self.icon)
ShaderKeyboard.Keys.LEFT_SHIFT = glfw.KEY_LEFT_SHIFT
ShaderKeyboard.Keys.LEFT_CTRL = glfw.KEY_LEFT_CONTROL
ShaderKeyboard.Keys.LEFT_ALT = glfw.KEY_LEFT_ALT
glfw.maximize_window(self.window._window)
log.debug(f"{self.who} Finished Window creation")
# ---------------------------------------------------------------------------------------------|
# Module
def handle(self, message: Message) -> None:
if isinstance(message, Message.Window.Close):
log.info(f"{self.who} Received Window Close Event")
self.quit()
elif isinstance(message, Message.Keyboard.KeyDown):
if message.key == ShaderKeyboard.Keys.TAB:
log.info(f"{self.who} (TAB) Toggling Menu")
self.render_ui = not self.render_ui
elif message.key == ShaderKeyboard.Keys.F1:
log.info(f"{self.who} ( F1) Toggling Exclusive Mode")
self.exclusive = not self.exclusive
elif message.key == ShaderKeyboard.Keys.F2:
import arrow
time = arrow.now().format("YYYY-MM-DD_HH-mm-ss")
image = PIL.Image.frombytes("RGB", self.resolution, self.read_screen())
image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
path = Broken.PROJECT.DIRECTORIES.SCREENSHOTS/f"({time}) {self.__name__}.jpg"
BrokenThread.new(target=image.save, fp=path, mode="JPEG", quality=95)
log.minor(f"{self.who} ( F2) Saved Screenshot to ({path})")
elif message.key == ShaderKeyboard.Keys.F11:
log.info(f"{self.who} (F11) Toggling Fullscreen")
self.fullscreen = not self.fullscreen
elif isinstance(message, (Message.Mouse.Drag, Message.Mouse.Position)):
self.mouse_gluv = (message.u, message.v)
def pipeline(self) -> Iterable[ShaderVariable]:
yield ShaderVariable("uniform", "float", "iTime", self.time)
yield ShaderVariable("uniform", "float", "iDuration", self.duration)
yield ShaderVariable("uniform", "float", "iDeltaTime", self.dt)
yield ShaderVariable("uniform", "vec2", "iResolution", self.resolution)
yield ShaderVariable("uniform", "float", "iQuality", self.quality/100)
yield ShaderVariable("uniform", "float", "iSSAA", self.ssaa)
yield ShaderVariable("uniform", "float", "iFrameRate", self.fps)
yield ShaderVariable("uniform", "int", "iFrame", self.frame)
yield ShaderVariable("uniform", "bool", "iRealtime", self.realtime)
yield ShaderVariable("uniform", "vec2", "iMouse", self.mouse_gluv)
yield ShaderVariable("uniform", "bool", "iMouseInside", self.mouse_inside)
for i in range(1, 6):
yield ShaderVariable("uniform", "bool", f"iMouse{i}", self.mouse_buttons[i])
# Todo: Move to a Utils class for other stuff such as theming?
# Fixme: Move to somewhere better
def _render_ui(self):
if not self.render_ui:
return
self._final.texture.fbo().use()
imgui.push_style_var(imgui.STYLE_WINDOW_BORDERSIZE, 0.0)
imgui.push_style_var(imgui.STYLE_WINDOW_ROUNDING, 8)
imgui.push_style_var(imgui.STYLE_TAB_ROUNDING, 8)
imgui.push_style_var(imgui.STYLE_GRAB_ROUNDING, 8)
imgui.push_style_var(imgui.STYLE_FRAME_ROUNDING, 8)
imgui.push_style_var(imgui.STYLE_CHILD_ROUNDING, 8)
imgui.push_style_color(imgui.COLOR_FRAME_BACKGROUND, 0.1, 0.1, 0.1, 0.5)
imgui.new_frame()
imgui.set_next_window_position(0, 0)
imgui.set_next_window_bg_alpha(0.6)
imgui.begin(f"{self.__name__}", False, imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_COLLAPSE | imgui.WINDOW_ALWAYS_AUTO_RESIZE)
# Render every module
for module in self.modules:
if imgui.tree_node(f"{module.uuid:>2} - {type(module).__name__.replace('ShaderFlow', '')}", imgui.TREE_NODE_BULLET):
imgui.separator()
module.__shaderflow_ui__()
imgui.separator()
imgui.spacing()
imgui.tree_pop()
imgui.end()
imgui.pop_style_color()
imgui.pop_style_var(6)
imgui.render()
self.imgui.render(imgui.get_draw_data())
def next(self, dt: float) -> Self:
# Limit maximum deltatime for framespikes or events catching up
dt = min(dt, 1)
# Temporal
self.time_scale.next(dt=abs(dt))
self.time += dt * self.time_scale
self.dt = dt * self.time_scale
self.rdt = dt
self.frame = int(self.time * self.fps)
self.vsync.fps = self.fps
# Update modules in reverse order of addition
# Note: Non-engine first as pipeline might change
for module in (self.modules):
if not isinstance(module, Shader):
module.update()
for module in (self.modules):
if isinstance(module, Shader):
module.update()
self._render_ui()
# Fixme: https://github.com/glfw/glfw/pull/1426
if not self.headless:
if self.backend == ShaderBackend.GLFW:
glfw.poll_events()
glfw.swap_buffers(self.window._window)
else:
self.window.swap_buffers()
return self
def __ui__(self) -> None:
# Render status
imgui.text(f"Resolution: {self.render_resolution} -> {self.resolution} @ {self.ssaa}x SSAA")
# Framerate
imgui.spacing()
if (state := imgui.slider_float("Framerate", self.fps, 10, 240, "%.0f"))[0]:
self.fps = round(state[1])
for fps in (options := [24, 30, 60, 120, 144, 240]):
if (state := imgui.button(f"{fps} Hz")):
self.fps = fps
if fps != options[-1]:
imgui.same_line()
# Temporal
imgui.spacing()
if (state := imgui.slider_float("Time Scale", self.time_scale.target, -2, 2, "%.2f"))[0]:
self.time_scale.target = state[1]
for scale in (options := [-10, -5, -2, -1, 0, 1, 2, 5, 10]):
if (state := imgui.button(f"{scale}x")):
self.time_scale.target = scale
if scale != options[-1]:
imgui.same_line()
# SSAA
imgui.spacing()
if (state := imgui.slider_float("SSAA", self.ssaa, 0.01, 2, "%.2f"))[0]:
self.ssaa = state[1]
for ssaa in (options := [0.1, 0.25, 0.5, 1.0, 1.25, 1.5, 2.0]):
if (state := imgui.button(f"{ssaa}x")):
self.ssaa = ssaa
if ssaa != options[-1]:
imgui.same_line()
# Quality
imgui.spacing()
if (state := imgui.slider_float("Quality", self.quality, 0, 100, "%.0f%%"))[0]:
self.quality = state[1]
# ---------------------------------------------------------------------------------------------|
# User actions
@abstractmethod
def commands(self) -> None:
"""
Let the user configure commands for the Scene
"""
...
# Workaround: Kill CFFI and Processes binaries on Windows.. PowerShell stupidly hangs..
def _exit_hook(self):
try:
getattr(self.window, "destroy", Ignore())()
if isinstance(self.ffmpeg, BrokenFFmpegPopenBuffered):
self.ffmpeg.close()
except Exception:
pass
_built: SameTracker = Factory(SameTracker)
def cli(self, *args: List[str]):
args = flatten(args)
self.broken_typer = BrokenTyper(chain=True, exit_hook=self._exit_hook)
self.broken_typer.command(self.main, context=True, default=True)
self.broken_typer.command(self.settings, context=True)
self.commands()
if ("--help" not in args) and (not self._built(True)):
self.build()
self.broken_typer(args)
@abstractmethod
def settings(self):
"""Optional scene settings to be configured on the CLI"""
pass
_quit: bool = False
rendering: bool = False
realtime: bool = False
headless: bool = False
def quit(self) -> None:
if self.realtime:
self._quit = True
@property
def directory(self) -> Path:
"""Directory of the current Scene script"""
# Fixme: How to deal with ShaderFlow as a dependency scenario?
return BrokenPath(SHADERFLOW.DIRECTORIES.CURRENT_SCENE)
def read_file(self, file: Path, bytes: bool=False) -> str | bytes:
"""
Read a file relative to the current Scene Python script
Args:
`file`: File to read, relative to the current Scene script directory
`bytes`: Whether to read the file as bytes, defaults to text
Returns:
File contents as text or bytes
"""
file = (self.directory/file)
log.info(f"{self.who} Reading file ({file})")
return LoaderBytes(file) if bytes else LoaderString(file)
def main(self,
width: Annotated[int, TyperOption("--width", "-w", help="(🌵 Basic ) Width of the Rendering Resolution")]=1920,
height: Annotated[int, TyperOption("--height", "-h", help="(🌵 Basic ) Height of the Rendering Resolution")]=1080,
fps: Annotated[float, TyperOption("--fps", "-f", help="(🌵 Basic ) Target Frames per Second (Exact when Exporting)")]=60,
fullscreen: Annotated[bool, TyperOption("--fullscreen", help="(🌵 Basic ) Start the Real Time Window in Fullscreen Mode")]=False,
benchmark: Annotated[bool, TyperOption("--benchmark", "-b", help="(🌵 Basic ) Benchmark the Scene's speed on raw rendering")]=False,
scale: Annotated[float, TyperOption("--scale", "-x", help="(💎 Quality ) Pre-multiply Width and Height by a Scale Factor")]=1.0,
quality: Annotated[float, TyperOption("--quality", "-q", help="(💎 Quality ) Shader Quality level if supported (0-100%)")]=80,
ssaa: Annotated[float, TyperOption("--ssaa", "-s", help="(💎 Quality ) Fractional Super Sampling Anti Aliasing factor, O(N²) GPU cost")]=1.0,
render: Annotated[bool, TyperOption("--render", "-r", help="(📦 Exporting) Export the current Scene to a Video File defined on --output")]=False,
output: Annotated[str, TyperOption("--output", "-o", help="(📦 Exporting) Output File Name: Absolute, Relative Path or Plain Name. Saved on ($DATA/$(plain_name or $scene-$date))")]=None,
format: Annotated[str, TyperOption("--format", help="(📦 Exporting) Output Video Container (mp4, mkv, webm, avi..), overrides --output one")]="mp4",
time: Annotated[float, TyperOption("--time-end", "-t", help="(📦 Exporting) How many seconds to render, defaults to 10 or longest Audio")]=None,
raw: Annotated[bool, TyperOption("--raw", help="(📦 Exporting) Send raw OpenGL Frames before GPU SSAA to FFmpeg (Enabled if SSAA < 1)")]=False,
open: Annotated[bool, TyperOption("--open", help="(📦 Exporting) Open the Video's Output Directory after render finishes")]=False,
) -> Optional[Path]:
self.relay(Message.Shader.ReloadShaders)
# Note: Implicit render mode if output is provided or benchmark
render = render or benchmark or bool(output)
output_resolution = (width*scale, height*scale)
# Set useful state flags
self.realtime = not render
self.rendering = render
self.benchmark = benchmark
self.headless = (self.rendering or self.benchmark)
# Window configuration based on launch mode
self.resolution = output_resolution
self.resizable = not self.rendering
self.visible = not self.headless
self.ssaa = ssaa
self.quality = quality
self.fps = fps
self.time = 0
self.duration = 0
self.fullscreen = fullscreen
self.title = f"ShaderFlow | {self.__name__}"
# When rendering, let FFmpeg apply the SSAA, I trust it more (higher quality?)
if self.rendering and (raw or self.ssaa < 1):
self.resolution = self.render_resolution
self.ssaa = 1
# Create the Vsync event loop
self.vsync = self.eloop.new(
callback=self.next,
frequency=self.fps,
decoupled=self.rendering,
precise=True,
)
# Setup
log.info(f"{self.who} Setting up Modules")
for module in self.modules:
module.setup()
# Find the longest audio duration or set duration
for module in (not bool(time)) * self.rendering * list(self.find(ShaderAudio)):
self.duration = max(self.duration, module.duration or 0)
else:
self.duration = self.duration or time or 10
if self.rendering:
import arrow
# Get video output path - if not absolute, save to data directory
output = Path(output or f"({arrow.utcnow().format('YYYY-MM-DD_HH-mm-ss')}) {self.__name__}")
output = output if output.is_absolute() else Broken.PROJECT.DIRECTORIES.DATA/output
output = output.with_suffix(output.suffix or f".{format}")
# Create FFmpeg process
self.ffmpeg = (
BrokenFFmpeg()
.quiet()
.overwrite()
.hwaccel(FFmpegHWAccel.Auto)
.format(FFmpegFormat.Rawvideo)
.pixel_format(FFmpegPixelFormat.RGB24)
.resolution(self.resolution)
.framerate(self.fps)
.filter(FFmpegFilterFactory.scale(output_resolution))
.filter(FFmpegFilterFactory.flip_vertical())
.input("-")
)
# Fixme: Is this the correct point for modules to manage FFmpeg?
for module in self.modules:
module.ffmpeg(self.ffmpeg)
# Add empty audio track if no input audio
# self.ffmpeg = (
# self.ffmpeg
# .custom("-f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100".split())
# .shortest()
# )
# Todo: Apply preset based config
self.ffmpeg = (
self.ffmpeg
.video_codec(FFmpegVideoCodec.H264)
.audio_codec(FFmpegAudioCodec.AAC)
.audio_bitrate("300k")
.preset(FFmpegH264Preset.Slow)
.tune(FFmpegH264Tune.Film)
.quality(FFmpegH264Quality.High)
.pixel_format(FFmpegPixelFormat.YUV420P)
.custom("-t", self.duration)
.custom("-movflags", "+faststart")
)
# Add output video
self.ffmpeg.output(output)
if not benchmark:
self.ffmpeg = self.ffmpeg.pipe()
# Add progress bar
progress_bar = tqdm.tqdm(
total=int(self.duration*self.fps),
desc=f"Scene ({type(self).__name__}) → Video",
dynamic_ncols=True,
colour="#43BFEF",
leave=False,
unit=" Frames",
mininterval=1/60,
maxinterval=0.1,
smoothing=0.1,
)
import time
# Benchmark and stats data
RenderStatus = DotMap(
render_start=time.perf_counter(),
total_frames=0,
)
# Main rendering loop
while (self.rendering) or (not self._quit):
# Keep calling event loop until self was updated
if (call := self.eloop.next().output) is not self:
continue
if not self.rendering:
continue
# Rendering logic
progress_bar.update(1)
RenderStatus.total_frames += 1
# Write new frame to FFmpeg
if not self.benchmark:
self.ffmpeg.write(self._final.texture.fbo().read())
# Render until time and end are Close
if (self.duration - self.time) > 1.5*self.frametime:
continue
if not self.benchmark:
self.ffmpeg.close()
# Log stats
progress_bar.refresh()
progress_bar.close()
RenderStatus.took = time.perf_counter() - RenderStatus.render_start
log.info(f"Finished rendering ({output})", echo=not self.benchmark)
log.info((
f"• Stats: "
f"(Took {RenderStatus.took:.2f} s) at "
f"({self.frame/RenderStatus.took:.2f} FPS | "
f"{self.duration/RenderStatus.took:.2f} x Realtime) with "
f"({RenderStatus.total_frames} Total Frames)"
))
if self.benchmark:
return
# Open output directory
if open: BrokenPath.open_in_file_explorer(output.parent)
break
return output
# # Window related events
def __window_resize__(self, width: int, height: int) -> None:
width, height = max(10, width), max(10, height)
self.imgui.resize(width, height)
self._width, self._height = width, height
self.relay(Message.Window.Resize(width=width, height=height))
self.relay(Message.Shader.Render)
def __window_close__(self) -> None:
self.relay(Message.Window.Close())
def __window_iconify__(self, state: bool) -> None:
self.relay(Message.Window.Iconify(state=state))
def __window_files_dropped_event__(self, *stuff: list[str]) -> None:
self.relay(Message.Window.FileDrop(files=stuff[1]))
# # Keyboard related events
def __window_key_event__(self, key: int, action: int, modifiers: int) -> None:
self.imgui.key_event(key, action, modifiers)
if self.imguio.want_capture_keyboard and self.render_ui:
return
if action == ShaderKeyboard.Keys.ACTION_PRESS:
self.relay(Message.Keyboard.KeyDown(key=key, modifiers=modifiers))
elif action == ShaderKeyboard.Keys.ACTION_RELEASE:
self.relay(Message.Keyboard.KeyUp(key=key, modifiers=modifiers))
self.relay(Message.Keyboard.Press(key=key, action=action, modifiers=modifiers))
def __window_unicode_char_entered__(self, char: str) -> None:
if self.imguio.want_capture_keyboard and self.render_ui:
return
self.relay(Message.Keyboard.Unicode(char=char))
# # Mouse related events
mouse_gluv: Tuple[float, float] = Factory(lambda: (0, 0))
def __xy2uv__(self, x: int=0, y: int=0) -> dict[str, float]:
"""Convert a XY pixel coordinate into a Center-UV normalized coordinate"""
return dict(
u=2*(x/self.width - 0.5),
v=2*(y/self.height - 0.5)*(-1),
x=x, y=y,
)
def __dxdy2dudv__(self, dx: int=0, dy: int=0) -> dict[str, float]:
"""Convert a dx dy pixel coordinate into a Center-UV normalized coordinate"""
return dict(
du=2*(dx/self.width)*self.aspect_ratio,
dv=2*(dy/self.height)*(-1),
dx=dx, dy=dy,
)
mouse_buttons: Dict[int, bool] = Factory(lambda: {k:0 for k in range(1, 6)})
def __window_mouse_press_event__(self, x: int, y: int, button: int) -> None:
self.imgui.mouse_press_event(x, y, button)
if self.imguio.want_capture_mouse and self.render_ui:
return
self.mouse_buttons[button] = True
self.relay(Message.Mouse.Press(
**self.__xy2uv__(x, y),
button=button
))
def __window_mouse_release_event__(self, x: int, y: int, button: int) -> None:
self.imgui.mouse_release_event(x, y, button)
if self.imguio.want_capture_mouse and self.render_ui:
return
self.mouse_buttons[button] = False
self.relay(Message.Mouse.Release(
**self.__xy2uv__(x, y),
button=button
))
mouse_inside: bool = False
def __window_mouse_enter_event__(self, inside: bool) -> None:
self.mouse_inside = inside
self.relay(Message.Mouse.Enter(state=inside))
def __window_mouse_scroll_event__(self, dx: int, dy: int) -> None:
self.imgui.mouse_scroll_event(dx, dy)
if self.imguio.want_capture_mouse and self.render_ui:
return
elif self.keyboard(ShaderKeyboard.Keys.LEFT_ALT):
self.time_scale.target += (dy)*0.2
return
self.relay(Message.Mouse.Scroll(
**self.__dxdy2dudv__(dx=dx, dy=dy)
))
def __window_mouse_position_event__(self, x: int, y: int, dx: int, dy: int) -> None:
self.imgui.mouse_position_event(x, y, dx, dy)
if self.imguio.want_capture_mouse and self.render_ui:
return
self.relay(Message.Mouse.Position(
**self.__dxdy2dudv__(dx=dx, dy=dy),
**self.__xy2uv__(x=x, y=y)
))
_mouse_drag_time_factor: float = 4
"""How much seconds to scroll in time when the mouse moves the full window height"""
def __window_mouse_drag_event__(self, x: int, y: int, dx: int, dy: int) -> None:
self.imgui.mouse_drag_event(x, y, dx, dy)
if self.imguio.want_capture_mouse and self.render_ui:
return
# Rotate the camera on Shift
if self.keyboard(ShaderKeyboard.Keys.LEFT_CTRL):
cx, cy = (x-self.width/2), (y-self.height/2)
angle = math.atan2(cy+dy, cx+dx) - math.atan2(cy, cx)
if (abs(angle) > math.pi): angle -= 2*math.pi
self.camera.rotate(self.camera.base_z, angle=math.degrees(angle))
return
elif self.exclusive:
self.camera.apply_zoom(-dy/500)
self.camera.rotate(self.camera.base_z, angle=-dx/10)
return
# Time Travel on Alt
elif self.keyboard(ShaderKeyboard.Keys.LEFT_ALT):
self.time -= self._mouse_drag_time_factor * (dy/self.height)
return
self.relay(Message.Mouse.Drag(
**self.__dxdy2dudv__(dx=dx, dy=dy),
**self.__xy2uv__(x=x, y=y)
))
ShaderFlow.ShaderScene = ShaderScene