--- /dev/null
+-- Nageru theme for TFK mini-tournament 2017, based on the default theme.
+
+local transition_start = -2.0
+local transition_end = -1.0
+local transition_type = 0
+local transition_src_signal = 0
+local transition_dst_signal = 0
+
+local neutral_colors = {
+ {0.5, 0.5, 0.5}, -- Input 0.
+ {0.5, 0.5, 0.5}, -- Input 1.
+ {0.5, 0.5, 0.5} -- Input 2.
+}
+
+local overlay_transition_start = -2.0
+local overlay_transition_end = -1.0
+local overlay_alpha_src = 0.0
+local overlay_alpha_dst = 1.0
+local overlay_enabled = false
+
+local live_signal_num = 0
+local preview_signal_num = 1
+
+-- Valid values for live_signal_num and preview_signal_num.
+local INPUT0_SIGNAL_NUM = 0
+local INPUT1_SIGNAL_NUM = 1
+local INPUT2_SIGNAL_NUM = 2
+local STATIC_SIGNAL_NUM = 3
+
+-- Preview-only signal showing the current signal with the overlay.
+-- Not valid for live_signal_num!
+local OVERLAY_SIGNAL_NUM = 4
+
+-- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
+local NO_TRANSITION = 0
+local FADE_TRANSITION = 2
+
+-- Last width/height/frame rate for each channel, if we have it.
+-- Note that unlike the values we get from Nageru, the resolution is per
+-- frame and not per field, since we deinterlace.
+local last_resolution = {}
+
+local caspar_input = VideoInput.new("unix:///tmp/caspar.sock")
+caspar_input:change_rate(2.0)
+
+-- Utility function to help creating many similar chains that can differ
+-- in a free set of chosen parameters.
+function make_cartesian_product(parms, callback)
+ return make_cartesian_product_internal(parms, callback, 1, {})
+end
+
+function make_cartesian_product_internal(parms, callback, index, args)
+ if index > #parms then
+ return callback(unpack(args))
+ end
+ local ret = {}
+ for _, value in ipairs(parms[index]) do
+ args[index] = value
+ ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
+ end
+ return ret
+end
+
+-- An overlay with variable alpha.
+function make_overlay(chain, base)
+ local image = chain:add_video_input(caspar_input, false)
+ local multiply_effect = chain:add_effect(MultiplyEffect.new())
+ local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
+ return {
+ image = image,
+ multiply_effect = multiply_effect,
+ overlay_effect = overlay_effect
+ }
+end
+
+function possibly_make_overlay(has_overlay, chain, base)
+ if has_overlay == true then
+ return make_overlay(chain, base)
+ else
+ return nil
+ end
+end
+
+function make_fade_input(chain, signal, live, deint, scale)
+ local input, wb_effect, resample_effect, last
+ if live then
+ input = chain:add_live_input(false, deint)
+ input:connect_signal(signal)
+ last = input
+ else
+ input = chain:add_effect(ImageInput.new("tfk_pause.png"))
+ last = input
+ end
+
+ -- If we cared about this for the non-main inputs, we would have
+ -- checked hq here and invoked ResizeEffect instead.
+ if scale then
+ resample_effect = chain:add_effect(ResampleEffect.new())
+ last = resample_effect
+ end
+
+ -- Make sure to put the white balance after the scaling (usually more efficient).
+ if live then
+ wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+ last = wb_effect
+ end
+
+ return {
+ input = input,
+ wb_effect = wb_effect,
+ resample_effect = resample_effect,
+ last = last
+ }
+end
+
+-- A chain to fade between two inputs, of which either can be a picture
+-- or a live input. In practice only used live, but we still support the
+-- hq parameter.
+function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
+ local chain = EffectChain.new(16, 9)
+
+ local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
+ local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
+
+ local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
+ local overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
+
+ chain:finalize(hq)
+
+ return {
+ chain = chain,
+ input0 = input0,
+ input1 = input1,
+ mix_effect = mix_effect,
+ overlay = overlay
+ }
+end
+
+-- Chains to fade between two inputs, in various configurations.
+local fade_chains = make_cartesian_product({
+ {"static", "live", "livedeint"}, -- input0_type
+ {true, false}, -- input0_scale
+ {"static", "live", "livedeint"}, -- input1_type
+ {true, false}, -- input1_scale
+ {true, false}, -- has_overlay
+ {true} -- hq
+}, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
+ local input0_live = (input0_type ~= "static")
+ local input1_live = (input1_type ~= "static")
+ local input0_deint = (input0_type == "livedeint")
+ local input1_deint = (input1_type == "livedeint")
+ return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
+end)
+
+-- A chain to show a single input on screen.
+function make_simple_chain(input_deint, input_scale, has_overlay, hq)
+ local chain = EffectChain.new(16, 9)
+
+ local input = chain:add_live_input(false, input_deint)
+ input:connect_signal(0) -- First input card. Can be changed whenever you want.
+
+ local resample_effect, resize_effect
+ if scale then
+ if hq then
+ resample_effect = chain:add_effect(ResampleEffect.new())
+ else
+ resize_effect = chain:add_effect(ResizeEffect.new())
+ end
+ end
+
+ local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+ local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
+
+ chain:finalize(hq)
+
+ return {
+ chain = chain,
+ input = input,
+ wb_effect = wb_effect,
+ resample_effect = resample_effect,
+ resize_effect = resize_effect,
+ overlay = overlay
+ }
+end
+
+-- Make all possible combinations of single-input chains.
+local simple_chains = make_cartesian_product({
+ {"live", "livedeint"}, -- input_type
+ {true, false}, -- input_scale
+ {true, false}, -- has_overlay
+ {true, false} -- hq
+}, function(input_type, input_scale, has_overlay, hq)
+ local input_deint = (input_type == "livedeint")
+ return make_simple_chain(input_deint, input_scale, has_overlay, hq)
+end)
+
+-- A chain to show a single static picture on screen.
+local static_chains = make_cartesian_product({
+ {true, false}, -- has_overlay
+ {true, false} -- hq
+}, function(has_overlay, hq)
+ local chain = EffectChain.new(16, 9)
+ local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
+ local overlay = possibly_make_overlay(has_overlay, chain, chain_input)
+
+ chain:finalize(hq)
+ return {
+ chain = chain,
+ overlay = overlay
+ }
+end)
+
+-- A chain to show the overlay and nothing more. LQ only,
+-- since it is not a valid live signal.
+local overlay_chain_lq = EffectChain.new(16, 9)
+local overlay_chain_lq_input = overlay_chain_lq:add_video_input(caspar_input, false)
+overlay_chain_lq:finalize(false)
+
+-- Used for indexing into the tables of chains.
+function get_input_type(signals, signal_num)
+ if signal_num == STATIC_SIGNAL_NUM then
+ return "static"
+ elseif signals:get_interlaced(signal_num) then
+ return "livedeint"
+ else
+ return "live"
+ end
+end
+
+function needs_scale(signals, signal_num, width, height)
+ if signal_num == STATIC_SIGNAL_NUM then
+ -- We assume this is already correctly scaled at load time.
+ return false
+ end
+ assert(is_plain_signal(signal_num))
+ return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
+end
+
+function set_scale_parameters_if_needed(chain_or_input, width, height)
+ if chain_or_input.resample_effect then
+ chain_or_input.resample_effect:set_int("width", width)
+ chain_or_input.resample_effect:set_int("height", height)
+ elseif chain_or_input.resize_effect then
+ chain_or_input.resize_effect:set_int("width", width)
+ chain_or_input.resize_effect:set_int("height", height)
+ end
+end
+
+-- API ENTRY POINT
+-- Returns the number of outputs in addition to the live (0) and preview (1).
+-- Called only once, at the start of the program.
+function num_channels()
+ return 5
+end
+
+function is_plain_signal(num)
+ return num >= INPUT0_SIGNAL_NUM and num <= INPUT2_SIGNAL_NUM
+end
+
+-- Helper function to write e.g. “720p60”. The difference between this
+-- and get_channel_resolution_raw() is that this one also can say that
+-- there's no signal.
+function get_channel_resolution(signal_num)
+ res = last_resolution[signal_num]
+ if (not res) or not res.is_connected then
+ return "disconnected"
+ end
+ if res.height <= 0 then
+ return "no signal"
+ end
+ if not res.has_signal then
+ if res.height == 525 then
+ -- Special mode for the USB3 cards.
+ return "no signal"
+ end
+ return get_channel_resolution_raw(res) .. ", no signal"
+ else
+ return get_channel_resolution_raw(res)
+ end
+end
+
+-- Helper function to write e.g. “60” or “59.94”.
+function get_frame_rate(res)
+ local nom = res.frame_rate_nom
+ local den = res.frame_rate_den
+ if nom % den == 0 then
+ return nom / den
+ else
+ return string.format("%.2f", nom / den)
+ end
+end
+
+-- Helper function to write e.g. “720p60”.
+function get_channel_resolution_raw(res)
+ if res.interlaced then
+ return res.height .. "i" .. get_frame_rate(res)
+ else
+ return res.height .. "p" .. get_frame_rate(res)
+ end
+end
+
+-- API ENTRY POINT
+-- Returns the name for each additional channel (starting from 2).
+-- Called at the start of the program, and then each frame for live
+-- channels in case they change resolution.
+function channel_name(channel)
+ local signal_num = channel - 2
+ if is_plain_signal(signal_num) then
+ return "Input " .. (signal_num + 1) .. " (" .. get_channel_resolution(signal_num) .. ")"
+ elseif signal_num == STATIC_SIGNAL_NUM then
+ return "Static picture"
+ elseif signal_num == OVERLAY_SIGNAL_NUM then
+ return "Overlay"
+ end
+end
+
+-- API ENTRY POINT
+-- Returns, given a channel number, which signal it corresponds to (starting from 0).
+-- Should return -1 if the channel does not correspond to a simple signal.
+-- (The information is used for whether right-click on the channel should bring up
+-- an input selector or not.)
+-- Called once for each channel, at the start of the program.
+-- Will never be called for live (0) or preview (1).
+function channel_signal(channel)
+ if channel == 2 then
+ return 0
+ elseif channel == 3 then
+ return 1
+ else
+ return -1
+ end
+end
+
+-- API ENTRY POINT
+-- Called every frame. Returns the color (if any) to paint around the given
+-- channel. Returns a CSS color (typically to mark live and preview signals);
+-- "transparent" is allowed.
+-- Will never be called for live (0) or preview (1).
+function channel_color(channel)
+ if transition_type ~= NO_TRANSITION then
+ if channel_involved_in(channel, transition_src_signal) or
+ channel_involved_in(channel, transition_dst_signal) then
+ return "#f00"
+ end
+ else
+ if channel_involved_in(channel, live_signal_num) then
+ return "#f00"
+ end
+ end
+ if channel_involved_in(channel, preview_signal_num) then
+ return "#0f0"
+ end
+ return "transparent"
+end
+
+function channel_involved_in(channel, signal_num)
+ if is_plain_signal(signal_num) then
+ return channel == (signal_num + 2)
+ end
+ if signal_num == STATIC_SIGNAL_NUM then
+ return (channel == 5)
+ end
+ return false
+end
+
+-- API ENTRY POINT
+-- Returns if a given channel supports setting white balance (starting from 2).
+-- Called only once for each channel, at the start of the program.
+function supports_set_wb(channel)
+ return is_plain_signal(channel - 2)
+end
+
+-- API ENTRY POINT
+-- Gets called with a new gray point when the white balance is changing.
+-- The color is in linear light (not sRGB gamma).
+function set_wb(channel, red, green, blue)
+ if is_plain_signal(channel - 2) then
+ neutral_colors[channel - 2 + 1] = { red, green, blue }
+ end
+end
+
+function finish_transitions(t)
+ if transition_type ~= NO_TRANSITION and t >= transition_end then
+ live_signal_num = transition_dst_signal
+ transition_type = NO_TRANSITION
+ end
+
+ -- Disable the overlay if it is no longer visible.
+ if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
+ overlay_enabled = false
+ print("Turning off overlay")
+ end
+end
+
+function in_transition(t)
+ return t >= transition_start and t <= transition_end
+end
+
+-- API ENTRY POINT
+-- Called every frame.
+function get_transitions(t)
+ if preview_signal_num == OVERLAY_SIGNAL_NUM then
+ if t < overlay_transition_end then
+ -- Fade in progress.
+ return {}
+ end
+ if overlay_enabled then
+ return {"Overlay off", "", "Fade ovl out"}
+ else
+ return {"Overlay on", "", "Fade ovl in"}
+ end
+ end
+
+ if in_transition(t) then
+ -- Transition already in progress, the only thing we can do is really
+ -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
+ return {"Cut"}
+ end
+
+ if live_signal_num == preview_signal_num then
+ -- No transitions possible.
+ return {}
+ end
+
+ if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
+ (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
+ return {"Cut", "", "Fade"}
+ end
+
+ return {"Cut"}
+end
+
+function swap_preview_live()
+ local temp = live_signal_num
+ live_signal_num = preview_signal_num
+ preview_signal_num = temp
+end
+
+function start_transition(type_, t, duration)
+ transition_start = t
+ transition_end = t + duration
+ transition_type = type_
+ transition_src_signal = live_signal_num
+ transition_dst_signal = preview_signal_num
+ swap_preview_live()
+end
+
+-- API ENTRY POINT
+-- Called when the user clicks a transition button.
+function transition_clicked(num, t)
+ if preview_signal_num == OVERLAY_SIGNAL_NUM then
+ if num == 0 then
+ -- Cut.
+ overlay_transition_start = -2.0
+ overlay_transition_end = -1.0
+ if overlay_enabled then
+ overlay_enabled = false
+ overlay_alpha_src = 0.0
+ overlay_alpha_dst = 0.0
+ else
+ overlay_enabled = true
+ overlay_alpha_src = 1.0
+ overlay_alpha_dst = 1.0
+ end
+ elseif num == 2 then
+ -- Fade.
+ overlay_transition_start = t
+ overlay_transition_end = t + 1.0
+ if overlay_enabled then
+ overlay_alpha_src = 1.0
+ overlay_alpha_dst = 0.0
+ else
+ overlay_alpha_src = 0.0
+ overlay_alpha_dst = 1.0
+ end
+ overlay_enabled = true
+ end
+ return
+ end
+
+ if num == 0 then
+ -- Cut.
+ if in_transition(t) then
+ -- Ongoing transition; finish it immediately before the cut.
+ finish_transitions(transition_end)
+ end
+
+ swap_preview_live()
+ elseif num == 1 then
+ -- Zoom.
+ finish_transitions(t)
+
+ if live_signal_num == preview_signal_num then
+ -- Nothing to do.
+ return
+ end
+
+ if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
+ -- We can't zoom between these. Just make a cut.
+ io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
+ swap_preview_live()
+ return
+ end
+ elseif num == 2 then
+ finish_transitions(t)
+
+ -- Fade.
+ if (live_signal_num ~= preview_signal_num) and
+ (is_plain_signal(live_signal_num) or
+ live_signal_num == STATIC_SIGNAL_NUM) and
+ (is_plain_signal(preview_signal_num) or
+ preview_signal_num == STATIC_SIGNAL_NUM) then
+ start_transition(FADE_TRANSITION, t, 1.0)
+ else
+ -- Fades involving SBS are ignored (we have no chain for it).
+ end
+ end
+end
+
+-- API ENTRY POINT
+function channel_clicked(num)
+ preview_signal_num = num
+end
+
+function get_fade_chain(signals, t, width, height, input_resolution)
+ local input0_type = get_input_type(signals, transition_src_signal)
+ local input0_scale = needs_scale(signals, transition_src_signal, width, height)
+ local input1_type = get_input_type(signals, transition_dst_signal)
+ local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
+ local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
+ prepare = function()
+ if input0_type == "live" or input0_type == "livedeint" then
+ chain.input0.input:connect_signal(transition_src_signal)
+ set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
+ end
+ set_scale_parameters_if_needed(chain.input0, width, height)
+ if input1_type == "live" or input1_type == "livedeint" then
+ chain.input1.input:connect_signal(transition_dst_signal)
+ set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
+ end
+ set_scale_parameters_if_needed(chain.input1, width, height)
+ local tt = calc_fade_progress(t, transition_start, transition_end)
+
+ chain.mix_effect:set_float("strength_first", 1.0 - tt)
+ chain.mix_effect:set_float("strength_second", tt)
+ prepare_overlay_live(chain, t)
+ end
+ return chain.chain, prepare
+end
+
+-- API ENTRY POINT
+-- Called every frame. Get the chain for displaying at input <num>,
+-- where 0 is live, 1 is preview, 2 is the first channel to display
+-- in the bottom bar, and so on up to num_channels()+1. t is the
+-- current time in seconds. width and height are the dimensions of
+-- the output, although you can ignore them if you don't need them
+-- (they're useful if you want to e.g. know what to resample by).
+--
+-- <signals> is basically an exposed InputState, which you can use to
+-- query for information about the signals at the point of the current
+-- frame. In particular, you can call get_width() and get_height()
+-- for any signal number, and use that to e.g. assist in chain selection.
+--
+-- You should return two objects; the chain itself, and then a
+-- function (taking no parameters) that is run just before rendering.
+-- The function needs to call connect_signal on any inputs, so that
+-- it gets updated video data for the given frame. (You are allowed
+-- to switch which input your input is getting from between frames,
+-- but not calling connect_signal results in undefined behavior.)
+-- If you want to change any parameters in the chain, this is also
+-- the right place.
+--
+-- NOTE: The chain returned must be finalized with the Y'CbCr flag
+-- if and only if num==0.
+function get_chain(num, t, width, height, signals)
+ local input_resolution = {}
+ for signal_num=0,2 do
+ local res = {
+ width = signals:get_width(signal_num),
+ height = signals:get_height(signal_num),
+ interlaced = signals:get_interlaced(signal_num),
+ has_signal = signals:get_has_signal(signal_num),
+ is_connected = signals:get_is_connected(signal_num),
+ frame_rate_nom = signals:get_frame_rate_nom(signal_num),
+ frame_rate_den = signals:get_frame_rate_den(signal_num)
+ }
+
+ if res.interlaced then
+ -- Convert height from frame height to field height.
+ -- (Needed for e.g. place_rectangle.)
+ res.height = res.height * 2
+
+ -- Show field rate instead of frame rate; really for cosmetics only
+ -- (and actually contrary to EBU recommendations, although in line
+ -- with typical user expectations).
+ res.frame_rate_nom = res.frame_rate_nom * 2
+ end
+
+ input_resolution[signal_num] = res
+ end
+ last_resolution = input_resolution
+
+ if num == 0 then -- Live.
+ -- See if we're in a transition.
+ finish_transitions(t)
+ if transition_type == FADE_TRANSITION then
+ return get_fade_chain(signals, t, width, height, input_resolution)
+ elseif is_plain_signal(live_signal_num) then
+ local input_type = get_input_type(signals, live_signal_num)
+ local input_scale = needs_scale(signals, live_signal_num, width, height)
+ local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
+ prepare = function()
+ chain.input:connect_signal(live_signal_num)
+ set_scale_parameters_if_needed(chain, width, height)
+ set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
+ prepare_overlay_live(chain, t)
+ end
+ return chain.chain, prepare
+ elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
+ local chain = static_chains[overlay_enabled][true]
+ prepare = function()
+ prepare_overlay_live(chain, t)
+ end
+ return chain.chain, prepare
+ else
+ assert(false)
+ end
+ end
+
+ -- We do not show overlays on the individual preview inputs.
+ -- The M/E preview matches what we'd put live by doing a transition, as always.
+ local show_overlay = false
+ if num == 1 then -- Preview.
+ if preview_signal_num == OVERLAY_SIGNAL_NUM then
+ num = live_signal_num + 2
+ show_overlay = not overlay_enabled
+
+ if transition_type ~= NO_TRANSITION then
+ num = transition_dst_signal + 2
+ end
+ else
+ num = preview_signal_num + 2
+ show_overlay = overlay_enabled
+ end
+ end
+
+ -- Individual preview inputs (usually without overlay).
+ if is_plain_signal(num - 2) then
+ local signal_num = num - 2
+ local input_type = get_input_type(signals, signal_num)
+ local input_scale = needs_scale(signals, signal_num, width, height)
+ local chain = simple_chains[input_type][input_scale][show_overlay][false]
+ prepare = function()
+ chain.input:connect_signal(signal_num)
+ set_scale_parameters_if_needed(chain, width, height)
+ set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
+ prepare_overlay_static(chain, t)
+ end
+ return chain.chain, prepare
+ end
+ if num == STATIC_SIGNAL_NUM + 2 then
+ local chain = static_chains[show_overlay][false]
+ prepare = function()
+ prepare_overlay_static(chain, t)
+ end
+ return chain.chain, prepare
+ end
+ if num == OVERLAY_SIGNAL_NUM + 2 then
+ prepare = function()
+-- prepare_overlay(overlay_chain_lq, t)
+ end
+ return overlay_chain_lq, prepare
+ end
+end
+
+-- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
+function round(x)
+ return math.floor(x + 0.5)
+end
+
+function prepare_overlay_live(chain, t)
+ if chain.overlay then
+ local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
+ overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
+ --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
+ if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
+ overlay_enabled = false -- Takes effect next frame.
+ -- print("Turning off overlay")
+ end
+ chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
+ end
+end
+
+function prepare_overlay_static(chain)
+ if chain.overlay then
+ chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
+ end
+end
+
+function set_neutral_color(effect, color)
+ effect:set_vec3("neutral_color", color[1], color[2], color[3])
+end
+
+function set_neutral_color_from_signal(effect, signal)
+ if is_plain_signal(signal) then
+ set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
+ end
+end
+
+function calc_zoom_progress(t)
+ if t < transition_start then
+ return 0.0
+ elseif t > transition_end then
+ return 1.0
+ else
+ local tt = (t - transition_start) / (transition_end - transition_start)
+ -- Smooth it a bit.
+ return math.sin(tt * 3.14159265358 * 0.5)
+ end
+end
+
+function calc_fade_progress(t, transition_start, transition_end)
+ local tt = (t - transition_start) / (transition_end - transition_start)
+ if tt < 0.0 then
+ return 0.0
+ elseif tt > 1.0 then
+ return 1.0
+ end
+
+ -- Make the fade look maybe a tad more natural, by pumping it
+ -- through a sigmoid function.
+ tt = 10.0 * tt - 5.0
+ tt = 1.0 / (1.0 + math.exp(-tt))
+
+ return tt
+end