B4A Library SpecialFX for libGDX

SpecialFX is a plugin for advanced users of libGDX. This helper for special effects will save you some headaches if you want to combine shaders and/or make multiple passes with a given shader. It allows an easy implementation of the ping-pong technique.
To use this plugin, you have to know how to create and use a shader program, what's a texture unit, and how to create and use a FrameBuffer object.

How to use it:
1) Declare in Globals the chain of effects (lgSpecialFX) and its parameters (lgSpecialFX_Param), e.g.:
B4X:
Dim RetroFX As lgSpecialFX
Dim RetroFX_Param As lgSpecialFX_Param

2) In LG_Create or LG_Resize, initialize the Param class, set the parameters, and create your chain of effects by adding the effects in the exact order of their application, e.g.:
B4X:
FXParam.Initialize
FXParam.SetFloatParam("bias", skPix.Value)
FXParam.SetFloatParam("gamma", (skGamma.Value + 1) / 10)
FXParam.SetFloatParamArray("rectangle", Array As Float(0.2, 0.2, 0.6, 0.6))
FX.AddEffect(MipMapShader, FXParam, Null, FirstBuffer, "FX")
FX.AddEffect(GammaShader, FXParam, FirstBuffer, SecondBuffer, "FX")

3) Call the Apply function in LG_Render. You can also set dynamic parameters in LG_Render, e.g.:
B4X:
NightVisionFX_Param.SetFloatParam("cosTime", MU.Cos(Time * 1000))
NightVisionFX.Apply(Null)

The examples show some common post-processing FX (blur, bloom, pixelation, gamma, night vision, vignette, hue and saturation). If you want to use the blur algorithm of Kasawe, here are various settings (the key of this map is the blur kernel size):
Iterations.Put(7, Array As Int(0, 0))
Iterations.Put(15, Array As Int(0, 1, 1))
Iterations.Put(23, Array As Int(0, 1, 1, 2))
Iterations.Put(35, Array As Int(0, 1, 2, 2, 3))
Iterations.Put(63, Array As Int(0, 1, 2, 3, 4, 4, 5))
Iterations.Put(127, Array As Int(0, 1, 2, 3, 4, 5, 7, 8, 9, 10))

The library is provided with its Java source code.
 

Attachments

  • LibGDX_SpecialFX_Examples.zip
    504.4 KB · Views: 598
  • LibGDX_SpecialFX + source v1.0.zip
    12.3 KB · Views: 543
Last edited:

wonder

Expert
Licensed User
Longtime User
Hi!

Regarding the Smileys example, when I reversed the "layer" order, I realized that the blurred "layer" is opaque.
Is there a way to draw the blurred smileys on top of the non-blurred ones?
 

Informatix

Expert
Licensed User
Longtime User
I understand that, but, is it possible to make such texture transparent?
To make it transparent, you should:
- draw the smileys to blur on a transparent background (GL.glClearColor(0, 0, 0, 0))
- set the alpha value in the fragment shader (gl_FragColor.a = texture2D(u_texture, vTexCoord).a)

EDIT: I fixed a mistake above and tested the code. It works.
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Capture.png

It works!! Thank you so much, once again! :)

By the way, just a curiosity... given the shader programs being uncompiled, is there a C compiler hidden somewhere in LibGDX or OpenGL?
If so, can we access it and use it for general purpose?
 

ilan

Expert
Licensed User
Longtime User
hi

can i create with specialfx an old game boy color theme look for my game? or turn rgb images to gray?

thanx

EDIT: something like this:

emulator-pokemon-featured.jpg
 

Informatix

Expert
Licensed User
Longtime User
hi

can i create with specialfx an old game boy color theme look for my game? or turn rgb images to gray?

thanx

EDIT: something like this:

View attachment 47786
Of course and that costs almost nothing as you replace just a color by another. But you don't need specifically SpecialFX, you can do that with libGDX alone. You just have to change the shader of your lgSpriteBatch renderer. Your fragment code for a gray scale will look like this:
B4X:
vec4 inputTexture = texture2D(u_texture, vTexCoord.xy);
float grayscale = inputTexture.r * 0.299 + inputTexture.g * 0.587 + inputTexture.b * 0.114;  
gl_FragColor = vec4(grayscale, grayscale, grayscale, 1.0);
 

ilan

Expert
Licensed User
Longtime User
thank you very much @Informatix i will try it out (after i will understand what you did in that code :confused::D)

do i have to do that to each texture or can i do it for the whole screen?
 

ilan

Expert
Licensed User
Longtime User
Of course and that costs almost nothing as you replace just a color by another. But you don't need specifically SpecialFX, you can do that with libGDX alone. You just have to change the shader of your lgSpriteBatch renderer. Your fragment code for a gray scale will look like this:
B4X:
vec4 inputTexture = texture2D(u_texture, vTexCoord.xy);
float grayscale = inputTexture.r * 0.299 + inputTexture.g * 0.587 + inputTexture.b * 0.114; 
gl_FragColor = vec4(grayscale, grayscale, grayscale, 1.0);

hi again,

i had not much time to deal with your example and now i try to understand but i cant :(

i cant find a vec4 object in libgdx so how can i change the whole shader color and draw all sprites gameboy style

if you have a simple example that would be awesome.

thank you for your help, ilan
 

Informatix

Expert
Licensed User
Longtime User
hi again,

i had not much time to deal with your example and now i try to understand but i cant :(

i cant find a vec4 object in libgdx so how can i change the whole shader color and draw all sprites gameboy style

if you have a simple example that would be awesome.

thank you for your help, ilan
You have to learn how to use shaders. They require two source files: the vertex and the fragment shaders, which are written in GLSL. Look at the libGDX examples (e.g. BrightnessContrast).
 
Top