粒子系统的性能 [英] Performance for particle system

查看:191
本文介绍了粒子系统的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况



我使用以下技术使用JavaFX创建了一个粒子系统:



每个粒子都是一个ImageView,其中包含一个带有径向渐变的图像:





粒子处理循环是一个AnimationTimer,其中粒子列表是通过list的stream()。parallel()方法处理的,它实际上为整个系统提供了一个提升。这样的事情:

  loop = new AnimationTimer(){

@Override
public void handle(现在很长){

addParticle();

// apply force:gravity
allParticles.stream()。parallel()。forEach(Particle :: applyForceGravity);

//移动粒子
allParticles.stream()。parallel()。forEach(Particle :: move);

//更新fx场景中的位置
allParticles.forEach(Particle :: display);

//删除所有不可见的粒子
removeDeadParticles();

}
};

粒子的颜色在其生命周期中通过ColorAdjust发生变化。我用火色进行测试,这样的东西包含1700个粒子:





我学到了什么:




  • 不使用parallel()更慢

  • 使用带透明度的圆形比使用ImageView慢

  • 使用混合模式非常慢

  • 使用PixelWriter更改图像颜色难以忍受。问题出现了:ColorAdjust如何改变颜色(通过d3d和硬件)?我没有在JavaFX源代码中找到该机制。



问题



在性能方面,有没有更好的方法在JavaFX(其他节点类型,线程等)中实现粒子系统?



如果有人想玩,我可以发布一些代码。



非常感谢您的专业知识!






编辑:因为有问题,你可以从


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.

解决方案

I think I can add an answer to my own question. But I hope someone else with more experience can share their knowledge, because what I came up with was just the result of toying around:

Painting on a Canvas instead of using JavaFX ImageView nodes surprisingly resulted in a factor of at least 10x speed increase. This is basically what I did:

  • pre-calculate the images with the gradients, i. e. the color and the size of them depending on the particle's lifespan
  • in the animation timer get the pre-calculated image and paint it on the canvas

If anyone is interested, you can get the full code from this gist. Just click "Download Zip" and put the code of the zip in an "application" package of a JavaFX project and start the Main class. The difference to the code in the question is in the Particle.java class. In the question the particles are used as nodes and moved in the animation timer, but in this answer only the data is used for drawing an image on a canvas in the animation timer, the node itself isn't put on the scene.

You can use the Settings class in order to specify resolution, number of new particles per frame, etc.

This screenshot shows 3 repellers and 25400 particles on the screen in Full-HD resolution, running at 60fps:

这篇关于粒子系统的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆