Custom HLSL material twist node Normal problem

Hi there,

I have a flattened torus, laid out in the XY plane which I am attempting to twist via a custom hlsl node. The twist in the code attached works for the WPO, but doesn’t recalculate the normals properly & I could do with some help!

Many thanks.

// input1 is float 3 local position
const float x = inLocalPosition.x;
const float y = inLocalPosition.y;
const float z = inLocalPosition.z;

// input 2 is WorldSpaceNormal
outWSNormal = inWSNormal;

// input 3 is the radius of spline at center of torus.
// ie the distance of the local origin from world origin
const float r = inRadius;

// input 4 is twist angle in radians.
const float phi = inTwistAngle;

// convert to Polar coordinates with new local axis w & store angle
const float w = sqrt(xx + yy);
const float theta = atan2(y, x);

// get w in terms of distance from spline center
const float w_local = w - r;

// calculate new values (in w and z axes)
const float w1_local = w_localcos(phi) - zsin(phi);
const float w1 = r + w1_local;
const float z1 = w_localsin(phi) + zcos(phi);

// convert w1 back into Cartesian coordinates
const float x1 = w1 * cos(theta);
const float y1 = w1 * sin(theta);

// subtract original positions to get offsets
const float3 outWPO = {x1-x, y1-y, z1-z};

// fix normals
// these should be local position in relation to spline, along w-axis, normalized
// But not working
outWSNormal.x = w1_local * cos(theta);
outWSNormal.y = w1_local * sin(theta);
outWSNormal.z = z1;
outWSNormal = normalize(outWSNormal);

//return
return outWPO;