Frag Shader and RGBA Colors

Started by
2 comments, last by L. Spiro 8 years, 11 months ago

So I'm trying to move up in the world when it comes to shaders. And I figured adding color tinting and transparency to my spritebatcher would be a good place to start.

I have added the ability to use a Color object in some of my methods. Where this object is made up of 4 float components for RGBA
And I have also added a separate AlphaValue for how transparent a sprite is even if its tinted by a color.

Now I'm wondering, can some one tell me if my idea is right?
That I should be multiplying the Alpha of my color tint against its own RGB before adding it to the over all texture color?

My fragment shader looks like this:


//Get the texture pixel data from the sampler
vec4 texColor = texture2D(texture, uvCoords);
 
//Make the image more transparent based on the alpha value
texColor *= alphaValue;
 
//Add the tint color
//Not sure part: multiple the tint color's RGB by its alpha then add it to the color of the texture
//And multiple it by the texture's alpha
texColor = (textColor + (tintColor.xyz * tintColor.w)) * textColor.a;
gl_FragColor = textColor;
 


When it comes to using methods that do not allow a Color object or tinting. EG: Draw(texture, xPos, yPos) vs Draw(texture, tintColor, xPos, yPos)

The color vertex data for the non tint color method is filled with 0s


VertexData[COLOR_R] = 0.0f;
VertexData[COLOR_G] = 0.0f;
VertexData[COLOR_B] = 0.0f;
VertexData[COLOR_A] = 0.0f;
Advertisement
#1: yes your tint should be modulated by its own alpha value.
#2: Tinting should be a modulate, not addition, unless you are going for a specific effect.
#3: Unless your pipeline is set up to handle premultiplied alpha you should not be multiplying your final color with its alpha. Send the color as-is out of the shader and let hardware alpha-blending handle it for you. You have to use alpha blending anyway in order to not entirely overwrite the back buffer.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

So I went back and changed my fragment shader to the following:


void main()
{
    vec4 texColor = texture2D(textureSampler, texCoordsOUT);
    texColor.a *= alpha;
    texColor.rgb *= (colorOUT.xyz * colorOUT.w);
 
    gl_FragColor = texColor;
}

And I noticed that when using pure white ( 1.0f, 1.0f, 1.0f, 1.0f) as tint color it has no effect. Any texture tinted pure white just comes out looking the same.
Is this normal or is my shader code wrong?

I would think reds would become pinkish, other colors would be come 'lighter', and etc

As I said, it depends on what kind of effect you want. Perhaps addition is better for you. X * 1.0 = X, so I’m not sure why you would question if that is normal or not. That is clearly the normal result of multiplying everything by 1, but if that is the result you actually want is another story. Again, perhaps addition was correct for you all along, depending on what results you were expecting.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement