-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineHexagon.cs
104 lines (80 loc) · 2.88 KB
/
LineHexagon.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class HexHelper
{
public static int[] CreateLineStripIndices(int length, bool loop)
{
int segmentCount = loop ? length : (length - 1);
int[] result = new int[segmentCount * 6];
int k = 0;
for(int i = 0; i < segmentCount; i++)
{
result[k++] = i;
result[k++] = i + length;
result[k++] = (i + 1) % length;
result[k++] = (i + 1) % length;
result[k++] = i + length;
result[k++] = length + ((i + 1) % length);
}
return result;
}
public static Vector3[] getCorners(float width)
{
return corners.Select(c => width * c).ToArray();
}
public static Vector3[] corners = {
new Vector3(0f, 0f, (1.0f / 0.866025404f)),
new Vector3(1.0f, 0f, 0.5f * (1.0f / 0.866025404f)),
new Vector3(1.0f, 0f, -0.5f * (1.0f / 0.866025404f)),
new Vector3(0f, 0f, -(1.0f / 0.866025404f)),
new Vector3(-1.0f, 0f, -0.5f * (1.0f / 0.866025404f)),
new Vector3(-1.0f, 0f, 0.5f * (1.0f / 0.866025404f))
};
internal static Vector3[] CreateLineStripVertices(Vector3[] lineStrip, float thickness)
{
Vector3[] result = new Vector3[lineStrip.Length * 2];
for (int i = 0; i < lineStrip.Length; i ++)
{
result[i] = shift(lineStrip, i, thickness);
}
for (int i = 0; i < lineStrip.Length; i++)
{
result[i + lineStrip.Length] = shift(lineStrip, i, -thickness);
}
return result;
}
private static Vector3 shift(Vector3[] lineStrip, int p, float thickness)
{
int prev = NeverdawnUtility.RepeatIndex(p - 1, lineStrip.Length);
int next = NeverdawnUtility.RepeatIndex(p + 1, lineStrip.Length);
Vector3 shiftDirection = lineStrip[next] - lineStrip[prev];
shiftDirection.y = 0.0f;
shiftDirection.Normalize();
shiftDirection = Quaternion.Euler(0.0f, 90.0f, 0.0f) * shiftDirection;
return lineStrip[p] + thickness * shiftDirection;
}
}
[RequireComponent(typeof(MeshFilter))]
public class LineHexagon : MonoBehaviour {
private Mesh mesh;
private MeshFilter filter;
public float thickness;
public float scale;
// Use this for initialization
public void CreateMesh() {
filter = GetComponent<MeshFilter>();
mesh = new Mesh();
mesh.vertices = HexHelper.CreateLineStripVertices(HexHelper.getCorners(.5f * scale), thickness / 2.0f);
mesh.SetIndices(HexHelper.CreateLineStripIndices(6, true), MeshTopology.Triangles, 0);
filter.mesh = mesh;
}
public Vector3[] worldSpaceCorners
{
get
{
return HexHelper.getCorners(.5f * scale).Select(c => transform.TransformPoint(c)).Select(v => new Vector3(v.x, 0.0f, v.z)).ToArray();
}
}
}