private void ConstructMesh() { trackMesh.Clear(); List <Vector2> texCoords = new List <Vector2> (); List <int> indices = new List <int> (); meshCoords = new List <Vector3> (); CatmullRom CRS = new CatmullRom(diffCoords); int diffCoordsSize = CRS.pts.Count; float step = 1.0f / (diffCoordsSize * smoothness); float iterator = 0.0f; List <Vector3> outerCoords = new List <Vector3> (); List <Vector3> innerCoords = new List <Vector3> (); for (int i = 0; i < diffCoordsSize * smoothness; i++) { outerCoords.Add(CRS.DisplaceBy(trackWidth, iterator, step)); innerCoords.Add(CRS.Interpolate(iterator)); iterator += step; } WeldOverlappingSections(outerCoords, innerCoords); for (int i = 0; i < diffCoordsSize * smoothness; i++) { meshCoords.Add(innerCoords [i]); meshCoords.Add(outerCoords [i]); meshCoords.Add(innerCoords [i]); meshCoords.Add(outerCoords [i]); texCoords.Add(new Vector2(0, 0)); texCoords.Add(new Vector2(1, 0)); texCoords.Add(new Vector2(0, 1)); texCoords.Add(new Vector2(1, 1)); } int meshCount = meshCoords.Count; for (int i = 0; i < meshCount; i++) { // Tri 1 indices.Add(i % meshCount); indices.Add((i + 2) % meshCount); indices.Add((i + 1) % meshCount); // Tri 2 indices.Add((i + 2) % meshCount); indices.Add((i + 3) % meshCount); indices.Add((i + 1) % meshCount); } trackMesh.vertices = meshCoords.ToArray(); trackMesh.uv = texCoords.ToArray(); trackMesh.RecalculateNormals(); trackMesh.Optimize(); trackMesh.SetIndices(indices.ToArray(), MeshTopology.LineStrip, 0); }