forked from keenanwoodall/Deform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpherifyDeformerEditor.cs
117 lines (98 loc) · 3.85 KB
/
SpherifyDeformerEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using UnityEngine;
using UnityEditor;
using Deform;
namespace DeformEditor
{
[CustomEditor (typeof (SpherifyDeformer)), CanEditMultipleObjects]
public class SpherifyDeformerEditor : DeformerEditor
{
private class Content
{
public static readonly GUIContent Factor = DeformEditorGUIUtility.DefaultContent.Factor;
public static readonly GUIContent Radius = new GUIContent (text: "Radius", tooltip: "The radius of the sphere that the points are pushed towards.");
public static readonly GUIContent Mode = new GUIContent (text: "Mode", tooltip: "Unlimited: Vertices' from any distance will interpolate towards the nearest point on the sphere.\nLimited: Vertices will only interpolate towards the sphere's surface when within the sphere.");
public static readonly GUIContent Smooth = new GUIContent (text: "Smooth", tooltip: "Should the interpolation towards the sphere be smoothed.");
public static readonly GUIContent Axis = DeformEditorGUIUtility.DefaultContent.Axis;
}
private class Properties
{
public SerializedProperty Factor;
public SerializedProperty Radius;
public SerializedProperty Mode;
public SerializedProperty Smooth;
public SerializedProperty Axis;
public Properties (SerializedObject obj)
{
Factor = obj.FindProperty ("factor");
Radius = obj.FindProperty ("radius");
Mode = obj.FindProperty ("mode");
Smooth = obj.FindProperty ("smooth");
Axis = obj.FindProperty ("axis");
}
}
private Properties properties;
protected override void OnEnable ()
{
base.OnEnable ();
properties = new Properties (serializedObject);
}
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
serializedObject.UpdateIfRequiredOrScript ();
EditorGUILayout.Slider (properties.Factor, 0f, 1f, Content.Factor);
EditorGUILayout.PropertyField (properties.Radius, Content.Radius);
EditorGUILayout.PropertyField (properties.Mode, Content.Mode);
using (new EditorGUI.DisabledScope (properties.Smooth.hasMultipleDifferentValues || (BoundsMode)properties.Mode.enumValueIndex == BoundsMode.Unlimited))
EditorGUILayout.PropertyField (properties.Smooth, Content.Smooth);
EditorGUILayout.PropertyField (properties.Axis, Content.Axis);
serializedObject.ApplyModifiedProperties ();
EditorApplication.QueuePlayerLoopUpdate ();
}
public override void OnSceneGUI ()
{
base.OnSceneGUI ();
var spherify = target as SpherifyDeformer;
DrawRadiusHandle (spherify);
DrawFactorHandle (spherify);
EditorApplication.QueuePlayerLoopUpdate ();
}
private void DrawRadiusHandle (SpherifyDeformer spherify)
{
using (new Handles.DrawingScope (Matrix4x4.TRS (spherify.Axis.position, spherify.Axis.rotation, spherify.Axis.lossyScale)))
{
using (var check = new EditorGUI.ChangeCheckScope ())
{
var newRadius = DeformHandles.Radius (Quaternion.identity, Vector3.zero, spherify.Radius);
if (check.changed)
{
Undo.RecordObject (spherify, "Changed Radius");
spherify.Radius = newRadius;
}
}
}
}
private void DrawFactorHandle (SpherifyDeformer spherify)
{
if (spherify.Radius == 0f)
return;
var direction = Vector3.forward;
var position = direction * (spherify.Factor * spherify.Radius);
using (new Handles.DrawingScope (Matrix4x4.TRS (spherify.Axis.position, spherify.Axis.rotation, spherify.Axis.lossyScale)))
{
DeformHandles.Line (Vector3.zero, position, DeformHandles.LineMode.Light);
DeformHandles.Line (position, direction * spherify.Radius, DeformHandles.LineMode.LightDotted);
using (var check = new EditorGUI.ChangeCheckScope ())
{
var newWorldPosition = DeformHandles.Slider (position, direction);
if (check.changed)
{
Undo.RecordObject (spherify, "Changed Factor");
var newFactor = newWorldPosition.z / spherify.Radius;
spherify.Factor = newFactor;
}
}
}
}
}
}