Search code examples
textureslibgdxparticles

Can libgdx Particle Effects use texture regions?


I have all of my images for a libgdx project in a single texture.

I'm about to add some nice particle effects, but the documentation implies that each type of emitter requires a separate graphics file for its particle.

Is that true? Or, is there a way of specifying a region of a texture to be used as the particle image, so that I may still keep all my images in that single file?


Solution

  • Yes it can but you need to have the texture inside of an TextureAtlas. Take a look at this article for it.

    here is an Example where i even use a TextureAtlas:

    m_effect = new ParticleEffect();
    m_effect.load(Gdx.files.internal("particle/effects/lightning.p"), this.getAtlas());
    

    Or in 2 steps:

    m_effect.loadEmitters(Gdx.files.internal("particle/effects/lightning.p"));
    m_effect.loadEmitterImages(this.getAtlas());
    

    here is whatt he LoadEmitterImage does:

    public void loadEmitterImages (TextureAtlas atlas) {
        for (int i = 0, n = emitters.size; i < n; i++) {
            ParticleEmitter emitter = emitters.get(i);
            String imagePath = emitter.getImagePath();
            if (imagePath == null) continue;
            String imageName = new File(imagePath.replace('\\', '/')).getName();
            int lastDotIndex = imageName.lastIndexOf('.');
            if (lastDotIndex != -1) imageName = imageName.substring(0, lastDotIndex);
            Sprite sprite = atlas.createSprite(imageName); /// <---- here it creates a Sprite with a textureregion
            if (sprite == null) throw new IllegalArgumentException("SpriteSheet missing image: " + imageName);
            emitter.setSprite(sprite);
        }
    }
    

    src from git