Navmesh.cginc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. struct appdata_color {
  2. float4 vertex : POSITION;
  3. fixed4 color : COLOR;
  4. float3 normal : NORMAL;
  5. float2 uv : TEXCOORD0;
  6. };
  7. struct line_v2f {
  8. half4 col : COLOR;
  9. float2 normal : TEXCOORD0;
  10. float4 screenPos : TEXCOORD1;
  11. float4 originScreenPos : TEXCOORD2;
  12. };
  13. // Redefine the matrix here in order to confuse the Unity shader upgrader.
  14. // Otherwise it will start modifying some things below even though this script is specifically
  15. // written to be compatible with multiple versions of Unity.
  16. #define MVP_Matrix UNITY_MATRIX_MVP
  17. // d = normalized distance to line
  18. float lineAA(float d) {
  19. d = max(min(d, 1.0), 0) * 1.116;
  20. float v = 0.93124*d*d*d - 1.42215*d*d - 0.42715*d + 0.95316;
  21. v /= 0.95316;
  22. return max(v, 0);
  23. }
  24. line_v2f line_vert (appdata_color v, float pixelWidth, out float4 outpos : SV_POSITION) {
  25. line_v2f o;
  26. // UnityObjectToClipPos only exists in Unity 5.4 or above and there it has to be used
  27. #if defined(UNITY_USE_PREMULTIPLIED_MATRICES)
  28. float4 Mv = UnityObjectToClipPos(v.vertex);
  29. float4 Mn = UnityObjectToClipPos(float4(v.normal.x, v.normal.y, v.normal.z, 0));
  30. #else
  31. float4 Mv = mul(MVP_Matrix, v.vertex);
  32. float4 Mn = mul(MVP_Matrix, float4(v.normal.x, v.normal.y, v.normal.z, 0));
  33. #endif
  34. // delta is the limit value of doing the calculation
  35. // x1 = M*v
  36. // x2 = M*(v + e*n)
  37. // lim e->0 (x2/x2.w - x1.w)/e
  38. // Where M = UNITY_MATRIX_MVP, v = v.vertex, n = v.normal, e = a very small value
  39. // Previously the above calculation was done with just e = 0.001, however this could yield graphical artifacts
  40. // at large coordinate values as the floating point coordinates would start to run out of precision.
  41. // Essentially we calculate the normal of the line in screen space.
  42. float4 delta = (Mn - Mv*Mn.w/Mv.w) / Mv.w;
  43. // Handle DirectX properly. See https://docs.unity3d.com/Manual/SL-PlatformDifferences.html
  44. float2 screenSpaceNormal = float2(-delta.y, delta.x) * _ProjectionParams.x;
  45. float2 normalizedScreenSpaceNormal = normalize(screenSpaceNormal);
  46. screenSpaceNormal = normalizedScreenSpaceNormal / _ScreenParams.xy;
  47. float4 sn = float4(screenSpaceNormal.x, screenSpaceNormal.y, 0, 0);
  48. if (Mv.w < 0) {
  49. // Seems to have a very minor effect, but the distance
  50. // seems to be more accurate with this enabled
  51. sn *= -1;
  52. }
  53. float side = (v.uv.x - 0.5)*2;
  54. outpos = (Mv / Mv.w) + side*sn*pixelWidth*0.5;
  55. // Multiply by w because homogeneous coordinates (it still needs to be clipped)
  56. outpos *= Mv.w;
  57. o.normal = normalizedScreenSpaceNormal;
  58. o.originScreenPos = ComputeScreenPos(Mv);
  59. o.screenPos = ComputeScreenPos(outpos);
  60. return o;
  61. }
  62. /** Copied from UnityCG.cginc because this function does not exist in Unity 5.2 */
  63. inline bool IsGammaSpaceCompatibility() {
  64. #if defined(UNITY_NO_LINEAR_COLORSPACE)
  65. return true;
  66. #else
  67. // unity_ColorSpaceLuminance.w == 1 when in Linear space, otherwise == 0
  68. return unity_ColorSpaceLuminance.w == 0;
  69. #endif
  70. }