Skip to content

Commit

Permalink
feat: add edge smoothing option
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Feb 17, 2022
1 parent 56dfb3c commit c5317de
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Scripts/Unmask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class Unmask : MonoBehaviour, IMaterialModifier
[Tooltip("Show the graphic that is associated with the unmask render area.")]
[SerializeField] private bool m_ShowUnmaskGraphic = false;

[Tooltip("Edge smoothing.")]
[Range(0f, 1f)]
[SerializeField] private float m_EdgeSmoothing = 0f;



//################################
// Public Members.
Expand Down Expand Up @@ -87,6 +92,15 @@ public bool onlyForChildren
}
}

/// <summary>
/// Edge smooting.
/// </summary>
public float edgeSmoothing
{
get { return m_EdgeSmoothing; }
set { m_EdgeSmoothing = value; }
}

/// <summary>
/// Perform material modification in this function.
/// </summary>
Expand Down Expand Up @@ -197,6 +211,8 @@ private void LateUpdate()
{
FitTo(m_FitTarget);
}

Smoothing(graphic, m_EdgeSmoothing);
}

#if UNITY_EDITOR
Expand All @@ -219,5 +235,31 @@ void SetDirty()
graphic.SetMaterialDirty();
}
}

private static void Smoothing(MaskableGraphic graphic, float smooth)
{
if (!graphic) return;

Profiler.BeginSample("[Unmask] Smoothing");
var canvasRenderer = graphic.canvasRenderer;
var currentColor = canvasRenderer.GetColor();
var targetAlpha = 1f;
if (graphic.maskable && 0 < smooth)
{
var currentAlpha = graphic.color.a * canvasRenderer.GetInheritedAlpha();
if (0 < currentAlpha)
{
targetAlpha = Mathf.Lerp(0.01f, 0.002f, smooth) / currentAlpha;
}
}

if (!Mathf.Approximately(currentColor.a, targetAlpha))
{
currentColor.a = Mathf.Clamp01(targetAlpha);
canvasRenderer.SetColor(currentColor);
}

Profiler.EndSample();
}
}
}

0 comments on commit c5317de

Please sign in to comment.