Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pixel shader conditional if


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 lucky6969b   Members   -  Reputation: 540

Like
0Likes
Like

Posted 04 May 2013 - 11:13 PM

//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;


Sponsor:

#2 MJP   Moderators   -  Reputation: 10243

Like
4Likes
Like

Posted 05 May 2013 - 12:17 AM

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;


#3 lucky6969b   Members   -  Reputation: 540

Like
0Likes
Like

Posted 05 May 2013 - 01:32 AM

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



#4 Adam_42   Crossbones+   -  Reputation: 2363

Like
1Likes
Like

Posted 05 May 2013 - 05:42 PM

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.



#5 lucky6969b   Members   -  Reputation: 540

Like
0Likes
Like

Posted 06 May 2013 - 12:07 AM

Thanks, it is working now.

Jack






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS