Colour effects are removed when using a Shader

I’m having a curious problem. When I apply colour tints to sprites using sprite batch, they work perfectly fine. However, the moment I use a shader in this sprite batch call, even if the shader simply returns the same colour, it removes all colour tint effects. The problem was mentioned exactly here, but the link in the solution (in the comments) is broken.
Any help would be appreciated.

Thanks

The Color tinting is most likey sent to the shader via a Color Element in the Vertex Structure - your shader has to use it otherwise it’s … not gonna be used …

You can create a default shader for the sprite batch using the mgcb-editor and make use of the color that should be available as input.

None of these solutions seem to work, I can’t seem to find the issue mentioned anywhere apart from that one question I linked in my original question. Anyone have any ideas?

Without supplying more information about your shader or your code, noone will be able to give you more information than you already received.

You asked about tinting, the link you posted basically talks about alpha blending - and for both solutions were supplied (even tho the link there isnt working anymore). Your Shader has to make use of the color value and your spritebatch’s BlendState has to be set to the appropriate value (like AlphaBlend instead of NonPremultiplied). As everything works fine with regular SpriteBatch, the problem has to be in your shader doing things differently than the stock implementation.

Without seeing your shader, we can’t help. Most likely you do not multiply the color sampled from the texture with the inputs Color before returning in the Pixelshader.

As mentioned above, the tinting is not happening in the shader. I meant the tinting done by passing a colour as a parameter to the spritebatch.draw method. This is exactly the same phenomenon mentioned in the linked post. The shader code was not worth posting, since no matter what the shader code is (just like I mentioned in my original post and mentioned in the linked question), the tinting is ignored.

I think you are misinterpreting the problem as my shader being the thing that tints the sprite, when in fact the shader is doing absolutely nothing.

The shader is supposed to be what tints the sprite. The shader SpriteBatch uses by default does this, so when you write your own shader, you’ll need to use the vertex’s color to tint the sprite yourself in the same way.

So to see if I’m understanding, the color parameter in spritebatch.draw is completely redundant if a shader is passed from spritebatch.begin?

Your shader can read it if you want. It’s just that SpriteBatch sends a VertexPositionColorTexture vertex attribute to the shader that’s being used.

If you want other information to be included in the info sent per-vertex for your shader, you’re going to have to write the vertex and index buffers yourself.

1 Like

Thank you ever so much, I’m sorry for the confusion. I’ve gotten it working now. I was trying to use the alpha channel as a rudimentary G Buffer to store depth values for deferred lighting.

Nice you got it working.

About using the Alpha as Depth - be aware, that the Alpha Part of a Color is just 8 bits so it’s really limited on precision for storing a regular depth value - if that depth is some value you dont need to be overly precise, then it’s alright. Afaik color (rgba) is stored as 4 bytes and only the GPU will handle it as a “float” value of 0-1, so make sure you work with thresholds if you need any exact representation of it

That’s a good point, but since my game is 2D, the depths will only be discrete layers, so there’s no need for smooth continuous depth values, so the alpha channel is more than suitable enough