musiclight2/particles.lua

147 lines
3.1 KiB
Lua
Raw Normal View History

2020-06-14 18:12:02 +02:00
COOLDOWN_FACTOR = 0.9998
MAX_ENERGY_PROPAGATION = 0.5
EXPONENT=1.8
W_EXPONENT=2.2
2020-06-14 18:12:02 +02:00
OVERDRIVE=1
FADE_FACTOR = 0.96
2020-06-18 21:54:30 +02:00
AVG_LEDS_ACTIVATED = 0.05
WHITE_EXTRA_SCALE = 0.5
2020-06-14 18:12:02 +02:00
num_modules = 1
num_strips = 1
-- maximum energy values for each band
maxRedEnergy = 1
maxGreenEnergy = 1
maxBlueEnergy = 1
maxWhiteEnergy = 1
2020-06-23 21:32:43 +02:00
-- array storing the energy for each pixel
2020-06-14 18:12:02 +02:00
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 fade2black(energyArray)
for s = 1,num_strips do
for m = 1,num_modules do
i = idx(s, m)
energyArray[i] = energyArray[i] * FADE_FACTOR
end
end
end
function distributeEnergy(newEnergy, energyArray)
remainingEnergy = AVG_LEDS_ACTIVATED * newEnergy * num_modules * num_strips
while remainingEnergy > 0 do
2020-06-18 21:54:30 +02:00
rndEnergy = math.random() * newEnergy * 5
2020-06-14 18:12:02 +02:00
rndStrip = math.floor(math.random() * num_strips)
rndModule = math.floor(math.random() * num_modules)
if rndEnergy > remainingEnergy then
rndEnergy = remainingEnergy
remainingEnergy = 0
else
remainingEnergy = remainingEnergy - rndEnergy
end
i = idx(rndStrip+1, rndModule+1)
energyArray[i] = energyArray[i] + rndEnergy
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
fade2black(fireRedEnergy)
fade2black(fireGreenEnergy)
fade2black(fireBlueEnergy)
fade2black(fireWhiteEnergy)
distributeEnergy((redEnergy / maxRedEnergy)^EXPONENT, fireRedEnergy)
distributeEnergy((greenEnergy / maxGreenEnergy)^EXPONENT, fireGreenEnergy)
distributeEnergy((blueEnergy / maxBlueEnergy)^EXPONENT, fireBlueEnergy)
distributeEnergy((whiteEnergy / maxWhiteEnergy)^W_EXPONENT, fireWhiteEnergy)
2020-06-14 18:12:02 +02:00
2020-06-23 21:32:43 +02:00
-- Color post-processing
2020-06-14 18:12:02 +02:00
for m = 1,num_modules do
for s = 1,num_strips do
i = idx(s, m)
rval = limit(OVERDRIVE * fireRedEnergy[i])
gval = limit(OVERDRIVE * fireGreenEnergy[i])
bval = limit(OVERDRIVE * fireBlueEnergy[i])
wval = limit(OVERDRIVE * fireWhiteEnergy[i] * WHITE_EXTRA_SCALE)
2020-06-14 18:12:02 +02:00
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