Palette Swapping, multiple textures for a material

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);

I think if the Defold cannot support 2 textures per material I have to set palette color via fragment shader constant. Now trying…

Ok, instead of using the palette image as another texture (which is more convenient). It works fine just using shader constants :slight_smile:

Hey! You should be able to use multiple textures for materials, at least for your own 3D meshes. But you might need to do some changes to your render_script and bind any secondary textures yourself. I could try whip up an example if you get stuck (I’m not at a computer at the moment), but for starters have a look here: http://www.defold.com/ref/render

Thanks @sven I’ll check it out :slight_smile:

1 Like