Vertex distance

I’m writing a new shader and I need the world-distance between the vertex being processed and a supplied point in the world. It sounds like a simple problem but I haven’t got a solution yet. I’ve tried many things. Here’s the solution I thought would work:

void vshader(
	 in float4 vtx_texcoord0 : TEXCOORD0,
	 out float4 l_texcoord0 : TEXCOORD0,
	 uniform float4x4 trans_model_to_view,
	 uniform float4x4 tpose_view_to_model,
	 in float4 vtx_normal : TEXCOORD1,
	 out float4 l_normal : TEXCOORD2,
	 out float4 l_pos : TEXCOORD1,
	 out float4 l_value,
	 float4 vtx_position : POSITION,
	 out float4 l_position : POSITION,
	 uniform float4 wspos_pos,
	 uniform float4x4 mat_modelproj
) {
	 l_position = mul(mat_modelproj, vtx_position);
	 l_texcoord0 = vtx_texcoord0;
	 l_pos = mul(trans_model_to_view, vtx_position); = mul((float3x3)tpose_view_to_model,;
	 l_value =  l_position-wspos_pos;

Here “l_value” is then passed to the fragment shader and it’s euclidean length is determined and that distance is used- the fragment shader part works fine. But somehow I don’t have the right transforms in the vertex shader. “wspos_pos” - “pos” is the name of a shader input nodepath that is placed at the world-space point I am comparing the vertex position to. “pos” is simply a bookmark nodepath that holds the position.

l_position is in clip-space, while wspos_pos is in world space.

It would be easiest to get the model-space distance, that would be just the length of “vtx_position - mspos_pos”.

To get the world-space distance, you will first need to transform the vertex into world-space (use trans_model_to_world for that), and then subtract wspos_pos.

Note that you will most likely need to do perspective divide! To get the correct length (do this in vertex shader!), do:

length( / pos1.w - / pos2.w)