/gdx-vfx

LibGDX post-processing visual effects

Primary LanguageJavaApache License 2.0Apache-2.0

gdx-vfx Logo

Build Status

This is a fork of gdx-vfx, a flexible post-processing library for libGDX. The main changes in this fork are:

  • An update to libGDX 1.10.0
  • Support for OpenGL 3 on macOS (shaders are automatically ported to GLSL 1.50)
  • Support for depth
  • Uses NestableFrameBuffers from guacamole instead of VfxFrameBuffers; removes the coupled Renderers
  • Improves alpha blending for the following effects: lens flare, old tv, radial distortion and vignetting
  • Renames some methods: beginInputCapture() -> beginCapture(), endInputCapture() -> endCapture(), cleanUpBuffers() -> clear(), anyEnabledEffects() -> hasEffects()
  • Heavily refactors a lot of the internal classes in the library

Gdx-vfx itself is based on libgdx-contribs-postprocessing, with lots of improvements and heavy refactoring. The goal is to focus on stability, offer lightweight integration and provide a simple mechanism to implement effects.

Read more about the library at the wiki introduction page.

Demo

Visit https://crashinvaders.github.io/gdx-vfx

Or clone and play with the demo locally:

git clone https://github.com/crashinvaders/gdx-vfx.git
cd gdx-vfx
./gradlew demo:desktop:run

Demo GIF

How to use

1. Add the library to the project

Gradle dependency

The library's releases are available through Jitpack.

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

Add the dependency:

dependencies {
    implementation "com.github.crykn.gdx-vfx:gdx-vfx-core:$vfxVersion"
    implementation "com.github.crykn.gdx-vfx:gdx-vfx-effects:$vfxVersion"    // Optional, if you need standard filter/effects.
}

HTML/GWT support

The library is fully HTML/GWT compatible, but requires an extra dependency to be included in the GWT module in order to work properly.
Please take a look at the GWT integration guide.

dependencies {
    implementation "com.github.crykn.gdx-vfx:gdx-vfx-gwt:$vfxVersion"
}

2. Sample code

A simple example of a LibGDX application that applies gaussian blur effect to a geometry drawn with ShapeRenderer.

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.crashinvaders.vfx.VfxManager;
import com.crashinvaders.vfx.effects.GaussianBlurEffect;

public class VfxExample extends ApplicationAdapter {
    private ShapeRenderer shapeRenderer;
    private VfxManager vfxManager;
    private GaussianBlurEffect vfxEffect;

    @Override
    public void create() {
        shapeRenderer = new ShapeRenderer();

        // VfxManager is a host for the effects.
        // It captures rendering into internal off-screen buffer and applies a chain of defined effects.
        vfxManager = new VfxManager();

        // Create and add an effect.
        // VfxEffect derivative classes serve as controllers for the effects.
        // They provide public properties to configure and control them.
        vfxEffect = new GaussianBlurEffect();
        vfxManager.addEffect(vfxEffect);
    }

    @Override
    public void resize(int width, int height) {
        // VfxManager manages internal off-screen buffers,
        // which should always match the required viewport (whole screen in our case).
        vfxManager.resize(width, height);

        shapeRenderer.getProjectionMatrix().setToOrtho2D(0f, 0f, width, height);
        shapeRenderer.updateMatrices();
    }

    @Override
    public void render() {
        // Clean up the screen.
        Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Clean up internal buffers, as we don't need any information from the last render.
        vfxManager.clear();

        // Begin render to an off-screen buffer.
        vfxManager.beginCapture();

        // Here's where game render should happen.
        // For demonstration purposes we just render some simple geometry.
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(Color.PINK);
        shapeRenderer.rect(250f, 100f, 250f, 175f);
        shapeRenderer.setColor(Color.ORANGE);
        shapeRenderer.circle(200f, 250f, 100f);
        shapeRenderer.end();

        // End render to an off-screen buffer.
        vfxManager.endCapture();
        
        vfxManager.update(Gdx.graphics.getDeltaTime());

        // Apply the effects chain to the captured frame.
        // In our case, only one effect (gaussian blur) will be applied.
        vfxManager.applyEffects();

        // Render result to the screen.
        vfxManager.renderToScreen();
    }

    @Override
    public void dispose() {
        // Since VfxManager has internal frame buffers,
        // it implements Disposable interface and thus should be utilized properly.
        vfxManager.dispose();

        // *** PLEASE NOTE ***
        // VfxManager doesn't dispose attached VfxEffects.
        // This is your responsibility to manage their lifecycle.
        vfxEffect.dispose();

        shapeRenderer.dispose();
    }
}

Result

The actual example code can be found here.