502 lines
18 KiB
C#
502 lines
18 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Процедурный генератор высококачественных сглаженных спрайтов для смартфона и интерфейса.
|
|
/// Позволяет создавать ультра-четкий современный UI без внешних тяжелых текстур.
|
|
/// </summary>
|
|
public static class ProceduralUISprites
|
|
{
|
|
private static readonly Dictionary<string, Sprite> spriteCache = new Dictionary<string, Sprite>();
|
|
|
|
/// <summary>
|
|
/// Создает 9-sliced скругленный прямоугольник с настраиваемым радиусом и рамкой.
|
|
/// </summary>
|
|
public static Sprite GetSlicedRoundedRect(Color fillColor, Color? borderColor = null, float borderWidth = 0f, int cornerRadius = 18)
|
|
{
|
|
int size = Mathf.Max(64, cornerRadius * 2 + 16);
|
|
string key = $"s_rr_{size}_{cornerRadius}_{fillColor}_{borderColor}_{borderWidth}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
Color border = borderColor ?? fillColor;
|
|
float half = size * 0.5f;
|
|
float r = cornerRadius;
|
|
float innerBoxHalf = half - r;
|
|
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
float px = x + 0.5f - half;
|
|
float py = y + 0.5f - half;
|
|
|
|
float qx = Mathf.Max(0f, Mathf.Abs(px) - innerBoxHalf);
|
|
float qy = Mathf.Max(0f, Mathf.Abs(py) - innerBoxHalf);
|
|
float dist = Mathf.Sqrt(qx * qx + qy * qy);
|
|
|
|
float outerAlpha = Mathf.Clamp01(r - dist + 0.6f);
|
|
if (outerAlpha <= 0.001f)
|
|
{
|
|
tex.SetPixel(x, y, Color.clear);
|
|
continue;
|
|
}
|
|
|
|
if (borderWidth > 0f)
|
|
{
|
|
float borderT = Mathf.Clamp01((r - borderWidth - dist + 0.6f));
|
|
Color pixelColor = Color.Lerp(border, fillColor, borderT);
|
|
pixelColor.a *= outerAlpha;
|
|
tex.SetPixel(x, y, pixelColor);
|
|
}
|
|
else
|
|
{
|
|
Color pixelColor = fillColor;
|
|
pixelColor.a *= outerAlpha;
|
|
tex.SetPixel(x, y, pixelColor);
|
|
}
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Vector4 borderSlice = new Vector4(cornerRadius + 2, cornerRadius + 2, cornerRadius + 2, cornerRadius + 2);
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), 100f, 0, SpriteMeshType.FullRect, borderSlice);
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает гладкий круг с опциональной обводкой.
|
|
/// </summary>
|
|
public static Sprite GetCircle(int size, Color fillColor, Color? borderColor = null, float borderWidth = 0f)
|
|
{
|
|
string key = $"circle_{size}_{fillColor}_{borderColor}_{borderWidth}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
Color border = borderColor ?? fillColor;
|
|
float center = (size - 1) * 0.5f;
|
|
float radius = size * 0.5f - 1f;
|
|
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
float dist = Vector2.Distance(new Vector2(x, y), new Vector2(center, center));
|
|
float alpha = Mathf.Clamp01(radius - dist + 0.6f);
|
|
|
|
if (alpha <= 0.001f)
|
|
{
|
|
tex.SetPixel(x, y, Color.clear);
|
|
continue;
|
|
}
|
|
|
|
if (borderWidth > 0f)
|
|
{
|
|
float borderT = Mathf.Clamp01(radius - borderWidth - dist + 0.6f);
|
|
Color col = Color.Lerp(border, fillColor, borderT);
|
|
col.a *= alpha;
|
|
tex.SetPixel(x, y, col);
|
|
}
|
|
else
|
|
{
|
|
Color col = fillColor;
|
|
col.a *= alpha;
|
|
tex.SetPixel(x, y, col);
|
|
}
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает стильный вертикальный градиент (например, для обоев рабочего стола телефона).
|
|
/// </summary>
|
|
public static Sprite GetGradient(int width, int height, Color topColor, Color bottomColor)
|
|
{
|
|
string key = $"grad_{width}_{height}_{topColor}_{bottomColor}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
float t = (float)y / Mathf.Max(1, height - 1);
|
|
Color col = Color.Lerp(bottomColor, topColor, t);
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
tex.SetPixel(x, y, col);
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает иконку Wi-Fi.
|
|
/// </summary>
|
|
public static Sprite GetWifiIcon(int size, Color color)
|
|
{
|
|
string key = $"wifi_{size}_{color}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
Vector2 center = new Vector2((size - 1) * 0.5f, size * 0.2f);
|
|
float[] radii = new float[] { size * 0.65f, size * 0.45f, size * 0.25f };
|
|
float arcHalfAngle = 42f;
|
|
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
Vector2 pos = new Vector2(x, y);
|
|
float dist = Vector2.Distance(pos, center);
|
|
Vector2 dir = pos - center;
|
|
float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
|
|
|
|
float alpha = 0f;
|
|
|
|
// Базовая точка внизу
|
|
if (dist <= size * 0.1f)
|
|
{
|
|
alpha = Mathf.Clamp01(size * 0.1f - dist + 0.6f);
|
|
}
|
|
else if (Mathf.Abs(angle) <= arcHalfAngle && dir.y > 0)
|
|
{
|
|
float angleFade = Mathf.Clamp01((arcHalfAngle - Mathf.Abs(angle)) / 6f);
|
|
foreach (float r in radii)
|
|
{
|
|
float ringDist = Mathf.Abs(dist - r);
|
|
float ringThickness = size * 0.05f;
|
|
if (ringDist <= ringThickness)
|
|
{
|
|
float ringAlpha = Mathf.Clamp01(ringThickness - ringDist + 0.5f) * angleFade;
|
|
alpha = Mathf.Max(alpha, ringAlpha);
|
|
}
|
|
}
|
|
}
|
|
|
|
Color c = color;
|
|
c.a *= alpha;
|
|
tex.SetPixel(x, y, c);
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает иконку полос мобильной сети (Cellular Signal Bars).
|
|
/// </summary>
|
|
public static Sprite GetSignalBarsIcon(int width, int height, Color activeColor, int activeBars = 4)
|
|
{
|
|
string key = $"bars_{width}_{height}_{activeColor}_{activeBars}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Point,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
// Заполняем прозрачным
|
|
for (int y = 0; y < height; y++)
|
|
for (int x = 0; x < width; x++)
|
|
tex.SetPixel(x, y, Color.clear);
|
|
|
|
int totalBars = 4;
|
|
float barWidth = (width - (totalBars - 1) * 2f) / totalBars;
|
|
|
|
for (int b = 0; b < totalBars; b++)
|
|
{
|
|
float barHeight = height * ((b + 1f) / totalBars);
|
|
float startX = b * (barWidth + 2f);
|
|
Color barCol = (b < activeBars) ? activeColor : new Color(activeColor.r, activeColor.g, activeColor.b, 0.25f);
|
|
|
|
for (int y = 0; y < Mathf.RoundToInt(barHeight); y++)
|
|
{
|
|
for (int x = Mathf.RoundToInt(startX); x < Mathf.RoundToInt(startX + barWidth); x++)
|
|
{
|
|
if (x < width && y < height)
|
|
tex.SetPixel(x, y, barCol);
|
|
}
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает иконку батареи с уровнем заряда.
|
|
/// </summary>
|
|
public static Sprite GetBatteryIcon(int width, int height, Color bodyColor, Color levelColor, float percentage = 0.85f)
|
|
{
|
|
string key = $"batt_{width}_{height}_{bodyColor}_{levelColor}_{percentage:F2}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
for (int y = 0; y < height; y++)
|
|
for (int x = 0; x < width; x++)
|
|
tex.SetPixel(x, y, Color.clear);
|
|
|
|
int tipW = 3;
|
|
int mainW = width - tipW - 1;
|
|
int tipH = Mathf.RoundToInt(height * 0.45f);
|
|
int tipYStart = (height - tipH) / 2;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
// Пипка справа
|
|
if (x >= mainW && x < width && y >= tipYStart && y < tipYStart + tipH)
|
|
{
|
|
tex.SetPixel(x, y, bodyColor);
|
|
continue;
|
|
}
|
|
|
|
// Корпус батарейки
|
|
if (x < mainW)
|
|
{
|
|
bool isBorder = (x == 0 || x == mainW - 1 || y == 0 || y == height - 1);
|
|
if (isBorder)
|
|
{
|
|
tex.SetPixel(x, y, bodyColor);
|
|
}
|
|
else
|
|
{
|
|
// Заполнение уровня заряда внутри (с отступом 2 пикселя)
|
|
float innerFillW = (mainW - 4) * Mathf.Clamp01(percentage);
|
|
if (x >= 2 && x < 2 + innerFillW && y >= 2 && y < height - 2)
|
|
{
|
|
tex.SetPixel(x, y, levelColor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает стилизованную векторную иконку автомобиля для приложения АвтоБазар.
|
|
/// </summary>
|
|
public static Sprite GetCarIcon(int size, Color color)
|
|
{
|
|
string key = $"car_ico_{size}_{color}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
for (int y = 0; y < size; y++)
|
|
for (int x = 0; x < size; x++)
|
|
tex.SetPixel(x, y, Color.clear);
|
|
|
|
// Рисуем силуэт автомобиля в профиль
|
|
float s = size;
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
float ny = y / s;
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
float nx = x / s;
|
|
bool isBody = false;
|
|
|
|
// Нижний кузов (от nx=0.1 до 0.9, ny=0.35 до 0.55)
|
|
if (nx >= 0.12f && nx <= 0.88f && ny >= 0.35f && ny <= 0.52f)
|
|
isBody = true;
|
|
|
|
// Кабина / крыша (от nx=0.28 до 0.72, ny=0.50 до 0.70)
|
|
if (ny >= 0.50f && ny <= 0.68f)
|
|
{
|
|
float roofMin = Mathf.Lerp(0.20f, 0.34f, (ny - 0.50f) / 0.18f);
|
|
float roofMax = Mathf.Lerp(0.85f, 0.68f, (ny - 0.50f) / 0.18f);
|
|
if (nx >= roofMin && nx <= roofMax)
|
|
isBody = true;
|
|
}
|
|
|
|
// Вырезы под колеса
|
|
float distWheel1 = Vector2.Distance(new Vector2(nx, ny), new Vector2(0.28f, 0.35f));
|
|
float distWheel2 = Vector2.Distance(new Vector2(nx, ny), new Vector2(0.72f, 0.35f));
|
|
if (distWheel1 < 0.11f || distWheel2 < 0.11f)
|
|
isBody = false;
|
|
|
|
// Сами колеса (шины)
|
|
if (distWheel1 < 0.09f || distWheel2 < 0.09f)
|
|
isBody = true;
|
|
|
|
if (isBody)
|
|
tex.SetPixel(x, y, color);
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает векторную иконку геометки / пина навигатора.
|
|
/// </summary>
|
|
public static Sprite GetMapPinIcon(int size, Color color)
|
|
{
|
|
string key = $"pin_ico_{size}_{color}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
for (int y = 0; y < size; y++)
|
|
for (int x = 0; x < size; x++)
|
|
tex.SetPixel(x, y, Color.clear);
|
|
|
|
Vector2 headCenter = new Vector2(size * 0.5f, size * 0.65f);
|
|
float headRadius = size * 0.28f;
|
|
Vector2 tip = new Vector2(size * 0.5f, size * 0.12f);
|
|
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
Vector2 pos = new Vector2(x, y);
|
|
float distHead = Vector2.Distance(pos, headCenter);
|
|
float alpha = 0f;
|
|
|
|
// Верхняя округлая часть пина
|
|
if (distHead <= headRadius)
|
|
{
|
|
alpha = Mathf.Clamp01(headRadius - distHead + 0.6f);
|
|
// Отверстие в центре пина
|
|
float holeRadius = headRadius * 0.42f;
|
|
if (distHead < holeRadius)
|
|
{
|
|
alpha = Mathf.Clamp01(distHead - holeRadius + 0.6f);
|
|
}
|
|
}
|
|
// Нижний конусообразный хвостик
|
|
else if (y < headCenter.y && y >= tip.y)
|
|
{
|
|
float progress = (headCenter.y - y) / (headCenter.y - tip.y);
|
|
float coneWidth = Mathf.Lerp(headRadius * 0.95f, 2f, progress);
|
|
float dx = Mathf.Abs(x - tip.x);
|
|
if (dx <= coneWidth)
|
|
{
|
|
alpha = Mathf.Clamp01(coneWidth - dx + 0.6f);
|
|
}
|
|
}
|
|
|
|
Color c = color;
|
|
c.a *= alpha;
|
|
tex.SetPixel(x, y, c);
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создает стрелку указателя направления (для указания на машину вне экрана).
|
|
/// </summary>
|
|
public static Sprite GetPointerArrow(int size, Color color)
|
|
{
|
|
string key = $"arrow_{size}_{color}";
|
|
if (spriteCache.TryGetValue(key, out Sprite cached) && cached != null)
|
|
return cached;
|
|
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
for (int y = 0; y < size; y++)
|
|
for (int x = 0; x < size; x++)
|
|
tex.SetPixel(x, y, Color.clear);
|
|
|
|
float center = (size - 1) * 0.5f;
|
|
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
float ny = (float)y / size; // 0..1 (вверх)
|
|
float halfW = Mathf.Lerp(0f, size * 0.45f, 1f - ny);
|
|
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
float dx = Mathf.Abs(x - center);
|
|
if (dx <= halfW)
|
|
{
|
|
float a = Mathf.Clamp01(halfW - dx + 0.6f);
|
|
Color c = color;
|
|
c.a *= a;
|
|
tex.SetPixel(x, y, c);
|
|
}
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f));
|
|
spriteCache[key] = sprite;
|
|
return sprite;
|
|
}
|
|
}
|