-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathRoadMeshCreator.cs
More file actions
199 lines (166 loc) · 7.52 KB
/
RoadMeshCreator.cs
File metadata and controls
199 lines (166 loc) · 7.52 KB
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
using System.Collections.Generic;
using UnityEngine;
using PathCreation.Utility;
namespace PathCreation.Examples
{
public class RoadMeshCreator : PathSceneTool
{
[Header("Road settings")]
public float roadWidth = .4f;
[Range(0, .5f)]
public float thickness = .15f;
public bool flattenSurface;
public bool meshCollider;
[Header("Material settings")]
public Material roadMaterial;
public Material undersideMaterial;
public float textureTiling = 1;
MeshFilter meshFilter;
MeshRenderer meshRenderer;
Transform meshHolder;
protected override void PathUpdated()
{
if (pathCreator != null)
{
AssignMeshComponents();
AssignMaterials();
meshFilter.mesh = CreateRoadMesh();
UpdateMeshCollider(meshCollider);
}
}
Mesh CreateRoadMesh()
{
Vector3[] verts = new Vector3[path.NumVertices * 8];
Vector2[] uvs = new Vector2[verts.Length];
Vector3[] normals = new Vector3[verts.Length];
int numTris = 2 * (path.NumVertices - 1) + ((path.isClosedLoop) ? 2 : 0);
int[] roadTriangles = new int[numTris * 3];
int[] underRoadTriangles = new int[numTris * 3];
int[] sideOfRoadTriangles = new int[numTris * 2 * 3];
int vertIndex = 0;
int triIndex = 0;
// Vertices for the top of the road are layed out:
// 0 1
// 8 9
// and so on... So the triangle map 0,8,1 for example, defines a triangle from top left to bottom left to bottom right.
int[] triangleMap = { 0, 8, 1, 1, 8, 9 };
int[] sidesTriangleMap = { 4, 6, 14, 12, 4, 14, 5, 15, 7, 13, 15, 5 };
bool usePathNormals = !(path.space == PathSpace.xyz && flattenSurface);
for (int i = 0; i < path.NumVertices; i++)
{
Vector3 localUp = (usePathNormals) ? Vector3.Cross(path.tangents[i], path.normals[i]) : path.up;
Vector3 localRight = (usePathNormals)?path.normals[i]:Vector3.Cross(localUp,path.tangents[i]);
// Find position to left and right of current path vertex
Vector3 vertSideA = path.vertices[i] - localRight * Mathf.Abs(roadWidth) -transform.position;
Vector3 vertSideB = path.vertices[i] + localRight * Mathf.Abs(roadWidth) - transform.position;
// Add top of road vertices
verts[vertIndex + 0] = vertSideA;
verts[vertIndex + 1] = vertSideB;
// Add bottom of road vertices
verts[vertIndex + 2] = vertSideA - localUp * thickness;
verts[vertIndex + 3] = vertSideB - localUp * thickness;
// Duplicate vertices to get flat shading for sides of road
verts[vertIndex + 4] = verts[vertIndex + 0];
verts[vertIndex + 5] = verts[vertIndex + 1];
verts[vertIndex + 6] = verts[vertIndex + 2];
verts[vertIndex + 7] = verts[vertIndex + 3];
// Set uv on y axis to path time (0 at start of path, up to 1 at end of path)
uvs[vertIndex + 0] = new Vector2(0, path.times[i]);
uvs[vertIndex + 1] = new Vector2(1, path.times[i]);
// Top of road normals
normals[vertIndex + 0] = localUp;
normals[vertIndex + 1] = localUp;
// Bottom of road normals
normals[vertIndex + 2] = -localUp;
normals[vertIndex + 3] = -localUp;
// Sides of road normals
normals[vertIndex + 4] = -localRight;
normals[vertIndex + 5] = localRight;
normals[vertIndex + 6] = -localRight;
normals[vertIndex + 7] = localRight;
// Set triangle indices
if (i < path.NumVertices - 1 || path.isClosedLoop)
{
for (int j = 0; j < triangleMap.Length; j++)
{
roadTriangles[triIndex + j] = (vertIndex + triangleMap[j]) % verts.Length;
// reverse triangle map for under road so that triangles wind the other way and are visible from underneath
underRoadTriangles[triIndex + j] = (vertIndex + triangleMap[triangleMap.Length - 1 - j] + 2) % verts.Length;
}
for (int j = 0; j < sidesTriangleMap.Length; j++)
{
sideOfRoadTriangles[triIndex * 2 + j] = (vertIndex + sidesTriangleMap[j]) % verts.Length;
}
}
vertIndex += 8;
triIndex += 6;
}
Mesh mesh = new Mesh();
mesh.vertices = verts;
mesh.uv = uvs;
mesh.normals = normals;
mesh.subMeshCount = 3;
mesh.SetTriangles(roadTriangles, 0);
mesh.SetTriangles(underRoadTriangles, 1);
mesh.SetTriangles(sideOfRoadTriangles, 2);
mesh.RecalculateBounds();
return mesh;
}
// Add MeshRenderer and MeshFilter components to this gameobject if not already attached
void AssignMeshComponents()
{
// Find/creator mesh holder object in children
string meshHolderName = "Mesh Holder";
meshHolder = transform.Find(meshHolderName);
if (meshHolder == null) {
meshHolder = new GameObject(meshHolderName).transform;
meshHolder.transform.parent = transform;
meshHolder.transform.localPosition = Vector3.zero;
}
//meshHolder.transform.position = Vector3.zero;
meshHolder.transform.rotation = Quaternion.identity;
// Ensure mesh renderer and filter components are assigned
if (!meshHolder.gameObject.GetComponent<MeshFilter>())
{
meshHolder.gameObject.AddComponent<MeshFilter>();
}
if (!meshHolder.GetComponent<MeshRenderer>())
{
meshHolder.gameObject.AddComponent<MeshRenderer>();
}
meshRenderer = meshHolder.GetComponent<MeshRenderer>();
meshFilter = meshHolder.GetComponent<MeshFilter>();
}
void AssignMaterials()
{
if (roadMaterial != null && undersideMaterial != null)
{
meshRenderer.sharedMaterials = new Material[] { roadMaterial, undersideMaterial, undersideMaterial };
meshRenderer.sharedMaterials[0].mainTextureScale = new Vector3(1, textureTiling);
}
}
//update meshcolider if enabled
void UpdateMeshCollider(bool meshColliderEnable)
{
if (meshHolder != null)
{
MeshCollider meshCol = meshHolder.GetComponent<MeshCollider>();
if (meshCol != null)
{
if (meshColliderEnable)
{
meshCol.sharedMesh = meshHolder.GetComponent<MeshFilter>().sharedMesh;
}
else
{
DestroyImmediate(meshCol);
}
}
else if (meshColliderEnable)
{
meshHolder.gameObject.AddComponent<MeshCollider>();
}
}
}
}
}