DebugDisplaySettingsMaterial.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using UnityEngine;
  2. using NameAndTooltip = UnityEngine.Rendering.DebugUI.Widget.NameAndTooltip;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. class DebugDisplaySettingsMaterial : IDebugDisplaySettingsData
  6. {
  7. #region Material validation
  8. internal enum AlbedoDebugValidationPreset
  9. {
  10. DefaultLuminance,
  11. BlackAcrylicPaint,
  12. DarkSoil,
  13. WornAsphalt,
  14. DryClaySoil,
  15. GreenGrass,
  16. OldConcrete,
  17. RedClayTile,
  18. DrySand,
  19. NewConcrete,
  20. WhiteAcrylicPaint,
  21. FreshSnow,
  22. BlueSky,
  23. Foliage,
  24. }
  25. struct AlbedoDebugValidationPresetData
  26. {
  27. public string name;
  28. public Color color;
  29. public float minLuminance;
  30. public float maxLuminance;
  31. }
  32. AlbedoDebugValidationPresetData[] m_AlbedoDebugValidationPresetData =
  33. {
  34. new AlbedoDebugValidationPresetData()
  35. {
  36. name = "Default Luminance",
  37. color = new Color(127f / 255f, 127f / 255f, 127f / 255f),
  38. minLuminance = 0.01f,
  39. maxLuminance = 0.90f
  40. },
  41. // colors taken from http://www.babelcolor.com/index_htm_files/ColorChecker_RGB_and_spectra.xls
  42. new AlbedoDebugValidationPresetData()
  43. {
  44. name = "Black Acrylic Paint",
  45. color = new Color(56f / 255f, 56f / 255f, 56f / 255f),
  46. minLuminance = 0.03f,
  47. maxLuminance = 0.07f
  48. },
  49. new AlbedoDebugValidationPresetData()
  50. {
  51. name = "Dark Soil",
  52. color = new Color(85f / 255f, 61f / 255f, 49f / 255f),
  53. minLuminance = 0.05f,
  54. maxLuminance = 0.14f
  55. },
  56. new AlbedoDebugValidationPresetData()
  57. {
  58. name = "Worn Asphalt",
  59. color = new Color(91f / 255f, 91f / 255f, 91f / 255f),
  60. minLuminance = 0.10f,
  61. maxLuminance = 0.15f
  62. },
  63. new AlbedoDebugValidationPresetData()
  64. {
  65. name = "Dry Clay Soil",
  66. color = new Color(137f / 255f, 120f / 255f, 102f / 255f),
  67. minLuminance = 0.15f,
  68. maxLuminance = 0.35f
  69. },
  70. new AlbedoDebugValidationPresetData()
  71. {
  72. name = "Green Grass",
  73. color = new Color(123f / 255f, 131f / 255f, 74f / 255f),
  74. minLuminance = 0.16f,
  75. maxLuminance = 0.26f
  76. },
  77. new AlbedoDebugValidationPresetData()
  78. {
  79. name = "Old Concrete",
  80. color = new Color(135f / 255f, 136f / 255f, 131f / 255f),
  81. minLuminance = 0.17f,
  82. maxLuminance = 0.30f
  83. },
  84. new AlbedoDebugValidationPresetData()
  85. {
  86. name = "Red Clay Tile",
  87. color = new Color(197f / 255f, 125f / 255f, 100f / 255f),
  88. minLuminance = 0.23f,
  89. maxLuminance = 0.33f
  90. },
  91. new AlbedoDebugValidationPresetData()
  92. {
  93. name = "Dry Sand",
  94. color = new Color(177f / 255f, 167f / 255f, 132f / 255f),
  95. minLuminance = 0.20f,
  96. maxLuminance = 0.45f
  97. },
  98. new AlbedoDebugValidationPresetData()
  99. {
  100. name = "New Concrete",
  101. color = new Color(185f / 255f, 182f / 255f, 175f / 255f),
  102. minLuminance = 0.32f,
  103. maxLuminance = 0.55f
  104. },
  105. new AlbedoDebugValidationPresetData()
  106. {
  107. name = "White Acrylic Paint",
  108. color = new Color(227f / 255f, 227f / 255f, 227f / 255f),
  109. minLuminance = 0.75f,
  110. maxLuminance = 0.85f
  111. },
  112. new AlbedoDebugValidationPresetData()
  113. {
  114. name = "Fresh Snow",
  115. color = new Color(243f / 255f, 243f / 255f, 243f / 255f),
  116. minLuminance = 0.85f,
  117. maxLuminance = 0.95f
  118. },
  119. new AlbedoDebugValidationPresetData()
  120. {
  121. name = "Blue Sky",
  122. color = new Color(93f / 255f, 123f / 255f, 157f / 255f),
  123. minLuminance = new Color(93f / 255f, 123f / 255f, 157f / 255f).linear.maxColorComponent - 0.05f,
  124. maxLuminance = new Color(93f / 255f, 123f / 255f, 157f / 255f).linear.maxColorComponent + 0.05f
  125. },
  126. new AlbedoDebugValidationPresetData()
  127. {
  128. name = "Foliage",
  129. color = new Color(91f / 255f, 108f / 255f, 65f / 255f),
  130. minLuminance = new Color(91f / 255f, 108f / 255f, 65f / 255f).linear.maxColorComponent - 0.05f,
  131. maxLuminance = new Color(91f / 255f, 108f / 255f, 65f / 255f).linear.maxColorComponent + 0.05f
  132. },
  133. };
  134. AlbedoDebugValidationPreset m_AlbedoDebugValidationPreset;
  135. internal AlbedoDebugValidationPreset albedoDebugValidationPreset
  136. {
  137. get => m_AlbedoDebugValidationPreset;
  138. set
  139. {
  140. m_AlbedoDebugValidationPreset = value;
  141. AlbedoDebugValidationPresetData presetData = m_AlbedoDebugValidationPresetData[(int)value];
  142. AlbedoMinLuminance = presetData.minLuminance;
  143. AlbedoMaxLuminance = presetData.maxLuminance;
  144. AlbedoCompareColor = presetData.color;
  145. }
  146. }
  147. internal float AlbedoMinLuminance = 0.01f;
  148. internal float AlbedoMaxLuminance = 0.90f;
  149. float m_AlbedoHueTolerance = 0.104f;
  150. internal float AlbedoHueTolerance
  151. {
  152. get => m_AlbedoDebugValidationPreset == AlbedoDebugValidationPreset.DefaultLuminance ? 1.0f : m_AlbedoHueTolerance;
  153. private set => m_AlbedoHueTolerance = value;
  154. }
  155. float m_AlbedoSaturationTolerance = 0.214f;
  156. internal float AlbedoSaturationTolerance
  157. {
  158. get => m_AlbedoDebugValidationPreset == AlbedoDebugValidationPreset.DefaultLuminance ? 1.0f : m_AlbedoSaturationTolerance;
  159. private set => m_AlbedoSaturationTolerance = value;
  160. }
  161. internal Color AlbedoCompareColor = new Color(127f / 255f, 127f / 255f, 127f / 255f, 255f / 255f);
  162. internal float MetallicMinValue = 0.0f;
  163. internal float MetallicMaxValue = 0.9f;
  164. internal DebugMaterialValidationMode MaterialValidationMode;
  165. #endregion
  166. internal DebugMaterialMode DebugMaterialModeData { get; private set; }
  167. internal DebugVertexAttributeMode DebugVertexAttributeIndexData { get; private set; }
  168. static class Strings
  169. {
  170. public const string AlbedoSettingsContainerName = "Albedo Settings";
  171. public const string MetallicSettingsContainerName = "Metallic Settings";
  172. public static readonly NameAndTooltip MaterialOverride = new() { name = "Material Override", tooltip = "Use the drop-down to select a Material property to visualize on every GameObject on screen." };
  173. public static readonly NameAndTooltip VertexAttribute = new() { name = "Vertex Attribute", tooltip = "Use the drop-down to select a 3D GameObject attribute, like Texture Coordinates or Vertex Color, to visualize on screen." };
  174. public static readonly NameAndTooltip MaterialValidationMode = new() { name = "Material Validation Mode", tooltip = "Debug and validate material properties." };
  175. public static readonly NameAndTooltip ValidationPreset = new() { name = "Validation Preset", tooltip = "Validate using a list of preset surfaces and inputs based on real-world surfaces." };
  176. public static readonly NameAndTooltip AlbedoMinLuminance = new() { name = "Min Luminance", tooltip = "Any values set below this field are invalid and appear red on screen." };
  177. public static readonly NameAndTooltip AlbedoMaxLuminance = new() { name = "Max Luminance", tooltip = "Any values set above this field are invalid and appear blue on screen." };
  178. public static readonly NameAndTooltip AlbedoHueTolerance = new() { name = "Hue Tolerance", tooltip = "Validate a material based on a specific hue." };
  179. public static readonly NameAndTooltip AlbedoSaturationTolerance = new() { name = "Saturation Tolerance", tooltip = "Validate a material based on a specific Saturation." };
  180. public static readonly NameAndTooltip MetallicMinValue = new() { name = "Min Value", tooltip = "Any values set below this field are invalid and appear red on screen." };
  181. public static readonly NameAndTooltip MetallicMaxValue = new() { name = "Max Value", tooltip = "Any values set above this field are invalid and appear blue on screen." };
  182. }
  183. internal static class WidgetFactory
  184. {
  185. internal static DebugUI.Widget CreateMaterialOverride(DebugDisplaySettingsMaterial data) => new DebugUI.EnumField
  186. {
  187. nameAndTooltip = Strings.MaterialOverride,
  188. autoEnum = typeof(DebugMaterialMode),
  189. getter = () => (int)data.DebugMaterialModeData,
  190. setter = (value) => { },
  191. getIndex = () => (int)data.DebugMaterialModeData,
  192. setIndex = (value) => data.DebugMaterialModeData = (DebugMaterialMode)value
  193. };
  194. internal static DebugUI.Widget CreateVertexAttribute(DebugDisplaySettingsMaterial data) => new DebugUI.EnumField
  195. {
  196. nameAndTooltip = Strings.VertexAttribute,
  197. autoEnum = typeof(DebugVertexAttributeMode),
  198. getter = () => (int)data.DebugVertexAttributeIndexData,
  199. setter = (value) => { },
  200. getIndex = () => (int)data.DebugVertexAttributeIndexData,
  201. setIndex = (value) => data.DebugVertexAttributeIndexData = (DebugVertexAttributeMode)value
  202. };
  203. internal static DebugUI.Widget CreateMaterialValidationMode(DebugDisplaySettingsMaterial data) => new DebugUI.EnumField
  204. {
  205. nameAndTooltip = Strings.MaterialValidationMode,
  206. autoEnum = typeof(DebugMaterialValidationMode),
  207. getter = () => (int)data.MaterialValidationMode,
  208. setter = (value) => { },
  209. getIndex = () => (int)data.MaterialValidationMode,
  210. setIndex = (value) => data.MaterialValidationMode = (DebugMaterialValidationMode)value,
  211. onValueChanged = (_, _) => DebugManager.instance.ReDrawOnScreenDebug()
  212. };
  213. internal static DebugUI.Widget CreateAlbedoPreset(DebugDisplaySettingsMaterial data) => new DebugUI.EnumField
  214. {
  215. nameAndTooltip = Strings.ValidationPreset,
  216. autoEnum = typeof(AlbedoDebugValidationPreset),
  217. getter = () => (int)data.albedoDebugValidationPreset,
  218. setter = (value) => { },
  219. getIndex = () => (int)data.albedoDebugValidationPreset,
  220. setIndex = (value) => data.albedoDebugValidationPreset = (AlbedoDebugValidationPreset)value,
  221. onValueChanged = (_, _) => DebugManager.instance.ReDrawOnScreenDebug()
  222. };
  223. internal static DebugUI.Widget CreateAlbedoMinLuminance(DebugDisplaySettingsMaterial data) => new DebugUI.FloatField
  224. {
  225. nameAndTooltip = Strings.AlbedoMinLuminance,
  226. getter = () => data.AlbedoMinLuminance,
  227. setter = (value) => data.AlbedoMinLuminance = value,
  228. incStep = 0.01f
  229. };
  230. internal static DebugUI.Widget CreateAlbedoMaxLuminance(DebugDisplaySettingsMaterial data) => new DebugUI.FloatField
  231. {
  232. nameAndTooltip = Strings.AlbedoMaxLuminance,
  233. getter = () => data.AlbedoMaxLuminance,
  234. setter = (value) => data.AlbedoMaxLuminance = value,
  235. incStep = 0.01f
  236. };
  237. internal static DebugUI.Widget CreateAlbedoHueTolerance(DebugDisplaySettingsMaterial data) => new DebugUI.FloatField
  238. {
  239. nameAndTooltip = Strings.AlbedoHueTolerance,
  240. getter = () => data.AlbedoHueTolerance,
  241. setter = (value) => data.AlbedoHueTolerance = value,
  242. incStep = 0.01f,
  243. isHiddenCallback = () => data.albedoDebugValidationPreset == AlbedoDebugValidationPreset.DefaultLuminance
  244. };
  245. internal static DebugUI.Widget CreateAlbedoSaturationTolerance(DebugDisplaySettingsMaterial data) => new DebugUI.FloatField
  246. {
  247. nameAndTooltip = Strings.AlbedoSaturationTolerance,
  248. getter = () => data.AlbedoSaturationTolerance,
  249. setter = (value) => data.AlbedoSaturationTolerance = value,
  250. incStep = 0.01f,
  251. isHiddenCallback = () => data.albedoDebugValidationPreset == AlbedoDebugValidationPreset.DefaultLuminance
  252. };
  253. internal static DebugUI.Widget CreateMetallicMinValue(DebugDisplaySettingsMaterial data) => new DebugUI.FloatField
  254. {
  255. nameAndTooltip = Strings.MetallicMinValue,
  256. getter = () => data.MetallicMinValue,
  257. setter = (value) => data.MetallicMinValue = value,
  258. incStep = 0.01f
  259. };
  260. internal static DebugUI.Widget CreateMetallicMaxValue(DebugDisplaySettingsMaterial data) => new DebugUI.FloatField
  261. {
  262. nameAndTooltip = Strings.MetallicMaxValue,
  263. getter = () => data.MetallicMaxValue,
  264. setter = (value) => data.MetallicMaxValue = value,
  265. incStep = 0.01f
  266. };
  267. }
  268. private class SettingsPanel : DebugDisplaySettingsPanel
  269. {
  270. public override string PanelName => "Material";
  271. public SettingsPanel(DebugDisplaySettingsMaterial data)
  272. {
  273. AddWidget(DebugDisplaySettingsCommon.WidgetFactory.CreateMissingDebugShadersWarning());
  274. AddWidget(new DebugUI.Foldout
  275. {
  276. displayName = "Material Filters",
  277. isHeader = true,
  278. opened = true,
  279. children =
  280. {
  281. WidgetFactory.CreateMaterialOverride(data),
  282. WidgetFactory.CreateVertexAttribute(data)
  283. }
  284. });
  285. AddWidget(new DebugUI.Foldout
  286. {
  287. displayName = "Material Validation",
  288. isHeader = true,
  289. opened = true,
  290. children =
  291. {
  292. WidgetFactory.CreateMaterialValidationMode(data),
  293. new DebugUI.Container()
  294. {
  295. displayName = Strings.AlbedoSettingsContainerName,
  296. isHiddenCallback = () => data.MaterialValidationMode != DebugMaterialValidationMode.Albedo,
  297. children =
  298. {
  299. WidgetFactory.CreateAlbedoPreset(data),
  300. WidgetFactory.CreateAlbedoMinLuminance(data),
  301. WidgetFactory.CreateAlbedoMaxLuminance(data),
  302. WidgetFactory.CreateAlbedoHueTolerance(data),
  303. WidgetFactory.CreateAlbedoSaturationTolerance(data)
  304. }
  305. },
  306. new DebugUI.Container()
  307. {
  308. displayName = Strings.MetallicSettingsContainerName,
  309. isHiddenCallback = () => data.MaterialValidationMode != DebugMaterialValidationMode.Metallic,
  310. children =
  311. {
  312. WidgetFactory.CreateMetallicMinValue(data),
  313. WidgetFactory.CreateMetallicMaxValue(data)
  314. }
  315. }
  316. }
  317. });
  318. }
  319. }
  320. #region IDebugDisplaySettingsData
  321. public bool AreAnySettingsActive =>
  322. (DebugMaterialModeData != DebugMaterialMode.None) ||
  323. (DebugVertexAttributeIndexData != DebugVertexAttributeMode.None) ||
  324. (MaterialValidationMode != DebugMaterialValidationMode.None);
  325. public bool IsPostProcessingAllowed => !AreAnySettingsActive;
  326. public bool IsLightingActive => !AreAnySettingsActive;
  327. public bool TryGetScreenClearColor(ref Color color)
  328. {
  329. return false;
  330. }
  331. public IDebugDisplaySettingsPanelDisposable CreatePanel()
  332. {
  333. return new SettingsPanel(this);
  334. }
  335. #endregion
  336. }
  337. }