My goal is a custom shader applied to any object which clips pixels based on the position of a shared bounding volume. That shared bounding volume is simply a cube, and I upload the min/max corners to the shader whenever it changes.
I thought it would be sufficient to get the world position within the shader, and then multiply that by the shared bounding volume’s worldToLocalMatrix (I upload this as a float4x4 matrix). However, this is incorrect – it works if all rotations are 0, but the clipping area moves around depending on the rotation, so I know that the coordinate space conversion is wrong.
This is what I do:
float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
o.objectPosInBoundingSpace = mul(_FadeWorldToLocal, posWorld);
Then in the fragment, I discard pixels based on the z axis of the Min/Max (right now I only care about the z axis)
if(_FadeMaxWorld.z < i.objectPosInBoundingSpace.z || _FadeMinWorld.z > i.objectPosInBoundingSpace.z)
{
discard;
}
What am I missing? Is the “worldspace” unique per gameobject at least where the shader is concerned? I thought that I could rely on worldspace for all conversion.