Color swap flickering

I’ve come across a weird behavior of my code today, and I hope you can help me figure out this problem. I’m trying to implement a simple color swap code. Here’s the line responsible for swapping colors:

col.rgb = tex2D( _BookPallete, float2( pow( col.r, 1/2.2), 0.0));

Unfortunately, this line is causing some flickering in the pallet swap, as you can see right here:

I’ve spent hours trying to figure this out, and in one attempt I’ve tried to write this same line using a switch statement (going crazy trying to figure this out).

switch( ceil( pow( col.r, 1/2.2) * 5))
                {
                    case 0:
                        col.rgb = tex2D(_BookPallet, float2( 0.0, 0.0)).rgb;
                    break;
                    case 1:
                        col.rgb = tex2D(_BookPallet, float2( 0.2, 0.0)).rgb;
                    break;
                    case 2:
                        col.rgb = tex2D(_BookPallet, float2( 0.4, 0.0)).rgb;
                    break;
                    case 3:
                        col.rgb = tex2D(_BookPallet, float2( 0.6, 0.0)).rgb;
                    break;
                    case 4:
                        col.rgb = tex2D(_BookPallet, float2( 0.8, 0.0)).rgb;
                    break;
                    case 5:
                        col.rgb = tex2D(_BookPallet, float2( 1.0, 0.0)).rgb;
                    break;
                }

Weirdly enough this actually solves the problem, but I don’t want to keep a gigantic switch in my fragment shader, so, does anyone can explain the issue with my first line of code?

Additional information: This is a panel rendering a render texture, so the main camera is rendering into a render texture, not directly into the Display. Camera recording the game has ProCamera 2D pixel-perfect on it. I’m not sure any of this actually explains this weird code behaviour… but who knows.

Book original sprite

_BookPallet texture

Thanks in advance

Can you check your palette texture settings? Are you using linear or point sampling mode? And try setting the repeat mode to clamped too. And it might a good idea to perform the lookup on the palette texture in the middle of the pixel. Those have been some of the culprits in my simple shader tests. But I’m a bit tired so I might not see the obvious point here, so sorry for that! :slight_smile:

EDIT: also check those texture scaling settings. If you use a palette texture that is not power of two, be sure to set it to “none” so that it’s not scaled. Those small palette textures might get altered too much. But that’s not the reason (most likely) as that flickering is happening.

P.S. When posting reference to textures you might maybe 32x them :).