ScenePartition/Editor/ScenePartitionSceneViewEditor.cs

99 lines
4.1 KiB
C#
Raw Normal View History

2023-06-29 00:20:01 +02:00
using UnityEditor;
using UnityEngine;
2023-07-03 00:47:53 +02:00
namespace VertexColor.ScenePartition.Editor
2023-06-29 00:20:01 +02:00
{
2023-07-03 00:47:53 +02:00
[InitializeOnLoad]
public class ScenePartitionSceneViewEditor : UnityEditor.Editor
2023-06-29 00:20:01 +02:00
{
public static int cellSize = 100;
private static ScenePartitionSO scenePartitionSO = null;
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
static ScenePartitionSceneViewEditor()
{
SceneView.duringSceneGui += HandleDuringSceneGui;
}
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
private static void HandleDuringSceneGui(SceneView sceneView)
{
if (Event.current.modifiers != EventModifiers.Control) return;
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out scenePartitionSO)) return;
2023-07-03 00:47:53 +02:00
Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition);
int gridId = SceneGrid.CalculateGridPosition(gridPosition, cellSize);
{ // Draw Cells.
foreach (int id in scenePartitionSO.Data.SceneGrid.Grid.Keys)
{
(int x, int y) = SceneGrid.IntToIntPair(id);
Vector3 cellOrign = new Vector3(x * cellSize, 0, y * cellSize);
Vector3[] lines = new Vector3[] {
new Vector3(0, 0, 0) + cellOrign, new Vector3(0, 0, cellSize) + cellOrign, // Left Bottom -> Left Top
new Vector3(cellSize, 0, 0) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Right Bottom -> Right Top
new Vector3(0, 0, 0) + cellOrign, new Vector3(cellSize, 0, 0) + cellOrign, // Left Bottom -> Right Bottom
new Vector3(0, 0, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Left Top -> Right Top
};
Handles.DrawLines(lines);
}
}
if (!scenePartitionSO.Data.SceneGrid.Grid.ContainsKey(gridId)) return;
2023-06-29 00:20:01 +02:00
{ // Draw Selection.
int x = Mathf.FloorToInt(gridPosition.x / cellSize);
int z = Mathf.FloorToInt(gridPosition.z / cellSize);
2023-06-29 00:20:01 +02:00
Vector3 cellOrign = new Vector3(x * cellSize, 0, z * cellSize);
Vector3[] lines = new Vector3[] {
new Vector3(0, 0, 0) + cellOrign, new Vector3(0, 0, cellSize) + cellOrign, // Left Bottom -> Left Top
new Vector3(cellSize, 0, 0) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Right Bottom -> Right Top
new Vector3(0, 0, 0) + cellOrign, new Vector3(cellSize, 0, 0) + cellOrign, // Left Bottom -> Right Bottom
new Vector3(0, 0, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Left Top -> Right Top
};
2023-06-29 00:20:01 +02:00
Color c = Handles.color;
Handles.color = Color.yellow;
Handles.DrawLines(lines);
Handles.color = c;
}
2023-06-29 00:20:01 +02:00
// Context menu to load.
2023-07-03 00:47:53 +02:00
if (Event.current.button == 1 && Event.current.type == EventType.MouseDown)
{
GenericMenu menu = new GenericMenu();
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
menu.AddItem(new GUIContent($"Load {gridId}"), false, Load, gridId);
menu.ShowAsContext();
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
Event.current.Use();
}
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
SceneView.RepaintAll();
}
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
private static Vector3 CalculateGridPosition(Vector2 mousePosition)
{
if (Event.current == null) return Vector3.zero;
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
Plane plane = new Plane(Vector3.up, Vector3.zero); // Horizontal plane at y = 0
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
if (!plane.Raycast(ray, out float distance)) return Vector3.zero;
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
Vector3 intersectionPoint = ray.GetPoint(distance);
return intersectionPoint;
}
2023-06-29 00:20:01 +02:00
2023-07-03 00:47:53 +02:00
private static void Load(object gridId)
{
if (scenePartitionSO == null) return;
2023-07-03 00:47:53 +02:00
if (gridId == null) return;
if (gridId is not int cellId) return;
2023-06-29 00:20:01 +02:00
scenePartitionSO.LoadCell(cellId);
2023-07-03 00:47:53 +02:00
}
2023-06-29 00:20:01 +02:00
}
}