8
$\begingroup$

The Cycle's math node has an "Inverse tangent" option. This function however cannot discriminate between two opposite points on the circle, as they have the same tangent. Usually, this is solved with a function called atan2, which receives two inputs, the x and y of the vector, and returns the angle. Can I construct something similar in Cycle's nodes?

$\endgroup$
7
$\begingroup$

EDIT July 2018

If you are using Blender 2.8 or a recent build of 2.79, the math node now has the atan2 operation since this commit by Lukas Stockner.

Blender 2.8 Artan2 node


This is in no way the most efficient way of doing it, but just a reminder that knowing a bit of math, you can painstakingly craft functions using NodeGroups.

arctan2 nodetree Things to know:

  • The Maximum Math node acts as an OR boolean operation.
  • The Minimum Math node acts as an AND boolean operation.
  • The Substract Math node acts as a NOT boolean operation with 1 as first input and your value as second.
  • The Mix Colors node acts as a switch with a boolean as first input, which returns second input if condition == False, or third input if condition == True.

http://en.wikipedia.org/wiki/Atan2

http://en.wikipedia.org/wiki/De_Morgan%27s_law

$\endgroup$
  • $\begingroup$ Thanks! I was aware of this method, but couldn't imagine it's the simplest way... If no one else answers in a few days I'll accept it as the answer. $\endgroup$ – olamundo Jul 4 '14 at 13:11
  • $\begingroup$ From what I gather, this is the only method, except modifying the source code... $\endgroup$ – Pisurquatre Jul 4 '14 at 15:11
  • $\begingroup$ Good job, but I have tried this setup and it fails to identify that y = 0 (the greater than 0 OR less than 0, negated). I replaced it with a more pragmatic "less than 0.00001" after an absolute node and it worked. Also, the switch should produce PI, not 0 (black). $\endgroup$ – Watcom Jul 30 '15 at 0:27
6
$\begingroup$

The simplest way would be an OSL script. Drawback is no GPU suport.

#include "stdosl.h"

shader atan2(
    float X = 0.0,
    float Y = 0.0,

    output float result = 0.0 )
{
    result = atan2(X,Y);
}

Paste that into a text block and select it in a script node.

$\endgroup$
  • $\begingroup$ I like this, but when I try it, the node puts out only a constant. Is this source correct? $\endgroup$ – DarenW May 23 '15 at 21:54
  • 1
    $\begingroup$ Found the trouble! There's a secret checkbox in the render settings which one must turn on, and is never mentioned in any tutorial/intro that I've seen so far. Thanks to Blender S.E. blender.stackexchange.com/a/20876/606 $\endgroup$ – DarenW May 24 '15 at 1:28
4
$\begingroup$

Very old question, I know. However, here's an alternative (and simpler) node-based solution to produce the atan2(x,y) function.

atan2 nodes

The difference between 'arctangent' and 'atan2' is that the 'arctangent' only provides unique values for one half of the rotation - the other have returns duplicate results. In order to extract the second half of the rotation (as is available from atan2(X,Y)) we need to use the sign of one of the input values (in this case X) to determine when we are in the second half of the full rotation.

The Less Than node detects the change in sign while the Multiply changes the 0.0/1.0 output of the Less Than into either 0.0 or 'pi' (which represents a 'half turn' (180 degrees) since we're working in radians rather than degrees). This is then added into the result of arctangent(Y/X) and, finally, adjusted by a quarter of a turn (pi/2 radians) to match the output from 'atan2(...)'.

In order to test this I compared it to the OSL example provided by @sambler to prove that it produces the same result as the OSL 'atan2(X,Y)' function.

$\endgroup$
  • 1
    $\begingroup$ It is indeed much simpler! $\endgroup$ – Pisurquatre Jul 24 '18 at 21:11
  • $\begingroup$ @Pisurquatre I notice it’s now even easier in the latest build of Blender (2.79.5) as there is an actual atan2 operation on the Maths node! $\endgroup$ – Rich Sedman Jul 24 '18 at 21:27
  • 1
    $\begingroup$ That's actually why I came back to this question ;) (see my other answer below). I didn't know however that is was backported to 2.79. $\endgroup$ – Pisurquatre Jul 24 '18 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.