feat: add Shadow Color and Shadow Blur options

This commit is contained in:
mob-sakai
2024-12-26 17:35:12 +09:00
parent a66baea1e8
commit d0a3ca6090
14 changed files with 663 additions and 243 deletions
+18 -3
View File
@@ -52,8 +52,10 @@ namespace Coffee.UIEffects.Editors
private SerializedProperty _shadowDistanceY; private SerializedProperty _shadowDistanceY;
private SerializedProperty _shadowIteration; private SerializedProperty _shadowIteration;
private SerializedProperty _shadowFade; private SerializedProperty _shadowFade;
private SerializedProperty _shadowEffectOnOrigin;
private SerializedProperty _shadowMirrorScale; private SerializedProperty _shadowMirrorScale;
private SerializedProperty _shadowBlurIntensity;
private SerializedProperty _shadowColorFilter;
private SerializedProperty _shadowColor;
private bool _expandOthers; private bool _expandOthers;
private SerializedProperty _allowExtendVertex; private SerializedProperty _allowExtendVertex;
@@ -102,8 +104,10 @@ namespace Coffee.UIEffects.Editors
_shadowDistanceY = _shadowDistance.FindPropertyRelative("y"); _shadowDistanceY = _shadowDistance.FindPropertyRelative("y");
_shadowIteration = serializedObject.FindProperty("m_ShadowIteration"); _shadowIteration = serializedObject.FindProperty("m_ShadowIteration");
_shadowFade = serializedObject.FindProperty("m_ShadowFade"); _shadowFade = serializedObject.FindProperty("m_ShadowFade");
_shadowEffectOnOrigin = serializedObject.FindProperty("m_ShadowEffectOnOrigin");
_shadowMirrorScale = serializedObject.FindProperty("m_ShadowMirrorScale"); _shadowMirrorScale = serializedObject.FindProperty("m_ShadowMirrorScale");
_shadowBlurIntensity = serializedObject.FindProperty("m_ShadowBlurIntensity");
_shadowColorFilter = serializedObject.FindProperty("m_ShadowColorFilter");
_shadowColor = serializedObject.FindProperty("m_ShadowColor");
_allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex"); _allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex");
} }
@@ -231,8 +235,19 @@ namespace Coffee.UIEffects.Editors
EditorGUILayout.PropertyField(_shadowIteration); EditorGUILayout.PropertyField(_shadowIteration);
} }
EditorGUILayout.PropertyField(_shadowColorFilter);
EditorGUILayout.PropertyField(_shadowColor);
EditorGUILayout.PropertyField(_shadowFade); EditorGUILayout.PropertyField(_shadowFade);
EditorGUILayout.PropertyField(_shadowEffectOnOrigin);
switch ((SamplingFilter)_samplingFilter.intValue)
{
case SamplingFilter.BlurFast:
case SamplingFilter.BlurMedium:
case SamplingFilter.BlurDetail:
EditorGUILayout.PropertyField(_shadowBlurIntensity);
break;
}
EditorGUI.indentLevel--; EditorGUI.indentLevel--;
} }
+54 -17
View File
@@ -120,13 +120,21 @@ namespace Coffee.UIEffects
[SerializeField] [SerializeField]
protected float m_ShadowFade = 0.9f; protected float m_ShadowFade = 0.9f;
[SerializeField]
protected bool m_ShadowEffectOnOrigin = false;
[Range(0, 2)] [Range(0, 2)]
[SerializeField] [SerializeField]
protected float m_ShadowMirrorScale = 0.5f; protected float m_ShadowMirrorScale = 0.5f;
[Range(0, 1)]
[SerializeField]
protected float m_ShadowBlurIntensity = 1;
[SerializeField]
protected ColorFilter m_ShadowColorFilter = ColorFilter.Replace;
[ColorUsage(false, true)]
[SerializeField]
protected Color m_ShadowColor = Color.white;
[SerializeField] [SerializeField]
protected bool m_AllowExtendVertex = true; protected bool m_AllowExtendVertex = true;
@@ -501,17 +509,6 @@ namespace Coffee.UIEffects
} }
} }
public bool shadowEffectOnOrigin
{
get => m_ShadowEffectOnOrigin;
set
{
if (m_ShadowEffectOnOrigin == value) return;
m_ShadowEffectOnOrigin = value;
SetVerticesDirty();
}
}
public float shadowMirrorScale public float shadowMirrorScale
{ {
get => m_ShadowMirrorScale; get => m_ShadowMirrorScale;
@@ -524,6 +521,40 @@ namespace Coffee.UIEffects
} }
} }
public float shadowBlurIntensity
{
get => m_ShadowBlurIntensity;
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_ShadowBlurIntensity, value)) return;
context.shadowBlurIntensity = m_ShadowBlurIntensity = value;
ApplyContextToMaterial();
}
}
public ColorFilter shadowColorFilter
{
get => m_ShadowColorFilter;
set
{
if (m_ShadowColorFilter == value) return;
context.shadowColorFilter = m_ShadowColorFilter = value;
ApplyContextToMaterial();
}
}
public Color shadowColor
{
get => m_ShadowColor;
set
{
if (m_ShadowColor == value) return;
context.shadowColor = m_ShadowColor = value;
ApplyContextToMaterial();
}
}
public bool allowExtendVertex public bool allowExtendVertex
{ {
get => m_AllowExtendVertex; get => m_AllowExtendVertex;
@@ -616,8 +647,10 @@ namespace Coffee.UIEffects
c.shadowDistance = m_ShadowDistance; c.shadowDistance = m_ShadowDistance;
c.shadowIteration = m_ShadowIteration; c.shadowIteration = m_ShadowIteration;
c.shadowFade = m_ShadowFade; c.shadowFade = m_ShadowFade;
c.shadowEffectOnOrigin = m_ShadowEffectOnOrigin;
c.shadowMirrorScale = m_ShadowMirrorScale; c.shadowMirrorScale = m_ShadowMirrorScale;
c.shadowBlurIntensity = m_ShadowBlurIntensity;
c.shadowColorFilter = m_ShadowColorFilter;
c.shadowColor = m_ShadowColor;
c.allowExtendVertex = m_AllowExtendVertex; c.allowExtendVertex = m_AllowExtendVertex;
} }
@@ -713,8 +746,10 @@ namespace Coffee.UIEffects
m_ShadowDistance = preset.m_ShadowDistance; m_ShadowDistance = preset.m_ShadowDistance;
m_ShadowIteration = preset.m_ShadowIteration; m_ShadowIteration = preset.m_ShadowIteration;
m_ShadowFade = preset.m_ShadowFade; m_ShadowFade = preset.m_ShadowFade;
m_ShadowEffectOnOrigin = preset.m_ShadowEffectOnOrigin;
m_ShadowMirrorScale = preset.m_ShadowMirrorScale; m_ShadowMirrorScale = preset.m_ShadowMirrorScale;
m_ShadowBlurIntensity = preset.m_ShadowBlurIntensity;
m_ShadowColorFilter = preset.m_ShadowColorFilter;
m_ShadowColor = preset.m_ShadowColor;
UpdateContext(context); UpdateContext(context);
ApplyContextToMaterial(); ApplyContextToMaterial();
@@ -761,8 +796,10 @@ namespace Coffee.UIEffects
m_ShadowDistance = c.shadowDistance; m_ShadowDistance = c.shadowDistance;
m_ShadowIteration = c.shadowIteration; m_ShadowIteration = c.shadowIteration;
m_ShadowFade = c.shadowFade; m_ShadowFade = c.shadowFade;
m_ShadowEffectOnOrigin = c.shadowEffectOnOrigin;
m_ShadowMirrorScale = c.shadowMirrorScale; m_ShadowMirrorScale = c.shadowMirrorScale;
m_ShadowBlurIntensity = c.shadowBlurIntensity;
m_ShadowColorFilter = c.shadowColorFilter;
m_ShadowColor = c.shadowColor;
m_AllowExtendVertex = c.allowExtendVertex; m_AllowExtendVertex = c.allowExtendVertex;
+90 -174
View File
@@ -1,11 +1,10 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using Coffee.UIEffectInternal;
using UnityEngine; using UnityEngine;
using UnityEngine.Profiling; using UnityEngine.Profiling;
using UnityEngine.Rendering; using UnityEngine.Rendering;
using UnityEngine.UI; using UnityEngine.UI;
#if TMP_ENABLE
using TMPro;
#endif
namespace Coffee.UIEffects namespace Coffee.UIEffects
{ {
@@ -32,6 +31,8 @@ namespace Coffee.UIEffects
private static readonly int s_TargetColor = Shader.PropertyToID("_TargetColor"); private static readonly int s_TargetColor = Shader.PropertyToID("_TargetColor");
private static readonly int s_TargetRange = Shader.PropertyToID("_TargetRange"); private static readonly int s_TargetRange = Shader.PropertyToID("_TargetRange");
private static readonly int s_TargetSoftness = Shader.PropertyToID("_TargetSoftness"); 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 string[] s_ToneKeywords = private static readonly string[] s_ToneKeywords =
{ {
@@ -100,6 +101,33 @@ namespace Coffee.UIEffects
"TARGET_LUMINANCE" "TARGET_LUMINANCE"
}; };
private static readonly string[] s_ShadowColorKeywords =
{
"",
"SHADOW_COLOR_MULTIPLY",
"SHADOW_COLOR_ADDITIVE",
"SHADOW_COLOR_SUBTRACTIVE",
"SHADOW_COLOR_REPLACE",
"SHADOW_COLOR_MULTIPLY_LUMINANCE",
"SHADOW_COLOR_MULTIPLY_ADDITIVE",
"SHADOW_COLOR_HSV_MODIFIER",
"SHADOW_COLOR_CONTRAST"
};
private static readonly Vector2[][] s_ShadowVectors = new[]
{
Array.Empty<Vector2>(), // None
new[] { Vector2.one }, // Shadow
new[] { Vector2.one, Vector2.right, Vector2.up }, // Shadow3
new[] { Vector2.one, -Vector2.one, new Vector2(1, -1), new Vector2(-1, 1) }, // Outline
new[]
{
Vector2.one, -Vector2.one, new Vector2(1, -1), new Vector2(-1, 1),
Vector2.right, Vector2.up, Vector2.left, Vector2.down
} // Outline8
};
public static Func<UIVertex, Rect, float, float, UIVertex> onModifyVertex;
public ToneFilter toneFilter = ToneFilter.None; public ToneFilter toneFilter = ToneFilter.None;
public float toneIntensity = 1; public float toneIntensity = 1;
public Vector4 toneParams = new Vector4(0, 0, 0, 0); public Vector4 toneParams = new Vector4(0, 0, 0, 0);
@@ -136,8 +164,10 @@ namespace Coffee.UIEffects
public Vector2 shadowDistance = new Vector2(1f, -1f); public Vector2 shadowDistance = new Vector2(1f, -1f);
public int shadowIteration = 1; public int shadowIteration = 1;
public float shadowFade = 0.9f; public float shadowFade = 0.9f;
public bool shadowEffectOnOrigin = false;
public float shadowMirrorScale = 0.5f; public float shadowMirrorScale = 0.5f;
public float shadowBlurIntensity;
public ColorFilter shadowColorFilter;
public Color shadowColor;
public bool allowExtendVertex; public bool allowExtendVertex;
@@ -147,7 +177,8 @@ namespace Coffee.UIEffects
|| toneFilter != ToneFilter.None || toneFilter != ToneFilter.None
|| colorFilter != ColorFilter.None || colorFilter != ColorFilter.None
|| srcBlendMode != BlendMode.One || srcBlendMode != BlendMode.One
|| dstBlendMode != BlendMode.OneMinusSrcAlpha; || dstBlendMode != BlendMode.OneMinusSrcAlpha
|| shadowMode != ShadowMode.None;
public void Reset() public void Reset()
{ {
@@ -192,7 +223,10 @@ namespace Coffee.UIEffects
shadowDistance = preset.shadowDistance; shadowDistance = preset.shadowDistance;
shadowIteration = preset.shadowIteration; shadowIteration = preset.shadowIteration;
shadowFade = preset.shadowFade; shadowFade = preset.shadowFade;
shadowEffectOnOrigin = preset.shadowEffectOnOrigin; shadowMirrorScale = preset.shadowMirrorScale;
shadowBlurIntensity = preset.shadowBlurIntensity;
shadowColorFilter = preset.shadowColorFilter;
shadowColor = preset.shadowColor;
allowExtendVertex = preset.allowExtendVertex; allowExtendVertex = preset.allowExtendVertex;
} }
@@ -223,13 +257,27 @@ namespace Coffee.UIEffects
transitionTexOffset.x, transitionTexOffset.y)); transitionTexOffset.x, transitionTexOffset.y));
material.SetFloat(s_TransitionWidth, Mathf.Clamp01(transitionWidth)); material.SetFloat(s_TransitionWidth, Mathf.Clamp01(transitionWidth));
material.SetFloat(s_TransitionSoftness, Mathf.Clamp01(transitionSoftness)); material.SetFloat(s_TransitionSoftness, Mathf.Clamp01(transitionSoftness));
material.SetColor(s_TransitionColor, transitionColor);
material.SetInt(s_TransitionColorFilter, (int)transitionColorFilter); material.SetInt(s_TransitionColorFilter, (int)transitionColorFilter);
material.SetColor(s_TransitionColor, transitionColor);
material.SetColor(s_TargetColor, targetColor); material.SetColor(s_TargetColor, targetColor);
material.SetFloat(s_TargetRange, Mathf.Clamp01(targetRange)); material.SetFloat(s_TargetRange, Mathf.Clamp01(targetRange));
material.SetFloat(s_TargetSoftness, Mathf.Clamp01(targetSoftness)); 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);
SetKeyword(material, s_ToneKeywords, (int)toneFilter); SetKeyword(material, s_ToneKeywords, (int)toneFilter);
SetKeyword(material, s_ColorKeywords, (int)colorFilter); SetKeyword(material, s_ColorKeywords, (int)colorFilter);
SetKeyword(material, s_SamplingKeywords, (int)samplingFilter); SetKeyword(material, s_SamplingKeywords, (int)samplingFilter);
@@ -250,6 +298,16 @@ namespace Coffee.UIEffects
break; break;
} }
switch (shadowMode)
{
case ShadowMode.None:
SetKeyword(material, s_ShadowColorKeywords, (int)ColorFilter.None);
break;
default:
SetKeyword(material, s_ShadowColorKeywords, (int)shadowColorFilter);
break;
}
SetKeyword(material, s_TargetKeywords, (int)targetMode); SetKeyword(material, s_TargetKeywords, (int)targetMode);
Profiler.EndSample(); Profiler.EndSample();
@@ -272,31 +330,13 @@ namespace Coffee.UIEffects
public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelper vh) public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelper vh)
{ {
#if TMP_ENABLE var processor = ContextProcessor.FindProcessor(graphic);
var isTextMeshPro = graphic is TextMeshProUGUI || graphic is TMP_SubMeshUI; var isText = processor.IsText(graphic);
var isText = isTextMeshPro || graphic is Text; processor.OnPreModifyMesh(graphic);
// Sprite mode.
if (graphic is TMP_SubMeshUI sub && sub.spriteAsset && sub.sharedMaterial == sub.spriteAsset.material)
{
isTextMeshPro = false;
}
#else
var isTextMeshPro = false;
var isText = graphic is Text;
#endif
var verts = s_WorkingVertices; var verts = s_WorkingVertices;
var expandSize = GetExpandSize(); var expandSize = GetExpandSize();
var useExpand = expandSize != Vector2.zero;
// Update capacity of workingVertices if needed.
var count = vh.currentIndexCount; var count = vh.currentIndexCount;
var neededCapacity = Mathf.NextPowerOfTwo(count * GetVertexCountMultiply());
if (verts.Capacity < neededCapacity)
{
verts.Capacity = neededCapacity;
}
// Get the rectangle to calculate the normalized position. // Get the rectangle to calculate the normalized position.
vh.GetUIVertexStream(verts); vh.GetUIVertexStream(verts);
@@ -339,102 +379,38 @@ namespace Coffee.UIEffects
for (var i = 0; i < count; i += bundleSize) for (var i = 0; i < count; i += bundleSize)
{ {
// min/max for bundled-quad // min/max for bundled-quad
GetBounds(verts, i, bundleSize, out var bounds, out var uvMask); UIVertexUtil.GetBounds(verts, i, bundleSize, out var bounds, out var uvMask);
UIVertexUtil.Expand(verts, i, bundleSize, expandSize, bounds);
// Quad (6 vertices) for (var j = 0; j < bundleSize; j++)
for (var j = 0; j < bundleSize; j += 6)
{ {
var size = default(Vector3); var vt = verts[i + j];
var extendPos = default(Vector3); ModifyVertex(ref vt, vt.position, vt.uv0, uvMask, rect, rot);
var extendUV = default(Vector3); verts[i + j] = vt;
var posLB = verts[i + j + 1].position;
var posRT = verts[i + j + 4].position;
var willExpand = useExpand
&& (bundleSize == 6 // Text or simple quad
|| !bounds.Contains(posLB) ||
!bounds.Contains(posRT)); // Outer 9-sliced quad
if (willExpand)
{
var uvLB = verts[i + j + 1].uv0;
var uvRT = verts[i + j + 4].uv0;
var posCenter = (posLB + posRT) / 2;
var uvCenter = (uvLB + uvRT) / 2;
size = posLB - posRT;
size.x = 1 + expandSize.x / Mathf.Abs(size.x);
size.y = 1 + expandSize.y / Mathf.Abs(size.y);
size.z = 1;
extendPos = posCenter - Vector3.Scale(size, posCenter);
extendUV = uvCenter - Vector4.Scale(size, uvCenter);
}
// Set vertex position, uv, uvMask and local normalized position.
for (var k = 0; k < 6; k++)
{
var vt = verts[i + j + k];
var pos = vt.position;
var uv0 = vt.uv0;
// Expand edge vertex
if (willExpand)
{
if (pos.x < bounds.xMin || bounds.xMax < pos.x)
{
pos.x = pos.x * size.x + extendPos.x;
uv0.x = uv0.x * size.x + extendUV.x;
}
if (pos.y < bounds.yMin || bounds.yMax < pos.y)
{
pos.y = pos.y * size.y + extendPos.y;
uv0.y = uv0.y * size.y + extendUV.y;
}
}
ModifyVertex(isTextMeshPro, ref vt, pos, uv0, uvMask, rect, rot);
verts[i + j + k] = vt;
}
} }
} }
// Apply shadow. // Apply shadow.
ApplyShadow(transitionRoot, verts);
vh.Clear();
vh.AddUIVertexTriangleStream(verts);
}
private void ApplyShadow(RectTransform transitionRoot, List<UIVertex> verts)
{
switch (shadowMode) switch (shadowMode)
{ {
case ShadowMode.Shadow: case ShadowMode.Shadow:
case ShadowMode.Shadow3: case ShadowMode.Shadow3:
case ShadowMode.Outline: case ShadowMode.Outline:
case ShadowMode.Outline8: case ShadowMode.Outline8:
count = verts.Count; ShadowUtil.DoShadow(verts, s_ShadowVectors[(int)shadowMode], shadowDistance, shadowIteration,
ShadowUtil.DoShadow(shadowMode, verts, shadowDistance, shadowIteration, shadowFade); shadowFade);
// Mark as shadow vertices.
for (var i = 0; i < verts.Count - count; i++)
{
var vt = verts[i];
MarkAsShadowVertex(isTextMeshPro, ref vt);
verts[i] = vt;
}
break; break;
case ShadowMode.Mirror: case ShadowMode.Mirror:
ShadowUtil.DoMirror(verts, shadowDistance, shadowMirrorScale, shadowFade, transitionRoot); ShadowUtil.DoMirror(verts, shadowDistance, shadowMirrorScale, shadowFade, transitionRoot);
break; break;
} }
vh.Clear();
vh.AddUIVertexTriangleStream(verts);
}
private int GetVertexCountMultiply()
{
switch (shadowMode)
{
case ShadowMode.Shadow: return 1 + shadowIteration * 1;
case ShadowMode.Shadow3: return 1 + shadowIteration * 3;
case ShadowMode.Outline: return 1 + shadowIteration * 4;
case ShadowMode.Outline8: return 1 + shadowIteration * 8;
case ShadowMode.Mirror: return 1 + 1;
default: return 1;
}
} }
private Vector2 GetExpandSize() private Vector2 GetExpandSize()
@@ -473,83 +449,23 @@ namespace Coffee.UIEffects
return expandSize; return expandSize;
} }
private static void ModifyVertex(bool isTextMeshPro, ref UIVertex vt, Vector2 pos, Vector2 uv, Rect uvMask, private static void ModifyVertex(ref UIVertex vt, Vector2 pos, Vector2 uv, Rect uvMask,
Rect rect, Matrix4x4 m) Rect rect, Matrix4x4 m)
{ {
vt.position = pos;
pos = m.MultiplyPoint3x4(pos); pos = m.MultiplyPoint3x4(pos);
var normalizedX = Mathf.InverseLerp(rect.xMin, rect.xMax, pos.x); var normalizedX = Mathf.InverseLerp(rect.xMin, rect.xMax, pos.x);
var normalizedY = Mathf.InverseLerp(rect.yMin, rect.yMax, pos.y); var normalizedY = Mathf.InverseLerp(rect.yMin, rect.yMax, pos.y);
if (isTextMeshPro) if (onModifyVertex != null)
{ {
vt.uv0.x = uv.x; vt = onModifyVertex(vt, uvMask, normalizedX, normalizedY);
vt.uv0.y = uv.y;
vt.uv1.z = normalizedX;
vt.uv1.w = normalizedY;
vt.uv2 = new Vector4(uvMask.xMin, uvMask.yMin, uvMask.xMax, uvMask.yMax);
} }
else else
{ {
vt.uv0 = new Vector4(uv.x, uv.y, normalizedX, normalizedY); vt.uv0.z = normalizedX;
vt.uv0.w = normalizedY;
vt.uv1 = new Vector4(uvMask.xMin, uvMask.yMin, uvMask.xMax, uvMask.yMax); vt.uv1 = new Vector4(uvMask.xMin, uvMask.yMin, uvMask.xMax, uvMask.yMax);
} }
} }
private static void MarkAsShadowVertex(bool isTextMeshPro, ref UIVertex vt)
{
if (isTextMeshPro)
{
vt.uv1.z -= 8;
vt.uv1.w -= 8;
}
else
{
vt.uv0.z -= 8;
vt.uv0.w -= 8;
}
}
private static void GetBounds(List<UIVertex> verts, int start, int count, out Rect posBounds,
out Rect uvBounds)
{
var minPos = new Vector2(float.MaxValue, float.MaxValue);
var maxPos = new Vector2(float.MinValue, float.MinValue);
var minUV = new Vector2(float.MaxValue, float.MaxValue);
var maxUV = new Vector2(float.MinValue, float.MinValue);
for (var i = start; i < start + count; i++)
{
var vt = verts[i];
var uv = vt.uv0;
var pos = vt.position;
// Left-Bottom
if (minPos.x >= pos.x && minPos.y >= pos.y)
{
minPos = pos;
}
// Right-Top
else if (maxPos.x <= pos.x && maxPos.y <= pos.y)
{
maxPos = pos;
}
// Left-Bottom
if (minUV.x >= uv.x && minUV.y >= uv.y)
{
minUV = uv;
}
// Right-Top
else if (maxUV.x <= uv.x && maxUV.y <= uv.y)
{
maxUV = uv;
}
}
// Shrink coordinate to avoid uv edge
posBounds = new Rect(minPos.x + 0.001f, minPos.y + 0.001f,
maxPos.x - minPos.x - 0.002f, maxPos.y - minPos.y - 0.002f);
uvBounds = new Rect(minUV.x, minUV.y, maxUV.x - minUV.x, maxUV.y - minUV.y);
}
} }
} }
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if TMP_ENABLE
using TMPro;
#endif
namespace Coffee.UIEffects
{
public class ContextProcessor
{
private static readonly List<ContextProcessor> s_Processors = new List<ContextProcessor>()
{
new ContextProcessor(),
#if TMP_ENABLE
new TmpContextProcessor(),
#endif
};
public static void RegisterProcessor(ContextProcessor processor)
{
// Register only once.
foreach (var p in s_Processors)
{
if (p.GetType() == processor.GetType()) return;
}
s_Processors.Add(processor);
}
public static ContextProcessor FindProcessor(Graphic graphic)
{
for (var i = s_Processors.Count - 1; i >= 0; i--)
{
var p = s_Processors[i];
if (p.IsValid(graphic)) return p;
}
return null;
}
/// <summary>
/// Check if the graphic is valid for this processor.
/// </summary>
protected virtual bool IsValid(Graphic graphic)
{
return graphic;
}
/// <summary>
/// Check if the graphic is a text.
/// </summary>
public virtual bool IsText(Graphic graphic)
{
return graphic is Text;
}
/// <summary>
/// Called before modifying the mesh.
/// </summary>
public virtual void OnPreModifyMesh(Graphic graphic)
{
UIVertexUtil.onLerpVertex = s_OnLerpVertex;
ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow;
UIEffectContext.onModifyVertex = s_OnModifyVertex;
}
private static readonly Func<UIVertex, UIVertex, UIVertex, float, UIVertex> s_OnLerpVertex =
null;
private static readonly Func<UIVertex, UIVertex> s_OnMarkAsShadow =
vt =>
{
vt.uv0.z -= 8;
vt.uv0.w -= 8;
return vt;
};
private static readonly Func<UIVertex, Rect, float, float, UIVertex> s_OnModifyVertex =
(vt, uvMask, normalizedX, normalizedY) =>
{
vt.uv0.z = normalizedX;
vt.uv0.w = normalizedY;
vt.uv1 = new Vector4(uvMask.xMin, uvMask.yMin, uvMask.xMax, uvMask.yMax);
return vt;
};
}
#if TMP_ENABLE
internal class TmpContextProcessor : ContextProcessor
{
protected override bool IsValid(Graphic graphic)
{
if (!graphic) return false;
if (graphic is TextMeshProUGUI) return true;
if (graphic is TMP_SubMeshUI sub)
{
// If TMP_SubMeshUI is used for text
return !sub.spriteAsset
|| sub.sharedMaterial != sub.spriteAsset.material;
}
return false;
}
/// <summary>
/// Check if the graphic is a text.
/// </summary>
public override bool IsText(Graphic graphic)
{
return true;
}
/// <summary>
/// Called before modifying the mesh.
/// </summary>
public override void OnPreModifyMesh(Graphic graphic)
{
UIVertexUtil.onLerpVertex = s_OnLerpVertex;
ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow;
UIEffectContext.onModifyVertex = s_OnModifyVertex;
}
private static readonly Func<UIVertex, UIVertex, UIVertex, float, UIVertex> s_OnLerpVertex =
#if UNITY_2023_2_OR_NEWER
null;
#else
(vt, a, b, t) =>
{
// TMP 3.x uses packed UV
vt.uv1.x = PackUV(Vector2.Lerp(UnpackUV(a.uv1.x), UnpackUV(b.uv1.x), t));
return vt;
static float PackUV(Vector2 input)
{
return (int)(input.x * 511) * 4096 + (int)(input.y * 511);
}
static Vector2 UnpackUV(float input)
{
return new Vector2(input / 4096 / 511, input % 4096 / 511);
}
};
#endif
private static readonly Func<UIVertex, UIVertex> s_OnMarkAsShadow =
vt =>
{
vt.uv1.z -= 8;
vt.uv1.w -= 8;
return vt;
};
private static readonly Func<UIVertex, Rect, float, float, UIVertex> s_OnModifyVertex =
(vt, uvMask, normalizedX, normalizedY) =>
{
vt.uv1.z = normalizedX;
vt.uv1.w = normalizedY;
vt.uv2 = new Vector4(uvMask.xMin, uvMask.yMin, uvMask.xMax, uvMask.yMax);
return vt;
};
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 402a2dd4d098a4d708b9f6e7ce0557cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+34 -44
View File
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@@ -5,24 +6,45 @@ namespace Coffee.UIEffects
{ {
public static class ShadowUtil public static class ShadowUtil
{ {
public static void DoShadow(ShadowMode mode, List<UIVertex> verts, Vector2 distance, int iteration, float fade) public static Func<UIVertex, UIVertex> onMarkAsShadow;
public static void DoShadow(List<UIVertex> verts, Vector2[] vectors, Vector2 distance, int iteration,
float fade)
{ {
UIVertexUtil.ExpandCapacity(verts, 1 + vectors.Length);
distance = new Vector2(Mathf.Clamp(distance.x, -600, 600), Mathf.Clamp(distance.y, -600, 600)); distance = new Vector2(Mathf.Clamp(distance.x, -600, 600), Mathf.Clamp(distance.y, -600, 600));
var count = verts.Count; var count = verts.Count;
var start = 0; var start = 0;
var end = count; var end = count;
var d = Vector2.zero; var d = Vector2.zero;
var a = 1f; var a = fade;
for (var i = 0; i < iteration; i++) for (var i = 0; i < iteration; i++)
{ {
d += distance / (i + 1); d += distance / (i + 1);
a *= fade; a *= 0.75f;
ApplyShadow(verts, ref start, ref end, d, mode, a); ApplyShadow(verts, vectors, ref start, ref end, d, a);
}
// Mark as shadow vertices.
for (var i = 0; i < verts.Count - count; i++)
{
if (onMarkAsShadow != null)
{
verts[i] = onMarkAsShadow(verts[i]);
}
else
{
var vt = verts[i];
vt.uv0.z -= 8;
vt.uv0.w -= 8;
verts[i] = vt;
}
} }
} }
public static void DoMirror(List<UIVertex> verts, Vector2 distance, float scale, float fade, RectTransform root) public static void DoMirror(List<UIVertex> verts, Vector2 distance, float scale, float fade, RectTransform root)
{ {
UIVertexUtil.ExpandCapacity(verts, 2);
distance = new Vector2(Mathf.Clamp(distance.x, -600, 600), Mathf.Clamp(distance.y, -600, 600)); distance = new Vector2(Mathf.Clamp(distance.x, -600, 600), Mathf.Clamp(distance.y, -600, 600));
var count = verts.Count; var count = verts.Count;
var rect2 = root.rect; var rect2 = root.rect;
@@ -61,24 +83,15 @@ namespace Coffee.UIEffects
if (lbRate < rate && rate < ltRate) if (lbRate < rate && rate < ltRate)
{ {
var t = (rate - lbRate) / (ltRate - lbRate); var t = (rate - lbRate) / (ltRate - lbRate);
lt.position = Vector3.Lerp(lb.position, lt.position, t); lt = UIVertexUtil.VertexLerp(lb, lt, t);
lt.uv0 = Vector4.Lerp(lb.uv0, lt.uv0, t);
lt.uv1 = Vector4.Lerp(lb.uv1, lt.uv1, t);
lt.uv2 = Vector4.Lerp(lb.uv2, lt.uv2, t);
lt.color = Color.Lerp(lb.color, lt.color, t);
} }
if (rbRate < rate && rate < rtRate) if (rbRate < rate && rate < rtRate)
{ {
var t = (rate - rbRate) / (rtRate - rbRate); var t = (rate - rbRate) / (rtRate - rbRate);
rt.position = Vector3.Lerp(rb.position, rt.position, t); rt = UIVertexUtil.VertexLerp(rb, rt, t);
rt.uv0 = Vector4.Lerp(rb.uv0, rt.uv0, t);
rt.uv1 = Vector4.Lerp(rb.uv1, rt.uv1, t);
rt.uv2 = Vector4.Lerp(rb.uv2, rt.uv2, t);
rt.color = Color.Lerp(rb.color, rt.color, t);
} }
lb.position.y = -lb.position.y * scale + offset; lb.position.y = -lb.position.y * scale + offset;
lt.position.y = -lt.position.y * scale + offset; lt.position.y = -lt.position.y * scale + offset;
rt.position.y = -rt.position.y * scale + offset; rt.position.y = -rt.position.y * scale + offset;
@@ -95,39 +108,16 @@ namespace Coffee.UIEffects
/// Append shadow vertices. /// Append shadow vertices.
/// * It is similar to Shadow component implementation. /// * It is similar to Shadow component implementation.
/// </summary> /// </summary>
private static void ApplyShadow(List<UIVertex> verts, ref int start, ref int end, Vector2 distance, private static void ApplyShadow(List<UIVertex> verts, Vector2[] vectors, ref int start, ref int end,
ShadowMode mode, float alpha) Vector2 distance, float alpha)
{ {
if (mode == ShadowMode.None) return;
var x = distance.x; var x = distance.x;
var y = distance.y; var y = distance.y;
switch (mode) for (var i = 0; i < vectors.Length; i++)
{ {
case ShadowMode.Shadow: var dx = x * vectors[i].x;
ApplyShadowZeroAlloc(verts, ref start, ref end, x, y, alpha); var dy = y * vectors[i].y;
break; ApplyShadowZeroAlloc(verts, ref start, ref end, dx, dy, alpha);
case ShadowMode.Shadow3:
ApplyShadowZeroAlloc(verts, ref start, ref end, x, y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, x, 0, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, 0, y, alpha);
break;
case ShadowMode.Outline:
ApplyShadowZeroAlloc(verts, ref start, ref end, x, y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, x, -y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, -x, y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, -x, -y, alpha);
break;
case ShadowMode.Outline8:
ApplyShadowZeroAlloc(verts, ref start, ref end, x, y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, x, -y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, -x, y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, -x, -y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, x, 0, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, 0, y, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, -x, 0, alpha);
ApplyShadowZeroAlloc(verts, ref start, ref end, 0, -y, alpha);
break;
} }
} }
@@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Coffee.UIEffects
{
internal static class UIVertexUtil
{
public static Func<UIVertex, UIVertex, UIVertex, float, UIVertex> onLerpVertex;
public static void ExpandCapacity(List<UIVertex> verts, int multiplier)
{
var capacity = Mathf.NextPowerOfTwo(verts.Count * multiplier);
if (verts.Capacity < capacity)
{
verts.Capacity = capacity;
}
}
public static void Expand(List<UIVertex> verts, int start, int bundleSize, Vector2 expandSize, Rect bounds)
{
if (expandSize == Vector2.zero) return;
// Quad (6 vertices)
for (var j = 0; j < bundleSize; j += 6)
{
var size = default(Vector3);
var extendPos = default(Vector3);
var extendUV = default(Vector3);
var posLB = verts[start + j + 1].position;
var posRT = verts[start + j + 4].position;
var willExpand = bundleSize == 6 // Text or simple quad
|| !bounds.Contains(posLB)
|| !bounds.Contains(posRT); // Outer 9-sliced quad
if (willExpand)
{
var uvLB = verts[start + j + 1].uv0;
var uvRT = verts[start + j + 4].uv0;
var posCenter = (posLB + posRT) / 2;
var uvCenter = (uvLB + uvRT) / 2;
size = posLB - posRT;
size.x = 1 + expandSize.x / Mathf.Abs(size.x);
size.y = 1 + expandSize.y / Mathf.Abs(size.y);
size.z = 1;
extendPos = posCenter - Vector3.Scale(size, posCenter);
extendUV = uvCenter - Vector4.Scale(size, uvCenter);
}
// Set vertex position, uv, uvMask and local normalized position.
for (var k = 0; k < 6; k++)
{
var vt = verts[start + j + k];
var pos = vt.position;
var uv0 = vt.uv0;
// Expand edge vertex
if (willExpand)
{
if (pos.x < bounds.xMin || bounds.xMax < pos.x)
{
pos.x = pos.x * size.x + extendPos.x;
uv0.x = uv0.x * size.x + extendUV.x;
}
if (pos.y < bounds.yMin || bounds.yMax < pos.y)
{
pos.y = pos.y * size.y + extendPos.y;
uv0.y = uv0.y * size.y + extendUV.y;
}
}
vt.position = pos;
vt.uv0.x = uv0.x;
vt.uv0.y = uv0.y;
verts[start + j + k] = vt;
}
}
}
public static UIVertex VertexLerp(UIVertex a, UIVertex b, float t)
{
var vt = new UIVertex();
vt.position = Vector3.Lerp(a.position, b.position, t);
vt.normal = Vector3.Lerp(a.normal, b.normal, t);
vt.tangent = Vector4.Lerp(a.tangent, b.tangent, t);
vt.color = Color.Lerp(a.color, b.color, t);
vt.uv0 = Vector4.Lerp(a.uv0, b.uv0, t);
vt.uv1 = Vector4.Lerp(a.uv1, b.uv1, t);
vt.uv2 = Vector4.Lerp(a.uv2, b.uv2, t);
if (onLerpVertex != null)
{
vt = onLerpVertex(vt, a, b, t);
}
return vt;
}
public static void GetBounds(List<UIVertex> verts, int start, int bundleSize, out Rect posBounds,
out Rect uvBounds)
{
var minPos = new Vector2(float.MaxValue, float.MaxValue);
var maxPos = new Vector2(float.MinValue, float.MinValue);
var minUV = new Vector2(float.MaxValue, float.MaxValue);
var maxUV = new Vector2(float.MinValue, float.MinValue);
for (var i = start; i < start + bundleSize; i++)
{
var vt = verts[i];
UpdateMinMax(ref minPos, ref maxPos, vt.position);
UpdateMinMax(ref minUV, ref maxUV, vt.uv0);
}
// Shrink coordinate to avoid uv edge
posBounds = new Rect(minPos.x + 0.001f, minPos.y + 0.001f,
maxPos.x - minPos.x - 0.002f, maxPos.y - minPos.y - 0.002f);
uvBounds = new Rect(minUV.x, minUV.y, maxUV.x - minUV.x, maxUV.y - minUV.y);
return;
static void UpdateMinMax(ref Vector2 min, ref Vector2 max, Vector2 value)
{
if (value.x < min.x) min.x = value.x; // Left
if (max.x < value.x) max.x = value.x; // Right
if (value.y < min.y) min.y = value.y; // Bottom
if (max.y < value.y) max.y = value.y; // Top
}
}
public static void GetQuad(List<UIVertex> verts, int i, out UIVertex lb, out UIVertex lt, out UIVertex rt,
out UIVertex rb)
{
lb = verts[i];
lt = verts[i + 1];
rt = verts[i + 2];
rb = verts[i + 4];
}
public static void SetQuad(List<UIVertex> verts, int i, UIVertex lb, UIVertex lt, UIVertex rt, UIVertex rb)
{
verts[i] = verts[i + 5] = lb;
verts[i + 1] = lt;
verts[i + 2] = verts[i + 3] = rt;
verts[i + 4] = rb;
}
public static void SplitHorizontal(List<UIVertex> verts, UIVertex lb, UIVertex lt, ref UIVertex rb,
ref UIVertex rt, Rect rect, List<float> times)
{
if (times == null || times.Count == 0) return;
var min = Mathf.InverseLerp(rect.xMin, rect.xMax, lb.position.x);
for (var i = 0; i < times.Count; i++)
{
var time = times[i];
if (time <= 0 || 1 <= time) continue;
var max = Mathf.InverseLerp(rect.xMin, rect.xMax, rt.position.x);
if (time <= min || max <= time) continue;
SplitHorizontal(verts, lb, lt, ref rb, ref rt, Mathf.InverseLerp(min, max, time));
}
}
public static void SplitHorizontal(List<UIVertex> verts, UIVertex lb, UIVertex lt, ref UIVertex rb,
ref UIVertex rt, float time)
{
var mb = VertexLerp(lb, rb, time);
var mt = VertexLerp(lt, rt, time);
verts.Add(mb);
verts.Add(mt);
verts.Add(rt);
verts.Add(rt);
verts.Add(rb);
verts.Add(mb);
rt = mt;
rb = mb;
}
public static void SplitVertical(List<UIVertex> verts, UIVertex lb, ref UIVertex lt, UIVertex rb,
ref UIVertex rt, Rect rect, List<float> times)
{
if (times == null || times.Count == 0) return;
var min = Mathf.InverseLerp(rect.yMin, rect.yMax, lb.position.y);
for (var i = 0; i < times.Count; i++)
{
var time = times[i];
var max = Mathf.InverseLerp(rect.yMin, rect.yMax, rt.position.y);
if (time <= min || max <= time) continue;
SplitVertical(verts, lb, ref lt, rb, ref rt, Mathf.InverseLerp(min, max, time));
}
}
public static void SplitVertical(List<UIVertex> verts, UIVertex lb, ref UIVertex lt, UIVertex rb,
ref UIVertex rt, float time)
{
var lm = VertexLerp(lb, lt, time);
var rm = VertexLerp(rb, rt, time);
verts.Add(lm);
verts.Add(lt);
verts.Add(rt);
verts.Add(rt);
verts.Add(rm);
verts.Add(lm);
lt = lm;
rt = rm;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c1b302b92b3d4460b4e61e5b0135668
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -64,6 +64,7 @@ SubShader {
#pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA #pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA
#pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN #pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN
#pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST #pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ SHADOW_COLOR_MULTIPLY SHADOW_COLOR_ADDITIVE SHADOW_COLOR_SUBTRACTIVE SHADOW_COLOR_REPLACE SHADOW_COLOR_MULTIPLY_LUMINANCE SHADOW_COLOR_MULTIPLY_ADDITIVE SHADOW_COLOR_HSV_MODIFIER SHADOW_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE #pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE
// ==== UIEFFECT END ==== // ==== UIEFFECT END ====
@@ -63,6 +63,7 @@ SubShader{
#pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA #pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA
#pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN #pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN
#pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST #pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ SHADOW_COLOR_MULTIPLY SHADOW_COLOR_ADDITIVE SHADOW_COLOR_SUBTRACTIVE SHADOW_COLOR_REPLACE SHADOW_COLOR_MULTIPLY_LUMINANCE SHADOW_COLOR_MULTIPLY_ADDITIVE SHADOW_COLOR_HSV_MODIFIER SHADOW_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE #pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE
// ==== UIEFFECT END ==== // ==== UIEFFECT END ====
@@ -102,6 +102,7 @@ SubShader {
#pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA #pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA
#pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN #pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN
#pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST #pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ SHADOW_COLOR_MULTIPLY SHADOW_COLOR_ADDITIVE SHADOW_COLOR_SUBTRACTIVE SHADOW_COLOR_REPLACE SHADOW_COLOR_MULTIPLY_LUMINANCE SHADOW_COLOR_MULTIPLY_ADDITIVE SHADOW_COLOR_HSV_MODIFIER SHADOW_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE #pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE
// ==== UIEFFECT END ==== // ==== UIEFFECT END ====
@@ -135,6 +135,7 @@ SubShader {
#pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA #pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA
#pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN #pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN
#pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST #pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ SHADOW_COLOR_MULTIPLY SHADOW_COLOR_ADDITIVE SHADOW_COLOR_SUBTRACTIVE SHADOW_COLOR_REPLACE SHADOW_COLOR_MULTIPLY_LUMINANCE SHADOW_COLOR_MULTIPLY_ADDITIVE SHADOW_COLOR_HSV_MODIFIER SHADOW_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE #pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE
// ==== UIEFFECT END ==== // ==== UIEFFECT END ====
+64 -5
View File
@@ -17,6 +17,8 @@ uniform float _TransitionWidth;
uniform fixed4 _TargetColor; uniform fixed4 _TargetColor;
uniform float _TargetRange; uniform float _TargetRange;
uniform float _TargetSoftness; uniform float _TargetSoftness;
uniform float _ShadowBlurIntensity;
uniform half4 _ShadowColor;
// For performance reasons, limit the sampling of blur in TextMeshPro. // For performance reasons, limit the sampling of blur in TextMeshPro.
#ifdef UIEFFECT_TEXTMESHPRO #ifdef UIEFFECT_TEXTMESHPRO
@@ -177,7 +179,8 @@ half4 apply_color_filter(half4 color, const half4 factor, const float intensity)
return color; return color;
} }
#endif #endif
return lerp(inColor, color, intensity); color = lerp(inColor, color, intensity);
return color;
} }
half4 apply_transition_color_filter(half4 color, const half4 factor, const float intensity) half4 apply_transition_color_filter(half4 color, const half4 factor, const float intensity)
@@ -223,15 +226,67 @@ half4 apply_transition_color_filter(half4 color, const half4 factor, const float
return color; return color;
} }
#endif #endif
color = lerp(inColor, color, intensity);
return color;
}
half4 apply_shadow_color_filter(half4 color, const half4 factor, const float intensity)
{
const half4 inColor = color;
#if SHADOW_COLOR_NONE // Color.None
{
return color;
}
#elif SHADOW_COLOR_MULTIPLY // Color.Multiply
{
color.rgb = color.rgb * factor.rgb;
}
#elif SHADOW_COLOR_ADDITIVE // Color.Additive
{
color.rgb = color.rgb + factor.rgb * color.a;
}
#elif SHADOW_COLOR_SUBTRACTIVE // Color.Subtractive
{
color.rgb = color.rgb - factor.rgb * color.a;
}
#elif SHADOW_COLOR_REPLACE // Color.Replace
{
color.rgb = factor.rgb * color.a;
}
#elif SHADOW_COLOR_MULTIPLY_LUMINANCE // Color.MultiplyLuminance
{
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb / 2 * color.a;
}
#elif SHADOW_COLOR_MULTIPLY_ADDITIVE // Color.MultiplyAdditive
{
color.rgb = color.rgb * (1 + factor.rgb);
}
#elif SHADOW_COLOR_HSV_MODIFIER // Color.HsvModifier
{
const float3 hsv = rgb_to_hsv(color.rgb);
color.rgb = hsv_to_rgb(hsv + factor.rgb) * color.a * factor.a;
color.a = color.a * factor.a;
}
#elif SHADOW_COLOR_CONTRAST // Color.Contrast
{
color.rgb = ((color.rgb - 0.5) * (factor.r + 1) + 0.5 + factor.g * 1.5) * color.a * factor.a;
color.a = color.a * factor.a;
}
#else
{
return color;
}
#endif #endif
return lerp(inColor, color, intensity); color = lerp(inColor, color, intensity);
return color;
} }
half4 apply_sampling_filter(float2 uv, const float4 uvMask, const float2 uvLocal) half4 apply_sampling_filter(float2 uv, const float4 uvMask, const float2 uvLocal)
{ {
#if SAMPLING_BLUR_FAST || SAMPLING_BLUR_MEDIUM || SAMPLING_BLUR_DETAIL #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 #if SAMPLING_BLUR_FAST
const int KERNEL_SIZE = 5; const int KERNEL_SIZE = 5;
@@ -247,7 +302,7 @@ half4 apply_sampling_filter(float2 uv, const float4 uvMask, const float2 uvLocal
float4 o = 0; float4 o = 0;
float sum = 0; float sum = 0;
float2 shift = 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++) for (int x = 0; x < KERNEL_SIZE; x++)
{ {
shift.x = blur.x * (float(x) - KERNEL_SIZE / 2); shift.x = blur.x * (float(x) - KERNEL_SIZE / 2);
@@ -335,7 +390,7 @@ half4 apply_transition_filter(half4 color, const float alpha)
} }
#endif #endif
color.rgb = lerp(color.rgb, bandColor, bandLerp * softLerp); color = lerp(color, bandColor, bandLerp * softLerp);
#if TRANSITION_DISSOLVE #if TRANSITION_DISSOLVE
{ {
@@ -374,6 +429,10 @@ half4 uieffect_internal(float2 uv, const float4 uvMask, const float2 uvLocal)
{ {
color = apply_color_filter(color, _ColorValue, _ColorIntensity); color = apply_color_filter(color, _ColorValue, _ColorIntensity);
} }
else
{
color = apply_shadow_color_filter(color, _ShadowColor, 1);
}
return color; return color;
} }
+1
View File
@@ -62,6 +62,7 @@
#pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA #pragma shader_feature_local_fragment _ SAMPLING_BLUR_FAST SAMPLING_BLUR_MEDIUM SAMPLING_BLUR_DETAIL SAMPLING_PIXELATION SAMPLING_RGB_SHIFT SAMPLING_EDGE_LUMINANCE SAMPLING_EDGE_ALPHA
#pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN #pragma shader_feature_local_fragment _ TRANSITION_FADE TRANSITION_CUTOFF TRANSITION_DISSOLVE TRANSITION_SHINY TRANSITION_MASK TRANSITION_MELT TRANSITION_BURN
#pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST #pragma shader_feature_local_fragment _ TRANSITION_COLOR_MULTIPLY TRANSITION_COLOR_ADDITIVE TRANSITION_COLOR_SUBTRACTIVE TRANSITION_COLOR_REPLACE TRANSITION_COLOR_MULTIPLY_LUMINANCE TRANSITION_COLOR_MULTIPLY_ADDITIVE TRANSITION_COLOR_HSV_MODIFIER TRANSITION_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ SHADOW_COLOR_MULTIPLY SHADOW_COLOR_ADDITIVE SHADOW_COLOR_SUBTRACTIVE SHADOW_COLOR_REPLACE SHADOW_COLOR_MULTIPLY_LUMINANCE SHADOW_COLOR_MULTIPLY_ADDITIVE SHADOW_COLOR_HSV_MODIFIER SHADOW_COLOR_CONTRAST
#pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE #pragma shader_feature_local_fragment _ TARGET_HUE TARGET_LUMINANCE
// ==== UIEFFECT END ==== // ==== UIEFFECT END ====