Sign in to follow this  
lucky6969b

Pixel shader conditional if

Recommended Posts

lucky6969b    1330

//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
MJP    19754

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
lucky6969b    1330

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
Adam_42    3629

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this