How can I get my palette-swap shader to work with semi-transparent sprites?

T

TaleLore

Guest
I've made a palette-swap shader that works fine if I only have fully-opaque and fully-transparent pixels in my sprites, but if I have a semi-transparent part of a sprite, then that part of the sprite doesn't look palette-swapped.

Here's the code:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D old_palette;
uniform sampler2D new_palette;

void main()
{   
    int num_colours = 8;
    float threshold = 0.00000005;

    vec4 c = texture2D(gm_BaseTexture, v_vTexcoord);
    vec4 new_c = vec4(c.r, c.g, c.b, c.a);
       
    for (int i = 0; i < num_colours; i++) {
        if ( distance(c.rgb, texture2D(old_palette, vec2(float(i) / float(num_colours), 0.0)).rgb) < threshold) {
            new_c.rgb = texture2D(new_palette, vec2(float(i) / float(num_colours), 0.0)).rgb;
        }
    }
    gl_FragColor = v_vColour * new_c;
}
I've looked everywhere for a solution to fix this but I can't. I'm not even sure what the problem is. Do you have any ideas?
 
I

icuurd12b42

Guest
your code does not seem to modify the .a element so I have no idea either
 

Simon Gust

Member
I think you have to take the alpha of
I've made a palette-swap shader that works fine if I only have fully-opaque and fully-transparent pixels in my sprites, but if I have a semi-transparent part of a sprite, then that part of the sprite doesn't look palette-swapped.

Here's the code:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D old_palette;
uniform sampler2D new_palette;

void main()
{  
    int num_colours = 8;
    float threshold = 0.00000005;

    vec4 c = texture2D(gm_BaseTexture, v_vTexcoord);
    vec4 new_c = vec4(c.r, c.g, c.b, c.a);
      
    for (int i = 0; i < num_colours; i++) {
        if ( distance(c.rgb, texture2D(old_palette, vec2(float(i) / float(num_colours), 0.0)).rgb) < threshold) {
            new_c.rgb = texture2D(new_palette, vec2(float(i) / float(num_colours), 0.0)).rgb;
        }
    }
    gl_FragColor = v_vColour * new_c;
}
I've looked everywhere for a solution to fix this but I can't. I'm not even sure what the problem is. Do you have any ideas?
I guess you could test if it works if you force it
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D old_palette;
uniform sampler2D new_palette;

void main()
{ 
    int num_colours = 8;
    float threshold = 0.00000005;

    vec4 c = texture2D(gm_BaseTexture, v_vTexcoord);
    vec4 new_c = vec4(c.r, c.g, c.b, c.a);
      
    for (int i = 0; i < num_colours; i++) {
        if ( distance(c.rgb, texture2D(old_palette, vec2(float(i) / float(num_colours), 0.0)).rgb) < threshold) {
            new_c.rgb = texture2D(new_palette, vec2(float(i) / float(num_colours), 0.0)).rgb;
        }
    }
    gl_FragColor = vec4(new_c.r, new_c.g, new_c.b, c.a);
    // or even
    gl_FragColor = vec4(new_c.r, new_c.g, new_c.b, 0.5);
}
 
Top