Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

Pixel shader conditional if

This topic is 2045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

//Pixel Shader

float4 ps_lighting(VS_OUTPUT IN) : COLOR0

{

 

    float4 color = tex2D(DiffuseSampler, IN.tex0);

    

    return IN.shade * color * materialColor;

}

How can I make a branch (if) if color has nothing in it?

So that

float4 color = tex2D(DiffuseSampler, IN.tex0);
if (color == 0)
     return IN.shade * materialColor;
else
     return IN.shader * color * materialColor;

Share this post


Link to post
Share on other sites
Advertisement

HLSL actually has the "any" intrinsic, which returns true if any of the components of a vector are non-zero. So you could use it like this:

 

float4 color = tex2D(DiffuseSampler, IN.tex0);
if (any(color))
     return IN.shader * color * materialColor;
else
    return IN.shade * materialColor;

Share this post


Link to post
Share on other sites

Hello MJP,

I have a problem,

since tex2D will return black for some time.

since float4(0, 0, 0, 0) is a color, so it will go to return IN.shader * color * materialColor; instead.

Do I have to take other things into account?

Thanks

Jack

Share this post


Link to post
Share on other sites

Note that black can have any alpha value so float4(0,0,0,1) is also black, and will pass the test because the alpha is non-zero.

 

If that's the problem then use "if (any(color.rgb))" instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!