Skip to content

Scene2D

Anton Chekulaev edited this page Jun 18, 2020 · 7 revisions

This is how gdx-vfx and LibGDX's Scene2D can live together.

1. Apply effects to the whole Stage

This one is simple. If you want to process the entire Stage scene, you should just wrap Stage#draw() with VfxManager's capture and apply effects chain.

Here is how it may look:

Stage stage;
VfxManager manager;

void draw(float delta) {
    stage.act(delta);
    manager.update(delta);
    manager.beginCapture();
    stage.draw();
    manager.endCapture();
    manager.render();
}

2. Apply effects to the specific Actor

This one is a bit more complicated as you cannot intercept specific Actor's render method to capture rendering and apply effects, unless you wrap it into a parent Group and override Group#render() to put required gdx-vfx code here.

And for sake of this case, the library has a solution...

It does exactly what described above, it extends WidgetGroup and applies the chain of effects to all of its children. Also, it comes with a couple of goodies packed it:

  1. You don't have to manage VfxManager manually (create/dispose it). VfxWidgetGroup does it internally when it gets added or removed from the Stage.
  2. Internal VfxManager will be resized when needed to match the VfxWidgetGroup's size at all times.
  3. Internal VfxManager is accessible through VfxWidgetGroup#getVfxManager(). This is how it can be configured and supplied with the Effects.
  4. By default, internal VfxManager will render into the frame buffer that matches the screen resolution (in proportion to the VfxWidgetGroup's size). If you need the effect buffer to be of the actual VfxWidgetGroup's size (virtual Scene2D viewport units), just call VfxWidgetGroup#setMatchWidgetSize(boolean).

Code example

Let's have look at a simple setup, where we want to have two Groups, where gdx-vfx effects will be applied only to the first one.

Stage stage;
VfxWidgetGroup vfxGroup;
WidgetGroup widgetGroup;

void create() {
    stage = new Stage();

    // Create a `gdx-vfx` group, where effects will be applied to its children actor hierarchy.
    vfxGroup = new VfxWidgetGroup(...);
    vfxGroup.setFillParent(true);  // Let the group fill entire stage.
    stage.addActor();

    // Create a regular group, where any actors should be rendered as usual.
    widgetGroup = new WidgetGroup();
    widgetGroup.setFillParent(true);  // Let the group fill entire stage.
    stage.addActor(widgetGroup);

    // From now on we have two root scene groups.
    //   * Any actors added to vfxGroup will be affected by the `gdx-vfx` effects.
    //   * Any actors added to widgetGroup will be rendered unaffected on top of processed vfxGroup.

    // Here's how effects can be added:
    vfxGroup.getVfxManager().addEffect(...);
}

void draw(float delta) {
    // No any extra code required for the rendering.
    stage.act(delta);
    stage.draw();
}