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: 695
  • LibGDX_SpecialFX + source v1.0.zip
    12.3 KB · Views: 645
Last edited:

ilan

Expert
Licensed User
Longtime User
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).

now i understand your code snipped above.
i had to change it in the .txt file. i thought it is a java snipped and i need to convert it to b4a :confused:

thank you very much fred, i will remember your great help when i become rich from my box2d games :D;)

btw i put a little bit green to it to get the old gameboy style :)

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 + 0.1, grayscale, 1.0);
 

ilan

Expert
Licensed User
Longtime User
ok i was celebrating to early :(

changing the txt file work on the example project but when i add it to my game (just for testing) i am not getting the result i need.
i was expecting everything will be drawn gray but this is not the case.
do i have to do something with the vertex.txt file too?
 

ilan

Expert
Licensed User
Longtime User
ok i got it.

the alpha settings was the problem, if it is set to 1.0 then transparent turns to white (or something else..) end everything looks weird.
changing it by using the texture alpha solved the problem.

fragment shader program:

B4X:
precision mediump float;

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform mat4 u_projTrans;

void main() {
vec4 color = texture2D(u_texture, v_texCoords);
float gray =  color.r * 0.299 + color.g * 0.587 + color.b * 0.114;
gl_FragColor =  vec4(gray, gray + 0.08, gray, color.a);
}

vertex.txt:

B4X:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;

varying vec4 v_color;
varying vec2 v_texCoords;

void main() {
    v_color = a_color;
    v_texCoords = a_texCoord0;
    gl_Position = u_projTrans * a_position;
}

RESULT:



ps, i have uploaded both txt files if someone would like to make an old gameboy style game (something i am planing to do)
he can use it. :) if you would like to reduce/add more green to it just change the green value in gl_FragColor = vec4(gray, gray + 0.08, gray, color.a)
 

Attachments

  • vertex.txt
    282 bytes · Views: 275
  • gameboy.txt
    327 bytes · Views: 256
Top