Hi there,
I want to do Palette Swapping in my game and I found an article that described as below. But, right now, it seems that Defold currently supports only a single texture per material. So, how can I do Palette Swapping in Defold?
Thanks.
Emulated Palette Swapping (GLES 2.0)
Emulating the palette swap is as simple as creating two textures. One, a 1xN color texture using a texture filter mode of GL_NEAREST to hold the colors of your palette and the other, a luminance texture where the brightness is really the color index. Then in your shader you do this:
uniform sampler2D image;
uniform sampler2D palette;//use a Sampler1D on regular OpenGL
varying vec2 texCoord;
vec4 index = texture2D(image, texCoord);
//the math is to make the indexes hit the pixel centers)
gl_FragColor = texture2D(palette, vec2(0,index.r);
Unlike traditional palette swapping, this method also allows you to use colors in between the palette entries. For example if your palette has 16 entries, but your index texture is 8bpp the colors 0 and 16 represent colors 0 and 1 respectively. Color 8 would be a blend half way between those colors. To do this, your texture filter mode needs to be GL_LINEAR. There’s a catch to this though, the index values will need to be offset to hit pixel centers by doing this: texture2D(image, vec2(0,index.r*255/256 + .5/256);