UpdateContext.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FairyGUI.Utils;
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. /// UpdateContext is for internal use.
  9. /// </summary>
  10. public class UpdateContext
  11. {
  12. public struct ClipInfo
  13. {
  14. public Rect rect;
  15. public Vector4 clipBox;
  16. public bool soft;
  17. public Vector4 softness;//left-top-right-bottom
  18. public uint clipId;
  19. public int rectMaskDepth;
  20. public int referenceValue;
  21. public bool reversed;
  22. }
  23. Stack<ClipInfo> _clipStack;
  24. public bool clipped;
  25. public ClipInfo clipInfo;
  26. public int renderingOrder;
  27. public int batchingDepth;
  28. public int rectMaskDepth;
  29. public int stencilReferenceValue;
  30. public int stencilCompareValue;
  31. public float alpha;
  32. public bool grayed;
  33. public static UpdateContext current;
  34. public static bool working;
  35. public static event Action OnBegin;
  36. public static event Action OnEnd;
  37. static Action _tmpBegin;
  38. public UpdateContext()
  39. {
  40. _clipStack = new Stack<ClipInfo>();
  41. }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public void Begin()
  46. {
  47. current = this;
  48. renderingOrder = 0;
  49. batchingDepth = 0;
  50. rectMaskDepth = 0;
  51. stencilReferenceValue = 0;
  52. alpha = 1;
  53. grayed = false;
  54. clipped = false;
  55. _clipStack.Clear();
  56. Stats.ObjectCount = 0;
  57. Stats.GraphicsCount = 0;
  58. _tmpBegin = OnBegin;
  59. OnBegin = null;
  60. //允许OnBegin里再次Add,这里没有做死锁检查
  61. while (_tmpBegin != null)
  62. {
  63. _tmpBegin.Invoke();
  64. _tmpBegin = OnBegin;
  65. OnBegin = null;
  66. }
  67. working = true;
  68. }
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. public void End()
  73. {
  74. working = false;
  75. if (OnEnd != null)
  76. OnEnd.Invoke();
  77. OnEnd = null;
  78. }
  79. /// <summary>
  80. ///
  81. /// </summary>
  82. /// <param name="clipId"></param>
  83. /// <param name="clipRect"></param>
  84. /// <param name="softness"></param>
  85. public void EnterClipping(uint clipId, Rect clipRect, Vector4? softness)
  86. {
  87. _clipStack.Push(clipInfo);
  88. if (rectMaskDepth > 0)
  89. clipRect = ToolSet.Intersection(ref clipInfo.rect, ref clipRect);
  90. clipped = true;
  91. clipInfo.rectMaskDepth = ++rectMaskDepth;
  92. /* clipPos = xy * clipBox.zw + clipBox.xy
  93. * 利用这个公式,使clipPos变为当前顶点距离剪切区域中心的距离值,剪切区域的大小为2x2
  94. * 那么abs(clipPos)>1的都是在剪切区域外
  95. */
  96. clipInfo.rect = clipRect;
  97. clipRect.x = clipRect.x + clipRect.width * 0.5f;
  98. clipRect.y = clipRect.y + clipRect.height * 0.5f;
  99. clipRect.width *= 0.5f;
  100. clipRect.height *= 0.5f;
  101. if (clipRect.width == 0 || clipRect.height == 0)
  102. clipInfo.clipBox = new Vector4(-2, -2, 0, 0);
  103. else
  104. clipInfo.clipBox = new Vector4(-clipRect.x / clipRect.width, -clipRect.y / clipRect.height,
  105. 1.0f / clipRect.width, 1.0f / clipRect.height);
  106. clipInfo.clipId = clipId;
  107. clipInfo.soft = softness != null;
  108. if (clipInfo.soft)
  109. {
  110. clipInfo.softness = (Vector4)softness;
  111. float vx = clipInfo.rect.width * Screen.height * 0.25f;
  112. float vy = clipInfo.rect.height * Screen.height * 0.25f;
  113. if (clipInfo.softness.x > 0)
  114. clipInfo.softness.x = vx / clipInfo.softness.x;
  115. else
  116. clipInfo.softness.x = 10000f;
  117. if (clipInfo.softness.y > 0)
  118. clipInfo.softness.y = vy / clipInfo.softness.y;
  119. else
  120. clipInfo.softness.y = 10000f;
  121. if (clipInfo.softness.z > 0)
  122. clipInfo.softness.z = vx / clipInfo.softness.z;
  123. else
  124. clipInfo.softness.z = 10000f;
  125. if (clipInfo.softness.w > 0)
  126. clipInfo.softness.w = vy / clipInfo.softness.w;
  127. else
  128. clipInfo.softness.w = 10000f;
  129. }
  130. }
  131. /// <summary>
  132. ///
  133. /// </summary>
  134. /// <param name="clipId"></param>
  135. /// <param name="reversedMask"></param>
  136. public void EnterClipping(uint clipId, bool reversedMask)
  137. {
  138. _clipStack.Push(clipInfo);
  139. if (stencilReferenceValue == 0)
  140. stencilReferenceValue = 1;
  141. else
  142. stencilReferenceValue = stencilReferenceValue << 1;
  143. if (reversedMask)
  144. {
  145. if (clipInfo.reversed)
  146. stencilCompareValue = (stencilReferenceValue >> 1) - 1;
  147. else
  148. stencilCompareValue = stencilReferenceValue - 1;
  149. }
  150. else
  151. stencilCompareValue = (stencilReferenceValue << 1) - 1;
  152. clipInfo.clipId = clipId;
  153. clipInfo.referenceValue = stencilReferenceValue;
  154. clipInfo.reversed = reversedMask;
  155. clipped = true;
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. public void LeaveClipping()
  161. {
  162. clipInfo = _clipStack.Pop();
  163. stencilReferenceValue = clipInfo.referenceValue;
  164. rectMaskDepth = clipInfo.rectMaskDepth;
  165. clipped = stencilReferenceValue != 0 || rectMaskDepth != 0;
  166. }
  167. public void EnterPaintingMode()
  168. {
  169. //Reset clipping
  170. _clipStack.Push(clipInfo);
  171. clipInfo.rectMaskDepth = 0;
  172. clipInfo.referenceValue = 0;
  173. clipInfo.reversed = false;
  174. clipped = false;
  175. }
  176. public void LeavePaintingMode()
  177. {
  178. clipInfo = _clipStack.Pop();
  179. stencilReferenceValue = clipInfo.referenceValue;
  180. rectMaskDepth = clipInfo.rectMaskDepth;
  181. clipped = stencilReferenceValue != 0 || rectMaskDepth != 0;
  182. }
  183. public void ApplyClippingProperties(Material mat, bool isStdMaterial)
  184. {
  185. if (rectMaskDepth > 0) //在矩形剪裁下,且不是遮罩对象
  186. {
  187. mat.SetVector(ShaderConfig.ID_ClipBox, clipInfo.clipBox);
  188. if (clipInfo.soft)
  189. mat.SetVector(ShaderConfig.ID_ClipSoftness, clipInfo.softness);
  190. }
  191. if (stencilReferenceValue > 0)
  192. {
  193. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal);
  194. mat.SetInt(ShaderConfig.ID_Stencil, stencilCompareValue);
  195. mat.SetInt(ShaderConfig.ID_Stencil2, stencilCompareValue);
  196. mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep);
  197. mat.SetInt(ShaderConfig.ID_StencilReadMask, stencilReferenceValue | (stencilReferenceValue - 1));
  198. mat.SetInt(ShaderConfig.ID_ColorMask, 15);
  199. }
  200. else
  201. {
  202. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always);
  203. mat.SetInt(ShaderConfig.ID_Stencil, 0);
  204. mat.SetInt(ShaderConfig.ID_Stencil2, 0);
  205. mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep);
  206. mat.SetInt(ShaderConfig.ID_StencilReadMask, 255);
  207. mat.SetInt(ShaderConfig.ID_ColorMask, 15);
  208. }
  209. if (!isStdMaterial)
  210. {
  211. if (rectMaskDepth > 0)
  212. {
  213. if (clipInfo.soft)
  214. mat.EnableKeyword("SOFT_CLIPPED");
  215. else
  216. mat.EnableKeyword("CLIPPED");
  217. }
  218. else
  219. {
  220. mat.DisableKeyword("CLIPPED");
  221. mat.DisableKeyword("SOFT_CLIPPED");
  222. }
  223. }
  224. }
  225. public void ApplyAlphaMaskProperties(Material mat, bool erasing)
  226. {
  227. if (!erasing)
  228. {
  229. if (stencilReferenceValue == 1)
  230. {
  231. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always);
  232. mat.SetInt(ShaderConfig.ID_Stencil, 1);
  233. mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace);
  234. mat.SetInt(ShaderConfig.ID_StencilReadMask, 255);
  235. mat.SetInt(ShaderConfig.ID_ColorMask, 0);
  236. }
  237. else
  238. {
  239. if (stencilReferenceValue != 0 & _clipStack.Peek().reversed)
  240. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.NotEqual);
  241. else
  242. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal);
  243. mat.SetInt(ShaderConfig.ID_Stencil, stencilReferenceValue | (stencilReferenceValue - 1));
  244. mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace);
  245. mat.SetInt(ShaderConfig.ID_StencilReadMask, stencilReferenceValue - 1);
  246. mat.SetInt(ShaderConfig.ID_ColorMask, 0);
  247. }
  248. }
  249. else
  250. {
  251. if (stencilReferenceValue != 0 & _clipStack.Peek().reversed)
  252. {
  253. int refValue = stencilReferenceValue | (stencilReferenceValue - 1);
  254. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal);
  255. mat.SetInt(ShaderConfig.ID_Stencil, refValue);
  256. mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Zero);
  257. mat.SetInt(ShaderConfig.ID_StencilReadMask, refValue);
  258. mat.SetInt(ShaderConfig.ID_ColorMask, 0);
  259. }
  260. else
  261. {
  262. int refValue = stencilReferenceValue - 1;
  263. mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal);
  264. mat.SetInt(ShaderConfig.ID_Stencil, refValue);
  265. mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace);
  266. mat.SetInt(ShaderConfig.ID_StencilReadMask, refValue);
  267. mat.SetInt(ShaderConfig.ID_ColorMask, 0);
  268. }
  269. }
  270. }
  271. }
  272. }