B4A Library SpecialFX for libGDX

Discussion in 'Additional libraries, classes and official updates' started by Informatix, Oct 17, 2014.

  1. Informatix

    Informatix Expert Licensed User

    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.:
    Code:
    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.:
    Code:
    FXParam.Initialize
    FXParam.SetFloatParam(
    "bias", skPix.Value)
    FXParam.SetFloatParam(
    "gamma", (skGamma.Value + 1) / 10)
    FXParam.SetFloatParamArray(
    "rectangle"Array As Float(0.20.20.60.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.:
    Code:
    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.
     

    Attached Files:

    Last edited: Jun 6, 2015
    Ohanian, thaonguyen, bobbx and 9 others like this.
  2. MarcTG

    MarcTG Active Member Licensed User

    Interesting, Thanks Fred :)
     
  3. Jaames

    Jaames Active Member Licensed User

    Awesome! Thanks.
     
  4. Informatix

    Informatix Expert Licensed User

    I added a new project to the examples: Hue_Saturation. It allows you to change the hue and saturation of an image in one pass. It uses a very fast algorithm suitable for real-time use.
     
    wonder likes this.
  5. Informatix

    Informatix Expert Licensed User

    I updated SpecialFX because it was not compatible with the version 1.1 of libGDX.
     
    Ed Brown, walterf25 and wonder like this.
  6. wonder

    wonder Expert Licensed 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?
     
  7. Informatix

    Informatix Expert Licensed User

    In this demo, I create a texture with all the smileys to blur and apply the effect on the texture, not invidually on each smiley (it would be too costly).
     
  8. wonder

    wonder Expert Licensed User

    I understand that, but, is it possible to make such texture transparent?
     
  9. Informatix

    Informatix Expert Licensed User

    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: Aug 1, 2016
    wonder likes this.
  10. wonder

    wonder Expert Licensed User

    Thank you so much! Awesome, as always! ;)
     
  11. wonder

    wonder Expert Licensed User

    [​IMG]
    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?
     
  12. Informatix

    Informatix Expert Licensed User

    It's compiled by OpenGL:
    https://www.opengl.org/wiki/Shader_Compilation
     
    wonder likes this.
  13. ilan

    ilan Expert Licensed 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
     
  14. Informatix

    Informatix Expert Licensed 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:
    Code:
    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);
     
    melonZgz, wonder and ilan like this.
  15. ilan

    ilan Expert Licensed 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?
     
    wonder likes this.
  16. Informatix

    Informatix Expert Licensed User

    If you change the shader of your SpriteBatch renderer, it will apply to everything rendered by this renderer.
     
  17. ilan

    ilan Expert Licensed User

    Yes, that make sense :)

    Thanks i will try it out when i get to my pc.
     
  18. ilan

    ilan Expert Licensed 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
     
  19. Informatix

    Informatix Expert Licensed 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).
     
    wonder likes this.
  20. wonder

    wonder Expert Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice