The IN.worldPos value is something that’s calculated by the surface shader’s internal vertex function after your custom vertex function is called. So to use the world position you will have to calculate it yourself, like you would if you were doing a vertex/fragment shader. Also if you’re looking for the dot product of the surface normals to the light direction you would need to also transform the normals into world space as the v.normal is in local model space and you’ll get peculiar results there as well.
So, like @Blackhart mentioned, you’ll need to use unity_ObjectToWorld, but his example isn’t quite right, you actually need this:
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
Then for the vertex normal you would need to do this:
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
Then you’ll also need to normalize the results of the light direction stuff in the case of needing the world position if you want to use it in the dot product, which will give you this:
o.balance = dot(worldNormal, normalize(_WorldSpaceLightDir0.xyz - worldPos * _WorldSpaceLightDir0.w));
The vector returned by UnityObjectToWorldNormal() is already normalized, so nothing addtional has to be done there.
Alternatively you could do what you attempted in transforming the direction into object space, but you still need to normalize the resulting vector before using it in a dot product. The v.normal will also already be normalized so like with the world space dot product nothing additional needs to be done to it, but if you need the world position (in the case of point and spot lights) there’s no optimization to be had as you’re still doing two muls. If you’re using noforwardadd and it’s only ever going to be a directional light then this should work:
o.balance = dot(v.normal, normalize(mul(unity_WorldToObject, _WorldSpaceLightPos0)));
Note I am not using .xyz on the _WorldSpaceLightPos0, but instead the full .xyzw value. Your example might also work, but it is nebulous as unity_WorldToObject is a float4x4 and _WorldSpaceLightPos0.xyz is a float3, and a float4x4 should only every be mul with a float4. Also since this is a direction you really only want the float3x3 of the float4x4 to be applied (a float4x4 has positional translation, a float3x3 is just scale and rotation), but since the w of the _WorldSpaceLightPos0 in the case of a directional light is 0.0 the positional translation won’t be applied. Using .xyz some GPUs will handle that how you want as a direction, and some won’t.
However I’m not entirely sure exactly what your end goal is. If you’re looking for a dot product of the surface normal to the light direction, then that’s already being calculated per pixel since that’s exactly what the Lambert lighting model is, so if you’re calculating it in the vertex you’re just doing it again and not saving any calculation, instead you’re doubling it.