# How do i rotate a Gradient relative to the Camera position?

So i need a Gradient on a disk to rotate in such a way that it follows the Camera. This is what i got:

The Gradient and the blue line towards the Camera. And this is what i want:

Now my idea was to use Trig. Since i know the lenght to the Camera and the Y or X depth i can theoretically calculate the relative angle between the disk and the Camera depending on the Location. Like this.

However when i go to impliment this angle i get this

With these nodes:

And i dont see where the problem is. The math is correct, i have the right axis and so on. And i can clearly rotate the Gradient manually. All i do is plug the node setup above into the Input for the Gradient Z Angle. If i do it manually it works, put with the Node set up i get this weird result.

My suspicion is that Blender does the Angle calculation for all pixels of the Disk and thusly arrives at widly differnt angles for each point. Instead of Rotating them all as a group they get rotate individiually.

Now yes i could keyframe this but i would like to have this gradiant track the Camera a bit more precisly. Also i cant rotate the disk, it has to be the shader.

So are there any other ways ? What am i doing work ?

For those wondering why i need this, i am building a Black Hole Shader that tries to impliment all physical effects. This shader graident tracking stuff is to account for Doppler effect. Preview seen below:

Well here is how I would do it. The Doppler effect fades as the camera becomes more parallel with the disk's normal which I think is accurate.

The key is, the object is flat in the z = 0 plane in object space. Then I take the cross product of the object coordinate position with (0,0,1) which yields a vector representing the rotation of the object. Then I take the dot product of that vector with the camera vector which yields a value that increases when the vector points towards the camera and decreases when pointing away. I use this value to control the hue shift by connecting it to a color ramp and a hue mix node.

• Holy hell, my plan was to adress the Z change of Doppler later but you mad man solved 2 issues at once :D Works perfectly ! And it even accounts for camera rotation, so if the camera flips 180 degress the doppler effect flips as well. This is perfect ! :D Commented Jan 28, 2022 at 0:52

Your nodes look like you're trying to get the angle of the sample. You're basically doing this right, but you're not doing any quadrant correction. The sin of 45 degrees is the same as the sin of 135 degrees, so arcsin(sin(135 degrees) = 45 degrees.

If you want the angle to a sample, my preferred method is atan2:

atan2 does the quadrant correction for you (and outputs an angle in radians, ranging from -pi to pi.)

Now, as for creating a gradient that follows the camera, there's more than one thing that can mean, but it sounds like you're after something like:

Notice the object axes. The direction of the gradient follows the camera, but remains centered on the object.

• Thank you very much for the answer ! Your implimintation seems to give the exact result i was looking for as well. I went with the one of the other answer but it helps a lot to understand what the issue was, so thank you a lot for that ! Commented Jan 28, 2022 at 1:02