Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Can't get a palette shader to work on sprites

$
0
0

@Plessy wrote:

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.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles