StraightLine.shader 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright(c) 2017 Funly LLC
  2. //
  3. // Author: Jason Ederle
  4. // Description: Draws a line between 2 points.
  5. // Contact: jason@funly.io
  6. Shader "Hidden/Funly/Sky/StraightLine"
  7. {
  8. Properties
  9. {
  10. _LineColor ("Line", Color) = (0, 1, 0, 1)
  11. _Background ("Background", Color) = (1, 1, 1, 1)
  12. _LeftPercent ("Left Value", Range(0, 1)) = 0
  13. _RightPercent ("Right Value", Range(0, 1)) = 1
  14. _Thickness ("Thickness", Range(0, .1)) = .01
  15. _Feathering ("Edge Feathering", Range(0, 1)) = .1
  16. _WidthRatio ("Width Ratio", Float) = 1
  17. }
  18. SubShader
  19. {
  20. Tags { "RenderType"="Opaque" }
  21. LOD 100
  22. Pass
  23. {
  24. CGPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #include "UnityCG.cginc"
  28. struct appdata
  29. {
  30. float4 vertex : POSITION;
  31. float2 uv : TEXCOORD0;
  32. };
  33. struct v2f
  34. {
  35. float2 uv : TEXCOORD0;
  36. float4 vertex : SV_POSITION;
  37. };
  38. fixed4 _LineColor;
  39. fixed4 _Background;
  40. float _LeftPercent;
  41. float _RightPercent;
  42. float _Thickness;
  43. float _Feathering;
  44. float _WidthRatio;
  45. v2f vert (appdata v)
  46. {
  47. v2f o;
  48. // Resize the width.
  49. v.vertex.x *= _WidthRatio;
  50. o.vertex = UnityObjectToClipPos(v.vertex);
  51. o.uv = v.uv;
  52. return o;
  53. }
  54. fixed4 frag (v2f i) : SV_Target
  55. {
  56. float yMin = _Thickness;
  57. float yMax = 1 - _Thickness;
  58. float2 leftPoint = float2(0, clamp(_LeftPercent, yMin, yMax));
  59. float2 rightPoint = float2(1, clamp(_RightPercent, yMin, yMax));
  60. float2 fullLineVect = rightPoint - leftPoint;
  61. float2 fragVector = i.uv - leftPoint;
  62. float2 fullLineDirection = normalize(fullLineVect);
  63. float2 projectedLength = dot(fullLineDirection, fragVector);
  64. float2 projectedVect = fullLineDirection * projectedLength;
  65. float2 projectedPoint = leftPoint + projectedVect;
  66. float distanceToFrag = distance(i.uv, projectedPoint);
  67. float mixPercent = smoothstep(_Thickness, _Thickness - (_Thickness * _Feathering), distanceToFrag);
  68. return lerp(_Background, _LineColor, mixPercent);
  69. }
  70. ENDCG
  71. }
  72. }
  73. }