0%

unity实现雷达图和描边

最近策划需要一个雷达战力图

RadarChart.cs 有兴趣的自己看下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RadarChart : MonoBehaviour
{
private MeshRenderer mMeshRenderer = null;
private MeshFilter mMeshFilter = null;
private Material mMat = null;
private Mesh mMesh;

public Color mainColor = new Color(0.4f, 0.6f, 1f);
public Color outLineColor = new Color(1f, 1f, 1f, 0.3f);
public Color addColor = Color.white;
public float alpha = 0.3f;


// 参数
private int VERTICES_COUNT; //网格模型顶点数量
public Vector3[] vertices;
private Vector3[] vec3OutLineList;
//三角形数组
int[] triangles;
public float scale;
public float radius = 45f;
public float outLineSize = 1f;
public GameObject obj;

public MeshRenderer meshRenderer
{
get
{
if (mMeshRenderer == null)
{
mMeshRenderer = GetComponent<MeshRenderer>();
if (mMeshRenderer == null)
{
mMeshRenderer = gameObject.AddComponent<MeshRenderer>();
}
}
return mMeshRenderer;
}
}

public MeshFilter meshFilter
{
get
{
if (mMeshFilter == null)
{
mMeshFilter = GetComponent<MeshFilter>();
if (mMeshFilter == null)
{
mMeshFilter = gameObject.AddComponent<MeshFilter>();
}
}
return mMeshFilter;
}
}

public Material material
{
get
{
if (mMat == null)
{
mMat = new Material(ShaderManager.instance.LoadShader("UI/ColorAdd", "ColorAdd"));
mMat.SetColor("_MainColor", mainColor);
mMat.SetColor("_AddColor", addColor);
mMat.SetFloat("_Alpha", alpha);
}
return mMat;
}
}

public Mesh mesh
{
get
{
if (mMesh == null)
{
mMesh = new Mesh();
mMesh.name = "UIRadarChart";
}
return mMesh;
}
}
void Start()
{
meshRenderer.material = material;
FillMeshRadarChart();
}

void Update()
{
//UpdateRadarChart();
}

float GetRadians(float angle)
{
return Mathf.PI / 180 * angle;
}

void FillMeshRadarChart()
{
if (!Application.isPlaying)
{
return;
}

VERTICES_COUNT = vertices.Length;
int triangles_count = VERTICES_COUNT - 1;

triangles = new int[triangles_count * 3];

//设定原点坐标
vertices[0] = new Vector3(0, 0, 1);
//首个在x轴上的坐标点
vertices[1] = new Vector3(radius, 0, 1);

//每个三角形角度
float everyAngle = 360 / triangles_count;
for (int i = 2; i < vertices.Length; i++)
{
var angle = GetRadians(everyAngle * (i - 1));
vertices[i] = new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 1);
}

int idx = 0;
int value = 0;
for (int i = 0; i < triangles.Length; i++)
{
if (i % 3 == 0)
{
triangles[i] = 0;
value = idx;
idx++;
}
else
{
value++;
if (value == VERTICES_COUNT)
value = 1;

triangles[i] = value;
}
}
UpdateRadarChart();
}

void DrawOutLine()
{
//obj.transform.localPosition = new Vector3(0, 0, -1);
MeshFilter mf = obj.GetComponent<MeshFilter>();
if (mf == null)
{
mf = obj.AddComponent<MeshFilter>();
}

MeshRenderer mr = obj.GetComponent<MeshRenderer>();
if (mr == null)
{
mr = obj.AddComponent<MeshRenderer>();
}

Material mat = new Material(ShaderManager.instance.LoadShader("UI/ColorAdd", "ColorAdd"));
mat.SetColor("_MainColor", outLineColor);
mr.material = mat;
Mesh meshOut = new Mesh();
meshOut.vertices = vec3OutLineList;
meshOut.triangles = triangles;
mf.mesh = meshOut;
}

Vector2 GetPreVec(int index)
{
int tmpIndex = (index - 1) < 1 ? vertices.Length - 1 : (index - 1);
return new Vector2(vertices[tmpIndex].x, vertices[tmpIndex].y);
}

Vector2 GetCurVec(int index)
{
return new Vector2(vertices[index].x, vertices[index].y);
}

Vector2 GetNextVec(int index)
{
int tmpIndex = (index + 1) >= vertices.Length ? 1 : index + 1;
return new Vector2(vertices[tmpIndex].x, vertices[tmpIndex].y);
}

Vector2 GetNormal(Vector2 dir)
{
return new Vector2(dir.y, -dir.x);
}

Vector3 GetIntersect(Vector2 s1, Vector2 d1, Vector2 s2, Vector2 d2)
{
Vector2 e1 = s1 + d1;
Vector2 e2 = s2 + d2;

float a1, a2, b1, b2, c1, c2;

a1 = e1.y - s1.y;
b1 = s1.x - e1.x;
c1 = (e1.x * s1.y) - (s1.x * e1.y);

a2 = e2.y - s2.y;
b2 = s2.x - e2.x;
c2 = (e2.x * s2.y) - (s2.x * e2.y);

float c = (a1 * b2 - a2 * b1);

float x = (b1 * c2 - b2 * c1) / c;
float y = (a2 * c1 - a1 * c2) / c;

return new Vector3(x, y, 0);
}

void UpdateRadarChart()
{
Vector3[] tmps = new Vector3[vertices.Length];
Vector3[] tmpo = new Vector3[vertices.Length];

float lineWidth = outLineSize;
Vector2 cur, next, pre, da, db, na, nb;
for (int i = 0; i < vertices.Length; i++)
{
tmps[i] = vertices[i] * vertices[i].z * scale;

// 描边顶点
cur = GetCurVec(i);
next = GetNextVec(i);
pre = GetPreVec(i);
da = (cur - pre).normalized;
db = (cur - next).normalized;
na = GetNormal(da);
nb = GetNormal(-db);
tmpo[i] = this.GetIntersect(pre + (na * lineWidth), da, next + (nb * lineWidth), db);
}
// 描边顶点
vec3OutLineList = tmpo;
DrawOutLine();
// 正常顶点
mesh.vertices = tmps;
mesh.triangles = triangles;
meshFilter.mesh = mesh;


}
}

或者可以弄个纹理贴图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
public class WuJiaoXingRender : MonoBehaviour
{
public static Vector3 posC = new Vector3(0, 0, 0); //中心
public static Vector3 pos1 = new Vector3(0, 1, 0); //最上
public static Vector3 pos2 = new Vector3(1f, 0.236f, 0); //右上
public static Vector3 pos3 = new Vector3(0.627f, -1f, 0); //右下
public static Vector3 pos4 = new Vector3(-0.627f, -1f, 0); //左下
public static Vector3 pos5 = new Vector3(-1f, 0.236f, 0); //左上

public static Vector2 uvC = new Vector2(0.5f, 0.5f); //中心
public static Vector2 uv1 = new Vector2(0.5f, 1f); //最上
public static Vector2 uv2 = new Vector2(1f, 1 - 0.38f); //右上
public static Vector2 uv3 = new Vector2(0.8f, 1 - 1); //右下
public static Vector2 uv4 = new Vector2(0.2f, 1 - 1); //左下
public static Vector2 uv5 = new Vector2(0f, 1 - 0.38f); //左上

private bool isInit = false;

public Texture2D texture; //五角星贴图
[Range(0.1f,1)]
public float rate1 = 1f;
[Range(0.1f, 1)]
public float rate2 = 1f;
[Range(0.1f, 1)]
public float rate3 = 1f;
[Range(0.1f, 1)]
public float rate4 = 1f;
[Range(0.1f, 1)]
public float rate5 = 1f;

private MeshFilter meshFilter;
private MeshRenderer meshRender;
private Mesh mesh;

private Vector2[] uvs = new Vector2[15];
private Vector3[] verts = new Vector3[15];
private int[] indexes = new int[15];
private Color[] colors = new Color[15];

private Vector2[] initUvs = new Vector2[15];
private Vector3[] initVerts = new Vector3[15];

// Use this for initialization
void Start()
{
if (isInit)
return;
if (meshFilter == null)
meshFilter = this.gameObject.AddComponent<MeshFilter>();
if (mesh == null)
{
CreateMesh();
meshFilter.sharedMesh = mesh;
}
meshRender = this.gameObject.AddComponent<MeshRenderer>();
meshRender.sharedMaterial = new Material(Shader.Find("Unlit/WuJiaoXingRender"));
meshRender.sharedMaterial.SetTexture("_MainTex", texture);
isInit = true;
}



#if UNITY_EDITOR
[ContextMenu("CreateEditor")]
private void CreateEditor()
{
Start();
}
#endif

private void CreateMesh()
{
verts[0] = pos1;
verts[1] = pos2;
verts[2] = posC;

verts[3] = pos2;
verts[4] = pos3;
verts[5] = posC;

verts[6] = pos3;
verts[7] = pos4;
verts[8] = posC;

verts[9] = pos4;
verts[10] = pos5;
verts[11] = posC;

verts[12] = pos5;
verts[13] = pos1;
verts[14] = posC;

uvs[0] = uv1;
uvs[1] = uv2;
uvs[2] = uvC;

uvs[3] = uv2;
uvs[4] = uv3;
uvs[5] = uvC;

uvs[6] = uv3;
uvs[7] = uv4;
uvs[8] = uvC;

uvs[9] = uv4;
uvs[10] = uv5;
uvs[11] = uvC;

uvs[12] = uv5;
uvs[13] = uv1;
uvs[14] = uvC;

for (int i = 0; i < 15; i++)
{
initUvs[i] = uvs[i];
initVerts[i] = verts[i];
colors[i] = Color.red;
indexes[i] = i;
}

mesh = new Mesh();
mesh.name = "WuJiaoXingRender";
mesh.vertices = verts;
mesh.colors = colors;
mesh.uv = uvs;
mesh.SetTriangles(indexes, 0);
}

// Update is called once per frame
void Update()
{
if (!isInit)
return;

updateRate(0, rate1);
updateRate(1, rate2);
updateRate(2, rate3);
updateRate(3, rate4);
updateRate(4, rate5);
}

private void updateRate(int index, float rate)
{
int outIndex1 = index * 3;
int outIndex2 = index * 3 - 2;
if (outIndex2 < 0)
{
outIndex2 += 15;
}
Vector3 pos = initVerts[outIndex1];
pos *= rate;
verts[outIndex1] = pos;
pos = initVerts[outIndex2];
pos *= rate;
verts[outIndex2] = pos;
mesh.vertices = verts;
}
}


可以参考下 我写了个粗糙版