Skip to content

Commit

Permalink
added option to use fast color add, may improve performance a little
Browse files Browse the repository at this point in the history
also fixed a bug in fire animation
  • Loading branch information
DedeHai committed Feb 8, 2024
1 parent 7d6965d commit d00126b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
27 changes: 15 additions & 12 deletions wled00/FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8387,19 +8387,22 @@ uint16_t mode_particlefire(void)
// initialize new flame: set properties of source
// from time to time, chang the flame position
// make some of the flames small and slow to add a bright base

if (random8(40) == 0) //from time to time, change flame position (about once per second)
{
if (SEGMENT.check1)
{ // wrap around in X direction, distribute randomly
flames[i].source.x = random16(PS_MAX_X);
}
else
{ // no wrapping
flames[i].source.x = PS_P_RADIUS * 3 + random16(PS_MAX_X - (PS_P_RADIUS * 6)); // distribute randomly but not close to the corners
}
}

if (i < (numFlames - (cols >> 1)))
{ // all but the last few are normal flames
if (random8(40) == 0)
{
if (SEGMENT.check1)
{ // wrap around in X direction, distribute randomly
flames[i].source.x = random16(PS_MAX_X);
}
else
{ // no wrapping
flames[i].source.x = PS_P_RADIUS * 3 + random16(PS_MAX_X - (PS_P_RADIUS * 6)); // distribute randomly but not close to the corners
}
}

flames[i].source.y = -1 * PS_P_RADIUS; // set the source below the frame so particles alredy spread a little when the appear
flames[i].source.vx = 0; // (rand() % 3) - 1;
flames[i].source.vy = 0;
Expand Down Expand Up @@ -8459,7 +8462,7 @@ uint16_t mode_particlefire(void)

return FRAMETIME;
}
static const char _data_FX_MODE_PARTICLEFIRE[] PROGMEM = "Particle Fire@Speed,Intensity,Base Flames,Wind Speed, Color Mode, WrapX;;!;012;sx=100,ix=120,c1=16,c2=128,c3=0,o1=0";
static const char _data_FX_MODE_PARTICLEFIRE[] PROGMEM = "Particle Fire@Speed,Intensity,Base Flames,Wind Speed, Color Scheme, WrapX;;!;012;sx=100,ix=120,c1=16,c2=128,c3=0,o1=0";
/*syntax for json configuration string:
@A,B,C,D,E,F,G,H;I,J,K;L;M;N mark commas and semicolons
A - speed
Expand Down
16 changes: 8 additions & 8 deletions wled00/FXparticleSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void Particle_Gravity_update(PSparticle *part, bool wrapX, bool bounceX, bool bo
// render particles to the LED buffer (uses palette to render the 8bit particle color value)
// if wrap is set, particles half out of bounds are rendered to the other side of the matrix
// saturation is color saturation, if not set to 255, hsv instead of palette is used (palette does not support saturation)
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY)
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY, bool fastcoloradd)
{

const uint16_t cols = strip.isMatrix ? SEGMENT.virtualWidth() : 1;
Expand All @@ -351,6 +351,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
CRGB baseRGB;
uint16_t i;
uint8_t brightess; // particle brightness, fades if dying


// go over particles and update matrix cells on the way
for (i = 0; i < numParticles; i++)
Expand Down Expand Up @@ -388,7 +389,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
x--; // shift x to next pixel left, will overflow to 255 if 0
dx = dx + (PS_P_RADIUS >> 1);
}
else // if jump has ocurred, fade pixel out
else // if jump has ocurred
{
dx = dx - (PS_P_RADIUS >> 1); // adjust dx so pixel fades
}
Expand All @@ -400,7 +401,6 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
}
else
{
// adjust dy so pixel fades
dy = dy - (PS_P_RADIUS >> 1);
}

Expand Down Expand Up @@ -429,7 +429,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
// calculate the intensity with linear interpolation
intensity = ((uint32_t)((PS_P_RADIUS)-dx) * ((PS_P_RADIUS)-dy) * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
// scale the particle base color by the intensity and add it to the pixel
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
}
// bottom right;
x++;
Expand All @@ -441,7 +441,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
if (x < cols && y < rows)
{
intensity = ((uint32_t)dx * ((PS_P_RADIUS)-dy) * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
}
// top right
y++;
Expand All @@ -453,7 +453,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
if (x < cols && y < rows)
{
intensity = ((uint32_t)dx * dy * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
}
// top left
x--;
Expand All @@ -467,7 +467,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
if (x < cols && y < rows)
{
intensity = ((uint32_t)((PS_P_RADIUS)-dx) * dy * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
}
}
}
Expand Down Expand Up @@ -665,7 +665,7 @@ void PartMatrix_addHeat(uint8_t col, uint8_t row, uint16_t heat)
// check if there is heat left over
if (newcolorvalue == 255)
{ // there cannot be a leftover if it is not full
heat = heat - (255 - currentcolor[i]); // heat added is difference from current red value to full red value, subtract it from the inital heat value so heat is the remaining heat not added yet
heat = heat - (255 - currentcolor[i]); // heat added is difference from current value to full value, subtract it from the inital heat value so heat is the remaining heat not added yet
// this cannot produce an underflow since we never add more than the initial heat value
}
else
Expand Down
2 changes: 1 addition & 1 deletion wled00/FXparticleSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void Particle_attractor(PSparticle *particle, PSparticle *attractor, uint8_t *co
void Particle_Move_update(PSparticle *part);
void Particle_Bounce_update(PSparticle *part, const uint8_t hardness);
void Particle_Gravity_update(PSparticle *part, bool wrapX, bool bounceX, bool bounceY, const uint8_t hardness);
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY);
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY, bool fastcoloradd = false);
void FireParticle_update(PSparticle *part, bool wrapX, bool WrapY);
void ParticleSys_renderParticleFire(PSparticle *particles, uint16_t numParticles, bool wrapX);
void PartMatrix_addHeat(uint8_t col, uint8_t row, uint16_t heat);
Expand Down

0 comments on commit d00126b

Please sign in to comment.