OutlineLayer.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // Copyright (C) 2019-2021 Alexander Bogarsukov. All rights reserved.
  2. // See the LICENSE.md file in the project root for more information.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace UnityFx.Outline
  9. {
  10. /// <summary>
  11. /// A collection of <see cref="GameObject"/> instances that share outline settings. An <see cref="OutlineLayer"/>
  12. /// can only belong to one <see cref="OutlineLayerCollection"/> at time.
  13. /// </summary>
  14. /// <seealso cref="OutlineLayerCollection"/>
  15. /// <seealso cref="OutlineEffect"/>
  16. [Serializable]
  17. public sealed class OutlineLayer : ICollection<GameObject>, IReadOnlyCollection<GameObject>, IOutlineSettings
  18. {
  19. #region data
  20. [SerializeField, HideInInspector]
  21. private OutlineSettingsInstance _settings = new OutlineSettingsInstance();
  22. [SerializeField, HideInInspector]
  23. private string _name;
  24. [SerializeField, HideInInspector]
  25. private bool _enabled = true;
  26. [SerializeField, HideInInspector]
  27. private bool _mergeLayerObjects;
  28. private OutlineLayerCollection _parentCollection;
  29. private Dictionary<GameObject, OutlineRendererCollection> _outlineObjects = new Dictionary<GameObject, OutlineRendererCollection>();
  30. private List<Renderer> _mergedRenderers;
  31. #endregion
  32. #region interface
  33. /// <summary>
  34. /// Gets the layer name.
  35. /// </summary>
  36. public string Name
  37. {
  38. get
  39. {
  40. if (string.IsNullOrEmpty(_name))
  41. {
  42. return "OutlineLayer #" + Index.ToString();
  43. }
  44. return _name;
  45. }
  46. }
  47. /// <summary>
  48. /// Gets or sets a value indicating whether the layer is enabled.
  49. /// </summary>
  50. /// <seealso cref="Priority"/>
  51. public bool Enabled
  52. {
  53. get
  54. {
  55. return _enabled;
  56. }
  57. set
  58. {
  59. _enabled = value;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets or sets a value indicating whether layer game objects should be trated as one.
  64. /// </summary>
  65. public bool MergeLayerObjects
  66. {
  67. get
  68. {
  69. return _mergeLayerObjects;
  70. }
  71. set
  72. {
  73. _mergeLayerObjects = value;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets index of the layer in parent collection.
  78. /// </summary>
  79. public int Index
  80. {
  81. get
  82. {
  83. if (_parentCollection != null)
  84. {
  85. return _parentCollection.IndexOf(this);
  86. }
  87. return -1;
  88. }
  89. }
  90. /// <summary>
  91. /// Gets or sets outline settings. Set this to non-<see langword="null"/> value to share settings with other components.
  92. /// </summary>
  93. public OutlineSettings OutlineSettings
  94. {
  95. get
  96. {
  97. return _settings.OutlineSettings;
  98. }
  99. set
  100. {
  101. _settings.OutlineSettings = value;
  102. }
  103. }
  104. /// <summary>
  105. /// Initializes a new instance of the <see cref="OutlineLayer"/> class.
  106. /// </summary>
  107. public OutlineLayer()
  108. {
  109. }
  110. /// <summary>
  111. /// Initializes a new instance of the <see cref="OutlineLayer"/> class.
  112. /// </summary>
  113. internal OutlineLayer(OutlineLayerCollection parentCollection)
  114. {
  115. _parentCollection = parentCollection;
  116. }
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="OutlineLayer"/> class.
  119. /// </summary>
  120. public OutlineLayer(string name)
  121. {
  122. _name = name;
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the <see cref="OutlineLayer"/> class.
  126. /// </summary>
  127. /// <exception cref="ArgumentNullException">Thrown if <paramref name="settings"/> is <see langword="null"/>.</exception>
  128. public OutlineLayer(OutlineSettings settings)
  129. {
  130. if (settings is null)
  131. {
  132. throw new ArgumentNullException(nameof(settings));
  133. }
  134. _settings.OutlineSettings = settings;
  135. }
  136. /// <summary>
  137. /// Initializes a new instance of the <see cref="OutlineLayer"/> class.
  138. /// </summary>
  139. /// <exception cref="ArgumentNullException">Thrown if <paramref name="settings"/> is <see langword="null"/>.</exception>
  140. public OutlineLayer(string name, OutlineSettings settings)
  141. {
  142. if (settings is null)
  143. {
  144. throw new ArgumentNullException(nameof(settings));
  145. }
  146. _name = name;
  147. _settings.OutlineSettings = settings;
  148. }
  149. /// <summary>
  150. /// Attempts to get renderers assosiated with the specified <see cref="GameObject"/>.
  151. /// </summary>
  152. /// <exception cref="ArgumentNullException">Thrown if <paramref name="go"/> is <see langword="null"/>.</exception>
  153. public bool TryGetRenderers(GameObject go, out ICollection<Renderer> renderers)
  154. {
  155. if (go is null)
  156. {
  157. throw new ArgumentNullException(nameof(go));
  158. }
  159. if (_outlineObjects.TryGetValue(go, out var result))
  160. {
  161. renderers = result;
  162. return true;
  163. }
  164. renderers = null;
  165. return false;
  166. }
  167. /// <summary>
  168. /// Gets the objects for rendering.
  169. /// </summary>
  170. public void GetRenderObjects(IList<OutlineRenderObject> renderObjects)
  171. {
  172. if (_enabled)
  173. {
  174. if (_mergeLayerObjects)
  175. {
  176. renderObjects.Add(new OutlineRenderObject(GetRenderers(), this, Name));
  177. }
  178. else
  179. {
  180. foreach (var kvp in _outlineObjects)
  181. {
  182. var go = kvp.Key;
  183. if (go && go.activeInHierarchy)
  184. {
  185. renderObjects.Add(new OutlineRenderObject(kvp.Value.GetList(), _settings, go.name));
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// Gets all layer renderers.
  193. /// </summary>
  194. public IReadOnlyList<Renderer> GetRenderers()
  195. {
  196. if (_enabled)
  197. {
  198. if (_mergedRenderers != null)
  199. {
  200. _mergedRenderers.Clear();
  201. }
  202. else
  203. {
  204. _mergedRenderers = new List<Renderer>();
  205. }
  206. foreach (var kvp in _outlineObjects)
  207. {
  208. var go = kvp.Key;
  209. if (go && go.activeInHierarchy)
  210. {
  211. var rl = kvp.Value.GetList();
  212. for (var i = 0; i < rl.Count; i++)
  213. {
  214. _mergedRenderers.Add(rl[i]);
  215. }
  216. }
  217. }
  218. return _mergedRenderers;
  219. }
  220. return Array.Empty<Renderer>();
  221. }
  222. #endregion
  223. #region internals
  224. internal string NameTag
  225. {
  226. get
  227. {
  228. return _name;
  229. }
  230. set
  231. {
  232. _name = value;
  233. }
  234. }
  235. internal OutlineLayerCollection ParentCollection => _parentCollection;
  236. internal void UpdateRenderers(int ignoreLayers)
  237. {
  238. foreach (var renderers in _outlineObjects.Values)
  239. {
  240. renderers.Reset(false, ignoreLayers);
  241. }
  242. }
  243. internal void Reset()
  244. {
  245. _outlineObjects.Clear();
  246. }
  247. internal void SetCollection(OutlineLayerCollection collection)
  248. {
  249. if (_parentCollection == null || collection == null || _parentCollection == collection)
  250. {
  251. _parentCollection = collection;
  252. }
  253. else
  254. {
  255. throw new InvalidOperationException("OutlineLayer can only belong to a single OutlineLayerCollection.");
  256. }
  257. }
  258. #endregion
  259. #region IOutlineSettings
  260. /// <inheritdoc/>
  261. public Color OutlineColor
  262. {
  263. get
  264. {
  265. return _settings.OutlineColor;
  266. }
  267. set
  268. {
  269. _settings.OutlineColor = value;
  270. }
  271. }
  272. /// <inheritdoc/>
  273. public int OutlineWidth
  274. {
  275. get
  276. {
  277. return _settings.OutlineWidth;
  278. }
  279. set
  280. {
  281. _settings.OutlineWidth = value;
  282. }
  283. }
  284. /// <inheritdoc/>
  285. public float OutlineIntensity
  286. {
  287. get
  288. {
  289. return _settings.OutlineIntensity;
  290. }
  291. set
  292. {
  293. _settings.OutlineIntensity = value;
  294. }
  295. }
  296. /// <inheritdoc/>
  297. public float OutlineAlphaCutoff
  298. {
  299. get
  300. {
  301. return _settings.OutlineAlphaCutoff;
  302. }
  303. set
  304. {
  305. _settings.OutlineAlphaCutoff = value;
  306. }
  307. }
  308. /// <inheritdoc/>
  309. public OutlineRenderFlags OutlineRenderMode
  310. {
  311. get
  312. {
  313. return _settings.OutlineRenderMode;
  314. }
  315. set
  316. {
  317. _settings.OutlineRenderMode = value;
  318. }
  319. }
  320. #endregion
  321. #region ICollection
  322. /// <inheritdoc/>
  323. public int Count => _outlineObjects.Count;
  324. /// <inheritdoc/>
  325. public bool IsReadOnly => false;
  326. /// <inheritdoc/>
  327. public void Add(GameObject go)
  328. {
  329. if (go is null)
  330. {
  331. throw new ArgumentNullException(nameof(go));
  332. }
  333. if (!_outlineObjects.ContainsKey(go))
  334. {
  335. var renderers = new OutlineRendererCollection(go);
  336. renderers.Reset(false, _parentCollection.IgnoreLayerMask);
  337. _outlineObjects.Add(go, renderers);
  338. }
  339. }
  340. /// <inheritdoc/>
  341. public bool Remove(GameObject go)
  342. {
  343. if (go is null)
  344. {
  345. return false;
  346. }
  347. return _outlineObjects.Remove(go);
  348. }
  349. /// <inheritdoc/>
  350. public bool Contains(GameObject go)
  351. {
  352. if (go is null)
  353. {
  354. return false;
  355. }
  356. return _outlineObjects.ContainsKey(go);
  357. }
  358. /// <inheritdoc/>
  359. public void Clear()
  360. {
  361. _outlineObjects.Clear();
  362. }
  363. /// <inheritdoc/>
  364. public void CopyTo(GameObject[] array, int arrayIndex)
  365. {
  366. _outlineObjects.Keys.CopyTo(array, arrayIndex);
  367. }
  368. #endregion
  369. #region IEnumerable
  370. /// <inheritdoc/>
  371. public IEnumerator<GameObject> GetEnumerator()
  372. {
  373. return _outlineObjects.Keys.GetEnumerator();
  374. }
  375. IEnumerator IEnumerable.GetEnumerator()
  376. {
  377. return _outlineObjects.Keys.GetEnumerator();
  378. }
  379. #endregion
  380. #region IEquatable
  381. /// <inheritdoc/>
  382. public bool Equals(IOutlineSettings other)
  383. {
  384. return OutlineSettings.Equals(this, other);
  385. }
  386. #endregion
  387. #region Object
  388. /// <inheritdoc/>
  389. public override string ToString()
  390. {
  391. var text = new StringBuilder();
  392. if (string.IsNullOrEmpty(_name))
  393. {
  394. text.Append("OutlineLayer");
  395. }
  396. else
  397. {
  398. text.Append(_name);
  399. }
  400. if (_parentCollection != null)
  401. {
  402. text.Append(" #");
  403. text.Append(_parentCollection.IndexOf(this));
  404. }
  405. if (_outlineObjects.Count > 0)
  406. {
  407. text.Append(" (");
  408. foreach (var go in _outlineObjects.Keys)
  409. {
  410. text.Append(go.name);
  411. text.Append(", ");
  412. }
  413. text.Remove(text.Length - 2, 2);
  414. text.Append(")");
  415. }
  416. return string.Format("{0}", text);
  417. }
  418. /// <inheritdoc/>
  419. public override bool Equals(object other)
  420. {
  421. return OutlineSettings.Equals(this, other as IOutlineSettings);
  422. }
  423. /// <inheritdoc/>
  424. public override int GetHashCode()
  425. {
  426. return base.GetHashCode();
  427. }
  428. #endregion
  429. #region implementation
  430. #endregion
  431. }
  432. }