Hi,
I'm looking to add a feature to my particle system that fades a particle from being one colour to being another colour.
Currently, my particles have an attribute called 'percentLife'. PercentLife is used to make the particles fade out and shrink before they are deleted (to make the deletion more seamless and make them not appear as if they vanish instantly).
particles[i].colour.a = particles[i].percentLife * 255;
particles[i].particleSprite.setColor(particles[i].colour);
particles[i].scale = particles[i].scale * particles[i].percentLife;
particles[i].particleSprite.setScale(particles[i].scale);
As you can see, I'm just using percentLife in order to morph the scale and alpha so that it fades and shrinks for when it dies. Percent life starts at one and goes down to 0 throughout the particle's lifetime.
I want to implement a similar system but so that, for example, a colour can morph from blue to yellow. I've been having a lot of trouble doing this and I think I might be missing some maths on how this is done.
How is this done? I've been trying to work it out but I'm at a loss.
Thanks :)
I found a solution :D
Firstly, here's the results:
Transition from orange to white: https://i.imgur.com/1HkZYm8.png?1 (https://i.imgur.com/1HkZYm8.png?1)
Transition from orange to blue: https://i.imgur.com/Z0hnf30.png?1 (https://i.imgur.com/Z0hnf30.png?1)
Turned down the rate so we can see how the colours change: https://i.imgur.com/SIGQJUw.png?1 (https://i.imgur.com/SIGQJUw.png?1)
I don't really think there were too many artifacts and I also did my interpolations entirely in RGB without converting between HSV and RGB (although, I read that converting to HSV produces better results, so I'll probably implement this later at some point).
If anyone in the future is reading this and asking the same questions as me with the fading colours, here's the code I used to do it:
particles[i].colour.r = (255 - particles[i].colour.r) * rate + particles[i].colour.r;
particles[i].colour.g = (255 - particles[i].colour.g) * rate + particles[i].colour.g;
particles[i].colour.b = (255 - particles[i].colour.b) * rate + particles[i].colour.b;
The rate is a float. I have set the rate at 0.02, though for the third image it was set at 0.009.
The formula I found was:
currentColour = (targetColour - currentColour) * rate + currentColour
As you can see in the code this is used 3 times for r, g and b.