Situation
I created a particle system with JavaFX using the following technique:
Each particle is an ImageView which contains an Image with a radial gradient:
The particle handling loop is an AnimationTimer in which the list of particles is handled via the list's stream().parallel() method, it actually gives the whole system a boost. Something like this:
loop = new AnimationTimer() {
@Override
public void handle(long now) {
addParticle();
// apply force: gravity
allParticles.stream().parallel().forEach(Particle::applyForceGravity);
// move particle
allParticles.stream().parallel().forEach(Particle::move);
// update position in fx scene
allParticles.forEach(Particle::display);
// remove all particles that aren't visible anymore
removeDeadParticles();
}
};
The particle's color changes via ColorAdjust during its lifecycle. I used fire colors for testing, something like this which contains 1700 particles:
What I've learned:
- not using parallel() is slower
- using Circle with transparency is way slower than using an ImageView
- using a Blend Mode is very slow
- using a PixelWriter to change the image color is unbearable slow. Question arises: how does ColorAdjust change the color (via d3d & hardware)? I haven't found the mechanism in the JavaFX source code.
Question
Is there a better way to implement a particle system in JavaFX (other Node types, Thread, etc) in regards to performance?
I can post some code if anyone wants to toy around.
Thank you very much for the expertise!
Edit: Since it was asked, you can get the full code which uses nodes as particles with coloradjust from this gist. Btw, even if you pre-render the images and don't use coloradjust, the performance is low.
However, the question is more of a theoretical kind, so digging through the code isn't really necessary.