How to obtain World Space View Direction vector in vertex+fragment shader (not in surface shader!) exactly in the way that the surface shader provides?
Found the solution (using #pragma debug in surface shader and looking at the compiled shader source). If anybody interested:
The trick is to use UnityCG.cginc directive TANGENT_SPACE_ROTATION; in v2f vert(appdata_tan v) vertex program, which gives actually:
float3 binormal = cross( v.normal, v.tangent.xyz ) * v.tangent.w;
float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal );
and then by o.viewDir = mul (rotation, ObjSpaceViewDir(v.vertex)); i can achieve vector that i can use in fragment program for dot(viewDir,normal) where normal = unpacknormal(blahblah)…
Works with deferred forward rendering paths. Gives the same result.
I tried multiplying the vertex’s normal with different combinations of the built in unity matrixes for model, view, projection etc. but none of them were quite right. This might be the solution i’m looking for. Thanks!