Shaders

This post will illustrate a minimum code sample to create a Shockwave Shader.

The end result being

This does not explain how Shaders work. For that a good start is:

  1. LibGDX Shader wiki
  2. The book of Shaders.

In this code sample I simply take a simple Shockwave / ripple Shader available on Shadertoy and demostrate how it can draw as an actor in a Scene2D.

This hopefully will give you the basics to translate more shaders into your game.

The Shadertoy shader used as the base is available here

How this works:

The shader is applied to a texture and rendered using the spritebatch. With Scene2D the spriteBatch is rendered through the draw method, called on each of the actors on the stage.

In order to apply the shader we create a Group Actor, add it to the stage and apply the shader to all the Group children: Anything you want to be passed through the shader is simply added to the Group rather than the Stage.

When the Group is drawn, it creates a Framebuffer, draws all the children in the frambuffer and then applies the Shader to the Framebuffer texture.

This then can be used for example for explosions or other ripple effects.

The Source Code is available on github here

Advertisements