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