Several days ago I asked about this question: How to construct a propeller clock in Unity? But I don't understand how the answer works.
I have 0 knowledge about shaders, so I picked up a tutorial to understand the math functions, but that still didn't helping much in understanding the code.
I cut off part of the code to check the effect on an image, and I want to understand why the code has this effect.
What is the range of values for the parameters
i.uv.y
ori.uv.x
?Next I discarded part of the code, simplifying the fragment shader to just:
fixed4 frag (v2f i) : SV_Target { // Simulate the strip of LEDs spinning around very fast. float headAngle = _Time.y ; // Compute the angle of the pixel we're rendering. float angle = atan2(i.uv.y, i.uv.x); float difference = frac( angle / (2.0f * 3.141592653589f)); return difference; }
And I rendered this on a plane:
According to the documentation,
atan2
gives an angle value in between -pi and pi. So the range ofangle / (2.0f * 3.141592653589f))
is in between -0.5 and 0.5.I tested with
frac(0.3)
,frac(0.25)
, which is the same asreturn 0.3
orreturn 0.25
sincefrac
preserves only the decimal number. But why I couldn't generate the same gradient image as the one above? Instead, the plane is all one shade of grey, no matter what number I use.Next I tried the code
float difference = frac(headAngle)
and got an image which is constantly varying between dark and bright images like these:Putting this together:
From the 1st picture, I have an image with a gradient that's dark in the 1st quadrant and bright in the 4th quadrant.
From the 2nd & 3rd pictures, I have all four quadrants at the same brightness, constantly varying between them.
What I don't understand is how the sum of these two pieces of code works. If I combine them like this:
float difference = frac(headAngle + angle / (2.0f * 3.141592653589f));
Shouldn't I get a gradient that stays in one place, like the 1st image, but just changes in brightness like in images 2 & 3?
Instead I get a A brightness gradient that is sweeping in a circle around the plane:
...and I don't understand why.