generated from max/template-unity-project
SceneGrid SceneView test
This commit is contained in:
parent
c42a23d6fa
commit
1556f95485
@ -6,6 +6,8 @@ namespace VertexColor.ScenePartition.Editor
|
|||||||
{
|
{
|
||||||
public class ScenePartitionEditorWindow : EditorWindow
|
public class ScenePartitionEditorWindow : EditorWindow
|
||||||
{
|
{
|
||||||
|
private const int cellSize = 10;
|
||||||
|
|
||||||
private Vector2 scrollPos = Vector2.zero;
|
private Vector2 scrollPos = Vector2.zero;
|
||||||
|
|
||||||
[MenuItem("Max/ScenePartitionWindow")]
|
[MenuItem("Max/ScenePartitionWindow")]
|
||||||
|
69
Editor/ScenePartitionSceneViewEditor.cs
Normal file
69
Editor/ScenePartitionSceneViewEditor.cs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
using VertexColor.ScenePartition.Editor;
|
||||||
|
|
||||||
|
[InitializeOnLoad]
|
||||||
|
public class ScenePartitionSceneViewEditor : Editor
|
||||||
|
{
|
||||||
|
private const int cellSize = 10;
|
||||||
|
|
||||||
|
static ScenePartitionSceneViewEditor()
|
||||||
|
{
|
||||||
|
SceneView.duringSceneGui += HandleDuringSceneGui;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void HandleDuringSceneGui(SceneView sceneView)
|
||||||
|
{
|
||||||
|
if (Event.current.modifiers != EventModifiers.Control) return;
|
||||||
|
|
||||||
|
Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition);
|
||||||
|
int gridId = SceneGrid.CalculateGridPosition(gridPosition, 10);
|
||||||
|
|
||||||
|
int x = Mathf.FloorToInt(gridPosition.x / cellSize);
|
||||||
|
int z = Mathf.FloorToInt(gridPosition.z / cellSize);
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
Handles.DrawDottedLines(lines, 10.0f);
|
||||||
|
Handles.Label(cellOrign + new Vector3(cellSize * 0.5f, 0, cellSize * 0.5f), new GUIContent($"{gridId}"));
|
||||||
|
|
||||||
|
if (Event.current.button == 1 && Event.current.type == EventType.MouseDown)
|
||||||
|
{
|
||||||
|
GenericMenu menu = new GenericMenu();
|
||||||
|
|
||||||
|
menu.AddItem(new GUIContent($"Load {gridId}"), false, Load, gridId);
|
||||||
|
menu.ShowAsContext();
|
||||||
|
|
||||||
|
Event.current.Use();
|
||||||
|
}
|
||||||
|
|
||||||
|
SceneView.RepaintAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Vector3 CalculateGridPosition(Vector2 mousePosition)
|
||||||
|
{
|
||||||
|
if (Event.current == null) return Vector3.zero;
|
||||||
|
|
||||||
|
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
|
||||||
|
Plane plane = new Plane(Vector3.up, Vector3.zero); // Horizontal plane at y = 0
|
||||||
|
|
||||||
|
if (!plane.Raycast(ray, out float distance)) return Vector3.zero;
|
||||||
|
|
||||||
|
Vector3 intersectionPoint = ray.GetPoint(distance);
|
||||||
|
return intersectionPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Load(object gridId)
|
||||||
|
{
|
||||||
|
if (gridId == null) return;
|
||||||
|
if (gridId is not int gridPos) return;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
11
Editor/ScenePartitionSceneViewEditor.cs.meta
Normal file
11
Editor/ScenePartitionSceneViewEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 997bd9a76e0528c4a901941b5b70bc5e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -16,7 +16,7 @@ public class SceneGrid
|
|||||||
|
|
||||||
public void Insert(uint id, Vector3 point)
|
public void Insert(uint id, Vector3 point)
|
||||||
{
|
{
|
||||||
int gridId = CalculateGridPosition(point);
|
int gridId = CalculateGridPosition(point, cellSize);
|
||||||
if (grid.TryGetValue(gridId, out List<uint> ids))
|
if (grid.TryGetValue(gridId, out List<uint> ids))
|
||||||
{
|
{
|
||||||
ids.Add(id);
|
ids.Add(id);
|
||||||
@ -27,7 +27,7 @@ public void Insert(uint id, Vector3 point)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CalculateGridPosition(Vector3 point)
|
public static int CalculateGridPosition(Vector3 point, int cellSize)
|
||||||
{
|
{
|
||||||
int x = Mathf.FloorToInt(point.x / cellSize);
|
int x = Mathf.FloorToInt(point.x / cellSize);
|
||||||
int z = Mathf.FloorToInt(point.z / cellSize);
|
int z = Mathf.FloorToInt(point.z / cellSize);
|
||||||
|
Loading…
Reference in New Issue
Block a user