Skip to content

Commit

Permalink
chore(RemoveMeshInBoxEditor): improve box editor
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Feb 5, 2023
1 parent b2fea4f commit 15fc931
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Editor/RemoveMeshInBoxEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,41 @@ private void OnSceneGUI()

centerProp.vector3Value = Handles.PositionHandle(centerProp.vector3Value, Quaternion.identity);
rotationProp.quaternionValue = Handles.RotationHandle(rotationProp.quaternionValue, centerProp.vector3Value);
sizeProp.vector3Value = Handles.ScaleHandle(sizeProp.vector3Value, centerProp.vector3Value,
rotationProp.quaternionValue, HandleUtility.GetHandleSize(centerProp.vector3Value) * 1.5f);

var size = sizeProp.vector3Value;
var center = centerProp.vector3Value;
var halfSize = size / 2;
var x = rotationProp.quaternionValue * new Vector3(halfSize.x, 0, 0);
var y = rotationProp.quaternionValue * new Vector3(0, halfSize.y, 0);
var z = rotationProp.quaternionValue * new Vector3(0, 0, halfSize.z);

BoxFaceSlider(ref center, ref size.x, x);
BoxFaceSlider(ref center, ref size.x, -x);
BoxFaceSlider(ref center, ref size.y, y);
BoxFaceSlider(ref center, ref size.y, -y);
BoxFaceSlider(ref center, ref size.z, z);
BoxFaceSlider(ref center, ref size.z, -z);

sizeProp.vector3Value = size;
centerProp.vector3Value = center;

serializedObject.ApplyModifiedProperties();
}

private void BoxFaceSlider(ref Vector3 center, ref float size, Vector3 directionInWorld)
{
var prev = center + directionInWorld;
var newer = Handles.Slider(prev, directionInWorld,
HandleUtility.GetHandleSize(prev) / 3, Handles.CubeHandleCap, -1f);

if (prev != newer)
{
var opposite = center - directionInWorld;
size = (opposite - newer).magnitude;
center = (opposite + newer) / 2;
}
}

// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
[DrawGizmo(GizmoType.Selected | GizmoType.Active)]
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
Expand Down

0 comments on commit 15fc931

Please sign in to comment.