Mesh GenerateFloor(EditorRoom room) { Vector3[] verticies = room.Points.Select(x => points[x]).ToArray(); Triangulator tr = new Triangulator(verticies.Select(x => new Vector2(x.x, x.z)).ToArray()); int[] indicies = tr.Triangulate(); Mesh mesh = new Mesh(); mesh.vertices = verticies; mesh.triangles = indicies; mesh.RecalculateNormals(); mesh.RecalculateBounds(); return(mesh); }
void DrawRoom(EditorRoom room, int index) { for (int i = 1; i < room.Points.Count; i++) { int p1 = room.Points[i - 1]; int p2 = room.Points[i]; Handles.color = Color.white; if (IsDoubleSided(p1, p2)) { Handles.color = Color.black; } Handles.DrawLine(points[p1], points[p2]); } int lastP1 = room.Points[room.Points.Count - 1]; int lastP2 = room.Points[0]; Handles.color = Color.white; if (IsDoubleSided(lastP1, lastP2)) { Handles.color = Color.black; } Handles.DrawLine(points[lastP1], points[lastP2]); Handles.color = new Color(0.18f, 0.68f, 0.7f, 0.25f); // Light blue if (state == RoomState.SetSpawn && index == GetClosestRoom()) { Handles.color = new Color(0.89f, 0.92f, 0.18f, 0.25f); // Light yellow } if (state != RoomState.SetSpawn && index == spawnRoom) { Handles.color = new Color(0.89f, 0.92f, 0.18f, 0.25f); // Light yellow } if (selectedRoom == index) { Handles.color = new Color(0.89f, 0.02f, 0.18f, 0.25f); // Light yellow } Handles.DrawAAConvexPolygon(room.Points.Select(x => points[x]).ToArray()); Handles.color = Color.white; }
void MouseDownEvent() { if (Event.current.button == 1) { state = RoomState.Move; selectedNodes.Clear(); } switch (state) { case RoomState.AddPoint: if (canClick) { int closest = GetClosestPoint(); Vector3 point = default; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); Plane plane = new Plane(Handles.matrix * Vector3.up, Handles.matrix.MultiplyPoint(Vector3.zero)); plane.Raycast(ray, out float dist); if (dist > 0) { point = ray.GetPoint(dist); } if (Event.current.control) { point = new Vector3(Mathf.Round(point.x), 0, Mathf.Round(point.z)); } points.Add(point); } canClick = false; if (!Event.current.shift) { state = RoomState.Move; } break; case RoomState.RemovePoint: if (canClick) { points.RemoveAt(GetClosestPoint()); } canClick = false; if (!Event.current.shift) { state = RoomState.Move; } break; case RoomState.CreateRoom: if (canClick) { int index = GetClosestPoint(); if (!selectedNodes.Contains(index)) { selectedNodes.Add(index); } if (index == selectedNodes[0] && selectedNodes.Count > 1) { Vector3 roomPos = selectedNodes.Select(x => points[x]).Aggregate((x, y) => x + y) / selectedNodes.Count; EditorRoom room = new EditorRoom(roomPos); room.AddRange(selectedNodes); rooms.Add(room); selectedNodes.Clear(); state = RoomState.Move; } } canClick = false; break; case RoomState.AddDoor: if (canClick) { var closesPoint = GetClosestLinePoint(); int p1 = closesPoint.index; int p2 = closesPoint.index - 1; if (p2 < 0) { p2 = closesPoint.room.Points.Count - 1; } var rooms = GetWallRooms(closesPoint.room.Points[p1], closesPoint.room.Points[p2]); doors.Add(new EditorDoor(rooms.r1, rooms.r2, closesPoint.room.Points[p1], closesPoint.room.Points[p2], closesPoint.p)); } state = RoomState.Move; canClick = false; break; case RoomState.RemoveDoor: if (canClick) { doors.RemoveAt(GetClosestDoor()); } canClick = false; if (!Event.current.shift) { state = RoomState.Move; } break; case RoomState.SetSpawn: spawnRoom = GetClosestRoom(); state = RoomState.Move; break; default: state = RoomState.Move; break; } }
public override void OnInspectorGUI() { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add point")) { state = RoomState.AddPoint; } if (GUILayout.Button("Remove point")) { state = RoomState.RemovePoint; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add door")) { state = RoomState.AddDoor; } if (GUILayout.Button("Remove door")) { state = RoomState.RemoveDoor; } EditorGUILayout.EndHorizontal(); if (GUILayout.Button("Create Room")) { state = RoomState.CreateRoom; } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Generate mesh")) { GenerateMesh(); } if (GUILayout.Button("Clear mesh")) { ClearMesh(); } EditorGUILayout.EndHorizontal(); for (int i = 0; i < rooms.Count; i++) { EditorRoom room = rooms[i]; EditorGUILayout.BeginHorizontal(); GUILayout.Label($"Room {i}: {room.Points.Count} verticies"); if (GUILayout.Button("P")) { if (selectedRoom == i) { selectedRoom = -1; } else { selectedRoom = i; } } if (GUILayout.Button("Remove")) { rooms.RemoveAt(i); doors.RemoveAt(doors.FindIndex(x => x.Room1 == i || x.Room2 == i)); } EditorGUILayout.EndHorizontal(); } if (rooms.Count > 0) { if (GUILayout.Button("Set spawn")) { state = RoomState.SetSpawn; } } if (meshGenerated && GUILayout.Button("Finish Mesh")) { FinishMesh(); } GUILayout.Space(10); GUILayout.Label($"Minimap"); EditorGUILayout.BeginHorizontal(); GUILayout.Label($"Size: "); minimapWidth = EditorGUILayout.IntField(minimapWidth); minimapHeight = EditorGUILayout.IntField(minimapHeight); EditorGUILayout.EndHorizontal(); minimapPadding = EditorGUILayout.IntField("Padding", minimapPadding); minimapWallThickness = EditorGUILayout.IntField("Wall Thickness", minimapWallThickness); if (GUILayout.Button("Generate minimap")) { Texture2D minimap = GenerateMinimap(); AssetDatabase.DeleteAsset($"Assets/Minimap.png"); File.WriteAllBytes($"{Application.dataPath}/Minimap.png", minimap.EncodeToPNG()); AssetDatabase.Refresh(); } }