void LateUpdate() { var meshFilter = transform.GetComponent <MeshFilter>(); var mesh = meshFilter.sharedMesh; mesh.Clear(); var sprites = VisibleSprites(panel.sprites); SortByGUIDepth(sprites); Material[] materials = new Material[sprites.Count]; mesh.subMeshCount = sprites.Count; for (int i = 0; i < sprites.Count; ++i) { var sprite = sprites[i]; Material material; MadList <int> triangles = new MadList <int>(); sprite.DrawOn(ref vertices, ref colors, ref uv, ref triangles, out material); triangles.Trim(); triangleList.Add(triangles); materials[i] = material; } vertices.Trim(); colors.Trim(); uv.Trim(); triangleList.Trim(); mesh.vertices = vertices.Array; mesh.colors32 = colors.Array; mesh.uv = uv.Array; mesh.RecalculateNormals(); for (int i = 0; i < triangleList.Count; ++i) { MadList <int> triangles = triangleList[i]; mesh.SetTriangles(triangles.Array, i); } renderer.sharedMaterials = materials; vertices.Clear(); colors.Clear(); uv.Clear(); triangleList.Clear(); }
static void FindChildren<T>(Transform parent, Predicate<T> predicate, int depth, int currDepth, ref MadList<T> output) where T : Component { int count = parent.childCount; for (int i = 0; i < count; ++i) { var child = parent.GetChild(i); T component = child.GetComponent<T>(); if (component != null && predicate(component)) { output.Add(component); } if (currDepth < depth) { FindChildren<T>(child, predicate, depth, currDepth + 1, ref output); } } }
public override void DrawOn(ref MadList <Vector3> vertices, ref MadList <Color32> colors, ref MadList <Vector2> uv, ref MadList <int> triangles, out Material material) { UpdatePivotPoint(); var matrix = PanelToSelfMatrix(); var bounds = GetBounds(); material = font.material; float x = 0; foreach (char c in text) { int offset = vertices.Count; var glyph = font.GlyphFor(c); if (glyph == null) { Debug.LogWarning("Glyph not found: '" + c + "'"); continue; } // float w = (scale / glyph.height) * glyph.width * font.textureAspect; float xAdvance; var gBounds = GlyphBounds(glyph, out xAdvance); if (c != ' ') // render anything but space { float left = x + gBounds.x; float bottom = gBounds.y; float right = left + gBounds.width; float top = bottom + gBounds.height; vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(left, bottom, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(left, top, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(right, top, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(right, bottom, 0), bounds))); colors.Add(tint); colors.Add(tint); colors.Add(tint); colors.Add(tint); uv.Add(new Vector2(glyph.uMin, glyph.vMin)); uv.Add(new Vector2(glyph.uMin, glyph.vMax)); uv.Add(new Vector2(glyph.uMax, glyph.vMax)); uv.Add(new Vector2(glyph.uMax, glyph.vMin)); triangles.Add(0 + offset); triangles.Add(1 + offset); triangles.Add(2 + offset); triangles.Add(0 + offset); triangles.Add(2 + offset); triangles.Add(3 + offset); // x += gBounds.width + letterSpacing; } else { // x += gBounds.width; // no letter spacing for blank characters } x += xAdvance; } }
public void DrawOnQuad(ref MadList <Vector3> vertices, ref MadList <Color32> colors, ref MadList <Vector2> uv, ref MadList <int> triangles) { bool invert = fillType == FillType.RadialCCW; var matrix = TransformMatrix(); var bounds = GetBounds(); var topLeftQuad = new Quad(invert); var topRightQuad = new Quad(invert); var bottomRightQuad = new Quad(invert); var bottomLeftQuad = new Quad(invert); topLeftQuad.anchor = Quad.Point.BottomRight; topRightQuad.anchor = Quad.Point.BottomLeft; bottomRightQuad.anchor = Quad.Point.TopLeft; bottomLeftQuad.anchor = Quad.Point.TopRight; var topLeftQuad2 = new Quad(topLeftQuad); var topRightQuad2 = new Quad(topRightQuad); var bottomRightQuad2 = new Quad(bottomRightQuad); var bottomLeftQuad2 = new Quad(bottomLeftQuad); // creating 8 quads instead of 8 because when using offset it may display one additional quad // and the simplest way is to create 8 quads and wrap around Quad[] ordered = new Quad[8]; if (!invert) { ordered[0] = topRightQuad; ordered[1] = bottomRightQuad; ordered[2] = bottomLeftQuad; ordered[3] = topLeftQuad; ordered[4] = topRightQuad2; ordered[5] = bottomRightQuad2; ordered[6] = bottomLeftQuad2; ordered[7] = topLeftQuad2; } else { ordered[7] = topRightQuad2; ordered[6] = bottomRightQuad2; ordered[5] = bottomLeftQuad2; ordered[4] = topLeftQuad2; ordered[3] = topRightQuad; ordered[2] = bottomRightQuad; ordered[1] = bottomLeftQuad; ordered[0] = topLeftQuad; } float rOffset = radialFillOffset % 1; if (rOffset < 0) { rOffset += 1; } float fillValue = Mathf.Clamp01(this.fillValue) * radialFillLength; float fillStart = rOffset * 4; float fillEnd = (rOffset + fillValue) * 4; for (int i = Mathf.FloorToInt(fillStart); i < Mathf.CeilToInt(fillEnd); ++i) { var quad = ordered[i % 8]; if (i < fillStart) { quad.offset = fillStart - i; } else { quad.offset = 0; } if (fillEnd > i + 1) { quad.progress = 1 - quad.offset; } else { quad.progress = fillEnd - i - quad.offset; } } float sx = initialSize.x; float sy = initialSize.y; float sx2 = sx / 2; float sy2 = sy / 2; // collect points anv uvs List <Vector2[]> genPoints = new List <Vector2[]>(); List <Vector2[]> genUvs = new List <Vector2[]>(); genPoints.Add(topRightQuad.Points(sx2, sy, sx, sy2)); genUvs.Add(topRightQuad.Points(0.5f, 1, 1, 0.5f)); genPoints.Add(topRightQuad2.Points(sx2, sy, sx, sy2)); genUvs.Add(topRightQuad2.Points(0.5f, 1, 1, 0.5f)); genPoints.Add(bottomRightQuad.Points(sx2, sy2, sx, 0)); genUvs.Add(bottomRightQuad.Points(0.5f, 0.5f, 1, 0)); genPoints.Add(bottomRightQuad2.Points(sx2, sy2, sx, 0)); genUvs.Add(bottomRightQuad2.Points(0.5f, 0.5f, 1, 0)); genPoints.Add(bottomLeftQuad.Points(0, sy2, sx2, 0)); genUvs.Add(bottomLeftQuad.Points(0, 0.5f, 0.5f, 0)); genPoints.Add(bottomLeftQuad2.Points(0, sy2, sx2, 0)); genUvs.Add(bottomLeftQuad2.Points(0, 0.5f, 0.5f, 0)); genPoints.Add(topLeftQuad.Points(0, sy, sx2, sy2)); genUvs.Add(topLeftQuad.Points(0, 1, 0.5f, 0.5f)); genPoints.Add(topLeftQuad2.Points(0, sy, sx2, sy2)); genUvs.Add(topLeftQuad2.Points(0, 1, 0.5f, 0.5f)); // add ass triangles Color32 color32 = (Color32)tint; for (int i = 0; i < 8; ++i) { var points = genPoints[i]; var uvs = genUvs[i]; if (points.Length == 0) { continue; } int offset = vertices.Count; for (int j = 0; j < points.Length; ++j) { vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(points[j], bounds))); uv.Add(FixUV(uvs[j])); colors.Add(color32); } triangles.Add(0 + offset); triangles.Add(1 + offset); triangles.Add(2 + offset); // for quads if (points.Length > 3) { triangles.Add(0 + offset); triangles.Add(2 + offset); triangles.Add(3 + offset); } } }
public void DrawOnRegular(ref MadList <Vector3> vertices, ref MadList <Color32> colors, ref MadList <Vector2> uv, ref MadList <int> triangles) { var matrix = TransformMatrix(); var bounds = GetBounds(); float vLeft = 0; float vTop = initialSize.y; float vRight = initialSize.x; float vBottom = 0; float uvLeft = textureOffset.x; float uvTop = textureOffset.y + textureRepeat.y; float uvRight = textureOffset.x + textureRepeat.x; float uvBottom = textureOffset.y; fillValue = Mathf.Clamp01(fillValue); Rect rect = new Rect(0, 0, 1, 1); if (renderLiveBoundsOnly) { uvLeft += LiveCoordX(0); vLeft += LiveCoordX(0) * initialSize.x; vRight *= LiveCoordX(1); uvRight += LiveCoordX(1) - 1; vTop *= LiveCoordY(1); uvTop += LiveCoordY(1) - 1; vBottom += LiveCoordY(0) * initialSize.y; uvBottom += LiveCoordY(0); rect = new Rect(LiveCoordX(0), LiveCoordY(0), LiveCoordX(1) - LiveCoordX(0), LiveCoordY(1) - LiveCoordY(0)); //Debug.Log(rect); } if (fillValue != 1) { switch (fillType) { case FillType.LeftToRight: uvRight = rect.xMin + rect.width * fillValue; vRight = uvRight * initialSize.x; break; case FillType.RightToLeft: uvLeft = rect.xMax - rect.width * fillValue; vLeft = uvLeft * initialSize.x; break; case FillType.BottomToTop: uvTop = rect.yMin + rect.height * fillValue; vTop = uvTop * initialSize.y; break; case FillType.TopToBottom: uvBottom = rect.yMax - rect.height * fillValue; vBottom = uvBottom * initialSize.y; break; case FillType.ExpandHorizontal: { float fv = 0.5f + fillValue / 2; uvRight = rect.xMin + rect.width * fv; vRight = uvRight * initialSize.x; uvLeft = rect.xMax - rect.width * fv; vLeft = uvLeft * initialSize.x; } break; case FillType.ExpandVertical: { float fv = 0.5f + fillValue / 2; uvTop = rect.yMin + rect.height * fv; vTop = uvTop * initialSize.y; uvBottom = rect.yMax - rect.height * fv; vBottom = uvBottom * initialSize.y; } break; case FillType.None: // change nothing break; } } // left-bottom vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vLeft, vBottom, 0), bounds))); // left-top vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vLeft, vTop, 0), bounds))); // right - top vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vRight, vTop, 0), bounds))); // right - bottom vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vRight, vBottom, 0), bounds))); Color32 color32 = (Color32)tint; colors.Add(color32); colors.Add(color32); colors.Add(color32); colors.Add(color32); uv.Add(FixUV(new Vector2(uvLeft, uvBottom))); uv.Add(FixUV(new Vector2(uvLeft, uvTop))); uv.Add(FixUV(new Vector2(uvRight, uvTop))); uv.Add(FixUV(new Vector2(uvRight, uvBottom))); int offset = vertices.Count - 4; triangles.Add(0 + offset); triangles.Add(1 + offset); triangles.Add(2 + offset); triangles.Add(0 + offset); triangles.Add(2 + offset); triangles.Add(3 + offset); }
public override void DrawOn(ref MadList <Vector3> vertices, ref MadList <Color32> colors, ref MadList <Vector2> uv, ref MadList <int> triangles, out Material material) { UpdateTextIfNeeded(); UpdatePivotPoint(); var matrix = TransformMatrix(); var bounds = GetBounds(); material = font.material; float x = 0; float y = linesCount * scale - scale; for (int i = 0; i < linesCount; ++i) { string text = lines[i]; float lineWidth = lineWidths[i]; switch (align) { case Align.Left: x = 0; break; case Align.Center: x = (bounds.width - lineWidth) / 2; break; case Align.Right: x = bounds.width - lineWidth; break; default: Debug.LogError("Unknown align: " + align); x = 0; break; } for (int j = 0; j < text.Length; ++j) { char c = text[j]; int offset = vertices.Count; var glyph = font.GlyphFor(c); if (glyph == null) { Debug.LogWarning("Glyph not found: '" + c + "' (code " + (int)c + ")"); continue; } // float w = (scale / glyph.height) * glyph.width * font.textureAspect; float xAdvance; var gBounds = GlyphBounds(glyph, out xAdvance); if (c != ' ') // render anything but space { float left = x + gBounds.x; float bottom = y + gBounds.y; float right = left + gBounds.width; float top = bottom + gBounds.height; vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(left, bottom, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(left, top, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(right, top, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(right, bottom, 0), bounds))); colors.Add(tint); colors.Add(tint); colors.Add(tint); colors.Add(tint); uv.Add(new Vector2(glyph.uMin, glyph.vMin)); uv.Add(new Vector2(glyph.uMin, glyph.vMax)); uv.Add(new Vector2(glyph.uMax, glyph.vMax)); uv.Add(new Vector2(glyph.uMax, glyph.vMin)); triangles.Add(0 + offset); triangles.Add(1 + offset); triangles.Add(2 + offset); triangles.Add(0 + offset); triangles.Add(2 + offset); triangles.Add(3 + offset); // x += gBounds.width + letterSpacing; } else { // x += gBounds.width; // no letter spacing for blank characters } x += xAdvance; } // end of line y -= scale; } }
public void DrawOnRegular(ref MadList <Vector3> vertices, ref MadList <Color32> colors, ref MadList <Vector2> uv, ref MadList <int> triangles) { var matrix = TransformMatrix(); var bounds = GetBounds(); float vLeft = 0; float vTop = initialSize.y; float vRight = initialSize.x; float vBottom = 0; float uvLeft = textureOffset.x; float uvTop = textureOffset.y + textureRepeat.y; float uvRight = textureOffset.x + textureRepeat.x; float uvBottom = textureOffset.y; fillValue = Mathf.Clamp01(fillValue); if (fillValue != 1) { switch (fillType) { case FillType.LeftToRight: vRight *= LiveCoordX(fillValue); uvRight += LiveCoordX(fillValue) - 1; break; case FillType.RightToLeft: vLeft += LiveCoordX(1 - fillValue) * initialSize.x; uvLeft += LiveCoordX(1 - fillValue); break; case FillType.BottomToTop: vTop *= LiveCoordY(fillValue); uvTop += LiveCoordY(fillValue) - 1; break; case FillType.TopToBottom: vBottom += LiveCoordY(1 - fillValue) * initialSize.y; uvBottom += LiveCoordY(1 - fillValue); break; case FillType.ExpandHorizontal: { float fv = 0.5f + fillValue / 2; vRight *= LiveCoordX(fv); uvRight += LiveCoordX(fv) - 1; vLeft += LiveCoordX(1 - fv) * initialSize.x; uvLeft += LiveCoordX(1 - fv); } break; case FillType.ExpandVertical: { float fv = 0.5f + fillValue / 2; vTop *= LiveCoordY(fv); uvTop += LiveCoordY(fv) - 1; vBottom += LiveCoordY(1 - fv) * initialSize.y; uvBottom += LiveCoordY(1 - fv); } break; case FillType.None: // change nothing break; } } // left-bottom vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vLeft, vBottom, 0), bounds))); // left-top vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vLeft, vTop, 0), bounds))); // right - top vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vRight, vTop, 0), bounds))); // right - bottom vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(vRight, vBottom, 0), bounds))); Color32 color32 = (Color32)tint; colors.Add(color32); colors.Add(color32); colors.Add(color32); colors.Add(color32); uv.Add(FixUV(new Vector2(uvLeft, uvBottom))); uv.Add(FixUV(new Vector2(uvLeft, uvTop))); uv.Add(FixUV(new Vector2(uvRight, uvTop))); uv.Add(FixUV(new Vector2(uvRight, uvBottom))); int offset = vertices.Count - 4; triangles.Add(0 + offset); triangles.Add(1 + offset); triangles.Add(2 + offset); triangles.Add(0 + offset); triangles.Add(2 + offset); triangles.Add(3 + offset); }
void LateUpdate() { if (panel == null) { panel = GetComponent <MadPanel>(); } var mesh = drawCall.mesh; mesh.Clear(); var visibleSprites = VisibleSprites(panel.sprites); SortByGUIDepth(visibleSprites); var batchedSprites = Batch(visibleSprites); Material[] materials = new Material[batchedSprites.Count]; mesh.subMeshCount = batchedSprites.Count; for (int i = 0; i < batchedSprites.Count; ++i) { List <MadSprite> sprites = batchedSprites[i]; MadList <int> triangles; if (!trianglesPool.CanTake()) { trianglesPool.Add(new MadList <int>()); } triangles = trianglesPool.Take(); for (int j = 0; j < sprites.Count; ++j) { var sprite = sprites[j]; Material material; sprite.DrawOn(ref vertices, ref colors, ref uv, ref triangles, out material); materials[i] = material; } triangles.Trim(); triangleList.Add(triangles); } vertices.Trim(); colors.Trim(); uv.Trim(); triangleList.Trim(); mesh.vertices = vertices.Array; mesh.colors32 = colors.Array; mesh.uv = uv.Array; // excluding for metro, because of a bug #if !UNITY_METRO mesh.RecalculateNormals(); #endif for (int i = 0; i < triangleList.Count; ++i) { MadList <int> triangles = triangleList[i]; mesh.SetTriangles(triangles.Array, i); triangles.Clear(); trianglesPool.Release(triangles); } //renderer.sharedMaterials = materials; drawCall.SetMaterials(materials); vertices.Clear(); colors.Clear(); uv.Clear(); triangleList.Clear(); }