Class TextureArrayShaders

java.lang.Object
com.github.tommyettinger.textra.TextureArrayShaders

public final class TextureArrayShaders extends Object
Utility methods that return vertex or fragment shader source code to be used in TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch. These methods all require one of those mentioned Batches to have been created before any shader source can be obtained, since creating either Texture Array Batch calculates the number of texture units the GPU can handle.
These shader sources can be passed to KnownFonts.initialize(String, String, String, String, String, String, String, String) if the only batch or batches you intend to use for Font types are Texture Array Batches. To do this more easily, make sure you have constructed a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch in create() or later, then call initializeTextureArrayShaders() before using any methods from KnownFonts or creating any Font.
Mostly taken from Hyperlap2D's GitHub repo. Originally licensed under Apache 2.0, like TextraTypist and libGDX.
  • Method Details

    • defaultArrayVertexShader

      public static String defaultArrayVertexShader()
      The vertex shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch, regardless of distance field. The other vertex shaders here are simply aliases for this method.
      Returns:
      a vertex shader String that works with TextureArray batches
    • defaultArrayFragmentShader

      public static String defaultArrayFragmentShader()
      The fragment shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch when no distance field effect is in use (or for a STANDARD font).
      Returns:
      a fragment shader String that works with TextureArray batches
    • sdfArrayFragmentShader

      public static String sdfArrayFragmentShader()
      The fragment shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch when Font.DistanceFieldType.SDF is used. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      Returns:
      a fragment shader String that works with TextureArray batches
    • sdfArrayVertexShader

      public static String sdfArrayVertexShader()
      An alias for defaultArrayVertexShader(); using that method is preferred.
      Returns:
      a vertex shader String that works with TextureArray batches
    • sdfDerivativeArrayFragmentShader

      public static String sdfDerivativeArrayFragmentShader()
      The fragment shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch when Font.DistanceFieldType.SDF is used. This particular shader uses the dFdx() and dFdy() methods from GLSL, which are only defined in desktop OpenGL or in mobile/browser OpenGL ES when the extension "GL_OES_standard_derivatives" is available and enabled. This tends to look a little fuzzy compared to sdfArrayFragmentShader(), and isn't quite as fast. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      Returns:
      a fragment shader String that works with TextureArray batches
    • sdfDerivativeArrayVertexShader

      public static String sdfDerivativeArrayVertexShader()
      An alias for defaultArrayVertexShader(); using that method is preferred.
      Returns:
      a vertex shader String that works with TextureArray batches
    • sdfAdaptiveArrayFragmentShader

      public static String sdfAdaptiveArrayFragmentShader()
      Returns either sdfArrayFragmentShader() or sdfDerivativeArrayFragmentShader(), depending on whether derivatives are supported. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      Returns:
      a fragment shader String for an SDF shader
    • sdfOutlineArrayFragmentShader

      public static String sdfOutlineArrayFragmentShader()
      The fragment shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch when Font.DistanceFieldType.SDF_OUTLINE is used. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      This draws a black outline around any text with an SDF_OUTLINE distance field, and leaves the color inside the outlined area the same. If the outline's thickness isn't suitable for your purposes, you may want to adjust the "closeness" constant in this code. You can replace the text "const float closeness = 0.0625 ;" (with multiple spaces around the value to make distinguishing it easier), using any value between 0.0 and 0.5 to change the outline thickness. Lower values lead to thicker outlines, and values closer to 0.5 should lead to thinner.
      Returns:
      a fragment shader String that works with TextureArray batches
    • sdfOutlineArrayVertexShader

      public static String sdfOutlineArrayVertexShader()
      An alias for defaultArrayVertexShader(); using that method is preferred.
      Returns:
      a vertex shader String that works with TextureArray batches
    • sdfOutlineDerivativeArrayFragmentShader

      public static String sdfOutlineDerivativeArrayFragmentShader()
      The fragment shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch when Font.DistanceFieldType.SDF_OUTLINE is used. This particular shader uses the dFdx() and dFdy() methods from GLSL, which are only defined in desktop OpenGL or in mobile/browser OpenGL ES when the extension "GL_OES_standard_derivatives" is available and enabled. This tends to look a little fuzzy compared to sdfOutlineArrayFragmentShader(), and isn't quite as fast. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      This draws a black outline around any text with an SDF_OUTLINE distance field, and leaves the color inside the outlined area the same. If the outline's thickness isn't suitable for your purposes, you may want to adjust the "closeness" constant in this code. You can replace the text "const float closeness = 0.0625 ;" (with multiple spaces around the value to make distinguishing it easier), using any value between 0.0 and 0.5 to change the outline thickness. Lower values lead to thicker outlines, and values closer to 0.5 should lead to thinner.
      Returns:
      a fragment shader String that works with TextureArray batches
    • sdfOutlineDerivativeArrayVertexShader

      public static String sdfOutlineDerivativeArrayVertexShader()
      An alias for defaultArrayVertexShader(); using that method is preferred.
      Returns:
      a vertex shader String that works with TextureArray batches
    • sdfOutlineAdaptiveArrayFragmentShader

      public static String sdfOutlineAdaptiveArrayFragmentShader()
      Returns either sdfOutlineArrayFragmentShader() or sdfOutlineDerivativeArrayFragmentShader(), depending on whether derivatives are supported. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      Returns:
      a fragment shader String for an SDF shader
    • msdfArrayFragmentShader

      public static String msdfArrayFragmentShader()
      The fragment shader used for any rendering with a TextureArrayPolygonSpriteBatch or TextureArrayCpuPolygonSpriteBatch when Font.DistanceFieldType.MSDF is used. This shader has the uniform u_smoothing and expects to be used with defaultArrayVertexShader() as its vertex shader (but any vertex shaders here are the same).
      Returns:
      a fragment shader String that works with TextureArray batches
    • msdfArrayVertexShader

      public static String msdfArrayVertexShader()
      An alias for defaultArrayVertexShader(); using that method is preferred.
      Returns:
      a vertex shader String that works with TextureArray batches
    • initializeTextureArrayShaders

      public static void initializeTextureArrayShaders()
      This is a convenience method to initialize the shaders in KnownFonts so they work with TextureArrayPolygonSpriteBatch and/or TextureArrayCpuPolygonSpriteBatch. This can only be called after one of those mentioned Batches has already been constructed, in create() or later. Because this calls KnownFonts.initialize(String, String, String, String, String, String, String, String), it can't be called after any methods from KnownFonts have already been called, or after any Font is created. Unlike initializeAdaptiveTextureArrayShaders(), this always uses the same shaders on all platforms. It doesn't ever use derivatives in shaders.
    • initializeAdaptiveTextureArrayShaders

      public static void initializeAdaptiveTextureArrayShaders()
      This is a convenience method to initialize the shaders in KnownFonts so they work with TextureArrayPolygonSpriteBatch and/or TextureArrayCpuPolygonSpriteBatch. This can only be called after one of those mentioned Batches has already been constructed, in create() or later. Because this calls KnownFonts.initialize(String, String, String, String, String, String, String, String), it can't be called after any methods from KnownFonts have already been called, or after any Font is created. This variant will adapt the SDF shaders depending on whether derivatives are available, and will use them if they are. If used heavily, the shaders that use derivatives may not perform as well as the ones that don't. They may look better (subjectively) when used, which can only be on desktop and mobile platforms right now (no web browsers).