private void AddSegment(Vector3[] points, int edgeLoops) { OrientedPoint[] path = new OrientedPoint[edgeLoops + 1]; CubicBezier3D bezier = new CubicBezier3D(points); GameObject segment = Instantiate(map.navMap.roadPrefab, transform.position, Quaternion.identity) as GameObject; Vector3? prev = null, curr = null; for (int i = 0; i <= edgeLoops; i++) { float t = (1f / edgeLoops) * i; prev = curr; curr = bezier.GetPoint(t); waypoints.Add((Vector3)curr); if (prev != null) { cost += ((Vector3)(curr - prev)).magnitude; } path[i] = new OrientedPoint((Vector3)curr, bezier.GetOrientation3D(t, Vector3.up)); } segment.transform.parent = transform; segment.name = gameObject.name + String.Format("-{0}", segments.Count); segments.Add(segment); paths.Add(path); }
void GenerateMesh() { ExtrudeShape shape = new ExtrudeShape(); Vertex2D[] verts = new Vertex2D[points.Length]; for (int i = 0; i < points.Length; ++i) { verts[i] = new Vertex2D(); verts[i].point = points[i].position; if (i == points.Length - 1) { Vector2 direction = points[i].position - points[i - 1].position; verts[i].normal = new Vector2(-direction.y, direction.x); } else { Vector2 direction = points[i + 1].position - points[i].position; verts[i].normal = new Vector2(-direction.y, direction.x); } verts[i].uCoord = 1.0f * i / (points.Length - 1); } int[] lines = new int[(points.Length - 1) * 2]; for (int i = 0; i < points.Length - 1; ++i) { lines[i * 2] = i; lines[i * 2 + 1] = i + 1; } shape.vert2Ds = verts; shape.lines = lines; Vector3[] controlPoints = { p0.position, p1.position, p2.position, p3.position, }; OrientedPoint[] path = new OrientedPoint[segmentCount + 1]; Vector3 lastPoint = Vector3.zero; for (int i = 0; i <= segmentCount; ++i) { float t = (float)i / segmentCount; Vector3 position = CubicBezier3D.GetPoint(controlPoints, t); Quaternion rotation = CubicBezier3D.GetOrientation3D(controlPoints, t, Vector3.up); path[i] = new OrientedPoint(position, rotation); Vector3 normal = CubicBezier3D.GetNormal3D(controlPoints, t, Vector3.up); //debug info // Debug.DrawLine(position, position + normal * 5, Color.blue); if (i != 0) { Debug.DrawLine(lastPoint, position, Color.green); } lastPoint = position; } arr = new float[2]; CalcLengthTableInto(arr, controlPoints); Extrude(mesh, shape, path); }