SoftMask.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Profiling;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Serialization;
  6. using UnityEngine.UI;
  7. using Object = UnityEngine.Object;
  8. namespace Coffee.UISoftMask
  9. {
  10. /// <summary>
  11. /// Soft mask.
  12. /// Use instead of Mask for smooth masking.
  13. /// </summary>
  14. public class SoftMask : Mask, IMeshModifier
  15. {
  16. /// <summary>
  17. /// Down sampling rate.
  18. /// </summary>
  19. public enum DownSamplingRate
  20. {
  21. None = 0,
  22. x1 = 1,
  23. x2 = 2,
  24. x4 = 4,
  25. x8 = 8,
  26. }
  27. private static readonly List<SoftMask>[] s_TmpSoftMasks = new List<SoftMask>[]
  28. {
  29. new List<SoftMask>(),
  30. new List<SoftMask>(),
  31. new List<SoftMask>(),
  32. new List<SoftMask>(),
  33. };
  34. private static readonly Color[] s_ClearColors = new Color[]
  35. {
  36. new Color(0, 0, 0, 0),
  37. new Color(1, 0, 0, 0),
  38. new Color(1, 1, 0, 0),
  39. new Color(1, 1, 1, 0),
  40. };
  41. private static bool s_UVStartsAtTop;
  42. private static bool s_IsMetal;
  43. private static Shader s_SoftMaskShader;
  44. private static Texture2D s_ReadTexture;
  45. private static readonly List<SoftMask> s_ActiveSoftMasks = new List<SoftMask>();
  46. private static readonly List<SoftMask> s_TempRelatables = new List<SoftMask>();
  47. private static readonly Dictionary<int, Matrix4x4> s_PreviousViewProjectionMatrices = new Dictionary<int, Matrix4x4>();
  48. private static readonly Dictionary<int, Matrix4x4> s_NowViewProjectionMatrices = new Dictionary<int, Matrix4x4>();
  49. private static int s_StencilCompId;
  50. private static int s_ColorMaskId;
  51. private static int s_MainTexId;
  52. private static int s_SoftnessId;
  53. private static int s_Alpha;
  54. private static int s_PreviousWidth;
  55. private static int s_PreviousHeight;
  56. private MaterialPropertyBlock _mpb;
  57. private CommandBuffer _cb;
  58. private Material _material;
  59. private RenderTexture _softMaskBuffer;
  60. private int _stencilDepth;
  61. private Mesh _mesh;
  62. private SoftMask _parent;
  63. internal readonly List<SoftMask> _children = new List<SoftMask>();
  64. private bool _hasChanged = false;
  65. private bool _hasStencilStateChanged = false;
  66. [FormerlySerializedAs("m_DesamplingRate")] [SerializeField, Tooltip("The down sampling rate for soft mask buffer.")]
  67. private DownSamplingRate m_DownSamplingRate = DownSamplingRate.x1;
  68. [SerializeField, Range(0, 1), Tooltip("The value used by the soft mask to select the area of influence defined over the soft mask's graphic.")]
  69. private float m_Softness = 1;
  70. [SerializeField, Range(0f, 1f), Tooltip("The transparency of the whole masked graphic.")]
  71. private float m_Alpha = 1;
  72. [Header("Advanced Options")] [SerializeField, Tooltip("Should the soft mask ignore parent soft masks?")]
  73. private bool m_IgnoreParent = false;
  74. [SerializeField, Tooltip("Is the soft mask a part of parent soft mask?")]
  75. private bool m_PartOfParent = false;
  76. [SerializeField, Tooltip("Self graphic will not be drawn to soft mask buffer.")]
  77. private bool m_IgnoreSelfGraphic;
  78. [SerializeField, Tooltip("Self graphic will not be written to stencil buffer.")]
  79. private bool m_IgnoreSelfStencil;
  80. /// <summary>
  81. /// The down sampling rate for soft mask buffer.
  82. /// </summary>
  83. public DownSamplingRate downSamplingRate
  84. {
  85. get { return m_DownSamplingRate; }
  86. set
  87. {
  88. if (m_DownSamplingRate == value) return;
  89. m_DownSamplingRate = value;
  90. hasChanged = true;
  91. }
  92. }
  93. /// <summary>
  94. /// The value used by the soft mask to select the area of influence defined over the soft mask's graphic.
  95. /// </summary>
  96. public float softness
  97. {
  98. get { return m_Softness; }
  99. set
  100. {
  101. value = Mathf.Clamp01(value);
  102. if (Mathf.Approximately(m_Softness, value)) return;
  103. m_Softness = value;
  104. hasChanged = true;
  105. }
  106. }
  107. /// <summary>
  108. /// The transparency of the whole masked graphic.
  109. /// </summary>
  110. public float alpha
  111. {
  112. get { return m_Alpha; }
  113. set
  114. {
  115. value = Mathf.Clamp01(value);
  116. if (Mathf.Approximately(m_Alpha, value)) return;
  117. m_Alpha = value;
  118. hasChanged = true;
  119. }
  120. }
  121. /// <summary>
  122. /// Should the soft mask ignore parent soft masks?
  123. /// </summary>
  124. /// <value>If set to true the soft mask will ignore any parent soft mask settings.</value>
  125. public bool ignoreParent
  126. {
  127. get { return m_IgnoreParent; }
  128. set
  129. {
  130. if (m_IgnoreParent == value) return;
  131. m_IgnoreParent = value;
  132. hasChanged = true;
  133. OnTransformParentChanged();
  134. }
  135. }
  136. /// <summary>
  137. /// Is the soft mask a part of parent soft mask?
  138. /// </summary>
  139. public bool partOfParent
  140. {
  141. get { return m_PartOfParent; }
  142. set
  143. {
  144. if (m_PartOfParent == value) return;
  145. m_PartOfParent = value;
  146. hasChanged = true;
  147. OnTransformParentChanged();
  148. }
  149. }
  150. /// <summary>
  151. /// The soft mask buffer.
  152. /// </summary>
  153. public RenderTexture softMaskBuffer
  154. {
  155. get
  156. {
  157. if (_parent)
  158. {
  159. ReleaseRt(ref _softMaskBuffer);
  160. return _parent.softMaskBuffer;
  161. }
  162. // Check the size of soft mask buffer.
  163. int w, h;
  164. GetDownSamplingSize(m_DownSamplingRate, out w, out h);
  165. if (_softMaskBuffer && (_softMaskBuffer.width != w || _softMaskBuffer.height != h))
  166. {
  167. ReleaseRt(ref _softMaskBuffer);
  168. }
  169. if (!_softMaskBuffer)
  170. {
  171. _softMaskBuffer = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, 1, RenderTextureMemoryless.Depth);
  172. hasChanged = true;
  173. _hasStencilStateChanged = true;
  174. }
  175. return _softMaskBuffer;
  176. }
  177. }
  178. public bool hasChanged
  179. {
  180. get { return _parent ? _parent.hasChanged : _hasChanged; }
  181. private set
  182. {
  183. if (_parent)
  184. {
  185. _parent.hasChanged = value;
  186. }
  187. _hasChanged = value;
  188. }
  189. }
  190. public SoftMask parent
  191. {
  192. get { return _parent; }
  193. }
  194. public bool ignoreSelfGraphic
  195. {
  196. get { return m_IgnoreSelfGraphic; }
  197. set
  198. {
  199. if (m_IgnoreSelfGraphic == value) return;
  200. m_IgnoreSelfGraphic = value;
  201. hasChanged = true;
  202. graphic.SetVerticesDirtyEx();
  203. }
  204. }
  205. public bool ignoreSelfStencil
  206. {
  207. get { return m_IgnoreSelfStencil; }
  208. set
  209. {
  210. if (m_IgnoreSelfStencil == value) return;
  211. m_IgnoreSelfStencil = value;
  212. hasChanged = true;
  213. graphic.SetVerticesDirtyEx();
  214. graphic.SetMaterialDirtyEx();
  215. }
  216. }
  217. Material material
  218. {
  219. get
  220. {
  221. return _material
  222. ? _material
  223. : _material =
  224. new Material(s_SoftMaskShader
  225. ? s_SoftMaskShader
  226. : s_SoftMaskShader = Resources.Load<Shader>("SoftMask")) {hideFlags = HideFlags.HideAndDontSave};
  227. }
  228. }
  229. Mesh mesh
  230. {
  231. get { return _mesh ? _mesh : _mesh = new Mesh() {hideFlags = HideFlags.HideAndDontSave}; }
  232. }
  233. /// <summary>
  234. /// Perform material modification in this function.
  235. /// </summary>
  236. /// <returns>Modified material.</returns>
  237. /// <param name="baseMaterial">Configured Material.</param>
  238. public override Material GetModifiedMaterial(Material baseMaterial)
  239. {
  240. hasChanged = true;
  241. if (ignoreSelfStencil) return baseMaterial;
  242. var result = base.GetModifiedMaterial(baseMaterial);
  243. if (m_IgnoreParent && result != baseMaterial)
  244. {
  245. result.SetInt(s_StencilCompId, (int) CompareFunction.Always);
  246. }
  247. return result;
  248. }
  249. /// <summary>
  250. /// Call used to modify mesh.
  251. /// </summary>
  252. void IMeshModifier.ModifyMesh(Mesh mesh)
  253. {
  254. hasChanged = true;
  255. _mesh = mesh;
  256. }
  257. /// <summary>
  258. /// Call used to modify mesh.
  259. /// </summary>
  260. void IMeshModifier.ModifyMesh(VertexHelper verts)
  261. {
  262. if (isActiveAndEnabled)
  263. {
  264. if (ignoreSelfGraphic)
  265. {
  266. verts.Clear();
  267. verts.FillMesh(mesh);
  268. }
  269. else if (ignoreSelfStencil)
  270. {
  271. verts.FillMesh(mesh);
  272. verts.Clear();
  273. }
  274. else
  275. {
  276. verts.FillMesh(mesh);
  277. }
  278. }
  279. hasChanged = true;
  280. }
  281. /// <summary>
  282. /// Given a point and a camera is the raycast valid.
  283. /// </summary>
  284. /// <returns>Valid.</returns>
  285. /// <param name="sp">Screen position.</param>
  286. /// <param name="eventCamera">Raycast camera.</param>
  287. /// <param name="g">Target graphic.</param>
  288. public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera, Graphic g, int[] interactions)
  289. {
  290. if (!isActiveAndEnabled || (g == graphic && !g.raycastTarget)) return true;
  291. int x = (int) ((softMaskBuffer.width - 1) * Mathf.Clamp01(sp.x / Screen.width));
  292. int y = s_UVStartsAtTop && !s_IsMetal
  293. ? (int) ((softMaskBuffer.height - 1) * (1 - Mathf.Clamp01(sp.y / Screen.height)))
  294. : (int) ((softMaskBuffer.height - 1) * Mathf.Clamp01(sp.y / Screen.height));
  295. return 0.5f < GetPixelValue(x, y, interactions);
  296. }
  297. public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
  298. {
  299. return true;
  300. }
  301. /// <summary>
  302. /// This function is called when the object becomes enabled and active.
  303. /// </summary>
  304. protected override void OnEnable()
  305. {
  306. hasChanged = true;
  307. // Register.
  308. if (s_ActiveSoftMasks.Count == 0)
  309. {
  310. Canvas.willRenderCanvases += UpdateMaskTextures;
  311. if (s_StencilCompId == 0)
  312. {
  313. s_UVStartsAtTop = SystemInfo.graphicsUVStartsAtTop;
  314. s_IsMetal = SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal;
  315. s_StencilCompId = Shader.PropertyToID("_StencilComp");
  316. s_ColorMaskId = Shader.PropertyToID("_ColorMask");
  317. s_MainTexId = Shader.PropertyToID("_MainTex");
  318. s_SoftnessId = Shader.PropertyToID("_Softness");
  319. s_Alpha = Shader.PropertyToID("_Alpha");
  320. }
  321. }
  322. s_ActiveSoftMasks.Add(this);
  323. // Reset the parent-child relation.
  324. GetComponentsInChildren<SoftMask>(false, s_TempRelatables);
  325. for (int i = s_TempRelatables.Count - 1; 0 <= i; i--)
  326. {
  327. s_TempRelatables[i].OnTransformParentChanged();
  328. }
  329. s_TempRelatables.Clear();
  330. // Create objects.
  331. _mpb = new MaterialPropertyBlock();
  332. _cb = new CommandBuffer();
  333. graphic.SetVerticesDirtyEx();
  334. base.OnEnable();
  335. _hasStencilStateChanged = false;
  336. }
  337. /// <summary>
  338. /// This function is called when the behaviour becomes disabled.
  339. /// </summary>
  340. protected override void OnDisable()
  341. {
  342. // Unregister.
  343. s_ActiveSoftMasks.Remove(this);
  344. if (s_ActiveSoftMasks.Count == 0)
  345. {
  346. Canvas.willRenderCanvases -= UpdateMaskTextures;
  347. }
  348. // Reset the parent-child relation.
  349. for (int i = _children.Count - 1; 0 <= i; i--)
  350. {
  351. _children[i].SetParent(_parent);
  352. }
  353. _children.Clear();
  354. SetParent(null);
  355. // Destroy objects.
  356. _mpb.Clear();
  357. _mpb = null;
  358. _cb.Release();
  359. _cb = null;
  360. ReleaseObject(_mesh);
  361. _mesh = null;
  362. ReleaseObject(_material);
  363. _material = null;
  364. ReleaseRt(ref _softMaskBuffer);
  365. base.OnDisable();
  366. _hasStencilStateChanged = false;
  367. }
  368. /// <summary>
  369. /// This function is called when the parent property of the transform of the GameObject has changed.
  370. /// </summary>
  371. protected override void OnTransformParentChanged()
  372. {
  373. hasChanged = true;
  374. SoftMask newParent = null;
  375. if (isActiveAndEnabled && !m_IgnoreParent)
  376. {
  377. var parentTransform = transform.parent;
  378. while (parentTransform && (!newParent || !newParent.enabled))
  379. {
  380. newParent = parentTransform.GetComponent<SoftMask>();
  381. parentTransform = parentTransform.parent;
  382. }
  383. }
  384. SetParent(newParent);
  385. hasChanged = true;
  386. }
  387. protected override void OnRectTransformDimensionsChange()
  388. {
  389. hasChanged = true;
  390. }
  391. #if UNITY_EDITOR
  392. /// <summary>
  393. /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
  394. /// </summary>
  395. protected override void OnValidate()
  396. {
  397. graphic.SetVerticesDirtyEx();
  398. graphic.SetMaterialDirtyEx();
  399. OnTransformParentChanged();
  400. base.OnValidate();
  401. _hasStencilStateChanged = false;
  402. }
  403. #endif
  404. /// <summary>
  405. /// Update all soft mask textures.
  406. /// </summary>
  407. private static void UpdateMaskTextures()
  408. {
  409. Profiler.BeginSample("UpdateMaskTextures");
  410. foreach (var sm in s_ActiveSoftMasks)
  411. {
  412. if (!sm || sm._hasChanged)
  413. continue;
  414. var canvas = sm.graphic.canvas;
  415. if (!canvas)
  416. continue;
  417. if (canvas.renderMode == RenderMode.WorldSpace)
  418. {
  419. var cam = canvas.worldCamera;
  420. if (!cam)
  421. continue;
  422. Profiler.BeginSample("Check view projection matrix changed (world space)");
  423. var nowVP = cam.projectionMatrix * cam.worldToCameraMatrix;
  424. var previousVP = default(Matrix4x4);
  425. var id = cam.GetInstanceID();
  426. s_PreviousViewProjectionMatrices.TryGetValue(id, out previousVP);
  427. s_NowViewProjectionMatrices[id] = nowVP;
  428. if (previousVP != nowVP)
  429. {
  430. sm.hasChanged = true;
  431. }
  432. Profiler.EndSample();
  433. }
  434. var rt = sm.rectTransform;
  435. if (rt.hasChanged)
  436. {
  437. rt.hasChanged = false;
  438. sm.hasChanged = true;
  439. }
  440. #if UNITY_EDITOR
  441. if (!Application.isPlaying)
  442. {
  443. sm.hasChanged = true;
  444. }
  445. #endif
  446. }
  447. Profiler.BeginSample("Update changed soft masks");
  448. foreach (var sm in s_ActiveSoftMasks)
  449. {
  450. if (!sm || !sm._hasChanged)
  451. continue;
  452. sm._hasChanged = false;
  453. if (sm._parent) continue;
  454. sm.UpdateMaskTexture();
  455. if (!sm._hasStencilStateChanged) continue;
  456. sm._hasStencilStateChanged = false;
  457. Profiler.BeginSample("Notify stencil state changed");
  458. MaskUtilities.NotifyStencilStateChanged(sm);
  459. Profiler.EndSample();
  460. }
  461. Profiler.EndSample();
  462. Profiler.BeginSample("Update previous view projection matrices");
  463. s_PreviousViewProjectionMatrices.Clear();
  464. foreach (var kv in s_NowViewProjectionMatrices)
  465. {
  466. s_PreviousViewProjectionMatrices.Add(kv.Key, kv.Value);
  467. }
  468. s_NowViewProjectionMatrices.Clear();
  469. Profiler.EndSample();
  470. Profiler.EndSample();
  471. #if UNITY_EDITOR
  472. var w = s_PreviousWidth;
  473. var h = s_PreviousHeight;
  474. GetDownSamplingSize(DownSamplingRate.None, out s_PreviousWidth, out s_PreviousHeight);
  475. if (w != s_PreviousWidth || h != s_PreviousHeight)
  476. {
  477. Canvas.ForceUpdateCanvases();
  478. }
  479. #endif
  480. }
  481. /// <summary>
  482. /// Update the mask texture.
  483. /// </summary>
  484. private void UpdateMaskTexture()
  485. {
  486. if (!graphic || !graphic.canvas) return;
  487. Profiler.BeginSample("UpdateMaskTexture");
  488. _stencilDepth = MaskUtilities.GetStencilDepth(transform, MaskUtilities.FindRootSortOverrideCanvas(transform));
  489. // Collect children soft masks.
  490. Profiler.BeginSample("Collect children soft masks");
  491. var depth = 0;
  492. s_TmpSoftMasks[0].Add(this);
  493. while (_stencilDepth + depth < 3)
  494. {
  495. var count = s_TmpSoftMasks[depth].Count;
  496. for (var i = 0; i < count; i++)
  497. {
  498. var children = s_TmpSoftMasks[depth][i]._children;
  499. var childCount = children.Count;
  500. for (var j = 0; j < childCount; j++)
  501. {
  502. var child = children[j];
  503. var childDepth = child.m_PartOfParent ? depth : depth + 1;
  504. s_TmpSoftMasks[childDepth].Add(child);
  505. }
  506. }
  507. depth++;
  508. }
  509. Profiler.EndSample();
  510. // CommandBuffer.
  511. Profiler.BeginSample("Initialize CommandBuffer");
  512. _cb.Clear();
  513. _cb.SetRenderTarget(softMaskBuffer);
  514. _cb.ClearRenderTarget(false, true, s_ClearColors[_stencilDepth]);
  515. Profiler.EndSample();
  516. // Set view and projection matrices.
  517. Profiler.BeginSample("Set view and projection matrices");
  518. var c = graphic.canvas.rootCanvas;
  519. var cam = c.worldCamera ?? Camera.main;
  520. if (c && c.renderMode != RenderMode.ScreenSpaceOverlay && cam)
  521. {
  522. var p = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false);
  523. _cb.SetViewProjectionMatrices(cam.worldToCameraMatrix, p);
  524. }
  525. else
  526. {
  527. var pos = c.transform.position;
  528. var vm = Matrix4x4.TRS(new Vector3(-pos.x, -pos.y, -1000), Quaternion.identity, new Vector3(1, 1, -1f));
  529. var pm = Matrix4x4.TRS(new Vector3(0, 0, -1), Quaternion.identity, new Vector3(1 / pos.x, 1 / pos.y, -2 / 10000f));
  530. _cb.SetViewProjectionMatrices(vm, pm);
  531. }
  532. Profiler.EndSample();
  533. // Draw soft masks.
  534. Profiler.BeginSample("Draw Mesh");
  535. for (var i = 0; i < s_TmpSoftMasks.Length; i++)
  536. {
  537. var count = s_TmpSoftMasks[i].Count;
  538. for (var j = 0; j < count; j++)
  539. {
  540. var sm = s_TmpSoftMasks[i][j];
  541. if (i != 0)
  542. {
  543. sm._stencilDepth = MaskUtilities.GetStencilDepth(sm.transform, MaskUtilities.FindRootSortOverrideCanvas(sm.transform));
  544. }
  545. // Set material property.
  546. sm.material.SetInt(s_ColorMaskId, (int) 1 << (3 - _stencilDepth - i));
  547. sm._mpb.SetTexture(s_MainTexId, sm.graphic.mainTexture);
  548. sm._mpb.SetFloat(s_SoftnessId, sm.m_Softness);
  549. sm._mpb.SetFloat(s_Alpha, sm.m_Alpha);
  550. // Draw mesh.
  551. _cb.DrawMesh(sm.mesh, sm.transform.localToWorldMatrix, sm.material, 0, 0, sm._mpb);
  552. }
  553. s_TmpSoftMasks[i].Clear();
  554. }
  555. Profiler.EndSample();
  556. Graphics.ExecuteCommandBuffer(_cb);
  557. Profiler.EndSample();
  558. }
  559. /// <summary>
  560. /// Gets the size of the down sampling.
  561. /// </summary>
  562. private static void GetDownSamplingSize(DownSamplingRate rate, out int w, out int h)
  563. {
  564. #if UNITY_EDITOR
  565. if (!Application.isPlaying)
  566. {
  567. var res = UnityEditor.UnityStats.screenRes.Split('x');
  568. w = Mathf.Max(64, int.Parse(res[0]));
  569. h = Mathf.Max(64, int.Parse(res[1]));
  570. }
  571. else
  572. #endif
  573. if (Screen.fullScreenMode == FullScreenMode.Windowed)
  574. {
  575. w = Screen.width;
  576. h = Screen.height;
  577. }
  578. else
  579. {
  580. w = Screen.currentResolution.width;
  581. h = Screen.currentResolution.height;
  582. }
  583. if (rate == DownSamplingRate.None)
  584. return;
  585. var aspect = (float) w / h;
  586. if (w < h)
  587. {
  588. h = Mathf.ClosestPowerOfTwo(h / (int) rate);
  589. w = Mathf.CeilToInt(h * aspect);
  590. }
  591. else
  592. {
  593. w = Mathf.ClosestPowerOfTwo(w / (int) rate);
  594. h = Mathf.CeilToInt(w / aspect);
  595. }
  596. }
  597. /// <summary>
  598. /// Release the specified obj.
  599. /// </summary>
  600. /// <param name="tmpRT">Object.</param>
  601. private static void ReleaseRt(ref RenderTexture tmpRT)
  602. {
  603. if (!tmpRT) return;
  604. tmpRT.Release();
  605. RenderTexture.ReleaseTemporary(tmpRT);
  606. tmpRT = null;
  607. }
  608. /// <summary>
  609. /// Release the specified obj.
  610. /// </summary>
  611. /// <param name="obj">Object.</param>
  612. private static void ReleaseObject(Object obj)
  613. {
  614. if (!obj) return;
  615. #if UNITY_EDITOR
  616. if (!Application.isPlaying)
  617. DestroyImmediate(obj);
  618. else
  619. #endif
  620. Destroy(obj);
  621. }
  622. /// <summary>
  623. /// Set the parent of the soft mask.
  624. /// </summary>
  625. /// <param name="newParent">The parent soft mask to use.</param>
  626. private void SetParent(SoftMask newParent)
  627. {
  628. if (_parent != newParent && this != newParent)
  629. {
  630. if (_parent && _parent._children.Contains(this))
  631. {
  632. _parent._children.Remove(this);
  633. _parent._children.RemoveAll(x => x == null);
  634. }
  635. _parent = newParent;
  636. }
  637. if (_parent && !_parent._children.Contains(this))
  638. {
  639. _parent._children.Add(this);
  640. }
  641. }
  642. /// <summary>
  643. /// Gets the pixel value.
  644. /// </summary>
  645. private float GetPixelValue(int x, int y, int[] interactions)
  646. {
  647. if (!s_ReadTexture)
  648. {
  649. s_ReadTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  650. }
  651. var currentRt = RenderTexture.active;
  652. RenderTexture.active = softMaskBuffer;
  653. s_ReadTexture.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
  654. s_ReadTexture.Apply(false, false);
  655. RenderTexture.active = currentRt;
  656. var colors = s_ReadTexture.GetRawTextureData();
  657. for (int i = 0; i < 4; i++)
  658. {
  659. switch (interactions[(i + 3) % 4])
  660. {
  661. case 0:
  662. colors[i] = 255;
  663. break;
  664. case 2:
  665. colors[i] = (byte) (255 - colors[i]);
  666. break;
  667. }
  668. }
  669. switch (_stencilDepth)
  670. {
  671. case 0: return (colors[1] / 255f);
  672. case 1: return (colors[1] / 255f) * (colors[2] / 255f);
  673. case 2: return (colors[1] / 255f) * (colors[2] / 255f) * (colors[3] / 255f);
  674. case 3: return (colors[1] / 255f) * (colors[2] / 255f) * (colors[3] / 255f) * (colors[0] / 255f);
  675. default: return 0;
  676. }
  677. }
  678. }
  679. }