No Description
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.

Debug.hlsl 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #ifndef UNITY_DEBUG_INCLUDED
  2. #define UNITY_DEBUG_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/GlobalSamplers.hlsl"
  5. // UX-verified colorblind-optimized debug colors, listed in order of increasing perceived "hotness"
  6. #define DEBUG_COLORS_COUNT 12
  7. #define kDebugColorBlack float4(0.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0) // #000000
  8. #define kDebugColorLightPurple float4(166.0 / 255.0, 70.0 / 255.0, 242.0 / 255.0, 1.0) // #A646F2
  9. #define kDebugColorDeepBlue float4(0.0 / 255.0, 26.0 / 255.0, 221.0 / 255.0, 1.0) // #001ADD
  10. #define kDebugColorSkyBlue float4(65.0 / 255.0, 152.0 / 255.0, 224.0 / 255.0, 1.0) // #4198E0
  11. #define kDebugColorLightBlue float4(158.0 / 255.0, 228.0 / 255.0, 251.0 / 255.0, 1.0) // #1A1D21
  12. #define kDebugColorTeal float4(56.0 / 255.0, 243.0 / 255.0, 176.0 / 255.0, 1.0) // #38F3B0
  13. #define kDebugColorBrightGreen float4(168.0 / 255.0, 238.0 / 255.0, 46.0 / 255.0, 1.0) // #A8EE2E
  14. #define kDebugColorBrightYellow float4(255.0 / 255.0, 253.0 / 255.0, 76.0 / 255.0, 1.0) // #FFFD4C
  15. #define kDebugColorDarkYellow float4(255.0 / 255.0, 214.0 / 255.0, 0.0 / 255.0, 1.0) // #FFD600
  16. #define kDebugColorOrange float4(253.0 / 255.0, 152.0 / 255.0, 0.0 / 255.0, 1.0) // #FD9800
  17. #define kDebugColorBrightRed float4(255.0 / 255.0, 67.0 / 255.0, 51.0 / 255.0, 1.0) // #FF4333
  18. #define kDebugColorDarkRed float4(132.0 / 255.0, 10.0 / 255.0, 54.0 / 255.0, 1.0) // #840A36
  19. // Shadow cascade debug colors. Keep in sync with the ones in ShadowCascadeGUI.cs.
  20. // Note: These colors are not 1:1 match to editor UI, in order to provide better contrast in the viewport.
  21. #define kDebugColorShadowCascade0 float4(0.4, 0.4, 0.9, 1.0)
  22. #define kDebugColorShadowCascade1 float4(0.4, 0.9, 0.4, 1.0)
  23. #define kDebugColorShadowCascade2 float4(0.9, 0.9, 0.4, 1.0)
  24. #define kDebugColorShadowCascade3 float4(0.9, 0.4, 0.4, 1.0)
  25. // UX-verified colorblind-optimized "heat color gradient"
  26. static const float4 kDebugColorGradient[DEBUG_COLORS_COUNT] = { kDebugColorBlack, kDebugColorLightPurple, kDebugColorDeepBlue,
  27. kDebugColorSkyBlue, kDebugColorLightBlue, kDebugColorTeal, kDebugColorBrightGreen, kDebugColorBrightYellow,
  28. kDebugColorDarkYellow, kDebugColorOrange, kDebugColorBrightRed, kDebugColorDarkRed };
  29. #define TRANSPARENCY_OVERDRAW_COST 1.0
  30. #define TRANSPARENCY_OVERDRAW_A 1.0
  31. // Given an enum (represented by an int here), return a color.
  32. // Use for DebugView of enum
  33. real3 GetIndexColor(int index)
  34. {
  35. real3 outColor = real3(1.0, 0.0, 0.0);
  36. if (index == 0)
  37. outColor = real3(1.0, 0.5, 0.5);
  38. else if (index == 1)
  39. outColor = real3(0.5, 1.0, 0.5);
  40. else if (index == 2)
  41. outColor = real3(0.5, 0.5, 1.0);
  42. else if (index == 3)
  43. outColor = real3(1.0, 1.0, 0.5);
  44. else if (index == 4)
  45. outColor = real3(1.0, 0.5, 1.0);
  46. else if (index == 5)
  47. outColor = real3(0.5, 1.0, 1.0);
  48. else if (index == 6)
  49. outColor = real3(0.25, 0.75, 1.0);
  50. else if (index == 7)
  51. outColor = real3(1.0, 0.75, 0.25);
  52. else if (index == 8)
  53. outColor = real3(0.75, 1.0, 0.25);
  54. else if (index == 9)
  55. outColor = real3(0.75, 0.25, 1.0);
  56. else if (index == 10)
  57. outColor = real3(0.25, 1.0, 0.75);
  58. else if (index == 11)
  59. outColor = real3(0.75, 0.75, 0.25);
  60. else if (index == 12)
  61. outColor = real3(0.75, 0.25, 0.75);
  62. else if (index == 13)
  63. outColor = real3(0.25, 0.75, 0.75);
  64. else if (index == 14)
  65. outColor = real3(0.25, 0.25, 0.75);
  66. else if (index == 15)
  67. outColor = real3(0.75, 0.25, 0.25);
  68. return outColor;
  69. }
  70. #define PACK_BITS25(_x0,_x1,_x2,_x3,_x4,_x5,_x6,_x7,_x8,_x9,_x10,_x11,_x12,_x13,_x14,_x15,_x16,_x17,_x18,_x19,_x20,_x21,_x22,_x23,_x24) (_x0|(_x1<<1)|(_x2<<2)|(_x3<<3)|(_x4<<4)|(_x5<<5)|(_x6<<6)|(_x7<<7)|(_x8<<8)|(_x9<<9)|(_x10<<10)|(_x11<<11)|(_x12<<12)|(_x13<<13)|(_x14<<14)|(_x15<<15)|(_x16<<16)|(_x17<<17)|(_x18<<18)|(_x19<<19)|(_x20<<20)|(_x21<<21)|(_x22<<22)|(_x23<<23)|(_x24<<24))
  71. #define _ 0
  72. #define x 1
  73. const static uint kFontData[9][2] = {
  74. { PACK_BITS25(_,_,x,_,_, _,_,x,_,_, _,x,x,x,_, x,x,x,x,x, _,_,_,x,_), PACK_BITS25(x,x,x,x,x, _,x,x,x,_, x,x,x,x,x, _,x,x,x,_, _,x,x,x,_) },
  75. { PACK_BITS25(_,x,_,x,_, _,x,x,_,_, x,_,_,_,x, _,_,_,_,x, _,_,_,x,_), PACK_BITS25(x,_,_,_,_, x,_,_,_,x, _,_,_,_,x, x,_,_,_,x, x,_,_,_,x) },
  76. { PACK_BITS25(x,_,_,_,x, x,_,x,_,_, x,_,_,_,x, _,_,_,x,_, _,_,x,x,_), PACK_BITS25(x,_,_,_,_, x,_,_,_,_, _,_,_,x,_, x,_,_,_,x, x,_,_,_,x) },
  77. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,_,_,_,x, _,_,x,_,_, _,x,_,x,_), PACK_BITS25(x,_,x,x,_, x,_,_,_,_, _,_,_,x,_, x,_,_,_,x, x,_,_,_,x) },
  78. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,_,_,x,_, _,x,x,x,_, _,x,_,x,_), PACK_BITS25(x,x,_,_,x, x,x,x,x,_, _,_,x,_,_, _,x,x,x,_, _,x,x,x,x) },
  79. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,_,x,_,_, _,_,_,_,x, x,_,_,x,_), PACK_BITS25(_,_,_,_,x, x,_,_,_,x, _,_,x,_,_, x,_,_,_,x, _,_,_,_,x) },
  80. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,x,_,_,_, _,_,_,_,x, x,x,x,x,x), PACK_BITS25(_,_,_,_,x, x,_,_,_,x, _,x,_,_,_, x,_,_,_,x, _,_,_,_,x) },
  81. { PACK_BITS25(_,x,_,x,_, _,_,x,_,_, x,_,_,_,_, x,_,_,_,x, _,_,_,x,_), PACK_BITS25(x,_,_,_,x, x,_,_,_,x, _,x,_,_,_, x,_,_,_,x, x,_,_,_,x) },
  82. { PACK_BITS25(_,_,x,_,_, x,x,x,x,x, x,x,x,x,x, _,x,x,x,_, _,_,_,x,_), PACK_BITS25(_,x,x,x,_, _,x,x,x,_, _,x,_,_,_, _,x,x,x,_, _,x,x,x,_) }
  83. };
  84. #undef _
  85. #undef x
  86. #undef PACK_BITS25
  87. bool SampleDebugFont(int2 pixCoord, uint digit)
  88. {
  89. if (pixCoord.x < 0 || pixCoord.y < 0 || pixCoord.x >= 5 || pixCoord.y >= 9 || digit > 9)
  90. return false;
  91. return (kFontData[8 - pixCoord.y][digit >= 5] >> ((digit % 5) * 5 + pixCoord.x)) & 1;
  92. }
  93. /*
  94. * Sample up to 2 digits of a number. (Excluding leading zeroes)
  95. *
  96. * Note: Digit have a size of 5x8 pixels and spaced by 1 pixel
  97. * See SampleDebugFontNumberAllDigits to sample all digits.
  98. *
  99. * @param pixCoord: pixel coordinate of the number sample
  100. * @param number: number to sample
  101. * @return true when the pixel is a pixel of a digit.
  102. */
  103. bool SampleDebugFontNumber2Digits(int2 pixCoord, uint number)
  104. {
  105. pixCoord.y -= 4;
  106. if (number <= 9)
  107. {
  108. return SampleDebugFont(pixCoord - int2(6, 0), number);
  109. }
  110. else
  111. {
  112. return (SampleDebugFont(pixCoord, number / 10) | SampleDebugFont(pixCoord - int2(6, 0), number % 10));
  113. }
  114. }
  115. /*
  116. * Sample up to 3 digits of a number. (Excluding leading zeroes)
  117. *
  118. * Note: Digit have a size of 5x8 pixels and spaced by 1 pixel
  119. * See SampleDebugFontNumberAllDigits to sample all digits.
  120. *
  121. * @param pixCoord: pixel coordinate of the number sample
  122. * @param number: number to sample
  123. * @return true when the pixel is a pixel of a digit.
  124. */
  125. bool SampleDebugFontNumber3Digits(int2 pixCoord, uint number)
  126. {
  127. pixCoord.y -= 4;
  128. if (number <= 9)
  129. {
  130. return SampleDebugFont(pixCoord - int2(6, 0), number);
  131. }
  132. else if (number <= 99)
  133. {
  134. return (SampleDebugFont(pixCoord, (number / 10) % 10) | SampleDebugFont(pixCoord - int2(6, 0), number % 10));
  135. }
  136. else
  137. {
  138. return (SampleDebugFont(pixCoord, (number / 100)) | SampleDebugFont(pixCoord - int2(4, 0),(number / 10) % 10) | SampleDebugFont(pixCoord - int2(8, 0),(number / 10) % 10) );
  139. }
  140. }
  141. /*
  142. * Sample all digits of a number. (Excluding leading zeroes)
  143. *
  144. * Note: Digit have a size of 5x8 pixels and spaced by 1 pixel
  145. * See SampleDebugFontNumber2Digits for a faster version supporting only 2 digits.
  146. *
  147. * @param pixCoord: pixel coordinate of the number sample
  148. * @param number: number to sample
  149. * @return true when the pixel is a pixel of a digit.
  150. */
  151. bool SampleDebugFontNumberAllDigits(int2 pixCoord, uint number)
  152. {
  153. const int digitCount = (int)max(1u, uint(log10(number)) + 1u);
  154. pixCoord.y -= 4;
  155. int2 offset = int2(6 * digitCount, 0);
  156. uint current = number;
  157. for (int i = 0; i < digitCount; ++i)
  158. {
  159. if (SampleDebugFont(pixCoord - offset, current % 10))
  160. return true;
  161. current /= 10;
  162. offset -= int2(6, 0);
  163. }
  164. return false;
  165. }
  166. TEXTURE2D(_DebugFont); // Debug font to write string in shader
  167. // DebugFont code assume black and white font with texture size 256x128 with bloc of 16x16
  168. #define DEBUG_FONT_TEXT_WIDTH 16
  169. #define DEBUG_FONT_TEXT_HEIGHT 16
  170. #define DEBUG_FONT_TEXT_COUNT_X 16
  171. #define DEBUG_FONT_TEXT_COUNT_Y 8
  172. #define DEBUG_FONT_TEXT_ASCII_START 32
  173. #define DEBUG_FONT_TEXT_SCALE_WIDTH 10 // This control the spacing between characters (if a character fill the text block it will overlap).
  174. /*
  175. * Draw a character
  176. *
  177. * Note: Only supports ASCII symbols from DEBUG_FONT_TEXT_ASCII_START to 126
  178. *
  179. * @param asciiValue: actual character we want to draw
  180. * @param fontColor: color of the font to use
  181. * @param currentUnormCoord: current unnormalized screen position
  182. * @param fixedUnormCoord: position where we want to draw a character (will be incremented by the provided `fontTextScaleWidth` in provided `direction`)
  183. * @param color: current screen color
  184. * @param direction: direction to draw a string (1 = left to right, -1 = right to left), so it determines the direction in which `fixedUnormCoord` will shift
  185. * @param fontTextScaleWidth: spacing between characters, so the amount by which `fixedUnormCoord` will shift
  186. * @return void, blends in `fontColor` into the `color` parameter if we hit font character
  187. */
  188. void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color, int direction, int fontTextScaleWidth)
  189. {
  190. // Are we inside a font display block on the screen ?
  191. uint2 localCharCoord = currentUnormCoord - fixedUnormCoord;
  192. if (localCharCoord.x >= 0 && localCharCoord.x < DEBUG_FONT_TEXT_WIDTH && localCharCoord.y >= 0 && localCharCoord.y < DEBUG_FONT_TEXT_HEIGHT)
  193. {
  194. localCharCoord.y = DEBUG_FONT_TEXT_HEIGHT - localCharCoord.y;
  195. asciiValue -= DEBUG_FONT_TEXT_ASCII_START; // Our font start at ASCII table 32;
  196. uint2 asciiCoord = uint2(asciiValue % DEBUG_FONT_TEXT_COUNT_X, asciiValue / DEBUG_FONT_TEXT_COUNT_X);
  197. // Unorm coordinate inside the font texture
  198. uint2 unormTexCoord = asciiCoord * uint2(DEBUG_FONT_TEXT_WIDTH, DEBUG_FONT_TEXT_HEIGHT) + localCharCoord;
  199. // normalized coordinate
  200. float2 normTexCoord = float2(unormTexCoord) / float2(DEBUG_FONT_TEXT_WIDTH * DEBUG_FONT_TEXT_COUNT_X, DEBUG_FONT_TEXT_HEIGHT * DEBUG_FONT_TEXT_COUNT_Y);
  201. normTexCoord.y = 1.0 - normTexCoord.y;
  202. float charColor = SAMPLE_TEXTURE2D_LOD(_DebugFont, sampler_PointClamp, normTexCoord, 0).r;
  203. color = color * (1.0 - charColor) + charColor * fontColor;
  204. }
  205. fixedUnormCoord.x += fontTextScaleWidth * direction;
  206. }
  207. void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color, int direction)
  208. {
  209. DrawCharacter(asciiValue, fontColor, currentUnormCoord, fixedUnormCoord, color, direction, DEBUG_FONT_TEXT_SCALE_WIDTH);
  210. }
  211. // Shortcut to not have to file direction
  212. void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color)
  213. {
  214. DrawCharacter(asciiValue, fontColor, currentUnormCoord, fixedUnormCoord, color, 1);
  215. }
  216. // Draws a heatmap with numbered tiles, with increasingly "hot" background colors depending on n,
  217. // where values at or above maxN receive strong red background color.
  218. float4 OverlayHeatMap(uint2 pixCoord, uint2 tileSize, uint n, uint maxN, float opacity)
  219. {
  220. int colorIndex = 1 + (int)floor(10 * (log2((float)n + 0.1f) / log2(float(maxN))));
  221. colorIndex = clamp(colorIndex, 0, DEBUG_COLORS_COUNT-1);
  222. float4 col = kDebugColorGradient[colorIndex];
  223. int2 coord = (pixCoord & (tileSize - 1)) - int2(tileSize.x/4+1, tileSize.y/3-3);
  224. float4 color = float4(PositivePow(col.rgb, 2.2), opacity * col.a);
  225. if (n >= 0)
  226. {
  227. if (SampleDebugFontNumber3Digits(coord, n)) // Shadow
  228. color = float4(0, 0, 0, 1);
  229. if (SampleDebugFontNumber3Digits(coord + 1, n)) // Text
  230. color = float4(1, 1, 1, 1);
  231. }
  232. return color;
  233. }
  234. // Draws a heatmap with numbered tiles, with increasingly "hot" background colors depending on n,
  235. // where values at or above maxN receive strong red background color.
  236. float4 OverlayHeatMapNoNumber(uint2 pixCoord, uint2 tileSize, uint n, uint maxN, float opacity)
  237. {
  238. int colorIndex = 1 + (int)floor(10 * (log2((float)n + 0.1f) / log2(float(maxN))));
  239. colorIndex = clamp(colorIndex, 0, DEBUG_COLORS_COUNT-1);
  240. float4 col = kDebugColorGradient[colorIndex];
  241. int2 coord = (pixCoord & (tileSize - 1)) - int2(tileSize.x/4+1, tileSize.y/3-3);
  242. return float4(PositivePow(col.rgb, 2.2), opacity * col.a);
  243. }
  244. // Convert an arbitrary range to color base on threshold provide to the function, threshold must be in growing order
  245. real3 GetColorCodeFunction(real value, real4 threshold)
  246. {
  247. const real3 red = { 1.0, 0.0, 0.0 };
  248. const real3 lightGreen = { 0.5, 1.0, 0.5 };
  249. const real3 darkGreen = { 0.1, 1.0, 0.1 };
  250. const real3 yellow = { 1.0, 1.0, 0.0 };
  251. real3 outColor = red;
  252. if (value < threshold[0])
  253. {
  254. outColor = red;
  255. }
  256. else if (value >= threshold[0] && value < threshold[1])
  257. {
  258. real scale = (value - threshold[0]) / (threshold[1] - threshold[0]);
  259. outColor = lerp(red, darkGreen, scale);
  260. }
  261. else if (value >= threshold[1] && value < threshold[2])
  262. {
  263. real scale = (value - threshold[1]) / (threshold[2] - threshold[1]);
  264. outColor = lerp(darkGreen, lightGreen, scale);
  265. }
  266. else if (value >= threshold[2] && value < threshold[3])
  267. {
  268. real scale = (value - threshold[2]) / (threshold[2] - threshold[2]);
  269. outColor = lerp(lightGreen, yellow, scale);
  270. }
  271. else
  272. {
  273. outColor = yellow;
  274. }
  275. return outColor;
  276. }
  277. /// Return the color of the overdraw debug.
  278. ///
  279. /// The color will go from
  280. /// (cheap) dark blue -> red -> violet -> white (expensive)
  281. ///
  282. /// * overdrawCount: the number of overdraw
  283. /// * maxOverdrawCount: the maximum number of overdraw.
  284. /// if the overdrawCount is above, the most expensive color is returned.
  285. real3 GetOverdrawColor(real overdrawCount, real maxOverdrawCount)
  286. {
  287. if (overdrawCount < 0.01)
  288. return real3(0, 0, 0);
  289. // cheapest hue
  290. const float initialHue = 240;
  291. // most expensive hue is initialHue - deltaHue
  292. const float deltaHue = 20;
  293. // the value in % of budget where we start to remove saturation
  294. const float xLight = 0.95;
  295. // minimum hue
  296. const float minHue = deltaHue - 360 + initialHue;
  297. // budget value of a single draw
  298. const float xCostOne = 1.0 / maxOverdrawCount;
  299. // current budget value
  300. const float x = saturate(overdrawCount / maxOverdrawCount);
  301. float hue = fmod(max(min((x - xCostOne) * (deltaHue - 360) * (1.0 / (xLight - xCostOne)) + initialHue, initialHue), minHue), 360)/360.0;
  302. float saturation = min(max((-1.0/(1 - xLight)) * (x - xLight), 0), 1);
  303. return HsvToRgb(real3(hue, saturation, 1.0));
  304. }
  305. uint OverdrawLegendBucketInterval(uint maxOverdrawCount)
  306. {
  307. if (maxOverdrawCount <= 10)
  308. return 1;
  309. if (maxOverdrawCount <= 50)
  310. return 5;
  311. if (maxOverdrawCount <= 100)
  312. return 10;
  313. const uint digitCount = floor(log10(maxOverdrawCount));
  314. const uint digitMultiplier = pow(10, digitCount);
  315. const uint biggestDigit = floor(maxOverdrawCount/digitMultiplier);
  316. if (biggestDigit < 5)
  317. return pow(10, digitCount - 1) * 5;
  318. return digitMultiplier;
  319. }
  320. /// Return the color of the overdraw debug legend.
  321. ///
  322. /// It will draw a bar with all the color buckets of the overdraw debug
  323. ///
  324. /// * texcoord: the texture coordinate of the pixel to draw
  325. /// * maxOverdrawCount: the maximum number of overdraw.
  326. /// * screenSize: screen size (w, h, 1/w, 1/h).
  327. /// * defaultColor: the default color used for other areas
  328. void DrawOverdrawLegend(real2 texCoord, real maxOverdrawCount, real4 screenSize, inout real3 color)
  329. {
  330. // Band parameters
  331. // Position of the band (fixed x, fixed y, rel x, rel y)
  332. const real4 bandPosition = real4(20, 20, 0, 0);
  333. // Position of the band labels (fixed x, fixed y, rel x, rel y)
  334. const real4 bandLabelPosition = real4(20, 50, 0, 0);
  335. // Size of the band (fixed x, fixed y, rel x, rel y)
  336. const real4 bandSize = real4(-bandPosition.x * 2, 20, 1, 0);
  337. // Thickness of the band (fixed x, fixed y, rel x, rel y)
  338. const real4 bandBorderThickness = real4(4, 4, 0, 0);
  339. // Compute UVs
  340. const real2 bandPositionUV = bandPosition.xy * screenSize.zw + bandPosition.zw;
  341. const real2 bandLabelPositionUV = bandLabelPosition.xy * screenSize.zw + bandLabelPosition.zw;
  342. const real2 bandSizeUV = bandSize.xy * screenSize.zw + bandSize.zw;
  343. const real4 bandBorderPosition = bandPosition - bandBorderThickness;
  344. const real4 bandBorderSize = bandSize + 2 * bandBorderThickness;
  345. const real2 bandBorderPositionUV = bandBorderPosition.xy * screenSize.zw + bandBorderPosition.zw;
  346. const real2 bandBorderSizeUV = bandBorderSize.xy * screenSize.zw + bandBorderSize.zw;
  347. // Transform coordinate
  348. const real2 bandBorderCoord = (texCoord - bandBorderPositionUV) / bandBorderSizeUV;
  349. const real2 bandCoord = (texCoord - bandPositionUV) / bandSizeUV;
  350. // Compute bucket index
  351. const real bucket = ceil(bandCoord.x * maxOverdrawCount);
  352. // Assign color when relevant
  353. // Band border
  354. if (all(bandBorderCoord >= 0) && all(bandBorderCoord <= 1))
  355. color = real3(0.1, 0.1, 0.1);
  356. // Band color
  357. if (all(bandCoord >= 0) && all(bandCoord <= 1))
  358. color = GetOverdrawColor(bucket, maxOverdrawCount);
  359. // Bucket label
  360. if (0 < bucket && bucket <= maxOverdrawCount)
  361. {
  362. const uint bucketInterval = OverdrawLegendBucketInterval(maxOverdrawCount);
  363. const uint bucketLabelIndex = (uint(bucket) / bucketInterval) * bucketInterval;
  364. const real2 labelStartCoord = real2(
  365. bandLabelPositionUV.x + (bucketLabelIndex - 1) * (bandSizeUV.x / maxOverdrawCount),
  366. bandLabelPositionUV.y
  367. );
  368. const uint2 pixCoord = uint2((texCoord - labelStartCoord) * screenSize.xy);
  369. if (SampleDebugFontNumberAllDigits(pixCoord, bucketLabelIndex))
  370. color = real3(1, 1, 1);
  371. }
  372. }
  373. // Returns the barycentric coordinates of a point p in a triangle defined by the vertices a, b, and c
  374. float3 GetBarycentricCoord(float2 p, float2 a, float2 b, float2 c)
  375. {
  376. float2 v0 = b - a;
  377. float2 v1 = c - a;
  378. float2 v2 = p - a;
  379. float d00 = dot(v0, v0);
  380. float d01 = dot(v0, v1);
  381. float d11 = dot(v1, v1);
  382. float d20 = dot(v2, v0);
  383. float d21 = dot(v2, v1);
  384. float denom = d00 * d11 - d01 * d01;
  385. float3 bary = 0;
  386. bary.y = (d11 * d20 - d01 * d21) / denom;
  387. bary.z = (d00 * d21 - d01 * d20) / denom;
  388. bary.x = 1.0f - bary.y - bary.z;
  389. return bary;
  390. }
  391. // Returns whether a point p is part of a triangle defined by the vertices a, b, and c
  392. bool IsPointInTriangle(float2 p, float2 a, float2 b, float2 c)
  393. {
  394. float3 bar = GetBarycentricCoord(p, a, b, c);
  395. return (bar.x >= 0 && bar.x <= 1 && bar.y >= 0 && bar.y <= 1 && (bar.x + bar.y) <= 1);
  396. }
  397. /// Return the color of the segment.
  398. ///
  399. /// It will draw a line between the given points with the given appearance (thickness and color).
  400. ///
  401. /// * texcoord: the texture coordinate of the pixel to draw
  402. /// * p1: coordinates of the line start
  403. /// * p2: coordinates of the line end
  404. /// * thickness: how thick the line should be
  405. /// * color: color of the line
  406. float4 DrawSegment(float2 texcoord, float2 p1, float2 p2, float thickness, float3 color)
  407. {
  408. float a = abs(distance(p1, texcoord));
  409. float b = abs(distance(p2, texcoord));
  410. float c = abs(distance(p1, p2));
  411. if (a >= c || b >= c) return 0;
  412. float p = (a + b + c) * 0.5;
  413. float h = 2 / c * sqrt(p * (p - a) * (p - b) * (p - c));
  414. float lineAlpha = lerp(1.0, 0.0, smoothstep(0.5 * thickness, 1.5 * thickness, h));
  415. return float4(color * lineAlpha, lineAlpha);
  416. }
  417. #endif // UNITY_DEBUG_INCLUDED