musiclight2/pixelation.lua

135 lines
3 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

COOLDOWN_FACTOR = 0.9998
MAX_ENERGY_PROPAGATION = 0.5
EXPONENT=1.0
W_EXPONENT=1.0
OVERDRIVE=1
num_modules = 1
num_strips = 1
-- maximum energy values for each band
maxRedEnergy = 1
maxGreenEnergy = 1
maxBlueEnergy = 1
maxWhiteEnergy = 1
-- array storing the flames energy for each pixel
fireRedEnergy = {}
fireGreenEnergy = {}
fireBlueEnergy = {}
fireWhiteEnergy = {}
-- output color buffers
red = {}
green = {}
blue = {}
white = {}
function limit(val)
if val > 1 then
return 1
elseif val < 0 then
return 0
else
return val
end
end
function updateFire(newEnergy, energyArray)
avgEnergyPerStrip = newEnergy * 2 / num_strips
for s = 1,num_strips do
-- Add new energy in the bottom row
i = idx(s, 1)
energyArray[i] = energyArray[i] + math.random() * avgEnergyPerStrip
-- remove energy at the top
i = idx(s, num_modules)
energyArray[i] = energyArray[i] * (1.0 - math.random() * MAX_ENERGY_PROPAGATION)
-- move energy upwards
for m_out = num_modules,2,-1 do
i_out = idx(s, m_out)
i_in = idx(s, m_out - 1)
energyMoved = energyArray[i_in] * math.random() * MAX_ENERGY_PROPAGATION
energyArray[i_in] = energyArray[i_in] - energyMoved
energyArray[i_out] = energyArray[i_out] + energyMoved
end
end
end
function periodic()
local redEnergy = get_energy_in_band(0, 400);
local greenEnergy = get_energy_in_band(400, 4000);
local blueEnergy = get_energy_in_band(4000, 12000);
local whiteEnergy = get_energy_in_band(12000, 22000);
maxRedEnergy = maxRedEnergy * COOLDOWN_FACTOR
if redEnergy > maxRedEnergy then
maxRedEnergy = redEnergy
end
maxGreenEnergy = maxGreenEnergy * COOLDOWN_FACTOR
if greenEnergy > maxGreenEnergy then
maxGreenEnergy = greenEnergy
end
maxBlueEnergy = maxBlueEnergy * COOLDOWN_FACTOR
if blueEnergy > maxBlueEnergy then
maxBlueEnergy = blueEnergy
end
maxWhiteEnergy = maxWhiteEnergy * COOLDOWN_FACTOR
if whiteEnergy > maxWhiteEnergy then
maxWhiteEnergy = whiteEnergy
end
updateFire(redEnergy / maxRedEnergy, fireRedEnergy)
updateFire(greenEnergy / maxGreenEnergy, fireGreenEnergy)
updateFire(blueEnergy / maxBlueEnergy, fireBlueEnergy)
updateFire(whiteEnergy / maxWhiteEnergy, fireWhiteEnergy)
-- make colors more exciting + remove the first (flickering) mass
-- TODO: update
for m = 1,num_modules do
for s = 1,num_strips do
i = idx(s, m)
rval = limit(OVERDRIVE * fireRedEnergy[i]^EXPONENT)
gval = limit(OVERDRIVE * fireGreenEnergy[i]^EXPONENT)
bval = limit(OVERDRIVE * fireBlueEnergy[i]^EXPONENT)
wval = limit(OVERDRIVE * fireWhiteEnergy[i]^W_EXPONENT)
red[i] = rval
green[i] = gval
blue[i] = bval
white[i] = wval
end
end
-- return the 4 color arrays
return red, green, blue, white
end
function init(nstrip, nmod, cmod)
num_strips = nstrip
num_modules = nmod
for i = 1,(nmod*nstrip) do
red[i] = 0
green[i] = 0
blue[i] = 0
white[i] = 0
fireRedEnergy[i] = 0
fireGreenEnergy[i] = 0
fireBlueEnergy[i] = 0
fireWhiteEnergy[i] = 0
end
-- don't use fading
return 0
end