Search code examples
3dlibgdxparticle-systemspritebatch

Render SpriteBatch + ParticleEffect in 3D space in libgdx?


I am using libgdx version 0.9.9. I want to render a fire effect using ParticleEffect in 3D space along with other 3D models.

Logical flow of my code:

  1. ModelBatch Begin
  2. Render all 3D models in Bullet World using ModelBatch
  3. ModelBatch End
  4. SpriteBatch Begin
  5. Render the fire effect using ParticleEffect (effect.draw) using SpriteBatch
  6. SpriteBatch End
  7. Draw the HUD using Stage

The problem: The fire effect is rendering fine at a point in 3D space. But when I rotate the camera so that a 3D model lies between the camera and the fire effect the fire effect renders over the 3D model instead of being hidden behind the 3D model.

Things that I have tried:

  1. I have tried rendering the SpriteBatch first and then the 3D models: In this case the fire effect is not visible. I am guessing that the 3D models (layer) is rendering over the fire effect (layer) and hence the effect is not visible.
  2. I tried rendering the SpriteBatch between steps 1 and 3 i.e. rendering the SpriteBatch between modelbatch.begin and modelbatch.end. In this case the fire effect is not visible at all.
  3. I have tried rendering the Particle Effect as an Actor (added to HUD Stage). As expected the fire effect renders as part of the topmost HUD layer and the same problem remains.
  4. I have tried exploring Decals but found that Particle Effect doesn't work with DebcalBatch. I didn't want to display an animated fire .gif over the Decal and hence didn't try it.

Has anyone faced similar problem? Any suggested workaround to make ParticleEffect behave as part of the 3D world so that it gets hidden when blocked by other 3D models? I have seen a video posted by Xoppa on youtube about 3D particles in libgdx but there are no steps / solution mentioned. Any help will be highly appreciated.


Solution

  • The reason SpriteBatch was not being displayed in the following rendering order:

    1. Render particle effect using SpriteBatch
    2. Render 3D Models

    was because the 3d models consisted of a Skydome that was overlapping the SpriteBatch, thus hiding it completely. I took out the Skydome from the set of 3D models rendered in step # 2 above and am rendering it before the particle effect.

    Following rendering order is working fine for me:

    1. Render Skydome using ModelBatch
    2. Render particle effect using SpriteBatch
    3. Render rest of the models using ModelBatch

    With this the particle effect is in front of the sky but behind the 3d models.