Can't get a palette shader to work on sprites

This is my shader. It’s supposed to draw an image that uses its red channel to correspond with the index of the color in the palette.

    #define SV_POSITION POSITION
    #define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_4_0_level_9_1
    #define PS_SHADERMODEL ps_4_0_level_9_1
#endif

#define PaletteSize 32

Texture2D SpriteTexture;
float4 Palette[PaletteSize];

sampler2D SpriteTextureSampler = sampler_state
{
    Texture = <SpriteTexture>;
};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Color : COLOR0;
    float2 TextureCoordinates : TEXCOORD0;
};

float4 NormalDraw(VertexShaderOutput input) : COLOR
{
    return tex2D(SpriteTextureSampler,input.TextureCoordinates) * input.Color;
}

float4 DrawWithPalette(VertexShaderOutput input) : COLOR
{
    float4 texcolor = tex2D(SpriteTextureSampler,input.TextureCoordinates);

    int index = clamp(texcolor.r * PaletteSize, 0, PaletteSize);

    return Palette[index] * texcolor.a;
}

technique NormalSpriteDrawing
{
    pass P0
    {
        PixelShader = compile PS_SHADERMODEL NormalDraw();
    }
};

technique PalettedSpriteDrawing
{
    pass P0
    {
        PixelShader = compile PS_SHADERMODEL DrawWithPalette();
    }
};

This draws it as a pure white image, shown here https://cdn.discordapp.com/attachments/121565307515961346/372643580105195522/unknown.png

The code I’m doing to set it up…

--- GameplayScreen tells everything to draw

spritebatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, _fx, _camera.GetMatrix());

--- The entity that's drawing it.

public virtual void Draw(SpriteBatch spritebatch, Effect effect)
        {
            effect.CurrentTechnique = effect.Techniques["PalettedSpriteDrawing"];
            SetPalette(effect);
            effect.CurrentTechnique.Passes[0].Apply();

            //Draw underside cannons
            foreach (var gun in MainGuns)
            {
                gun.Draw(spritebatch, effect);
            }

            foreach (var gun in SubGuns)
            {
                gun.Draw(spritebatch, effect);
            }

            foreach (var launcher in MissileLaunchers)
            {
                launcher.Draw(spritebatch, effect);
            }

            //Draw thrusters
            DrawThrusters(spritebatch, effect);

            //Draw main body
            TextureInfo.Draw(spritebatch, Transform.Position, Transform.Rotation, Origin, Vector2.One);

            //Draw mounted heavy weapon
            foreach (var heavy in MountedHeavyWeapons)
            {
                heavy.Draw(spritebatch, effect);
            }
            
            effect.CurrentTechnique = effect.Techniques["NormalSpriteDrawing"];
            effect.CurrentTechnique.Passes[0].Apply();
        }

There is no other code other than this that effects spritebatch or the effect. Am I using the effect wrong? Is there a problem with the shader? It’s displaying as all white and my palette of 32 colors has no white in it at all. I think I’ve provided all the information I can. Thank you for your help.

1 Like

Can you try visualizing your palette to make sure that it has the right colors?

Yeah, I’ve done that already using step debugging. I’ve confirmed it at 4 spots, on creation in the ship file, on giving it to the effect, after giving it to the effect (by checking elements) and even using GetValue, and they all match. I’m positive it has nothing to do with the palette values themselves, as it only shows everything as white and sometimes very rarely a cyan that is in my palette. The color being outputted is 255, 255, 255 white, which is not in my palette at all. I’m wondering if I messed up somewhere else. The is another oddity as well. Let’s say instead of “float3 color = Palette[index];” I put “float3 color = Palette[5];”. This crashes with an error when Apply is hit. https://cdn.discordapp.com/attachments/121565307515961346/372662222251622400/unknown.png The only number I can use for this is 31, and my palette is of size 32. I have no idea what’s going on.

Turns out that my problem was that I was giving the colors integer values for the constructors as that’s all I use, but I wasn’t properly converting that to float when I made the palette. Just one of those oddities. Guess it’s time to figure out how to lock this…

1 Like

Why is this so difficult?: