i recently came across a new function while trying to make a shader which uses a Toon ramp projected onto the object in ObjectSpace (thanks to Scroodge) which is the ‘frac()’ math function:
// Here's a line from my pixel shader
fixed4 diff = tex2D( _Ramp, frac( i.wPos ).xy );
As you can see, nothing special, just defining the texture’s uvs by using a world-based position (wPos), but wether I use frac() or not, I always get the same results visually, and I was wondering what is this function supposed to do? I looked it up on google and found some in-depth( way too in-depth) explanations which didn’t make much sense to me…I need a ‘dumbed-down’ explanation hahaha.
If anyone could enlighten me it would make my self-induced headache go away and i would be a happy man
First off, I’ve never programmed any Unity shaders.
But, by inferring from the context that it is used, I would say to strips the leading values from a floating point number.
For example: frac(1.23) would return ‘0.23’, frac(123.4) would return ‘0.4’.
I’m deducting this because it is being used for a texture coordinate, which may be expecting a value between 0.0 and 1.0 (or this shader requires this range).
[Edit] I never noticed the ‘.xy’ at the end, so now I have my doubts!