In a pixel shader I calculate the probability of 4 items in RGBA (R+G+B+A = 1.0). Now I want to randomly pick one weighted by its probability. For example, lets say that R=0.6 and A = 0.4 and G,B = 0. That means that 60% of the time I want to pick R and 40% of the time I want to pick A. An easy but lengthy way to do this would be to make X = R, Y = R+G, Z = R+G+B, choose a random number between 0 and 1.0, then test the random number against XYZ:
Is there an easier way based on some HLSL PS instruction?
Thanks
Code:
float4 sum;
float random;
sum.X = prob.R;
sum.Y = prob.R + prob.G;
sum.Z = prob.R + prob.G + prob.B;
random = MyRandomFunction();
if( random < sum.X )
pick R
else if( random < sum.Y )
pick G
else if( random < sum.Z )
pick B
else
pick A
Is there an easier way based on some HLSL PS instruction?
Thanks