Box Cut out Shader function

I am trying to create a Custom function to be used in Shader Graph that cuts out a box on an object.
I want to be able to feed in the World coordinates of a cube to the function and it to return a value float and connect it to the alpha channel.

So far i have this:

void  IsVertexInsideBox_float(float3 vertexPosition, float3 boxPosition, float3 boxScale, float4 boxRotation, float4 objectWorldRotation , out float Out)
{
    float4x4 rotationMatrix = quaternion_to_matrix(boxRotation);
    // Transform the point into the local space of the cube
    float3 localPoint = mul(rotationMatrix, vertexPosition - boxPosition) / boxScale;

    // Check if the transformed point is within the cube
    bool insideCube = abs(localPoint.x) <= 0.5 && abs(localPoint.y) <= 0.5 && abs(localPoint.z) <= 0.5;

    Out =  insideCube ? 1 : 0;
}

At first it seems like it works fine, i can move the box and rotate its Y axis and all looks good.

But if i rotate other axis or the “shaded” object it all goes wrong from there… :frowning:

Any tips, links or solution on how to solve this?

Thanks in advance!!

Looks good,

Sad life

Well, what is this boxRotation? Is it the local2World or the world2Local rotation? That said, wouldn’t it make more sense to just provide the whole transform matrix instead of a position, rotation and scale manually? I mean that’s why we have matrices in the first place :slight_smile:

So what exactly do you pass as boxRotation? The box’s rotation quaternion or the inverse of that?