Culling 裁剪模块
这个模块主要用在了Mask。这里有2个比较重要的函数
这个函数用在了RectMask2D中
public static Rect FindCullAndClipWorldRect(List<RectMask2D> rectMaskParents, out bool validRect)
{
if (rectMaskParents.Count == 0)
{
validRect = false;
return new Rect();
}
这里计算的是RectMask2D(方块)
循环计算得到遮罩最大的方块
var compoundRect = rectMaskParents[0].canvasRect;
for (var i = 0; i < rectMaskParents.Count; ++i)
compoundRect = RectIntersect(compoundRect, rectMaskParents[i].canvasRect);
var cull = compoundRect.width <= 0 || compoundRect.height <= 0;
if (cull)
{
validRect = false;
return new Rect();
}
再整合成Rect类
Vector3 point1 = new Vector3(compoundRect.x, compoundRect.y, 0.0f);
Vector3 point2 = new Vector3(compoundRect.x + compoundRect.width, compoundRect.y + compoundRect.height, 0.0f);
validRect = true;
return new Rect(point1.x, point1.y, point2.x - point1.x, point2.y - point1.y);
}
计算出的xMin和yMin是左下角最左位置,xMax和yMax是右上角最右位置
private static Rect RectIntersect(Rect a, Rect b)
{
float xMin = Mathf.Max(a.x, b.x);
float xMax = Mathf.Min(a.x + a.width, b.x + b.width);
float yMin = Mathf.Max(a.y, b.y);
float yMax = Mathf.Min(a.y + a.height, b.y + b.height);
if (xMax >= xMin && yMax >= yMin)
return new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
return new Rect(0f, 0f, 0f, 0f);
}
Layout模块
CanvasScaler画布根据不同屏幕尺寸自适应
protected virtual void HandleScaleWithScreenSize()
{
得到屏幕真实尺寸
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
float scaleFactor = 0;
switch (m_ScreenMatchMode)
{
case ScreenMatchMode.MatchWidthOrHeight:
{
这里求以kLogBase为底(真实分辨率/设置的依赖分辨率)的对数
float logWidth = Mathf.Log(screenSize.x / m_ReferenceResolution.x, kLogBase);
float logHeight = Mathf.Log(screenSize.y / m_ReferenceResolution.y, kLogBase);
float logWeightedAverage = Mathf.Lerp(logWidth, logHeight, m_MatchWidthOrHeight);
这里就是Log的幂运算 相当于又将(真实分辨率/设置的依赖分辨率)这个还原了
scaleFactor = Mathf.Pow(kLogBase, logWeightedAverage);
break;
}
case ScreenMatchMode.Expand:
{
scaleFactor = Mathf.Min(screenSize.x / m_ReferenceResolution.x, screenSize.y / m_ReferenceResolution.y);
break;
}
case ScreenMatchMode.Shrink:
{
scaleFactor = Mathf.Max(screenSize.x / m_ReferenceResolution.x, screenSize.y / m_ReferenceResolution.y);
break;
}
}
SetScaleFactor(scaleFactor);
SetReferencePixelsPerUnit(m_ReferencePixelsPerUnit);
}
VertexModifiers
此模块用于修改图形网格。这个模块BaseMeshEffect继承于UIBehaviour, IMeshModifier提供修改网格的变量和接口相关
IMeshModifier接口只有一个方法
public interface IMeshModifier
{
[Obsolete("use IMeshModifier.ModifyMesh (VertexHelper verts) instead", false)]
void ModifyMesh(Mesh mesh);
void ModifyMesh(VertexHelper verts);
}
![](https://i-blog.csdnimg.cn/blog_migrate/38464eb25812d2522eac4ad91798f9c8.png)
VertexHelper类就是一个数据集类
而描边OutLine继承于Shadow,Shadow继承自BaseMeshEffect。这些类都实现了IMeshModifier接口的ModifyMesh
Shadow中ModifyMesh实现中最终调用了一个方法:
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
return;
var output = ListPool<UIVertex>.Get();
vh.GetUIVertexStream(output);
这里调用这个函数本体就是下面的函数,并且start = 0
output,就是VertexHelper 暂时存的顶点数据
ApplyShadow(output, effectColor, 0, output.Count, effectDistance.x, effectDistance.y);
vh.Clear();
vh.AddUIVertexTriangleStream(output);
ListPool<UIVertex>.Release(output);
}
protected void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
{
UIVertex vt; ui定点结构体 包含位置,法线,切线、颜色、4个uv坐标
上面说过start = 0。这里neededCapacity也就等于原来verts.Count + output.Count。verts也是output
这里neededCapacity就是verts列表数量的2倍
var neededCapacity = verts.Count + end - start;
if (verts.Capacity < neededCapacity)
verts.Capacity = neededCapacity;
列表扩容到原来的2倍
for (int i = start; i < end; ++i)
{
vt = verts[i];
verts.Add(vt);
Vector3 v = vt.position;
这里复制顶点信息将原顶点位置信息加上Inspector上设置的偏移量,并且添加到List中
v.x += x;
v.y += y;
vt.position = v;
var newColor = color;
if (m_UseGraphicAlpha) 如果勾选了使用alpha则进行计算
newColor.a = (byte)((newColor.a * verts[i].color.a) / 255);
vt.color = newColor;
verts[i] = vt;
}
}
最后上面代码注释可以发现UI层面使用ListPool。Shadow组件的使用直接顶点翻倍了
OutLine是继承自Shadow,现在来看看OutLine代码
![](https://i-blog.csdnimg.cn/blog_migrate/d7281d44a6952223424f991af816495f.png)
OutLine ModifyMesh函数
是不是直接问号了。。。容量直接给你扩了5倍。也就是顶点直接比原来多了4倍。里面干什么也就是朝4个方向各顶点扩容了一次