mx_atan2
MaterialX‑compatible two‑argument arctangent. Takes in1 as y and in2 as x and returns the signed polar angle of the vector (x=in2, y=in1) w.r.t. the x‑axis, in radians, within [-PI, PI]. Defaults in1=float(0), in2=float(1) yield 0.
Core Advantages
More robust than single‑arg atan(in1/in2): correct across all quadrants, avoids division by zero, and maps directly to GLSL atan(y, x). It turns a 2D direction into a scalar angle for polar‑style effects.
Common Uses
Convert 2D directions (e.g., position.xz, UV gradients) to an angle for polar shading or sector masks.
Build radial/annular stripes, radar sweeps, rotating highlights.
Derive tangent angles from normals/flow fields to drive anisotropy or flow maps.
Map the angle to [0,1] for gradients/Hue control, phase animation, or tiled repetition.
How to adjust
Mind the argument order: mx_atan2(in1, in2) == GLSL atan(y, x). Typical patterns: mx_atan2(p.y, p.x) or mx_atan2(p.z, p.x) for the XZ plane. Remap angle a to [0,1] via a01 = a.add( PI ).div( PI2 ); repeat into N sectors with fract( a01.mul( N ) ); offset the start angle by adding a constant or rotating/swapping inputs. Defaults provide a safe fallback.
Code Examples
1
2// Compute angle on the world XZ plane and remap to [0,1]
3const angle = mx_atan2( positionWorld.z, positionWorld.x );
4const t = angle.add( PI ).div( PI2 );
5
6// Use t to blend two colors for a polar look
7material.colorNode = mix( color( 0xff0080 ), color( 0x00ffff ), t );
8