Skip to content

Commit

Permalink
✨ feat(transform-tool): Use bounds option
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Apr 8, 2022
1 parent ef51e0a commit ff26cf7
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions Assets/EsnyaUnityTools/Editor/TransformTool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Ludiq.OdinSerializer.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
Expand All @@ -21,6 +22,7 @@ private static void ShowWindow()
public SerializedObject serializedObject;
public float maxDistance = 1.0f;
public LayerMask layerMask = 0x801;
public bool useBounds;
public QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.Ignore;


Expand Down Expand Up @@ -55,6 +57,17 @@ private static Vector2 ParseLatLon(string text)
return new Vector2(float.Parse(split[0]), float.Parse(split[1]));
}

private static float GetGroundDistance(Vector3 origin, float maxDistance, LayerMask layerMask, QueryTriggerInteraction queryTriggerInteraction)
{
var hitUp = Physics.Raycast(origin + Vector3.up * maxDistance, Vector3.down, out var up, maxDistance, layerMask, queryTriggerInteraction);
if (hitUp) return maxDistance - up.distance;

var hitDown = Physics.Raycast(origin, Vector3.down, out var down, maxDistance, layerMask, queryTriggerInteraction);
if (hitDown) return -down.distance;

return 0;
}

private void OnGUI()
{
serializedObject.Update();
Expand Down Expand Up @@ -97,18 +110,51 @@ private void OnGUI()
case nameof(queryTriggerInteraction):
if (GUILayout.Button("Snap To Ground"))
{
var transforms = Selection.transforms;
foreach (var transform in transforms)
try
{
Undo.RecordObject(transform, "Snap To Ground");
var hitUp = Physics.Raycast(transform.position + Vector3.up * maxDistance, Vector3.down, out var up, maxDistance, layerMask, queryTriggerInteraction);
if (hitUp) transform.position = up.point;
else
var transforms = Selection.transforms;
var i = 0;
foreach (var transform in transforms)
{
var hitDown = Physics.Raycast(transform.position, Vector3.down, out var down, maxDistance, layerMask, queryTriggerInteraction);
if (hitDown) transform.position = down.point;
EditorUtility.DisplayProgressBar("Snap To Ground", transform.gameObject.name, (float)i++ / transforms.Length);
Undo.RecordObject(transform, "Snap To Ground");

var renderer = transform.GetComponent<Renderer>();
if (useBounds && renderer)
{
var bounds = renderer.bounds;
var center = bounds.center;
var extents = bounds.extents;

var y = center.y - extents.y;

var left = center.x - extents.x;
var right = center.x + extents.x;

var top = center.z + extents.z;
var bottom = center.z - extents.z;

var points = new[] {
transform.position,
center + Vector3.Scale(extents, new Vector3( 1, -1, 1)),
center + Vector3.Scale(extents, new Vector3(-1, -1, 1)),
center + Vector3.Scale(extents, new Vector3( 1, -1, -1)),
center + Vector3.Scale(extents, new Vector3(-1, -1, -1)),
};

var distance = points.Select(origin => GetGroundDistance(origin, maxDistance, layerMask, queryTriggerInteraction)).Min();
transform.position += Vector3.up * distance;
}
else
{
transform.position += Vector3.up * GetGroundDistance(transform.position, maxDistance, layerMask, queryTriggerInteraction);
}
}
}
finally
{
EditorUtility.ClearProgressBar();
}
}
EditorGUILayout.Separator();
break;
Expand Down

0 comments on commit ff26cf7

Please sign in to comment.