Author Topic: Particle system problem  (Read 2115 times)

Offline Zyphuris55

  • byte
  • *
  • Posts: 30
    • View Profile
Particle system problem
« on: February 12, 2013, 02:53:17 pm »
I've been working on making a 3D particle system for a class project. It's been going well so far....except for the speed.

At first, I was trying to do 3D objects (cubes), but half of the sides wouldn't be visible (plus, the particles would be small majority of the time unless they were near the camera). So I decided on doing them all in 2D with billboarding, and each object has only 2 triangles to worry about.
With this current setup, I'm getting around 30 fps with 100 objects and 5 fps with 400 objects. Which isn't the most appealing thing, since the game is supposed to have more particles (about 2k).

After checking out the Rajawali's implementation (http://www.rozengain.com/blog/2012/05/03/rajawali-tutorial-22-more-optimisation/) of a large particle system, I tried to do the same with jPCT (see, I'm not abandoning this engine).

First I was having problems changing the vertices (using the vertexController, still haven't completely got it working :-/) Then I found out that the code to change each particle's transparency/ color would no longer work in this method (because of how vertices share color with each other)

So, what would be the best way to do this system?
Requirements:
- particles which move in 3D space
- each particle has its own position/velocity/color/transparency/life
- many particles (around 2k) with a smooth frame rate
- dynamic creation of particles

Note: The particles don't need any high-quality stuff or collision. Although eventual gravity would be nice, it's on the far back burner.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Particle system problem
« Reply #1 on: February 12, 2013, 08:19:09 pm »
Using a single object for each particle isn't particularly fast but it's actually not that slow either. For example, An3DBenchXL's particle test uses the same approach and renders 500 particles @30fps on a Galaxy Nexus and @45fps on a Samsung S3. If you are interested, you can get the sources from this thread: http://www.jpct.net/forum2/index.php?topic=2784.0

If you really need something more sophisticated, using the IVertexController approach might work somehow, but it won't be a fast as it gets either because it requires to upload the updated geometry to gpu, which takes its time.

If you really need performance, you can do what the rajawali example does: Write your own shaders to do it, i.e. create a single object with all the particles in it and do the animation in your shader's code. You have to enable OpenGL ES 2.0 for that and you have to come up with some clever idea to suppress the rendering of unused particles...or if you have everything up and running apart from that, we could try to add some hook into the engine's render pipeline to limit the number of drawn vertices.

Offline Zyphuris55

  • byte
  • *
  • Posts: 30
    • View Profile
Re: Particle system problem
« Reply #2 on: February 16, 2013, 12:48:31 pm »
After being hung up with other projects, I'm finally able get back to this project.
In what you said about writing up my own shader and vertex arrays...is there a way I can piggyback the stuff on a Object3D? Or would it be easier to just skip the Object3D and do the whole thing from scratch?

I ask that because I like the features of camera/world classes and I'm not sure how to begin with writing up new classes to replicate those which are already made.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Particle system problem
« Reply #3 on: February 16, 2013, 01:30:32 pm »
In what you said about writing up my own shader and vertex arrays...is there a way I can piggyback the stuff on a Object3D?
Sure. You can create an Object3D that contains the data and assign your shader to it. There's a constructor for Object3D that takes raw data as input.

Offline Zyphuris55

  • byte
  • *
  • Posts: 30
    • View Profile
Re: Particle system problem
« Reply #4 on: February 16, 2013, 03:44:24 pm »
Would this be as fast as the rajawali method?

If it is, can you provide some example code? I tried doing something with the mesh and polygon manager....and I ended up having no clue what I was doing.  :P

Additionally, how can I change the color of each triangle in this array? I'm looking at the included document of the engine under the shader stuff, and I don't see anything about loading in an array of vertex colors...or an array of alpha values for the vertex points.
« Last Edit: February 16, 2013, 05:06:01 pm by Zyphuris55 »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Particle system problem
« Reply #5 on: February 16, 2013, 05:33:52 pm »
Of course it would be as fast. A shader doesn't care about the engine which sets its data. But i don't have any example code for a shader based particle system, i'm afraid. If i had, i would have added it already. The shader that rajawali uses should be a good start.

About setting the values, you can inject float[] and SimpleVector[] as uniforms into your shader (have a look at http://www.jpct.net/jpct-ae/doc/com/threed/jpct/GLSLShader.html), which should enable you to do what you want.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Particle system problem
« Reply #6 on: February 16, 2013, 06:16:54 pm »
Thinking about it, you'll more likely need setters for vertex attributes instead of uniforms for this task. You can already do that directly via OpenGL by getting the program handle from the GLSLShader, but i'll look into why i haven't exposed these setters yet...there was some reason that i can't remember ATM.