How can I generate a random int from a short range (1-16, for example) inside a .shader? And if it’s possible: Is it cheap or costly to do?
The way this is generally done in shaders is through the use of a pre-generated noise texture. It would be done in a similar way to the following;
int Random (int min, int max, float2 uv) //Pass this function a minimum and maximum value, as well as your texture UV
{
if (min > max)
return 1; //If the minimum is greater than the maximum, return a default value
float cap = max - min; //Subtract the minimum from the maximum
int rand = tex2D (_NoiseTex, uv + _Time.x).r * cap + min; //Make the texture UV random (add time) and multiply noise texture value by the cap, then add the minimum back on to keep between min and max
return rand; //Return this value
}
For the texture itself, I’d reccomend just generating some white noise and running a slight gaussian blur on it, so that the values are gradual from 0 to 1.