You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.8 KiB

  1. Shader "Graphy/Graph Standard"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6. _Color("Tint", Color) = (1,1,1,1)
  7. [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
  8. _GoodColor("Good Color", Color) = (1,1,1,1)
  9. _CautionColor("Caution Color", Color) = (1,1,1,1)
  10. _CriticalColor("Critical Color", Color) = (1,1,1,1)
  11. _GoodThreshold("Good Threshold", Float) = 0.5
  12. _CautionThreshold("Caution Threshold", Float) = 0.25
  13. }
  14. SubShader
  15. {
  16. // Tags
  17. // {
  18. // "Queue" = "Overlay"
  19. // "IgnoreProjector" = "True"
  20. // "RenderType" = "Transparent"
  21. // "PreviewType" = "Plane"
  22. // "CanUseSpriteAtlas" = "True"
  23. // }
  24. Cull Off
  25. Lighting Off
  26. ZWrite Off
  27. ZTest Off
  28. Blend One OneMinusSrcAlpha
  29. Pass
  30. {
  31. CGPROGRAM
  32. #pragma vertex vert
  33. #pragma fragment frag
  34. #pragma multi_compile _ PIXELSNAP_ON
  35. #include "UnityCG.cginc"
  36. struct appdata_t
  37. {
  38. float4 vertex : POSITION;
  39. float4 color : COLOR;
  40. float2 texcoord : TEXCOORD0;
  41. };
  42. struct v2f
  43. {
  44. float4 vertex : SV_POSITION;
  45. fixed4 color : COLOR;
  46. float2 texcoord : TEXCOORD0;
  47. };
  48. fixed4 _Color;
  49. v2f vert(appdata_t IN)
  50. {
  51. v2f OUT;
  52. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  53. OUT.texcoord = IN.texcoord;
  54. OUT.color = IN.color * _Color;
  55. #ifdef PIXELSNAP_ON
  56. OUT.vertex = UnityPixelSnap(OUT.vertex);
  57. #endif
  58. return OUT;
  59. }
  60. sampler2D _MainTex;
  61. sampler2D _AlphaTex;
  62. float _AlphaSplitEnabled;
  63. fixed4 SampleSpriteTexture(float2 uv)
  64. {
  65. fixed4 color = tex2D(_MainTex, uv);
  66. #if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
  67. if (_AlphaSplitEnabled)
  68. color.a = tex2D(_AlphaTex, uv).r;
  69. #endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
  70. return color;
  71. }
  72. fixed4 _GoodColor;
  73. fixed4 _CautionColor;
  74. fixed4 _CriticalColor;
  75. fixed _GoodThreshold;
  76. fixed _CautionThreshold;
  77. uniform float Average;
  78. // NOTE: The size of this array can break compatibility with some older GPUs
  79. // If you see a pink box or that the graphs are not working, try lowering this value
  80. // or using the GraphMobile.shader
  81. uniform float GraphValues[512];
  82. uniform float GraphValues_Length;
  83. fixed4 frag(v2f IN) : SV_Target
  84. {
  85. fixed4 color = IN.color;
  86. fixed xCoord = IN.texcoord.x;
  87. fixed yCoord = IN.texcoord.y;
  88. float graphValue = GraphValues[floor(xCoord * GraphValues_Length)];
  89. // Define the width of each element of the graph
  90. float increment = 1.0f / (GraphValues_Length - 1);
  91. // Assign the corresponding color
  92. if (graphValue > _GoodThreshold)
  93. {
  94. color *= _GoodColor;
  95. }
  96. else if (graphValue > _CautionThreshold)
  97. {
  98. color *= _CautionColor;
  99. }
  100. else
  101. {
  102. color *= _CriticalColor;
  103. }
  104. // Point coloring
  105. if (graphValue - yCoord > increment * 4)
  106. {
  107. //color.a = yCoord * graphValue * 0.3;
  108. color.a *= yCoord * 0.3 / graphValue;
  109. }
  110. // Set as transparent the part on top of the current point value
  111. if (yCoord > graphValue)
  112. {
  113. color.a = 0;
  114. }
  115. // Average white bar
  116. if (yCoord < Average && yCoord > Average - 0.02)
  117. {
  118. color = fixed4(1, 1, 1, 1);
  119. }
  120. // CautionColor bar
  121. if (yCoord < _CautionThreshold && yCoord > _CautionThreshold - 0.02)
  122. {
  123. color = _CautionColor;
  124. }
  125. // GoodColor bar
  126. if (yCoord < _GoodThreshold && yCoord > _GoodThreshold - 0.02)
  127. {
  128. color = _GoodColor;
  129. }
  130. // Fade the alpha of the sides of the graph
  131. if (xCoord < 0.03)
  132. {
  133. color.a *= 1 - (0.03 - xCoord) / 0.03;
  134. }
  135. else if (xCoord > 0.97)
  136. {
  137. color.a *= (1 - xCoord) / 0.03;
  138. }
  139. fixed4 c = SampleSpriteTexture(IN.texcoord) * color;
  140. c.rgb *= c.a;
  141. return c;
  142. }
  143. ENDCG
  144. }
  145. }
  146. }