public override void GetVertexs(ref List <UIVertex> vertexs) { if (m_Poses.Count < 2) { return; } int count = m_Poses.Count; Vector2 dir = Math2d.GetHorizontalDir((m_Poses[1] - m_Poses[0]).XY()); vertexs.Add(new UIVertex() { position = m_Poses[0].XY() - dir * width, color = color, }); vertexs.Add(new UIVertex() { position = m_Poses[0].XY() + dir * width, color = color, }); for (int i = 1; i < count - 1; i++) { dir = Math2d.GetHorizontalDir((m_Poses[i + 1] - m_Poses[i - 1]).XY()); vertexs.Add(new UIVertex() { position = m_Poses[i].XY() - dir * width, color = color, }); vertexs.Add(new UIVertex() { position = m_Poses[i].XY() + dir * width, color = color }); } dir = Math2d.GetHorizontalDir((m_Poses[count - 1] - m_Poses[count - 2]).XY()); vertexs.Add(new UIVertex() { position = m_Poses[count - 1].XY() - dir * width, color = color, }); vertexs.Add(new UIVertex() { position = m_Poses[count - 1].XY() + dir * width, color = color, }); }
public override void OnUpdate() { MouseLeft(); // 路面创建测试 if (Input.GetKeyDown(KeyCode.K)) { List <Vector3> points_out = new List <Vector3>(); List <Vector3> points_in = new List <Vector3>(); Vector3 dirr; dirr = Math2d.GetHorizontalDir(positions[1] - positions[0]); points_in.Add(positions[0] + dirr * 4); points_out.Add(positions[0] - dirr * 4); for (int i = 1; i < positions.Count - 1; i++) { dirr = Math2d.GetHorizontalDir(positions[i + 1] - positions[i - 1]); points_in.Add(positions[i] + dirr * 4); points_out.Add(positions[i] - dirr * 4); } dirr = Math2d.GetHorizontalDir(positions[positions.Count - 1] - positions[positions.Count - 2]); points_in.Add(positions[positions.Count - 1] + dirr * 4); points_out.Add(positions[positions.Count - 1] - dirr * 4); for (int i = 0; i < positions.Count; i++) { outCurve.AddNode(points_out[i], c); inCurve.AddNode(points_in[i], c); centerCurve.AddNode(positions[i], c); } outCurve.AddCatmull_RomControl(); inCurve.AddCatmull_RomControl(); centerCurve.AddCatmull_RomControl(); for (int i = 0; i < outCurve.segmentList.Count; i++) { float add = 1f / 20; for (float j = 0; j < 1; j += add) { Vector3 point = centerCurve.segmentList[i].GetPoint(j); path.Add(point); objs.Add(Utility.CreatPrimitiveType(PrimitiveType.Sphere, Color.red, point, 1)); //point = outCurve.segmentList[i].GetPoint(j); //path.Add(point); //objs.Add(Utility.CreatPrimitiveType(PrimitiveType.Sphere, point, 1, Color.red)); //point = inCurve.segmentList[i].GetPoint(j); //path.Add(point); //objs.Add(Utility.CreatPrimitiveType(PrimitiveType.Sphere, point, 1, Color.red)); } } CreateRoads(Terrain.activeTerrain, meshFilter, path, 6); } if (Input.GetKeyDown(KeyCode.C)) { meshFilter.mesh.Clear(); objs.ForEach((a) => { Object.Destroy(a); }); objs.Clear(); outCurve = new SplineCurve(); inCurve = new SplineCurve(); centerCurve = new SplineCurve(); positions.Clear(); path.Clear(); } if (Input.GetKeyDown(KeyCode.J)) { Utility.SendRay(out hit, LayerMask.GetMask("Terrain")); } // 刷新地图 if (Input.GetKeyDown(KeyCode.L)) { //TerrainUtility.Refresh(); } }
/// <summary> /// 根据传参 路径点信息 创建路面: /// 0 --- 2 /// | | /// 1 --- 3 /// </summary> /// <param name="meshFilter">路面网格</param> /// <param name="_roadPoints">路点</param> /// <param name="_width">路面宽度</param> public static void CreateRoads(Terrain terrain, MeshFilter meshFilter, List <Vector3> wayPoints, float _width = 5) { if (wayPoints.Count < 2) { return; // 路点数量不能低于2个 } List <Vector3> _roadPoints = wayPoints; // 取出路径点 List <Vector3> vertice = new List <Vector3>(); // 顶点 List <int> triangles = new List <int>(); // 三角形排序 List <Vector2> uv = new List <Vector2>(); // uv排序 Vector3 dir = Math2d.GetHorizontalDir(_roadPoints[1], _roadPoints[0]); // 获取两点间的垂直向量 vertice.Add(_roadPoints[0] + dir * _width); // 添加初始顶点 vertice.Add(_roadPoints[0] - dir * _width); uv.Add(Vector2.zero); // 添加初始顶点对应uv uv.Add(Vector2.right); for (int i = 1, count = _roadPoints.Count; i < count; i++) { // 添加由 路径点 生成的路面点集 dir = Math2d.GetHorizontalDir(_roadPoints[i], _roadPoints[i - 1]); vertice.Add(_roadPoints[i] + dir * _width); vertice.Add(_roadPoints[i] - dir * _width); // 添加三jio形排序 triangles.Add(2 * i - 2); triangles.Add(2 * i); triangles.Add(2 * i - 1); triangles.Add(2 * i); triangles.Add(2 * i + 1); triangles.Add(2 * i - 1); // 添加uv排序 if (i % 2 == 1) { uv.Add(Vector2.up); uv.Add(Vector2.one); } else { uv.Add(Vector2.zero); uv.Add(Vector2.right); } } List <float> roadHeights = PointsFitToTerrain(terrain, ref vertice); // 路面高度适配地形 //TerrainUtility.ChangeHeights(terrain, _roadPoints.ToArray(), roadHeights.ToArray()); // 将道路整平 meshFilter.mesh.Clear(); meshFilter.mesh.vertices = vertice.ToArray(); meshFilter.mesh.triangles = triangles.ToArray(); meshFilter.mesh.uv = uv.ToArray(); meshFilter.mesh.RecalculateBounds(); // 重置范围 meshFilter.mesh.RecalculateNormals(); // 重置法线 meshFilter.mesh.RecalculateTangents(); // 重置切线 }