From d0a3ca60904c0eebf63178a0fb899bb17ecefe75 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Thu, 26 Dec 2024 17:35:12 +0900 Subject: [PATCH] feat: add `Shadow Color` and `Shadow Blur` options --- Packages/src/Editor/UIEffectEditor.cs | 21 +- Packages/src/Runtime/UIEffect.cs | 71 +++-- Packages/src/Runtime/UIEffectContext.cs | 264 ++++++------------ .../src/Runtime/Utilities/ContextProcessor.cs | 166 +++++++++++ .../Utilities/ContextProcessor.cs.meta | 11 + Packages/src/Runtime/Utilities/ShadowUtil.cs | 78 +++--- .../src/Runtime/Utilities/UIVertexUtil.cs | 210 ++++++++++++++ .../Runtime/Utilities/UIVertexUtil.cs.meta | 11 + .../Hidden-TMP_Bitmap-Mobile-UIEffect.shader | 1 + .../Hidden-TMP_Bitmap-UIEffect.shader | 1 + .../Hidden-TMP_SDF-Mobile-UIEffect.shader | 1 + .../Hidden-TMP_SDF-UIEffect.shader | 1 + Packages/src/Shaders/UIEffect.cginc | 69 ++++- Packages/src/Shaders/UIEffect.shader | 1 + 14 files changed, 663 insertions(+), 243 deletions(-) create mode 100644 Packages/src/Runtime/Utilities/ContextProcessor.cs create mode 100644 Packages/src/Runtime/Utilities/ContextProcessor.cs.meta create mode 100644 Packages/src/Runtime/Utilities/UIVertexUtil.cs create mode 100644 Packages/src/Runtime/Utilities/UIVertexUtil.cs.meta diff --git a/Packages/src/Editor/UIEffectEditor.cs b/Packages/src/Editor/UIEffectEditor.cs index 50d1ec5..22d4d34 100644 --- a/Packages/src/Editor/UIEffectEditor.cs +++ b/Packages/src/Editor/UIEffectEditor.cs @@ -52,8 +52,10 @@ namespace Coffee.UIEffects.Editors private SerializedProperty _shadowDistanceY; private SerializedProperty _shadowIteration; private SerializedProperty _shadowFade; - private SerializedProperty _shadowEffectOnOrigin; private SerializedProperty _shadowMirrorScale; + private SerializedProperty _shadowBlurIntensity; + private SerializedProperty _shadowColorFilter; + private SerializedProperty _shadowColor; private bool _expandOthers; private SerializedProperty _allowExtendVertex; @@ -102,8 +104,10 @@ namespace Coffee.UIEffects.Editors _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"); + _shadowColorFilter = serializedObject.FindProperty("m_ShadowColorFilter"); + _shadowColor = serializedObject.FindProperty("m_ShadowColor"); _allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex"); } @@ -231,8 +235,19 @@ namespace Coffee.UIEffects.Editors EditorGUILayout.PropertyField(_shadowIteration); } + EditorGUILayout.PropertyField(_shadowColorFilter); + EditorGUILayout.PropertyField(_shadowColor); 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--; } diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index d3a02ba..cd117f8 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -120,13 +120,21 @@ namespace Coffee.UIEffects [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; + + [SerializeField] + protected ColorFilter m_ShadowColorFilter = ColorFilter.Replace; + + [ColorUsage(false, true)] + [SerializeField] + protected Color m_ShadowColor = Color.white; + [SerializeField] 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 { 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 { get => m_AllowExtendVertex; @@ -616,8 +647,10 @@ namespace Coffee.UIEffects 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.shadowColorFilter = m_ShadowColorFilter; + c.shadowColor = m_ShadowColor; c.allowExtendVertex = m_AllowExtendVertex; } @@ -713,8 +746,10 @@ namespace Coffee.UIEffects 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_ShadowColorFilter = preset.m_ShadowColorFilter; + m_ShadowColor = preset.m_ShadowColor; UpdateContext(context); ApplyContextToMaterial(); @@ -761,8 +796,10 @@ namespace Coffee.UIEffects 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_ShadowColorFilter = c.shadowColorFilter; + m_ShadowColor = c.shadowColor; m_AllowExtendVertex = c.allowExtendVertex; diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index fdfd9de..ccbfa6a 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -1,11 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using Coffee.UIEffectInternal; using UnityEngine; using UnityEngine.Profiling; using UnityEngine.Rendering; using UnityEngine.UI; -#if TMP_ENABLE -using TMPro; -#endif namespace Coffee.UIEffects { @@ -32,6 +31,8 @@ namespace Coffee.UIEffects 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 string[] s_ToneKeywords = { @@ -100,6 +101,33 @@ namespace Coffee.UIEffects "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(), // 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 onModifyVertex; public ToneFilter toneFilter = ToneFilter.None; public float toneIntensity = 1; public Vector4 toneParams = new Vector4(0, 0, 0, 0); @@ -136,8 +164,10 @@ namespace Coffee.UIEffects 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 ColorFilter shadowColorFilter; + public Color shadowColor; public bool allowExtendVertex; @@ -147,7 +177,8 @@ namespace Coffee.UIEffects || toneFilter != ToneFilter.None || colorFilter != ColorFilter.None || srcBlendMode != BlendMode.One - || dstBlendMode != BlendMode.OneMinusSrcAlpha; + || dstBlendMode != BlendMode.OneMinusSrcAlpha + || shadowMode != ShadowMode.None; public void Reset() { @@ -192,7 +223,10 @@ namespace Coffee.UIEffects shadowDistance = preset.shadowDistance; shadowIteration = preset.shadowIteration; shadowFade = preset.shadowFade; - shadowEffectOnOrigin = preset.shadowEffectOnOrigin; + shadowMirrorScale = preset.shadowMirrorScale; + shadowBlurIntensity = preset.shadowBlurIntensity; + shadowColorFilter = preset.shadowColorFilter; + shadowColor = preset.shadowColor; allowExtendVertex = preset.allowExtendVertex; } @@ -223,13 +257,27 @@ namespace Coffee.UIEffects transitionTexOffset.x, transitionTexOffset.y)); material.SetFloat(s_TransitionWidth, Mathf.Clamp01(transitionWidth)); material.SetFloat(s_TransitionSoftness, Mathf.Clamp01(transitionSoftness)); - material.SetColor(s_TransitionColor, transitionColor); material.SetInt(s_TransitionColorFilter, (int)transitionColorFilter); + material.SetColor(s_TransitionColor, transitionColor); material.SetColor(s_TargetColor, targetColor); 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); + SetKeyword(material, s_ToneKeywords, (int)toneFilter); SetKeyword(material, s_ColorKeywords, (int)colorFilter); SetKeyword(material, s_SamplingKeywords, (int)samplingFilter); @@ -250,6 +298,16 @@ namespace Coffee.UIEffects 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); Profiler.EndSample(); @@ -272,31 +330,13 @@ namespace Coffee.UIEffects public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelper vh) { -#if TMP_ENABLE - var isTextMeshPro = graphic is TextMeshProUGUI || graphic is TMP_SubMeshUI; - var isText = isTextMeshPro || graphic is Text; - - // 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 processor = ContextProcessor.FindProcessor(graphic); + var isText = processor.IsText(graphic); + processor.OnPreModifyMesh(graphic); var verts = s_WorkingVertices; var expandSize = GetExpandSize(); - var useExpand = expandSize != Vector2.zero; - - // Update capacity of workingVertices if needed. 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. vh.GetUIVertexStream(verts); @@ -339,102 +379,38 @@ namespace Coffee.UIEffects for (var i = 0; i < count; i += bundleSize) { // min/max for bundled-quad - GetBounds(verts, i, bundleSize, out var bounds, out var uvMask); - - // Quad (6 vertices) - for (var j = 0; j < bundleSize; j += 6) + UIVertexUtil.GetBounds(verts, i, bundleSize, out var bounds, out var uvMask); + UIVertexUtil.Expand(verts, i, bundleSize, expandSize, bounds); + for (var j = 0; j < bundleSize; j++) { - var size = default(Vector3); - var extendPos = default(Vector3); - var extendUV = default(Vector3); - 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; - } + var vt = verts[i + j]; + ModifyVertex(ref vt, vt.position, vt.uv0, uvMask, rect, rot); + verts[i + j] = vt; } } // Apply shadow. + ApplyShadow(transitionRoot, verts); + + vh.Clear(); + vh.AddUIVertexTriangleStream(verts); + } + + private void ApplyShadow(RectTransform transitionRoot, List verts) + { switch (shadowMode) { case ShadowMode.Shadow: case ShadowMode.Shadow3: case ShadowMode.Outline: case ShadowMode.Outline8: - count = verts.Count; - ShadowUtil.DoShadow(shadowMode, verts, shadowDistance, shadowIteration, 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; - } - + ShadowUtil.DoShadow(verts, s_ShadowVectors[(int)shadowMode], shadowDistance, shadowIteration, + shadowFade); break; case ShadowMode.Mirror: ShadowUtil.DoMirror(verts, shadowDistance, shadowMirrorScale, shadowFade, transitionRoot); 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() @@ -473,83 +449,23 @@ namespace Coffee.UIEffects 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) { - vt.position = pos; pos = m.MultiplyPoint3x4(pos); var normalizedX = Mathf.InverseLerp(rect.xMin, rect.xMax, pos.x); var normalizedY = Mathf.InverseLerp(rect.yMin, rect.yMax, pos.y); - if (isTextMeshPro) + if (onModifyVertex != null) { - vt.uv0.x = uv.x; - 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); + vt = onModifyVertex(vt, uvMask, normalizedX, normalizedY); } 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); } } - - 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 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); - } } } diff --git a/Packages/src/Runtime/Utilities/ContextProcessor.cs b/Packages/src/Runtime/Utilities/ContextProcessor.cs new file mode 100644 index 0000000..ec1e62f --- /dev/null +++ b/Packages/src/Runtime/Utilities/ContextProcessor.cs @@ -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 s_Processors = new List() + { + 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; + } + + /// + /// Check if the graphic is valid for this processor. + /// + protected virtual bool IsValid(Graphic graphic) + { + return graphic; + } + + /// + /// Check if the graphic is a text. + /// + public virtual bool IsText(Graphic graphic) + { + return graphic is Text; + } + + /// + /// Called before modifying the mesh. + /// + public virtual void OnPreModifyMesh(Graphic graphic) + { + UIVertexUtil.onLerpVertex = s_OnLerpVertex; + ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow; + UIEffectContext.onModifyVertex = s_OnModifyVertex; + } + + private static readonly Func s_OnLerpVertex = + null; + + private static readonly Func s_OnMarkAsShadow = + vt => + { + vt.uv0.z -= 8; + vt.uv0.w -= 8; + return vt; + }; + + private static readonly Func 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; + } + + /// + /// Check if the graphic is a text. + /// + public override bool IsText(Graphic graphic) + { + return true; + } + + /// + /// Called before modifying the mesh. + /// + public override void OnPreModifyMesh(Graphic graphic) + { + UIVertexUtil.onLerpVertex = s_OnLerpVertex; + ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow; + UIEffectContext.onModifyVertex = s_OnModifyVertex; + } + + private static readonly Func 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 s_OnMarkAsShadow = + vt => + { + vt.uv1.z -= 8; + vt.uv1.w -= 8; + return vt; + }; + + private static readonly Func 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 +} diff --git a/Packages/src/Runtime/Utilities/ContextProcessor.cs.meta b/Packages/src/Runtime/Utilities/ContextProcessor.cs.meta new file mode 100644 index 0000000..0267478 --- /dev/null +++ b/Packages/src/Runtime/Utilities/ContextProcessor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 402a2dd4d098a4d708b9f6e7ce0557cd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Runtime/Utilities/ShadowUtil.cs b/Packages/src/Runtime/Utilities/ShadowUtil.cs index 064c4ca..ff9026f 100644 --- a/Packages/src/Runtime/Utilities/ShadowUtil.cs +++ b/Packages/src/Runtime/Utilities/ShadowUtil.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using UnityEngine; @@ -5,24 +6,45 @@ namespace Coffee.UIEffects { public static class ShadowUtil { - public static void DoShadow(ShadowMode mode, List verts, Vector2 distance, int iteration, float fade) + public static Func onMarkAsShadow; + + public static void DoShadow(List 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)); var count = verts.Count; var start = 0; var end = count; var d = Vector2.zero; - var a = 1f; + var a = fade; for (var i = 0; i < iteration; i++) { d += distance / (i + 1); - a *= fade; - ApplyShadow(verts, ref start, ref end, d, mode, a); + a *= 0.75f; + 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 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)); var count = verts.Count; var rect2 = root.rect; @@ -61,24 +83,15 @@ namespace Coffee.UIEffects if (lbRate < rate && rate < ltRate) { var t = (rate - lbRate) / (ltRate - lbRate); - lt.position = Vector3.Lerp(lb.position, lt.position, 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); + lt = UIVertexUtil.VertexLerp(lb, lt, t); } if (rbRate < rate && rate < rtRate) { var t = (rate - rbRate) / (rtRate - rbRate); - rt.position = Vector3.Lerp(rb.position, rt.position, 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); + rt = UIVertexUtil.VertexLerp(rb, rt, t); } - lb.position.y = -lb.position.y * scale + offset; lt.position.y = -lt.position.y * scale + offset; rt.position.y = -rt.position.y * scale + offset; @@ -95,39 +108,16 @@ namespace Coffee.UIEffects /// Append shadow vertices. /// * It is similar to Shadow component implementation. /// - private static void ApplyShadow(List verts, ref int start, ref int end, Vector2 distance, - ShadowMode mode, float alpha) + private static void ApplyShadow(List verts, Vector2[] vectors, ref int start, ref int end, + Vector2 distance, float alpha) { - if (mode == ShadowMode.None) return; - var x = distance.x; var y = distance.y; - switch (mode) + for (var i = 0; i < vectors.Length; i++) { - case ShadowMode.Shadow: - ApplyShadowZeroAlloc(verts, ref start, ref end, x, y, alpha); - break; - 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; + var dx = x * vectors[i].x; + var dy = y * vectors[i].y; + ApplyShadowZeroAlloc(verts, ref start, ref end, dx, dy, alpha); } } diff --git a/Packages/src/Runtime/Utilities/UIVertexUtil.cs b/Packages/src/Runtime/Utilities/UIVertexUtil.cs new file mode 100644 index 0000000..f1356c9 --- /dev/null +++ b/Packages/src/Runtime/Utilities/UIVertexUtil.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace Coffee.UIEffects +{ + internal static class UIVertexUtil + { + public static Func onLerpVertex; + + public static void ExpandCapacity(List verts, int multiplier) + { + var capacity = Mathf.NextPowerOfTwo(verts.Count * multiplier); + if (verts.Capacity < capacity) + { + verts.Capacity = capacity; + } + } + + public static void Expand(List 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 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 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 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 verts, UIVertex lb, UIVertex lt, ref UIVertex rb, + ref UIVertex rt, Rect rect, List 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 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 verts, UIVertex lb, ref UIVertex lt, UIVertex rb, + ref UIVertex rt, Rect rect, List 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 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; + } + } +} diff --git a/Packages/src/Runtime/Utilities/UIVertexUtil.cs.meta b/Packages/src/Runtime/Utilities/UIVertexUtil.cs.meta new file mode 100644 index 0000000..b62f411 --- /dev/null +++ b/Packages/src/Runtime/Utilities/UIVertexUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9c1b302b92b3d4460b4e61e5b0135668 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-Mobile-UIEffect.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-Mobile-UIEffect.shader index c7d4418..35ad6c3 100755 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-Mobile-UIEffect.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-Mobile-UIEffect.shader @@ -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 _ 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 _ 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 // ==== UIEFFECT END ==== diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-UIEffect.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-UIEffect.shader index a23639d..201f36e 100755 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-UIEffect.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Bitmap-UIEffect.shader @@ -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 _ 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 _ 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 // ==== UIEFFECT END ==== diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-UIEffect.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-UIEffect.shader index d66562d..7d1d310 100644 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-UIEffect.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-UIEffect.shader @@ -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 _ 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 _ 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 // ==== UIEFFECT END ==== diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-UIEffect.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-UIEffect.shader index 9bcef2f..9d0265c 100644 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-UIEffect.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-UIEffect.shader @@ -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 _ 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 _ 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 // ==== UIEFFECT END ==== diff --git a/Packages/src/Shaders/UIEffect.cginc b/Packages/src/Shaders/UIEffect.cginc index 9a1f8c4..1db89bb 100644 --- a/Packages/src/Shaders/UIEffect.cginc +++ b/Packages/src/Shaders/UIEffect.cginc @@ -17,6 +17,8 @@ uniform float _TransitionWidth; uniform fixed4 _TargetColor; uniform float _TargetRange; uniform float _TargetSoftness; +uniform float _ShadowBlurIntensity; +uniform half4 _ShadowColor; // For performance reasons, limit the sampling of blur in TextMeshPro. #ifdef UIEFFECT_TEXTMESHPRO @@ -177,7 +179,8 @@ half4 apply_color_filter(half4 color, const half4 factor, const float intensity) return color; } #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) @@ -223,15 +226,67 @@ half4 apply_transition_color_filter(half4 color, const half4 factor, const float return color; } #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 - return lerp(inColor, color, intensity); + color = lerp(inColor, color, intensity); + return color; } 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; @@ -247,7 +302,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); @@ -335,7 +390,7 @@ half4 apply_transition_filter(half4 color, const float alpha) } #endif - color.rgb = lerp(color.rgb, bandColor, bandLerp * softLerp); + color = lerp(color, bandColor, bandLerp * softLerp); #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); } + else + { + color = apply_shadow_color_filter(color, _ShadowColor, 1); + } return color; } diff --git a/Packages/src/Shaders/UIEffect.shader b/Packages/src/Shaders/UIEffect.shader index 5a53e11..764618c 100644 --- a/Packages/src/Shaders/UIEffect.shader +++ b/Packages/src/Shaders/UIEffect.shader @@ -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 _ 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 _ 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 // ==== UIEFFECT END ====