Skip to content

Commit f0a7642

Browse files
committed
fix: blur with 'Filled' Image type may cause error
close #313
1 parent f2753e9 commit f0a7642

File tree

5 files changed

+115
-36
lines changed

5 files changed

+115
-36
lines changed

Packages/src/Runtime/UIEffectContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelp
447447
effectProxy.OnPreModifyMesh(graphic);
448448

449449
var verts = s_WorkingVertices;
450-
var expandSize = GetExpandSize(canModifyShape);
450+
var expandSize = effectProxy.ModifyExpandSize(graphic, GetExpandSize(canModifyShape));
451451
var count = vh.currentIndexCount;
452452

453453
// Get the rectangle to calculate the normalized position.

Packages/src/Runtime/Utilities/GraphicProxy.cs

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public virtual void OnPreModifyMesh(Graphic graphic)
7272
{
7373
}
7474

75+
public virtual Vector4 ModifyExpandSize(Graphic graphic, Vector2 expandSize)
76+
{
77+
return new Vector4(expandSize.x, expandSize.y, expandSize.x, expandSize.y);
78+
}
79+
7580
private static readonly Func<UIVertex, UIVertex, UIVertex, float, UIVertex> s_OnLerpVertex =
7681
null;
7782

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
namespace Coffee.UIEffects
6+
{
7+
internal class ImageProxy : GraphicProxy
8+
{
9+
#if UNITY_EDITOR
10+
[InitializeOnLoadMethod]
11+
#endif
12+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
13+
private static void InitializeOnLoad()
14+
{
15+
Register(new ImageProxy());
16+
}
17+
18+
/// <summary>
19+
/// Check if the graphic is valid for this proxy.
20+
/// </summary>
21+
protected override bool IsValid(Graphic graphic)
22+
{
23+
if (!graphic) return false;
24+
if (graphic is Image) return true;
25+
return false;
26+
}
27+
28+
/// <summary>
29+
/// Check if the graphic is a text.
30+
/// </summary>
31+
public override bool IsText(Graphic graphic)
32+
{
33+
return false;
34+
}
35+
36+
public override Vector4 ModifyExpandSize(Graphic graphic, Vector2 expandSize)
37+
{
38+
var image = graphic as Image;
39+
var ret = new Vector4(expandSize.x, expandSize.y, expandSize.x, expandSize.y);
40+
41+
if (image
42+
&& image.type == Image.Type.Filled
43+
&& image.fillMethod == Image.FillMethod.Radial360
44+
&& image.fillAmount <= 0.5f)
45+
{
46+
ret[(image.fillOrigin + (image.fillClockwise ? 2 : 0)) % 4] = 0;
47+
48+
if (image.fillAmount <= 0.25f)
49+
{
50+
ret[(image.fillOrigin + 3) % 4] = 0;
51+
}
52+
}
53+
54+
return ret;
55+
}
56+
}
57+
}

Packages/src/Runtime/Utilities/ImageProxy.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/src/Runtime/Utilities/UIVertexUtil.cs

+41-35
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,62 @@ public static void ExpandCapacity(List<UIVertex> verts, int multiplier)
1818
}
1919
}
2020

21-
public static void Expand(List<UIVertex> verts, int start, int bundleSize, Vector2 expandSize, Rect bounds)
21+
public static void Expand(List<UIVertex> verts, int start, int bundleSize, Vector4 expandSize, Rect bounds)
2222
{
23-
if (expandSize == Vector2.zero) return;
23+
if (expandSize == Vector4.zero) return;
2424

25-
// Quad (6 vertices)
26-
for (var j = 0; j < bundleSize; j += 6)
25+
for (var j = 0; j < bundleSize; j += 3)
2726
{
28-
var size = default(Vector3);
29-
var extendPos = default(Vector3);
30-
var extendUV = default(Vector3);
31-
var posLB = verts[start + j + 1].position;
32-
var posRT = verts[start + j + 4].position;
33-
var willExpand = bundleSize == 6 // Text or simple quad
34-
|| !bounds.Contains(posLB)
35-
|| !bounds.Contains(posRT); // Outer 9-sliced quad
36-
if (willExpand)
27+
if (bounds.Contains(verts[start + j + 0].position)
28+
&& bounds.Contains(verts[start + j + 1].position)
29+
&& bounds.Contains(verts[start + j + 2].position))
3730
{
38-
var uvLB = verts[start + j + 1].uv0;
39-
var uvRT = verts[start + j + 4].uv0;
40-
var posCenter = (posLB + posRT) / 2;
41-
var uvCenter = (uvLB + uvRT) / 2;
42-
size = posLB - posRT;
43-
size.x = 1 + expandSize.x / Mathf.Abs(size.x);
44-
size.y = 1 + expandSize.y / Mathf.Abs(size.y);
45-
size.z = 1;
46-
extendPos = posCenter - Vector3.Scale(size, posCenter);
47-
extendUV = uvCenter - Vector4.Scale(size, uvCenter);
31+
continue;
4832
}
4933

34+
GetBounds(verts, start + j, 3, out var posBounds, out var uvBounds);
35+
var posCenter = (Vector4)posBounds.center;
36+
posCenter.z = posCenter.x;
37+
posCenter.w = posCenter.y;
38+
var uvCenter = (Vector4)uvBounds.center;
39+
uvCenter.z = uvCenter.x;
40+
uvCenter.w = uvCenter.y;
41+
var size = (Vector4)posBounds.size;
42+
size.z = 1 + expandSize.z / Mathf.Abs(size.x);
43+
size.w = 1 + expandSize.w / Mathf.Abs(size.y);
44+
size.x = 1 + expandSize.x / Mathf.Abs(size.x);
45+
size.y = 1 + expandSize.y / Mathf.Abs(size.y);
46+
var extendPos = posCenter - Vector4.Scale(size, posCenter);
47+
var extendUV = uvCenter - Vector4.Scale(size, uvCenter);
48+
5049
// Set vertex position, uv, uvMask and local normalized position.
51-
for (var k = 0; k < 6; k++)
50+
for (var k = 0; k < 3; k++)
5251
{
5352
var vt = verts[start + j + k];
5453
var pos = vt.position;
5554
var uv0 = vt.uv0;
5655

5756
// Expand edge vertex
58-
if (willExpand)
57+
if (pos.x < bounds.xMin)
5958
{
60-
if (pos.x < bounds.xMin || bounds.xMax < pos.x)
61-
{
62-
pos.x = pos.x * size.x + extendPos.x;
63-
uv0.x = uv0.x * size.x + extendUV.x;
64-
}
59+
pos.x = pos.x * size.x + extendPos.x;
60+
uv0.x = uv0.x * size.x + extendUV.x;
61+
}
62+
else if (bounds.xMax < pos.x)
63+
{
64+
pos.x = pos.x * size.z + extendPos.z;
65+
uv0.x = uv0.x * size.z + extendUV.z;
66+
}
6567

66-
if (pos.y < bounds.yMin || bounds.yMax < pos.y)
67-
{
68-
pos.y = pos.y * size.y + extendPos.y;
69-
uv0.y = uv0.y * size.y + extendUV.y;
70-
}
68+
if (pos.y < bounds.yMin)
69+
{
70+
pos.y = pos.y * size.y + extendPos.y;
71+
uv0.y = uv0.y * size.y + extendUV.y;
72+
}
73+
else if (bounds.yMax < pos.y)
74+
{
75+
pos.y = pos.y * size.w + extendPos.w;
76+
uv0.y = uv0.y * size.w + extendUV.w;
7177
}
7278

7379
vt.position = pos;

0 commit comments

Comments
 (0)