for (int i = 0; i < 4; ++i) { s_VertScratch[i].x += rect.x; s_VertScratch[i].y += rect.y; }
s_UVScratch[0] = new Vector2(outer.x, outer.y); s_UVScratch[1] = new Vector2(inner.x, inner.y); s_UVScratch[2] = new Vector2(inner.z, inner.w); s_UVScratch[3] = new Vector2(outer.z, outer.w);
我们会根据image中设置的Pixel Per Unit Multiplier参数调整border,随后进行设置,其中s_VertScratch记录了必要的信息,它是一个四个元素的Vector2数组,第一个和第四个记录了padding的左下角和右上角(即整个矩形的左下角和右下角),第二个和第三个参数记录了border的左下角和右下角,最后将每个元素加上rect的信息就获得了padding和rect实际的左下角和右下角信息。s_UVScratch记录了内区域(5)左上角和右下角的UV(第2、3个元素)和外区域(整个矩形)左上角和右下角UV(第1、4个元素)。
最后对VertexHelper进行填充:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
toFill.Clear(); for (int x = 0; x < 3; ++x) { int x2 = x + 1; for (int y = 0; y < 3; ++y) { if (!m_FillCenter && x == 1 && y == 1) continue; int y2 = y + 1; AddQuad(toFill, new Vector2(s_VertScratch[x].x, s_VertScratch[y].y), new Vector2(s_VertScratch[x2].x, s_VertScratch[y2].y), color, new Vector2(s_UVScratch[x].x, s_UVScratch[y].y), new Vector2(s_UVScratch[x2].x, s_UVScratch[y2].y)); } } }
var uvMin = new Vector2(inner.x, inner.y); var uvMax = new Vector2(inner.z, inner.w);
// Min to max max range for tiled region in coordinates relative to lower left corner. float xMin = border.x; float xMax = rect.width - border.z; float yMin = border.y; float yMax = rect.height - border.w;
if (activeSprite != null && (hasBorder || activeSprite.packed || activeSprite.texture != null && activeSprite.texture.wrapMode != TextureWrapMode.Repeat)) { // Sprite has border, or is not in repeat mode, or cannot be repeated because of packing. // We cannot use texture tiling so we will generate a mesh of quads to tile the texture. // Evaluate how many vertices we will generate. Limit this number to something sane, // especially since meshes can not have more than 65000 vertices.
long nTilesW = 0; long nTilesH = 0; if (m_FillCenter) { nTilesW = (long)Math.Ceiling((xMax - xMin) / tileWidth); nTilesH = (long)Math.Ceiling((yMax - yMin) / tileHeight);
if (hasBorder) { clipped = uvMax; for (long j = 0; j < nTilesH; j++) { float y1 = yMin + j * tileHeight; float y2 = yMin + (j + 1) * tileHeight; if (y2 > yMax) { clipped.y = uvMin.y + (uvMax.y - uvMin.y) * (yMax - y1) / (y2 - y1); y2 = yMax; } AddQuad(toFill, new Vector2(0, y1) + rect.position, new Vector2(xMin, y2) + rect.position, color, new Vector2(outer.x, uvMin.y), new Vector2(uvMin.x, clipped.y)); AddQuad(toFill, new Vector2(xMax, y1) + rect.position, new Vector2(rect.width, y2) + rect.position, color, new Vector2(uvMax.x, uvMin.y), new Vector2(outer.z, clipped.y)); }
// Bottom and top tiled border clipped = uvMax; for (long i = 0; i < nTilesW; i++) { float x1 = xMin + i * tileWidth; float x2 = xMin + (i + 1) * tileWidth; if (x2 > xMax) { clipped.x = uvMin.x + (uvMax.x - uvMin.x) * (xMax - x1) / (x2 - x1); x2 = xMax; } AddQuad(toFill, new Vector2(x1, 0) + rect.position, new Vector2(x2, yMin) + rect.position, color, new Vector2(uvMin.x, outer.y), new Vector2(clipped.x, uvMin.y)); AddQuad(toFill, new Vector2(x1, yMax) + rect.position, new Vector2(x2, rect.height) + rect.position, color, new Vector2(uvMin.x, uvMax.y), new Vector2(clipped.x, outer.w)); }
// Corners AddQuad(toFill, new Vector2(0, 0) + rect.position, new Vector2(xMin, yMin) + rect.position, color, new Vector2(outer.x, outer.y), new Vector2(uvMin.x, uvMin.y)); AddQuad(toFill, new Vector2(xMax, 0) + rect.position, new Vector2(rect.width, yMin) + rect.position, color, new Vector2(uvMax.x, outer.y), new Vector2(outer.z, uvMin.y)); AddQuad(toFill, new Vector2(0, yMax) + rect.position, new Vector2(xMin, rect.height) + rect.position, color, new Vector2(outer.x, uvMax.y), new Vector2(uvMin.x, outer.w)); AddQuad(toFill, new Vector2(xMax, yMax) + rect.position, new Vector2(rect.width, rect.height) + rect.position, color, new Vector2(uvMax.x, uvMax.y), new Vector2(outer.z, outer.w)); } }