Hlsl - distance to plane?

Anyone know how in HLSL I might get the distance from a vertex position to an arbitrary 2d plane (positioned in 3d space)?

I want to change a parameter when the point is close to (intersecting with) the plane

cheers!

in what form do you have the plane represented? Ax + By + Cz = D?

Hi Tonfilm

Ideally just as a transform matrix in to the shader, for a plane initialised at the origin and oriented in the x/y axis

Bumpity bump. Anyone got an idea?

yes, you can get the normal and a position on the plane with the transformation matrix and then use any point-plane-distance code you find on the net:

code(lang=hlsl):

float DistToXYPlane(float3 pos, float4x4 planeTrans)
{
float3 nPlane = mul(float4(0, 0, 1, 0), planeTrans);
float3 pPlane = float3(planeTrans30, planeTrans31, planeTrans32);

float sn = -dot(nPlane, pos - pPlane);
float sd = dot(nPlane, nPlane);

return distance(pos, pos + (sn / sd) * nPlane);
}

plane distance.zip (2.8 kB)

Ah, sweet! Thanks Ton

How is

mul(float4(0, 0, 1, 0), planeTrans);

giving us the normal? (sorry I should probably just Google some matrix multiplication stuff ;)

Also, if I wanted to have multiple planes acting, can I use the DistToXYPlane function in a for loop?

when you set the w component of a 4d vector in homogeneous coordinates to 0 the matrix transformation affects only the direction, but not the position. in fact it would be smarter to also input the vector of the initial orientation of the plane. something like this:

code(lang=hlsl):
float DistToPlane(float3 pos, float4x4 planeTrans, float3 initialPlaneNormal)
{
float3 nPlane = mul(float4(initialPlaneNormal, 0), planeTrans);
float3 pPlane = float3(planeTrans30, planeTrans31, planeTrans32);

float sn = -dot(nPlane, pos - pPlane);
float sd = dot(nPlane, nPlane);

return distance(pos, pos + (sn / sd) * nPlane);

}

and sure you can use it in a loop, it has no side effects…