Skip to content

Commit 3e7fd5b

Browse files
committed
Bugfixes and added more color modes to PS Fireworks 1D
- fixed inconsitencies in size rendering - fixed palette being wrapped in color by position and color by age modes - Fixed bug in memory layout: for some unknown reason, if flags come before particles, last flag is sometimes overwritten, changing memory laout seems to fix that
1 parent 98b214a commit 3e7fd5b

File tree

3 files changed

+71
-47
lines changed

3 files changed

+71
-47
lines changed

wled00/FX.cpp

+28-20
Original file line numberDiff line numberDiff line change
@@ -8127,7 +8127,7 @@ uint16_t mode_particlepit(void) {
81278127
PartSys->particles[i].sat = ((SEGMENT.custom3) << 3) + 7;
81288128
// set particle size
81298129
if (SEGMENT.custom1 == 255) {
8130-
PartSys->setParticleSize(1); // set global size to 1 for advanced rendering
8130+
PartSys->setParticleSize(1); // set global size to 1 for advanced rendering (no single pixel particles)
81318131
PartSys->advPartProps[i].size = hw_random16(SEGMENT.custom1); // set each particle to random size
81328132
} else {
81338133
PartSys->setParticleSize(SEGMENT.custom1); // set global size
@@ -9085,7 +9085,6 @@ uint16_t mode_particlePinball(void) {
90859085
PartSys->sources[0].sourceFlags.collide = true; // seeded particles will collide (if enabled)
90869086
PartSys->sources[0].source.x = PS_P_RADIUS_1D; //emit at bottom
90879087
PartSys->setKillOutOfBounds(true); // out of bounds particles dont return
9088-
PartSys->setUsedParticles(255); // use all available particles for init
90899088
SEGENV.aux0 = 1;
90909089
SEGENV.aux1 = 5000; //set out of range to ensure uptate on first call
90919090
}
@@ -9321,9 +9320,7 @@ uint16_t mode_particleFireworks1D(void) {
93219320
// Particle System settings
93229321
PartSys->updateSystem(); // update system properties (dimensions and data pointers)
93239322
forcecounter = PartSys->PSdataEnd;
9324-
PartSys->setParticleSize(SEGMENT.check3); // 1 or 2 pixel rendering
93259323
PartSys->setMotionBlur(SEGMENT.custom2); // anable motion blur
9326-
93279324
int32_t gravity = (1 + (SEGMENT.speed >> 3)); // gravity value used for rocket speed calculation
93289325
PartSys->setGravity(SEGMENT.speed ? gravity : 0); // set gravity
93299326

@@ -9337,17 +9334,17 @@ uint16_t mode_particleFireworks1D(void) {
93379334
SEGENV.aux0 = 0;
93389335

93399336
PartSys->sources[0].sourceFlags.custom1 = 0; //flag used for rocket state
9340-
PartSys->sources[0].source.hue = hw_random16();
9337+
PartSys->sources[0].source.hue = hw_random16(); // different color for each launch
93419338
PartSys->sources[0].var = 10; // emit variation
93429339
PartSys->sources[0].v = -10; // emit speed
93439340
PartSys->sources[0].minLife = 30;
9344-
PartSys->sources[0].maxLife = SEGMENT.check2 ? 400 : 40;
9341+
PartSys->sources[0].maxLife = SEGMENT.check2 ? 400 : 60;
93459342
PartSys->sources[0].source.x = 0; // start from bottom
93469343
uint32_t speed = sqrt((gravity * ((PartSys->maxX >> 2) + hw_random16(PartSys->maxX >> 1))) >> 4); // set speed such that rocket explods in frame
93479344
PartSys->sources[0].source.vx = min(speed, (uint32_t)127);
93489345
PartSys->sources[0].source.ttl = 4000;
93499346
PartSys->sources[0].sat = 30; // low saturation exhaust
9350-
PartSys->sources[0].size = 0; // default size
9347+
PartSys->sources[0].size = SEGMENT.check3; // single or double pixel rendering
93519348
PartSys->sources[0].sourceFlags.reversegrav = false ; // normal gravity
93529349

93539350
if (SEGENV.aux0) { // inverted rockets launch from end
@@ -9360,17 +9357,17 @@ uint16_t mode_particleFireworks1D(void) {
93609357
}
93619358
else { // rocket is launched
93629359
int32_t rocketgravity = -gravity;
9363-
int32_t speed = PartSys->sources[0].source.vx;
9360+
int32_t currentspeed = PartSys->sources[0].source.vx;
93649361
if (SEGENV.aux0) { // negative speed rocket
93659362
rocketgravity = -rocketgravity;
9366-
speed = -speed;
9363+
currentspeed = -currentspeed;
93679364
}
93689365
PartSys->applyForce(PartSys->sources[0].source, rocketgravity, forcecounter[0]);
93699366
PartSys->particleMoveUpdate(PartSys->sources[0].source, PartSys->sources[0].sourceFlags);
9370-
PartSys->particleMoveUpdate(PartSys->sources[0].source, PartSys->sources[0].sourceFlags); // increase speed by calling the move function twice, also ages twice
9367+
PartSys->particleMoveUpdate(PartSys->sources[0].source, PartSys->sources[0].sourceFlags); // increase rocket speed by calling the move function twice, also ages twice
93719368
uint32_t rocketheight = SEGENV.aux0 ? PartSys->maxX - PartSys->sources[0].source.x : PartSys->sources[0].source.x;
93729369

9373-
if (speed < 0 && PartSys->sources[0].source.ttl > 50) // reached apogee
9370+
if (currentspeed < 0 && PartSys->sources[0].source.ttl > 50) // reached apogee
93749371
PartSys->sources[0].source.ttl = min((uint32_t)50, rocketheight >> (PS_P_RADIUS_SHIFT_1D + 3)); // alive for a few more frames
93759372

93769373
if (PartSys->sources[0].source.ttl < 2) { // explode
@@ -9379,19 +9376,32 @@ uint16_t mode_particleFireworks1D(void) {
93799376
PartSys->sources[0].minLife = 600;
93809377
PartSys->sources[0].maxLife = 1300;
93819378
PartSys->sources[0].source.ttl = 100 + hw_random16(64 - (SEGMENT.speed >> 2)); // standby time til next launch
9382-
PartSys->sources[0].sat = 7 + (SEGMENT.custom3 << 3); //color saturation TODO: replace saturation with something more useful?
9383-
PartSys->sources[0].size = hw_random16(SEGMENT.intensity); // random particle size in explosion
9379+
PartSys->sources[0].sat = SEGMENT.custom3 < 16 ? 10 + (SEGMENT.custom3 << 4) : 255; //color saturation
9380+
PartSys->sources[0].size = SEGMENT.check3 ? hw_random16(SEGMENT.intensity) : 0; // random particle size in explosion
93849381
uint32_t explosionsize = 8 + (PartSys->maxXpixel >> 2) + (PartSys->sources[0].source.x >> (PS_P_RADIUS_SHIFT_1D - 1));
93859382
explosionsize += hw_random16((explosionsize * SEGMENT.intensity) >> 8);
93869383
for (uint32_t e = 0; e < explosionsize; e++) { // emit explosion particles
9387-
if (SEGMENT.check1) // colorful mode
9388-
PartSys->sources[0].source.hue = hw_random16(); //random color for each particle
9389-
PartSys->sprayEmit(PartSys->sources[0]); // emit a particle
9384+
int idx = PartSys->sprayEmit(PartSys->sources[0]); // emit a particle
9385+
if(SEGMENT.custom3 > 23) {
9386+
if(SEGMENT.custom3 == 31) { // highest slider value
9387+
PartSys->setColorByAge(SEGMENT.check1); // color by age if colorful mode is enabled
9388+
PartSys->setColorByPosition(!SEGMENT.check1); // color by position otherwise
9389+
}
9390+
else { // if custom3 is set to high value (but not highest), set particle color by initial speed
9391+
PartSys->particles[idx].hue = map(abs(PartSys->particles[idx].vx), 0, PartSys->sources[0].var, 0, 16 + hw_random16(200)); // set hue according to speed, use random amount of palette width
9392+
PartSys->particles[idx].hue += PartSys->sources[0].source.hue; // add hue offset of the rocket (random starting color)
9393+
}
9394+
}
9395+
else {
9396+
if (SEGMENT.check1) // colorful mode
9397+
PartSys->sources[0].source.hue = hw_random16(); //random color for each particle
9398+
}
93909399
}
93919400
}
93929401
}
9393-
if ((SEGMENT.call & 0x01) == 0 && PartSys->sources[0].sourceFlags.custom1 == false) // every second frame and not in standby
9402+
if ((SEGMENT.call & 0x01) == 0 && PartSys->sources[0].sourceFlags.custom1 == false && PartSys->sources[0].source.ttl > 50) // every second frame and not in standby and not about to explode
93949403
PartSys->sprayEmit(PartSys->sources[0]); // emit exhaust particle
9404+
93959405
if ((SEGMENT.call & 0x03) == 0) // every fourth frame
93969406
PartSys->applyFriction(1); // apply friction to all particles
93979407

@@ -9401,10 +9411,9 @@ uint16_t mode_particleFireworks1D(void) {
94019411
if (PartSys->particles[i].ttl > 10) PartSys->particles[i].ttl -= 10; //ttl is linked to brightness, this allows to use higher brightness but still a short spark lifespan
94029412
else PartSys->particles[i].ttl = 0;
94039413
}
9404-
94059414
return FRAMETIME;
94069415
}
9407-
static const char _data_FX_MODE_PS_FIREWORKS1D[] PROGMEM = "PS Fireworks 1D@Gravity,Explosion,Firing side,Blur,Saturation,Colorful,Trail,Smooth;,!;!;1;sx=150,c2=30,c3=31,o1=1,o2=1";
9416+
static const char _data_FX_MODE_PS_FIREWORKS1D[] PROGMEM = "PS Fireworks 1D@Gravity,Explosion,Firing side,Blur,Color,Colorful,Trail,Smooth;,!;!;1;c2=30,o1=1";
94089417

94099418
/*
94109419
Particle based Sparkle effect
@@ -9925,7 +9934,6 @@ uint16_t mode_particle1DGEQ(void) {
99259934
PartSys->sources[i].maxLife = 240 + SEGMENT.intensity;
99269935
PartSys->sources[i].sat = 255;
99279936
PartSys->sources[i].size = SEGMENT.custom1;
9928-
PartSys->setParticleSize(SEGMENT.custom1);
99299937
PartSys->sources[i].source.x = (spacing >> 1) + spacing * i; //distribute evenly
99309938
}
99319939

wled00/FXparticleSystem.cpp

+37-21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ParticleSystem2D::ParticleSystem2D(uint32_t width, uint32_t height, uint32_t num
4848
for (uint32_t i = 0; i < numSources; i++) {
4949
sources[i].source.sat = 255; //set saturation to max by default
5050
sources[i].source.ttl = 1; //set source alive
51+
sources[i].sourceFlags.asByte = 0; // all flags disabled
5152
}
5253

5354
}
@@ -559,6 +560,10 @@ void ParticleSystem2D::pointAttractor(const uint32_t particleindex, PSparticle &
559560
void ParticleSystem2D::render() {
560561
CRGB baseRGB;
561562
uint32_t brightness; // particle brightness, fades if dying
563+
TBlendType blend = LINEARBLEND; // default color rendering: wrap palette
564+
if (particlesettings.colorByAge) {
565+
blend = LINEARBLEND_NOWRAP;
566+
}
562567

563568
if (motionBlur) { // motion-blurring active
564569
for (int32_t y = 0; y <= maxYpixel; y++) {
@@ -581,11 +586,11 @@ void ParticleSystem2D::render() {
581586
if (fireIntesity) { // fire mode
582587
brightness = (uint32_t)particles[i].ttl * (3 + (fireIntesity >> 5)) + 20;
583588
brightness = min(brightness, (uint32_t)255);
584-
baseRGB = ColorFromPaletteWLED(SEGPALETTE, brightness, 255);
589+
baseRGB = ColorFromPaletteWLED(SEGPALETTE, brightness, 255, LINEARBLEND_NOWRAP);
585590
}
586591
else {
587592
brightness = min((particles[i].ttl << 1), (int)255);
588-
baseRGB = ColorFromPaletteWLED(SEGPALETTE, particles[i].hue, 255);
593+
baseRGB = ColorFromPaletteWLED(SEGPALETTE, particles[i].hue, 255, blend);
589594
if (particles[i].sat < 255) {
590595
CHSV32 baseHSV;
591596
rgb2hsv((uint32_t((byte(baseRGB.r) << 16) | (byte(baseRGB.g) << 8) | (byte(baseRGB.b)))), baseHSV); // convert to HSV
@@ -598,14 +603,15 @@ void ParticleSystem2D::render() {
598603
renderParticle(i, brightness, baseRGB, particlesettings.wrapX, particlesettings.wrapY);
599604
}
600605

606+
// apply global size rendering
601607
if (particlesize > 1) {
602608
uint32_t passes = particlesize / 64 + 1; // number of blur passes, four passes max
603609
uint32_t bluramount = particlesize;
604610
uint32_t bitshift = 0;
605611
for (uint32_t i = 0; i < passes; i++) {
606612
if (i == 2) // for the last two passes, use higher amount of blur (results in a nicer brightness gradient with soft edges)
607613
bitshift = 1;
608-
blur2D(framebuffer, maxXpixel + 1, maxYpixel + 1, bluramount << bitshift, bluramount << bitshift);
614+
blur2D(framebuffer, maxXpixel + 1, maxYpixel + 1, bluramount << bitshift, bluramount << bitshift);
609615
bluramount -= 64;
610616
}
611617
}
@@ -626,7 +632,11 @@ void ParticleSystem2D::render() {
626632

627633
// calculate pixel positions and brightness distribution and render the particle to local buffer or global buffer
628634
__attribute__((optimize("O2"))) void ParticleSystem2D::renderParticle(const uint32_t particleindex, const uint8_t brightness, const CRGB& color, const bool wrapX, const bool wrapY) {
629-
if (particlesize == 0) { // single pixel rendering
635+
uint32_t size = particlesize;
636+
if (advPartProps && advPartProps[particleindex].size > 0) // use advanced size properties (0 means use global size including single pixel rendering)
637+
size = advPartProps[particleindex].size;
638+
639+
if (size == 0) { // single pixel rendering
630640
uint32_t x = particles[particleindex].x >> PS_P_RADIUS_SHIFT;
631641
uint32_t y = particles[particleindex].y >> PS_P_RADIUS_SHIFT;
632642
if (x <= (uint32_t)maxXpixel && y <= (uint32_t)maxYpixel) {
@@ -667,7 +677,7 @@ __attribute__((optimize("O2"))) void ParticleSystem2D::renderParticle(const uint
667677
pxlbrightness[2] = (dx * precal3) >> PS_P_SURFACE; // top right value equal to (dx * dy * brightness) >> PS_P_SURFACE
668678
pxlbrightness[3] = (precal1 * precal3) >> PS_P_SURFACE; // top left value equal to ((PS_P_RADIUS-dx) * dy * brightness) >> PS_P_SURFACE
669679

670-
if (advPartProps && advPartProps[particleindex].size > 0) { //render particle to a bigger size
680+
if (advPartProps && advPartProps[particleindex].size > 1) { //render particle to a bigger size
671681
CRGB renderbuffer[100]; // 10x10 pixel buffer
672682
memset(renderbuffer, 0, sizeof(renderbuffer)); // clear buffer
673683
//particle size to pixels: < 64 is 4x4, < 128 is 6x6, < 192 is 8x8, bigger is 10x10
@@ -962,15 +972,13 @@ void ParticleSystem2D::updateSystem(void) {
962972
// FX handles the PSsources, need to tell this function how many there are
963973
void ParticleSystem2D::updatePSpointers(bool isadvanced, bool sizecontrol) {
964974
PSPRINTLN("updatePSpointers");
965-
// DEBUG_PRINT(F("*** PS pointers ***"));
966-
// DEBUG_PRINTF_P(PSTR("this PS %p "), this);
967975
// Note on memory alignment:
968976
// a pointer MUST be 4 byte aligned. sizeof() in a struct/class is always aligned to the largest element. if it contains a 32bit, it will be padded to 4 bytes, 16bit is padded to 2byte alignment.
969977
// The PS is aligned to 4 bytes, a PSparticle is aligned to 2 and a struct containing only byte sized variables is not aligned at all and may need to be padded when dividing the memoryblock.
970978
// by making sure that the number of sources and particles is a multiple of 4, padding can be skipped here as alignent is ensured, independent of struct sizes.
971-
particleFlags = reinterpret_cast<PSparticleFlags *>(this + 1); // pointer to particle flags
972-
particles = reinterpret_cast<PSparticle *>(particleFlags + numParticles); // pointer to particles
973-
sources = reinterpret_cast<PSsource *>(particles + numParticles); // pointer to source(s) at data+sizeof(ParticleSystem2D)
979+
particles = reinterpret_cast<PSparticle *>(this + 1); // pointer to particles
980+
particleFlags = reinterpret_cast<PSparticleFlags *>(particles + numParticles); // pointer to particle flags
981+
sources = reinterpret_cast<PSsource *>(particleFlags + numParticles); // pointer to source(s) at data+sizeof(ParticleSystem2D)
974982
framebuffer = reinterpret_cast<CRGB *>(sources + numSources); // pointer to framebuffer
975983
// align pointer after framebuffer
976984
uintptr_t p = reinterpret_cast<uintptr_t>(framebuffer + (maxXpixel+1)*(maxYpixel+1));
@@ -1155,6 +1163,7 @@ ParticleSystem1D::ParticleSystem1D(uint32_t length, uint32_t numberofparticles,
11551163
// initialize some default non-zero values most FX use
11561164
for (uint32_t i = 0; i < numSources; i++) {
11571165
sources[i].source.ttl = 1; //set source alive
1166+
sources[i].sourceFlags.asByte = 0; // all flags disabled
11581167
}
11591168

11601169
if (isadvanced) {
@@ -1269,7 +1278,7 @@ int32_t ParticleSystem1D::sprayEmit(const PSsource1D &emitter) {
12691278
particles[emitIndex].x = emitter.source.x;
12701279
particles[emitIndex].hue = emitter.source.hue;
12711280
particles[emitIndex].ttl = hw_random16(emitter.minLife, emitter.maxLife);
1272-
particleFlags[emitIndex].collide = emitter.sourceFlags.collide;
1281+
particleFlags[emitIndex].collide = emitter.sourceFlags.collide; // TODO: could just set all flags (asByte) but need to check if that breaks any of the FX
12731282
particleFlags[emitIndex].reversegrav = emitter.sourceFlags.reversegrav;
12741283
particleFlags[emitIndex].perpetual = emitter.sourceFlags.perpetual;
12751284
if (advPartProps) {
@@ -1419,6 +1428,10 @@ void ParticleSystem1D::applyFriction(int32_t coefficient) {
14191428
void ParticleSystem1D::render() {
14201429
CRGB baseRGB;
14211430
uint32_t brightness; // particle brightness, fades if dying
1431+
TBlendType blend = LINEARBLEND; // default color rendering: wrap palette
1432+
if (particlesettings.colorByAge || particlesettings.colorByPosition) {
1433+
blend = LINEARBLEND_NOWRAP;
1434+
}
14221435

14231436
#ifdef ESP8266 // no local buffer on ESP8266
14241437
if (motionBlur)
@@ -1442,7 +1455,7 @@ void ParticleSystem1D::render() {
14421455

14431456
// generate RGB values for particle
14441457
brightness = min(particles[i].ttl << 1, (int)255);
1445-
baseRGB = ColorFromPaletteWLED(SEGPALETTE, particles[i].hue, 255);
1458+
baseRGB = ColorFromPaletteWLED(SEGPALETTE, particles[i].hue, 255, blend);
14461459

14471460
if (advPartProps) { //saturation is advanced property in 1D system
14481461
if (advPartProps[i].sat < 255) {
@@ -1489,9 +1502,9 @@ void ParticleSystem1D::render() {
14891502
// calculate pixel positions and brightness distribution and render the particle to local buffer or global buffer
14901503
__attribute__((optimize("O2"))) void ParticleSystem1D::renderParticle(const uint32_t particleindex, const uint8_t brightness, const CRGB &color, const bool wrap) {
14911504
uint32_t size = particlesize;
1492-
if (advPartProps) { // use advanced size properties
1505+
if (advPartProps) // use advanced size properties (1D system has no large size global rendering TODO: add large global rendering?)
14931506
size = advPartProps[particleindex].size;
1494-
}
1507+
14951508
if (size == 0) { //single pixel particle, can be out of bounds as oob checking is made for 2-pixel particles (and updating it uses more code)
14961509
uint32_t x = particles[particleindex].x >> PS_P_RADIUS_SHIFT_1D;
14971510
if (x <= (uint32_t)maxXpixel) { //by making x unsigned there is no need to check < 0 as it will overflow
@@ -1736,30 +1749,32 @@ void ParticleSystem1D::updatePSpointers(bool isadvanced) {
17361749
// a pointer MUST be 4 byte aligned. sizeof() in a struct/class is always aligned to the largest element. if it contains a 32bit, it will be padded to 4 bytes, 16bit is padded to 2byte alignment.
17371750
// The PS is aligned to 4 bytes, a PSparticle is aligned to 2 and a struct containing only byte sized variables is not aligned at all and may need to be padded when dividing the memoryblock.
17381751
// by making sure that the number of sources and particles is a multiple of 4, padding can be skipped here as alignent is ensured, independent of struct sizes.
1739-
particleFlags = reinterpret_cast<PSparticleFlags1D *>(this + 1); // pointer to particle flags
1740-
particles = reinterpret_cast<PSparticle1D *>(particleFlags + numParticles); // pointer to particles
1741-
sources = reinterpret_cast<PSsource1D *>(particles + numParticles); // pointer to source(s)
1752+
particles = reinterpret_cast<PSparticle1D *>(this + 1); // pointer to particles
1753+
particleFlags = reinterpret_cast<PSparticleFlags1D *>(particles + numParticles); // pointer to particle flags
1754+
sources = reinterpret_cast<PSsource1D *>(particleFlags + numParticles); // pointer to source(s)
17421755
#ifdef ESP8266 // no local buffer on ESP8266
17431756
PSdataEnd = reinterpret_cast<uint8_t *>(sources + numSources);
17441757
#else
17451758
framebuffer = reinterpret_cast<CRGB *>(sources + numSources); // pointer to framebuffer
17461759
// align pointer after framebuffer to 4bytes
1747-
uintptr_t p = reinterpret_cast<uintptr_t>(framebuffer + (maxXpixel+1));
1760+
uintptr_t p = reinterpret_cast<uintptr_t>(framebuffer + (maxXpixel+1)); // maxXpixel is SEGMENT.virtualLength() - 1
17481761
p = (p + 3) & ~0x03; // align to 4-byte boundary
17491762
PSdataEnd = reinterpret_cast<uint8_t *>(p); // pointer to first available byte after the PS for FX additional data
17501763
#endif
17511764
if (isadvanced) {
17521765
advPartProps = reinterpret_cast<PSadvancedParticle1D *>(PSdataEnd);
1753-
PSdataEnd = reinterpret_cast<uint8_t *>(advPartProps + numParticles);
1766+
PSdataEnd = reinterpret_cast<uint8_t *>(advPartProps + numParticles); // since numParticles is a multiple of 4, this is always aligned to 4 bytes. No need to add padding bytes here
17541767
}
17551768
#ifdef WLED_DEBUG_PS
17561769
PSPRINTLN(" PS Pointers: ");
17571770
PSPRINT(" PS : 0x");
17581771
Serial.println((uintptr_t)this, HEX);
1759-
PSPRINT(" Sources : 0x");
1760-
Serial.println((uintptr_t)sources, HEX);
1772+
PSPRINT(" Particleflags : 0x");
1773+
Serial.println((uintptr_t)particleFlags, HEX);
17611774
PSPRINT(" Particles : 0x");
17621775
Serial.println((uintptr_t)particles, HEX);
1776+
PSPRINT(" Sources : 0x");
1777+
Serial.println((uintptr_t)sources, HEX);
17631778
#endif
17641779
}
17651780

@@ -1780,6 +1795,7 @@ uint32_t calculateNumberOfParticles1D(const uint32_t fraction, const bool isadva
17801795
numberofParticles = numberofParticles < 20 ? 20 : numberofParticles; // 20 minimum
17811796
//make sure it is a multiple of 4 for proper memory alignment (easier than using padding bytes)
17821797
numberofParticles = (numberofParticles+3) & ~0x03; // note: with a separate particle buffer, this is probably unnecessary
1798+
PSPRINTLN(" calc numparticles:" + String(numberofParticles))
17831799
return numberofParticles;
17841800
}
17851801

0 commit comments

Comments
 (0)