Search code examples
androidlibgdx

Libgdx - setting Sprite color


Is it possible to .setColor(x,x,x,1) for a border of a circle like this:

enter image description here

else I have to use 2 sprites, and I already have 500 sprites referenced. Do not want to use a 1000.


Solution

  • To do it in one pass with a custom shader, encode your source sprite in a certain way. Like this:

    enter image description here

    Now you can blend between white and your border color using the R channel as the interpolation factor. You can copy the vertex shader from the SpriteBatch source code and modify the fragment shader main function to look like this:

    vec4 texColor = texture2D(u_texture, v_texCoords);
    gl_FragColor = vec4(mix(vec3(1.0), v_color.rgb, texColor.rrr), v_color.a * texColor.a);