public static Vector3 GetHeight(CenterRoadWnd.RoadMeshHeightType roadHeightType, Vector3 point) { switch (roadHeightType) { case CenterRoadWnd.RoadMeshHeightType.MESH_HEIGHT_GROUND_LAYER: { Vector3 rayOrigPoint = point + Vector3.up * 500; RaycastHit hitinfo; if (Physics.Raycast(rayOrigPoint, Vector3.down, out hitinfo, 1000, LayerDefine.layerMask)) { return(hitinfo.point); } break; } case CenterRoadWnd.RoadMeshHeightType.MESH_HEIGHT_NAVIGATION: { Vector3 rayOrigPoint = point + Vector3.up * 500; RaycastHit hitinfo; if (Physics.Raycast(rayOrigPoint, Vector3.down, out hitinfo, 1000, 1 << LayerDefine.Invisible)) { return(hitinfo.point); } break; } case CenterRoadWnd.RoadMeshHeightType.MESH_HEIGHT_POINT: { break; } } return(point); }
public static void AddOneMesh(Vector3 start, Vector3 end, float width, int count, List <Vector3> _vertices, List <int> _triangles, CenterRoadWnd.RoadMeshHeightType roadHeightType) { int segment = count + 1; Vector3 normal = Vector3.Cross((end - start), Vector3.up).normalized; Vector3[] vertices = new Vector3[segment * 2]; for (int i = 0; i < segment; i++) { vertices[i * 2] = Vector3.Lerp(start, end, i / (float)count) + normal * width; vertices[i * 2 + 1] = Vector3.Lerp(start, end, i / (float)count) - normal * width; vertices[i * 2] = GetHeight(roadHeightType, vertices[i * 2]); vertices[i * 2 + 1] = GetHeight(roadHeightType, vertices[i * 2 + 1]); } int[] triangles = new int[count * 6]; for (int i = 0; i < count; i++) { triangles[i * 6] = _vertices.Count + i * 2; triangles[i * 6 + 1] = _vertices.Count + i * 2 + 2; triangles[i * 6 + 2] = _vertices.Count + i * 2 + 1; triangles[i * 6 + 3] = _vertices.Count + i * 2 + 1; triangles[i * 6 + 4] = _vertices.Count + i * 2 + 2; triangles[i * 6 + 5] = _vertices.Count + i * 2 + 3; } _vertices.AddRange(vertices); _triangles.AddRange(triangles); }
//[MenuItem("Tools/路点/创建路点mesh")] //static void CreateNodeMesh() //{ // //CopyAllNavigation(FindAllNavigationMesh()); // AstarPath aStarPath = GameObject.FindObjectOfType<AstarPath>(); // if (aStarPath != null) // { // Transform nodeRoot = aStarPath.transform.Find("NodeRoot"); // if (nodeRoot) // { // int count = 1; // //List<Vector3> offsets = new List<Vector3>() { Vector3.zero, -Vector3.left * 0.5f, Vector3.left * 0.5f, -Vector3.forward * 0.5f, Vector3.forward * 0.5f, }; // List<float> offsets = new List<float>() { 0, -0.3f, 0.3f, -0.6f, 0.6f }; // float height = 0.0f; // for (int j = 0; j < count; j++) // { // GameObject road_layer = new GameObject("road_layer" + j.ToString()); // NodeLink[] nodeLinks = nodeRoot.GetComponentsInChildren<NodeLink>(); // //单独创建mesh // //for (int i = 0; i < nodeLinks.Length; i++) // //{ // // if (nodeLinks[i].Start != null && nodeLinks[i].End != null) // // { // // Vector3 normal = Vector3.Cross((nodeLinks[i].End.position - nodeLinks[i].Start.position), Vector3.up).normalized; // // GameObject road = CreateMesh(nodeLinks[i].Start.position + offsets[j] * normal, nodeLinks[i].End.position + offsets[j] * normal); // // road.transform.parent = road_layer.transform; // // GameObjectUtility.SetStaticEditorFlags(road, StaticEditorFlags.NavigationStatic); // // GameObjectUtility.SetNavMeshArea(road, 3 + j); // // } // //} // int maxVerticesCount = 10000; // List<Vector3> vertices = new List<Vector3>(); // List<int> triangles = new List<int>(); // for (int i = 0; i < nodeLinks.Length; i++) // { // if (nodeLinks[i].Start != null && nodeLinks[i].End != null) // { // Vector3 normal = Vector3.Cross((nodeLinks[i].End.position - nodeLinks[i].Start.position), Vector3.up).normalized; // AddOneMesh(nodeLinks[i].Start.position + offsets[j] * normal, nodeLinks[i].End.position + offsets[j] * normal, 0.5f, 100, vertices, triangles); // if (i == nodeLinks.Length - 1 || vertices.Count > maxVerticesCount) // { // GameObject road = CreateMesh(vertices, triangles); // road.transform.parent = road_layer.transform; // GameObjectUtility.SetStaticEditorFlags(road, StaticEditorFlags.NavigationStatic); // GameObjectUtility.SetNavMeshArea(road, 3 + j); // road_layer.transform.position = new Vector3(0, height, 0); // vertices.Clear(); // triangles.Clear(); // } // } // } // } // } // } // UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()); //} #endregion public static GameObject CreateMesh(Vector3 start, Vector3 end, float width, int count, CenterRoadWnd.RoadMeshHeightType roadHeightType) { int segment = count + 1; Vector3 normal = Vector3.Cross((end - start), Vector3.up).normalized; Vector3[] vertices = new Vector3[segment * 2]; for (int i = 0; i < segment; i++) { vertices[i * 2] = Vector3.Lerp(start, end, i / (float)count) + normal * width; vertices[i * 2 + 1] = Vector3.Lerp(start, end, i / (float)count) - normal * width; vertices[i * 2] = GetHeight(roadHeightType, vertices[i * 2]); vertices[i * 2 + 1] = GetHeight(roadHeightType, vertices[i * 2 + 1]); } int[] triangles = new int[count * 6]; for (int i = 0; i < count; i++) { triangles[i * 6] = i * 2; triangles[i * 6 + 1] = i * 2 + 2; triangles[i * 6 + 2] = i * 2 + 1; triangles[i * 6 + 3] = i * 2 + 1; triangles[i * 6 + 4] = i * 2 + 2; triangles[i * 6 + 5] = i * 2 + 3; } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); GameObject go = new GameObject(); MeshFilter meshFilter = go.AddComponent <MeshFilter>(); meshFilter.mesh = mesh; MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>(); return(go); }