Skip to content

Commit

Permalink
feat: add Shadow Blur, Shadow Color and Shadow Glow options
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 13, 2024
1 parent 85b8275 commit 2c5dd5a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 25 deletions.
22 changes: 18 additions & 4 deletions Packages/src/Editor/UIEffectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public class UIEffect2Editor : Editor
private SerializedProperty _shadowDistanceY;
private SerializedProperty _shadowIteration;
private SerializedProperty _shadowFade;
private SerializedProperty _shadowEffectOnOrigin;
private SerializedProperty _shadowGlow;
private SerializedProperty _shadowMirrorScale;
private SerializedProperty _shadowBlurIntensity;
private SerializedProperty _shadowColor;

private SerializedProperty _gradationMode;
private SerializedProperty _gradationColor1;
Expand Down Expand Up @@ -107,8 +109,10 @@ private void OnEnable()
_shadowDistanceY = _shadowDistance.FindPropertyRelative("y");
_shadowIteration = serializedObject.FindProperty("m_ShadowIteration");
_shadowFade = serializedObject.FindProperty("m_ShadowFade");
_shadowEffectOnOrigin = serializedObject.FindProperty("m_ShadowEffectOnOrigin");
_shadowMirrorScale = serializedObject.FindProperty("m_ShadowMirrorScale");
_shadowBlurIntensity = serializedObject.FindProperty("m_ShadowBlurIntensity");
_shadowColor = serializedObject.FindProperty("m_ShadowColor");
_shadowGlow = serializedObject.FindProperty("m_ShadowGlow");

_gradationMode = serializedObject.FindProperty("m_GradationMode");
_gradationColor1 = serializedObject.FindProperty("m_GradationColor1");
Expand Down Expand Up @@ -226,6 +230,7 @@ public void DrawProperties()
EditorGUILayout.Slider(_shadowDistanceX, 0, 1, "Reflection");
EditorGUILayout.PropertyField(_shadowDistanceY, EditorGUIUtility.TrTempContent("Offset"));
EditorGUILayout.PropertyField(_shadowMirrorScale);
EditorGUILayout.PropertyField(_shadowFade);
}
else
{
Expand All @@ -237,10 +242,19 @@ public void DrawProperties()

EditorGUILayout.PropertyField(_shadowDistance);
EditorGUILayout.PropertyField(_shadowIteration);
EditorGUILayout.PropertyField(_shadowFade);
EditorGUILayout.PropertyField(_shadowColor);
EditorGUILayout.PropertyField(_shadowGlow);
switch ((SamplingFilter)_samplingFilter.intValue)
{
case SamplingFilter.BlurFast:
case SamplingFilter.BlurMedium:
case SamplingFilter.BlurDetail:
EditorGUILayout.PropertyField(_shadowBlurIntensity);
break;
}
}

EditorGUILayout.PropertyField(_shadowFade);
EditorGUILayout.PropertyField(_shadowEffectOnOrigin);
EditorGUI.indentLevel--;
}

Expand Down
69 changes: 53 additions & 16 deletions Packages/src/Runtime/UIEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,21 @@ public class UIEffect : UIEffectBase
[SerializeField]
protected float m_ShadowFade = 0.9f;

[SerializeField]
protected bool m_ShadowEffectOnOrigin = false;

[Range(0, 2)]
[SerializeField]
protected float m_ShadowMirrorScale = 0.5f;

[Range(0, 1)]
[SerializeField]
protected float m_ShadowBlurIntensity = 1;

[ColorUsage(false, true)]
[SerializeField]
protected Color m_ShadowColor = Color.white;

[SerializeField]
protected bool m_ShadowGlow = false;

[SerializeField]
protected GradationMode m_GradationMode = GradationMode.None;

Expand Down Expand Up @@ -510,26 +518,49 @@ public int shadowIteration
}
}

public bool shadowEffectOnOrigin
public float shadowMirrorScale
{
get => m_ShadowEffectOnOrigin;
get => m_ShadowMirrorScale;
set
{
if (m_ShadowEffectOnOrigin == value) return;
m_ShadowEffectOnOrigin = value;
value = Mathf.Clamp(value, 0f, 2f);
if (Mathf.Approximately(m_ShadowMirrorScale, value)) return;
context.shadowMirrorScale = m_ShadowMirrorScale = value;
SetVerticesDirty();
}
}

public float shadowMirrorScale
public float shadowBlurIntensity
{
get => m_ShadowMirrorScale;
get => m_ShadowBlurIntensity;
set
{
value = Mathf.Clamp(value, 0f, 2f);
if (Mathf.Approximately(m_ShadowMirrorScale, value)) return;
m_ShadowMirrorScale = value;
SetVerticesDirty();
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_ShadowBlurIntensity, value)) return;
context.shadowBlurIntensity = m_ShadowBlurIntensity = value;
ApplyContextToMaterial();
}
}

public Color shadowColor
{
get => m_ShadowColor;
set
{
if (m_ShadowColor == value) return;
context.shadowColor = m_ShadowColor = value;
ApplyContextToMaterial();
}
}

public bool shadowGlow
{
get => m_ShadowGlow;
set
{
if (m_ShadowGlow == value) return;
context.shadowGlow = m_ShadowGlow = value;
ApplyContextToMaterial();
}
}

Expand Down Expand Up @@ -658,8 +689,10 @@ protected override void UpdateContext(UIEffectContext c)
c.shadowDistance = m_ShadowDistance;
c.shadowIteration = m_ShadowIteration;
c.shadowFade = m_ShadowFade;
c.shadowEffectOnOrigin = m_ShadowEffectOnOrigin;
c.shadowMirrorScale = m_ShadowMirrorScale;
c.shadowBlurIntensity = m_ShadowBlurIntensity;
c.shadowColor = m_ShadowColor;
c.shadowGlow = m_ShadowGlow;
c.gradationMode = m_GradationMode;
c.gradationColor1 = m_GradationColor1;
c.gradationColor2 = m_GradationColor2;
Expand Down Expand Up @@ -758,8 +791,10 @@ public void LoadPreset(UIEffect preset)
m_ShadowDistance = preset.m_ShadowDistance;
m_ShadowIteration = preset.m_ShadowIteration;
m_ShadowFade = preset.m_ShadowFade;
m_ShadowEffectOnOrigin = preset.m_ShadowEffectOnOrigin;
m_ShadowMirrorScale = preset.m_ShadowMirrorScale;
m_ShadowBlurIntensity = preset.m_ShadowBlurIntensity;
m_ShadowColor = preset.m_ShadowColor;
m_ShadowGlow = preset.m_ShadowGlow;

UpdateContext(context);
ApplyContextToMaterial();
Expand Down Expand Up @@ -806,8 +841,10 @@ internal void CopyFrom(UIEffectContext c)
m_ShadowDistance = c.shadowDistance;
m_ShadowIteration = c.shadowIteration;
m_ShadowFade = c.shadowFade;
m_ShadowEffectOnOrigin = c.shadowEffectOnOrigin;
m_ShadowMirrorScale = c.shadowMirrorScale;
m_ShadowBlurIntensity = c.shadowBlurIntensity;
m_ShadowColor = c.shadowColor;
m_ShadowGlow = c.shadowGlow;

m_GradationMode = c.gradationMode;
m_GradationColor1 = c.gradationColor1;
Expand Down
30 changes: 27 additions & 3 deletions Packages/src/Runtime/UIEffectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class UIEffectContext
private static readonly int s_TargetColor = Shader.PropertyToID("_TargetColor");
private static readonly int s_TargetRange = Shader.PropertyToID("_TargetRange");
private static readonly int s_TargetSoftness = Shader.PropertyToID("_TargetSoftness");
private static readonly int s_ShadowColor = Shader.PropertyToID("_ShadowColor");
private static readonly int s_ShadowBlurIntensity = Shader.PropertyToID("_ShadowBlurIntensity");
private static readonly int s_ShadowGlow = Shader.PropertyToID("_ShadowGlow");

private static readonly string[] s_ToneKeywords =
{
Expand Down Expand Up @@ -130,8 +133,10 @@ public class UIEffectContext
public Vector2 shadowDistance = new Vector2(1f, -1f);
public int shadowIteration = 1;
public float shadowFade = 0.9f;
public bool shadowEffectOnOrigin = false;
public float shadowMirrorScale = 0.5f;
public float shadowBlurIntensity;
public Color shadowColor;
public bool shadowGlow;
public GradationMode gradationMode;
public Color gradationColor1;
public Color gradationColor2;
Expand All @@ -142,7 +147,8 @@ public class UIEffectContext
|| toneFilter != ToneFilter.None
|| colorFilter != ColorFilter.None
|| srcBlendMode != BlendMode.One
|| dstBlendMode != BlendMode.OneMinusSrcAlpha;
|| dstBlendMode != BlendMode.OneMinusSrcAlpha
|| shadowMode != ShadowMode.None;

public void Reset()
{
Expand Down Expand Up @@ -187,7 +193,10 @@ public void CopyFrom(UIEffectContext preset)
shadowDistance = preset.shadowDistance;
shadowIteration = preset.shadowIteration;
shadowFade = preset.shadowFade;
shadowEffectOnOrigin = preset.shadowEffectOnOrigin;
shadowMirrorScale = preset.shadowMirrorScale;
shadowBlurIntensity = preset.shadowBlurIntensity;
shadowColor = preset.shadowColor;
shadowGlow = preset.shadowGlow;

gradationMode = preset.gradationMode;
gradationColor1 = preset.gradationColor1;
Expand Down Expand Up @@ -228,6 +237,21 @@ public void ApplyToMaterial(Material material, float actualSamplingScale = 1f)
material.SetFloat(s_TargetRange, Mathf.Clamp01(targetRange));
material.SetFloat(s_TargetSoftness, Mathf.Clamp01(targetSoftness));

switch (samplingFilter)
{
case SamplingFilter.BlurFast:
case SamplingFilter.BlurMedium:
case SamplingFilter.BlurDetail:
material.SetFloat(s_ShadowBlurIntensity, Mathf.Clamp01(shadowBlurIntensity));
break;
default:
material.SetFloat(s_ShadowBlurIntensity, Mathf.Clamp01(samplingIntensity));
break;
}

material.SetColor(s_ShadowColor, shadowColor);
material.SetFloat(s_ShadowGlow, shadowGlow ? 1 : 0);

SetKeyword(material, s_ToneKeywords, (int)toneFilter);
SetKeyword(material, s_ColorKeywords, (int)colorFilter);
SetKeyword(material, s_SamplingKeywords, (int)samplingFilter);
Expand Down
13 changes: 11 additions & 2 deletions Packages/src/Shaders/UIEffect.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ uniform float _TransitionWidth;
uniform fixed4 _TargetColor;
uniform float _TargetRange;
uniform float _TargetSoftness;
uniform float _ShadowBlurIntensity;
uniform half4 _ShadowColor;
uniform int _ShadowGlow;

// For performance reasons, limit the sampling of blur in TextMeshPro.
#ifdef UIEFFECT_TEXTMESHPRO
Expand Down Expand Up @@ -239,7 +242,8 @@ half4 apply_sampling_filter(float2 uv, const float4 uvMask, const float2 uvLocal
{
#if SAMPLING_BLUR_FAST || SAMPLING_BLUR_MEDIUM || SAMPLING_BLUR_DETAIL
{
if (0 < _SamplingIntensity && -4 < uvLocal.x + uvLocal.y)
float intensity = -4 < uvLocal.x + uvLocal.y ? _SamplingIntensity : _ShadowBlurIntensity;
if (0 < intensity)
{
#if SAMPLING_BLUR_FAST
const int KERNEL_SIZE = 5;
Expand All @@ -255,7 +259,7 @@ half4 apply_sampling_filter(float2 uv, const float4 uvMask, const float2 uvLocal
float4 o = 0;
float sum = 0;
float2 shift = 0;
const half2 blur = texel_size() * _SamplingIntensity * 2;
const half2 blur = texel_size() * intensity * 2;
for (int x = 0; x < KERNEL_SIZE; x++)
{
shift.x = blur.x * (float(x) - KERNEL_SIZE / 2);
Expand Down Expand Up @@ -381,6 +385,11 @@ half4 uieffect_internal(float2 uv, const float4 uvMask, const float2 uvLocal)
{
color = apply_color_filter(color, _ColorValue, _ColorIntensity);
}
else
{
color.rgb = _ShadowColor.rgb * color.a;
color.a *= 1 - _ShadowGlow;
}

return color;
}
Expand Down

0 comments on commit 2c5dd5a

Please sign in to comment.