HoleImage.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using UnityEngine.UI;
  8. public class HoleImage : Image
  9. {
  10. public override Material GetModifiedMaterial(Material baseMaterial)
  11. {
  12. var toUse = baseMaterial;
  13. if (m_ShouldRecalculateStencil)
  14. {
  15. var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
  16. m_StencilValue = maskable ? MaskUtilities.GetStencilDepth(transform, rootCanvas) : 0;
  17. m_ShouldRecalculateStencil = false;
  18. }
  19. // if we have a enabled Mask component then it will
  20. // generate the mask material. This is an optimisation
  21. // it adds some coupling between components though :(
  22. Mask maskComponent = GetComponent<Mask>();
  23. if (m_StencilValue > 0 && (maskComponent == null || !maskComponent.IsActive()))
  24. {
  25. var maskMat = StencilMaterial.Add(toUse, (1 << m_StencilValue) - 1, StencilOp.Keep, CompareFunction.NotEqual, ColorWriteMask.All, (1 << m_StencilValue) - 1, 0);
  26. StencilMaterial.Remove(m_MaskMaterial);
  27. m_MaskMaterial = maskMat;
  28. toUse = m_MaskMaterial;
  29. }
  30. return toUse;
  31. }
  32. }