]> git.sesse.net Git - ultimatescore/blob - nageru/ultimate.lua
Name the cameras.
[ultimatescore] / nageru / ultimate.lua
1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
2
3 local transition_start = -2.0
4 local transition_end = -1.0
5 local transition_type = 0
6 local transition_src_signal = 0
7 local transition_dst_signal = 0
8
9 local neutral_colors = {
10         {0.5, 0.5, 0.5},  -- Input 0.
11         {0.5, 0.5, 0.5},  -- Input 1.
12         {0.5, 0.5, 0.5}   -- Input 2.
13 }
14
15 local overlay_transition_start = -2.0
16 local overlay_transition_end = -1.0
17 local overlay_alpha_src = 0.0
18 local overlay_alpha_dst = 1.0
19 local overlay_enabled = false
20
21 local live_signal_num = 0
22 local preview_signal_num = 1
23
24 -- Valid values for live_signal_num and preview_signal_num.
25 local INPUT0_SIGNAL_NUM = 0
26 local INPUT1_SIGNAL_NUM = 1
27 local INPUT2_SIGNAL_NUM = 2
28 local STATIC_SIGNAL_NUM = 3
29
30 -- Preview-only signal showing the current signal with the overlay.
31 -- Not valid for live_signal_num!
32 local OVERLAY_SIGNAL_NUM = 4
33
34 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
35 local NO_TRANSITION = 0
36 local FADE_TRANSITION = 2
37
38 -- Last width/height/frame rate for each channel, if we have it.
39 -- Note that unlike the values we get from Nageru, the resolution is per
40 -- frame and not per field, since we deinterlace.
41 local last_resolution = {}
42
43 local caspar_input = VideoInput.new("unix:///tmp/caspar.sock")
44 caspar_input:change_rate(2.0)
45
46 -- Utility function to help creating many similar chains that can differ
47 -- in a free set of chosen parameters.
48 function make_cartesian_product(parms, callback)
49         return make_cartesian_product_internal(parms, callback, 1, {})
50 end
51
52 function make_cartesian_product_internal(parms, callback, index, args)
53         if index > #parms then
54                 return callback(unpack(args))
55         end
56         local ret = {}
57         for _, value in ipairs(parms[index]) do
58                 args[index] = value
59                 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
60         end
61         return ret
62 end
63
64 -- An overlay with variable alpha.
65 function make_overlay(chain, base)
66         local image = chain:add_video_input(caspar_input, false)
67         local multiply_effect = chain:add_effect(MultiplyEffect.new())
68         local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
69         return {
70                 image = image,
71                 multiply_effect = multiply_effect,
72                 overlay_effect = overlay_effect
73         }
74 end
75
76 function possibly_make_overlay(has_overlay, chain, base)
77         if has_overlay == true then
78                 return make_overlay(chain, base)
79         else
80                 return nil
81         end
82 end
83
84 function make_fade_input(chain, signal, live, deint, scale)
85         local input, wb_effect, resample_effect, last
86         if live then
87                 input = chain:add_live_input(false, deint)
88                 input:connect_signal(signal)
89                 last = input
90         else
91                 input = chain:add_effect(ImageInput.new("tfk_pause.png"))
92                 last = input
93         end
94
95         -- If we cared about this for the non-main inputs, we would have
96         -- checked hq here and invoked ResizeEffect instead.
97         if scale then
98                 resample_effect = chain:add_effect(ResampleEffect.new())
99                 last = resample_effect
100         end
101
102         -- Make sure to put the white balance after the scaling (usually more efficient).
103         if live then
104                 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
105                 last = wb_effect
106         end
107
108         return {
109                 input = input,
110                 wb_effect = wb_effect,
111                 resample_effect = resample_effect,
112                 last = last
113         }
114 end
115
116 -- A chain to fade between two inputs, of which either can be a picture
117 -- or a live input. In practice only used live, but we still support the
118 -- hq parameter.
119 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
120         local chain = EffectChain.new(16, 9)
121
122         local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
123         local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
124
125         local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
126         local overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
127
128         chain:finalize(hq)
129
130         return {
131                 chain = chain,
132                 input0 = input0,
133                 input1 = input1,
134                 mix_effect = mix_effect,
135                 overlay = overlay
136         }
137 end
138
139 -- Chains to fade between two inputs, in various configurations.
140 local fade_chains = make_cartesian_product({
141         {"static", "live", "livedeint"},  -- input0_type
142         {true, false},                    -- input0_scale
143         {"static", "live", "livedeint"},  -- input1_type
144         {true, false},                    -- input1_scale
145         {true, false},                    -- has_overlay
146         {true}                            -- hq
147 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
148         local input0_live = (input0_type ~= "static")
149         local input1_live = (input1_type ~= "static")
150         local input0_deint = (input0_type == "livedeint")
151         local input1_deint = (input1_type == "livedeint")
152         return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
153 end)
154
155 -- A chain to show a single input on screen.
156 function make_simple_chain(input_deint, input_scale, has_overlay, hq)
157         local chain = EffectChain.new(16, 9)
158
159         local input = chain:add_live_input(false, input_deint)
160         input:connect_signal(0)  -- First input card. Can be changed whenever you want.
161
162         local resample_effect, resize_effect
163         if scale then
164                 if hq then
165                         resample_effect = chain:add_effect(ResampleEffect.new())
166                 else
167                         resize_effect = chain:add_effect(ResizeEffect.new())
168                 end
169         end
170
171         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
172         local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
173
174         chain:finalize(hq)
175
176         return {
177                 chain = chain,
178                 input = input,
179                 wb_effect = wb_effect,
180                 resample_effect = resample_effect,
181                 resize_effect = resize_effect,
182                 overlay = overlay
183         }
184 end
185
186 -- Make all possible combinations of single-input chains.
187 local simple_chains = make_cartesian_product({
188         {"live", "livedeint"},  -- input_type
189         {true, false},          -- input_scale
190         {true, false},          -- has_overlay
191         {true, false}           -- hq
192 }, function(input_type, input_scale, has_overlay, hq)
193         local input_deint = (input_type == "livedeint")
194         return make_simple_chain(input_deint, input_scale, has_overlay, hq)
195 end)
196
197 -- A chain to show a single static picture on screen.
198 local static_chains = make_cartesian_product({
199         {true, false},           -- has_overlay
200         {true, false}            -- hq
201 }, function(has_overlay, hq)
202         local chain = EffectChain.new(16, 9)
203         local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
204         local overlay = possibly_make_overlay(has_overlay, chain, chain_input)
205
206         chain:finalize(hq)
207         return {
208                 chain = chain,
209                 overlay = overlay
210         }
211 end)
212
213 -- A chain to show the overlay and nothing more. LQ only,
214 -- since it is not a valid live signal.
215 local overlay_chain_lq = EffectChain.new(16, 9)
216 local overlay_chain_lq_input = overlay_chain_lq:add_video_input(caspar_input, false)
217 overlay_chain_lq:finalize(false)
218
219 -- Used for indexing into the tables of chains.
220 function get_input_type(signals, signal_num)
221         if signal_num == STATIC_SIGNAL_NUM then
222                 return "static"
223         elseif signals:get_interlaced(signal_num) then
224                 return "livedeint"
225         else
226                 return "live"
227         end
228 end
229
230 function needs_scale(signals, signal_num, width, height)
231         if signal_num == STATIC_SIGNAL_NUM then
232                 -- We assume this is already correctly scaled at load time.
233                 return false
234         end
235         assert(is_plain_signal(signal_num))
236         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
237 end
238
239 function set_scale_parameters_if_needed(chain_or_input, width, height)
240         if chain_or_input.resample_effect then
241                 chain_or_input.resample_effect:set_int("width", width)
242                 chain_or_input.resample_effect:set_int("height", height)
243         elseif chain_or_input.resize_effect then
244                 chain_or_input.resize_effect:set_int("width", width)
245                 chain_or_input.resize_effect:set_int("height", height)
246         end
247 end
248
249 -- API ENTRY POINT
250 -- Returns the number of outputs in addition to the live (0) and preview (1).
251 -- Called only once, at the start of the program.
252 function num_channels()
253         return 5
254 end
255
256 function is_plain_signal(num)
257         return num >= INPUT0_SIGNAL_NUM and num <= INPUT2_SIGNAL_NUM
258 end
259
260 -- Helper function to write e.g. “720p60”. The difference between this
261 -- and get_channel_resolution_raw() is that this one also can say that
262 -- there's no signal.
263 function get_channel_resolution(signal_num)
264         res = last_resolution[signal_num]
265         if (not res) or not res.is_connected then
266                 return "disconnected"
267         end
268         if res.height <= 0 then
269                 return "no signal"
270         end
271         if not res.has_signal then
272                 if res.height == 525 then
273                         -- Special mode for the USB3 cards.
274                         return "no signal"
275                 end
276                 return get_channel_resolution_raw(res) .. ", no signal"
277         else
278                 return get_channel_resolution_raw(res)
279         end
280 end
281
282 -- Helper function to write e.g. “60” or “59.94”.
283 function get_frame_rate(res)
284         local nom = res.frame_rate_nom
285         local den = res.frame_rate_den
286         if nom % den == 0 then
287                 return nom / den
288         else
289                 return string.format("%.2f", nom / den)
290         end
291 end
292
293 -- Helper function to write e.g. “720p60”.
294 function get_channel_resolution_raw(res)
295         if res.interlaced then
296                 return res.height .. "i" .. get_frame_rate(res)
297         else
298                 return res.height .. "p" .. get_frame_rate(res)
299         end
300 end
301
302 -- API ENTRY POINT
303 -- Returns the name for each additional channel (starting from 2).
304 -- Called at the start of the program, and then each frame for live
305 -- channels in case they change resolution.
306 function channel_name(channel)
307         local signal_num = channel - 2
308         if signal_num == 0 then
309                 return "Main (" .. get_channel_resolution(signal_num) .. ")"
310         elseif signal_num == 1 then
311                 return "Goal 1 (" .. get_channel_resolution(signal_num) .. ")"
312         elseif signal_num == 2 then
313                 return "Goal 2 (" .. get_channel_resolution(signal_num) .. ")"
314         elseif signal_num == STATIC_SIGNAL_NUM then
315                 return "Static picture"
316         elseif signal_num == OVERLAY_SIGNAL_NUM then
317                 return "Overlay"
318         end
319 end
320
321 -- API ENTRY POINT
322 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
323 -- Should return -1 if the channel does not correspond to a simple signal.
324 -- (The information is used for whether right-click on the channel should bring up
325 -- an input selector or not.)
326 -- Called once for each channel, at the start of the program.
327 -- Will never be called for live (0) or preview (1).
328 function channel_signal(channel)
329         if channel == 2 then
330                 return 0
331         elseif channel == 3 then
332                 return 1
333         else
334                 return -1
335         end
336 end
337
338 -- API ENTRY POINT
339 -- Called every frame. Returns the color (if any) to paint around the given
340 -- channel. Returns a CSS color (typically to mark live and preview signals);
341 -- "transparent" is allowed.
342 -- Will never be called for live (0) or preview (1).
343 function channel_color(channel)
344         if transition_type ~= NO_TRANSITION then
345                 if channel_involved_in(channel, transition_src_signal) or
346                    channel_involved_in(channel, transition_dst_signal) then
347                         return "#f00"
348                 end
349         else
350                 if channel_involved_in(channel, live_signal_num) then
351                         return "#f00"
352                 end
353         end
354         if channel_involved_in(channel, preview_signal_num) then
355                 return "#0f0"
356         end
357         return "transparent"
358 end
359
360 function channel_involved_in(channel, signal_num)
361         if is_plain_signal(signal_num) then
362                 return channel == (signal_num + 2)
363         end
364         if signal_num == STATIC_SIGNAL_NUM then
365                 return (channel == 5)
366         end
367         return false
368 end
369
370 -- API ENTRY POINT
371 -- Returns if a given channel supports setting white balance (starting from 2).
372 -- Called only once for each channel, at the start of the program.
373 function supports_set_wb(channel)
374         return is_plain_signal(channel - 2)
375 end
376
377 -- API ENTRY POINT
378 -- Gets called with a new gray point when the white balance is changing.
379 -- The color is in linear light (not sRGB gamma).
380 function set_wb(channel, red, green, blue)
381         if is_plain_signal(channel - 2) then
382                 neutral_colors[channel - 2 + 1] = { red, green, blue }
383         end
384 end
385
386 function finish_transitions(t)
387         if transition_type ~= NO_TRANSITION and t >= transition_end then
388                 live_signal_num = transition_dst_signal
389                 transition_type = NO_TRANSITION
390         end
391
392         -- Disable the overlay if it is no longer visible.
393         if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
394                 overlay_enabled = false
395                 print("Turning off overlay")
396         end
397 end
398
399 function in_transition(t)
400         return t >= transition_start and t <= transition_end
401 end
402
403 -- API ENTRY POINT
404 -- Called every frame.
405 function get_transitions(t)
406         if preview_signal_num == OVERLAY_SIGNAL_NUM then
407                 if t < overlay_transition_end then
408                         -- Fade in progress.
409                         return {}
410                 end
411                 if overlay_enabled then
412                         return {"Overlay off", "", "Fade ovl out"}
413                 else
414                         return {"Overlay on", "", "Fade ovl in"}
415                 end
416         end
417
418         if in_transition(t) then
419                 -- Transition already in progress, the only thing we can do is really
420                 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
421                 return {"Cut"}
422         end
423
424         if live_signal_num == preview_signal_num then
425                 -- No transitions possible.
426                 return {}
427         end
428
429         if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
430            (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
431                 return {"Cut", "", "Fade"}
432         end
433
434         return {"Cut"}
435 end
436
437 function swap_preview_live()
438         local temp = live_signal_num
439         live_signal_num = preview_signal_num
440         preview_signal_num = temp
441 end
442
443 function start_transition(type_, t, duration)
444         transition_start = t
445         transition_end = t + duration
446         transition_type = type_
447         transition_src_signal = live_signal_num
448         transition_dst_signal = preview_signal_num
449         swap_preview_live()
450 end
451
452 -- API ENTRY POINT
453 -- Called when the user clicks a transition button.
454 function transition_clicked(num, t)
455         if preview_signal_num == OVERLAY_SIGNAL_NUM then
456                 if num == 0 then
457                         -- Cut.
458                         overlay_transition_start = -2.0
459                         overlay_transition_end = -1.0
460                         if overlay_enabled then
461                                 overlay_enabled = false
462                                 overlay_alpha_src = 0.0
463                                 overlay_alpha_dst = 0.0
464                         else
465                                 overlay_enabled = true
466                                 overlay_alpha_src = 1.0
467                                 overlay_alpha_dst = 1.0
468                         end
469                 elseif num == 2 then
470                         -- Fade.
471                         overlay_transition_start = t
472                         overlay_transition_end = t + 1.0
473                         if overlay_enabled then
474                                 overlay_alpha_src = 1.0
475                                 overlay_alpha_dst = 0.0
476                         else
477                                 overlay_alpha_src = 0.0
478                                 overlay_alpha_dst = 1.0
479                         end
480                         overlay_enabled = true
481                 end
482                 return
483         end
484
485         if num == 0 then
486                 -- Cut.
487                 if in_transition(t) then
488                         -- Ongoing transition; finish it immediately before the cut.
489                         finish_transitions(transition_end)
490                 end
491
492                 swap_preview_live()
493         elseif num == 1 then
494                 -- Zoom.
495                 finish_transitions(t)
496
497                 if live_signal_num == preview_signal_num then
498                         -- Nothing to do.
499                         return
500                 end
501
502                 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
503                         -- We can't zoom between these. Just make a cut.
504                         io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
505                         swap_preview_live()
506                         return
507                 end
508         elseif num == 2 then
509                 finish_transitions(t)
510
511                 -- Fade.
512                 if (live_signal_num ~= preview_signal_num) and
513                    (is_plain_signal(live_signal_num) or
514                     live_signal_num == STATIC_SIGNAL_NUM) and
515                    (is_plain_signal(preview_signal_num) or
516                     preview_signal_num == STATIC_SIGNAL_NUM) then
517                         start_transition(FADE_TRANSITION, t, 1.0)
518                 else
519                         -- Fades involving SBS are ignored (we have no chain for it).
520                 end
521         end
522 end
523
524 -- API ENTRY POINT
525 function channel_clicked(num)
526         preview_signal_num = num
527 end
528
529 function get_fade_chain(signals, t, width, height, input_resolution)
530         local input0_type = get_input_type(signals, transition_src_signal)
531         local input0_scale = needs_scale(signals, transition_src_signal, width, height)
532         local input1_type = get_input_type(signals, transition_dst_signal)
533         local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
534         local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
535         prepare = function()
536                 if input0_type == "live" or input0_type == "livedeint" then
537                         chain.input0.input:connect_signal(transition_src_signal)
538                         set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
539                 end
540                 set_scale_parameters_if_needed(chain.input0, width, height)
541                 if input1_type == "live" or input1_type == "livedeint" then
542                         chain.input1.input:connect_signal(transition_dst_signal)
543                         set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
544                 end
545                 set_scale_parameters_if_needed(chain.input1, width, height)
546                 local tt = calc_fade_progress(t, transition_start, transition_end)
547
548                 chain.mix_effect:set_float("strength_first", 1.0 - tt)
549                 chain.mix_effect:set_float("strength_second", tt)
550                 prepare_overlay_live(chain, t)
551         end
552         return chain.chain, prepare
553 end
554
555 -- API ENTRY POINT
556 -- Called every frame. Get the chain for displaying at input <num>,
557 -- where 0 is live, 1 is preview, 2 is the first channel to display
558 -- in the bottom bar, and so on up to num_channels()+1. t is the
559 -- current time in seconds. width and height are the dimensions of
560 -- the output, although you can ignore them if you don't need them
561 -- (they're useful if you want to e.g. know what to resample by).
562 --
563 -- <signals> is basically an exposed InputState, which you can use to
564 -- query for information about the signals at the point of the current
565 -- frame. In particular, you can call get_width() and get_height()
566 -- for any signal number, and use that to e.g. assist in chain selection.
567 --
568 -- You should return two objects; the chain itself, and then a
569 -- function (taking no parameters) that is run just before rendering.
570 -- The function needs to call connect_signal on any inputs, so that
571 -- it gets updated video data for the given frame. (You are allowed
572 -- to switch which input your input is getting from between frames,
573 -- but not calling connect_signal results in undefined behavior.)
574 -- If you want to change any parameters in the chain, this is also
575 -- the right place.
576 --
577 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
578 -- if and only if num==0.
579 function get_chain(num, t, width, height, signals)
580         local input_resolution = {}
581         for signal_num=0,2 do
582                 local res = {
583                         width = signals:get_width(signal_num),
584                         height = signals:get_height(signal_num),
585                         interlaced = signals:get_interlaced(signal_num),
586                         has_signal = signals:get_has_signal(signal_num),
587                         is_connected = signals:get_is_connected(signal_num),
588                         frame_rate_nom = signals:get_frame_rate_nom(signal_num),
589                         frame_rate_den = signals:get_frame_rate_den(signal_num)
590                 }
591
592                 if res.interlaced then
593                         -- Convert height from frame height to field height.
594                         -- (Needed for e.g. place_rectangle.)
595                         res.height = res.height * 2
596
597                         -- Show field rate instead of frame rate; really for cosmetics only
598                         -- (and actually contrary to EBU recommendations, although in line
599                         -- with typical user expectations).
600                         res.frame_rate_nom = res.frame_rate_nom * 2
601                 end
602
603                 input_resolution[signal_num] = res
604         end
605         last_resolution = input_resolution
606
607         if num == 0 then  -- Live.
608                 -- See if we're in a transition.
609                 finish_transitions(t)
610                 if transition_type == FADE_TRANSITION then
611                         return get_fade_chain(signals, t, width, height, input_resolution)
612                 elseif is_plain_signal(live_signal_num) then
613                         local input_type = get_input_type(signals, live_signal_num)
614                         local input_scale = needs_scale(signals, live_signal_num, width, height)
615                         local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
616                         prepare = function()
617                                 chain.input:connect_signal(live_signal_num)
618                                 set_scale_parameters_if_needed(chain, width, height)
619                                 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
620                                 prepare_overlay_live(chain, t)
621                         end
622                         return chain.chain, prepare
623                 elseif live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
624                         local chain = static_chains[overlay_enabled][true]
625                         prepare = function()
626                                 prepare_overlay_live(chain, t)
627                         end
628                         return chain.chain, prepare
629                 else
630                         assert(false)
631                 end
632         end
633
634         -- We do not show overlays on the individual preview inputs.
635         -- The M/E preview matches what we'd put live by doing a transition, as always.
636         local show_overlay = false
637         if num == 1 then  -- Preview.
638                 if preview_signal_num == OVERLAY_SIGNAL_NUM then
639                         num = live_signal_num + 2
640                         show_overlay = not overlay_enabled
641
642                         if transition_type ~= NO_TRANSITION then
643                                 num = transition_dst_signal + 2
644                         end
645                 else
646                         num = preview_signal_num + 2
647                         show_overlay = overlay_enabled
648                 end
649         end
650
651         -- Individual preview inputs (usually without overlay).
652         if is_plain_signal(num - 2) then
653                 local signal_num = num - 2
654                 local input_type = get_input_type(signals, signal_num)
655                 local input_scale = needs_scale(signals, signal_num, width, height)
656                 local chain = simple_chains[input_type][input_scale][show_overlay][false]
657                 prepare = function()
658                         chain.input:connect_signal(signal_num)
659                         set_scale_parameters_if_needed(chain, width, height)
660                         set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
661                         prepare_overlay_static(chain, t)
662                 end
663                 return chain.chain, prepare
664         end
665         if num == STATIC_SIGNAL_NUM + 2 then
666                 local chain = static_chains[show_overlay][false]
667                 prepare = function()
668                         prepare_overlay_static(chain, t)
669                 end
670                 return chain.chain, prepare
671         end
672         if num == OVERLAY_SIGNAL_NUM + 2 then
673                 prepare = function()
674 --                      prepare_overlay(overlay_chain_lq, t)
675                 end
676                 return overlay_chain_lq, prepare
677         end
678 end
679
680 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
681 function round(x)
682         return math.floor(x + 0.5)
683 end
684
685 function prepare_overlay_live(chain, t)
686         if chain.overlay then
687                 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
688                 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
689                 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
690                 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
691                         overlay_enabled = false  -- Takes effect next frame.
692         --              print("Turning off overlay")
693                 end
694                 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
695         end
696 end
697
698 function prepare_overlay_static(chain)
699         if chain.overlay then
700                 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
701         end
702 end
703
704 function set_neutral_color(effect, color)
705         effect:set_vec3("neutral_color", color[1], color[2], color[3])
706 end
707
708 function set_neutral_color_from_signal(effect, signal)
709         if is_plain_signal(signal) then
710                 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
711         end
712 end
713
714 function calc_zoom_progress(t)
715         if t < transition_start then
716                 return 0.0
717         elseif t > transition_end then
718                 return 1.0
719         else
720                 local tt = (t - transition_start) / (transition_end - transition_start)
721                 -- Smooth it a bit.
722                 return math.sin(tt * 3.14159265358 * 0.5)
723         end
724 end
725
726 function calc_fade_progress(t, transition_start, transition_end)
727         local tt = (t - transition_start) / (transition_end - transition_start)
728         if tt < 0.0 then
729                 return 0.0
730         elseif tt > 1.0 then
731                 return 1.0
732         end
733
734         -- Make the fade look maybe a tad more natural, by pumping it
735         -- through a sigmoid function.
736         tt = 10.0 * tt - 5.0
737         tt = 1.0 / (1.0 + math.exp(-tt))
738
739         return tt
740 end