fix: blur with 'Filled' Image type may cause error

close #313
This commit is contained in:
mob-sakai
2025-03-06 10:40:44 +09:00
parent f2753e9a3d
commit f0a76424e6
5 changed files with 115 additions and 36 deletions
+1 -1
View File
@@ -447,7 +447,7 @@ namespace Coffee.UIEffects
effectProxy.OnPreModifyMesh(graphic);
var verts = s_WorkingVertices;
var expandSize = GetExpandSize(canModifyShape);
var expandSize = effectProxy.ModifyExpandSize(graphic, GetExpandSize(canModifyShape));
var count = vh.currentIndexCount;
// Get the rectangle to calculate the normalized position.
@@ -72,6 +72,11 @@ namespace Coffee.UIEffects
{
}
public virtual Vector4 ModifyExpandSize(Graphic graphic, Vector2 expandSize)
{
return new Vector4(expandSize.x, expandSize.y, expandSize.x, expandSize.y);
}
private static readonly Func<UIVertex, UIVertex, UIVertex, float, UIVertex> s_OnLerpVertex =
null;
@@ -0,0 +1,57 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace Coffee.UIEffects
{
internal class ImageProxy : GraphicProxy
{
#if UNITY_EDITOR
[InitializeOnLoadMethod]
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void InitializeOnLoad()
{
Register(new ImageProxy());
}
/// <summary>
/// Check if the graphic is valid for this proxy.
/// </summary>
protected override bool IsValid(Graphic graphic)
{
if (!graphic) return false;
if (graphic is Image) return true;
return false;
}
/// <summary>
/// Check if the graphic is a text.
/// </summary>
public override bool IsText(Graphic graphic)
{
return false;
}
public override Vector4 ModifyExpandSize(Graphic graphic, Vector2 expandSize)
{
var image = graphic as Image;
var ret = new Vector4(expandSize.x, expandSize.y, expandSize.x, expandSize.y);
if (image
&& image.type == Image.Type.Filled
&& image.fillMethod == Image.FillMethod.Radial360
&& image.fillAmount <= 0.5f)
{
ret[(image.fillOrigin + (image.fillClockwise ? 2 : 0)) % 4] = 0;
if (image.fillAmount <= 0.25f)
{
ret[(image.fillOrigin + 3) % 4] = 0;
}
}
return ret;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cddfadaf298c14befb49972983c16f2d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+41 -35
View File
@@ -18,56 +18,62 @@ namespace Coffee.UIEffects
}
}
public static void Expand(List<UIVertex> verts, int start, int bundleSize, Vector2 expandSize, Rect bounds)
public static void Expand(List<UIVertex> verts, int start, int bundleSize, Vector4 expandSize, Rect bounds)
{
if (expandSize == Vector2.zero) return;
if (expandSize == Vector4.zero) return;
// Quad (6 vertices)
for (var j = 0; j < bundleSize; j += 6)
for (var j = 0; j < bundleSize; j += 3)
{
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)
if (bounds.Contains(verts[start + j + 0].position)
&& bounds.Contains(verts[start + j + 1].position)
&& bounds.Contains(verts[start + j + 2].position))
{
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);
continue;
}
GetBounds(verts, start + j, 3, out var posBounds, out var uvBounds);
var posCenter = (Vector4)posBounds.center;
posCenter.z = posCenter.x;
posCenter.w = posCenter.y;
var uvCenter = (Vector4)uvBounds.center;
uvCenter.z = uvCenter.x;
uvCenter.w = uvCenter.y;
var size = (Vector4)posBounds.size;
size.z = 1 + expandSize.z / Mathf.Abs(size.x);
size.w = 1 + expandSize.w / Mathf.Abs(size.y);
size.x = 1 + expandSize.x / Mathf.Abs(size.x);
size.y = 1 + expandSize.y / Mathf.Abs(size.y);
var extendPos = posCenter - Vector4.Scale(size, posCenter);
var extendUV = uvCenter - Vector4.Scale(size, uvCenter);
// Set vertex position, uv, uvMask and local normalized position.
for (var k = 0; k < 6; k++)
for (var k = 0; k < 3; 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)
{
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;
}
pos.x = pos.x * size.x + extendPos.x;
uv0.x = uv0.x * size.x + extendUV.x;
}
else if (bounds.xMax < pos.x)
{
pos.x = pos.x * size.z + extendPos.z;
uv0.x = uv0.x * size.z + extendUV.z;
}
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;
}
if (pos.y < bounds.yMin)
{
pos.y = pos.y * size.y + extendPos.y;
uv0.y = uv0.y * size.y + extendUV.y;
}
else if (bounds.yMax < pos.y)
{
pos.y = pos.y * size.w + extendPos.w;
uv0.y = uv0.y * size.w + extendUV.w;
}
vt.position = pos;