Help with Pixel Shader effect for brightness and contrast
Asked Answered
U

1

13

What is a simple pixel shader script effect to apply brightness and contrast?

I found this one, but it doesn't seem to be correct:

sampler2D input : register(s0);
float brightness : register(c0);
float contrast : register(c1);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(input, uv); 
    float4 result = color;
    result = color + brightness;
    result = result * (1.0+contrast)/1.0;

    return result;
}

thanks!

Undertrump answered 3/6, 2009 at 13:22 Comment(1)
Surely dividing by 1.0 in the second to last line of your example has no effect...Avigdor
M
31

Is this what you are looking for?

float Brightness : register(C0);
float Contrast :   register(C1);

sampler2D Texture1Sampler : register(S0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
   
   float4 pixelColor = tex2D(Texture1Sampler, uv);
   pixelColor.rgb /= pixelColor.a;

  // Apply contrast.
  pixelColor.rgb = ((pixelColor.rgb - 0.5f) * max(Contrast, 0)) + 0.5f;

  // Apply brightness.
  pixelColor.rgb += Brightness;

  // Return final pixel color.
  pixelColor.rgb *= pixelColor.a;


 return pixelColor;
}
Mandatory answered 12/6, 2010 at 5:23 Comment(4)
Hmmm. I thought this question was from this week, now I see that it was asked a year ago.Mandatory
... and is still a valuable answer in 2013. I'm wandering how hard is to add also the saturation... :)Timothytimour
@Timothytimour damn, me too! (I'll keep you guys posted if smth)Mopup
The easy way to do saturation for anyone still wondering is to convert RGB to HSL or HSV, adjust the satuation, and then convert backJacksmelt

© 2022 - 2024 — McMap. All rights reserved.