Squid, dildos and bezier curves
This commit is contained in:
+263
@@ -0,0 +1,263 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
|
||||
[CustomEditor(typeof(BezierCurve))]
|
||||
public class BezierCurveEditor : Editor
|
||||
{
|
||||
BezierCurve curve;
|
||||
SerializedProperty resolutionProp;
|
||||
SerializedProperty closeProp;
|
||||
SerializedProperty pointsProp;
|
||||
SerializedProperty colorProp;
|
||||
|
||||
private static bool showPoints = true;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
curve = (BezierCurve)target;
|
||||
|
||||
resolutionProp = serializedObject.FindProperty("resolution");
|
||||
closeProp = serializedObject.FindProperty("_close");
|
||||
pointsProp = serializedObject.FindProperty("points");
|
||||
colorProp = serializedObject.FindProperty("drawColor");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
serializedObject.Update();
|
||||
|
||||
EditorGUILayout.PropertyField(resolutionProp);
|
||||
EditorGUILayout.PropertyField(closeProp);
|
||||
EditorGUILayout.PropertyField(colorProp);
|
||||
|
||||
showPoints = EditorGUILayout.Foldout(showPoints, "Points");
|
||||
|
||||
if(showPoints)
|
||||
{
|
||||
int pointCount = pointsProp.arraySize;
|
||||
|
||||
for(int i = 0; i < pointCount; i++)
|
||||
{
|
||||
DrawPointInspector(curve[i], i);
|
||||
}
|
||||
|
||||
if(GUILayout.Button("Add Point"))
|
||||
{
|
||||
Undo.RegisterSceneUndo("Add Point");
|
||||
|
||||
GameObject pointObject = new GameObject("Point "+pointsProp.arraySize);
|
||||
pointObject.transform.parent = curve.transform;
|
||||
pointObject.transform.localPosition = Vector3.zero;
|
||||
BezierPoint newPoint = pointObject.AddComponent<BezierPoint>();
|
||||
|
||||
newPoint.curve = curve;
|
||||
newPoint.handle1 = Vector3.right*0.1f;
|
||||
newPoint.handle2 = -Vector3.right*0.1f;
|
||||
|
||||
pointsProp.InsertArrayElementAtIndex(pointsProp.arraySize);
|
||||
pointsProp.GetArrayElementAtIndex(pointsProp.arraySize - 1).objectReferenceValue = newPoint;
|
||||
}
|
||||
}
|
||||
|
||||
if(GUI.changed)
|
||||
{
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(target);
|
||||
}
|
||||
}
|
||||
|
||||
void OnSceneGUI()
|
||||
{
|
||||
for(int i = 0; i < curve.pointCount; i++)
|
||||
{
|
||||
DrawPointSceneGUI(curve[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawPointInspector(BezierPoint point, int index)
|
||||
{
|
||||
SerializedObject serObj = new SerializedObject(point);
|
||||
|
||||
SerializedProperty handleStyleProp = serObj.FindProperty("handleStyle");
|
||||
SerializedProperty handle1Prop = serObj.FindProperty("_handle1");
|
||||
SerializedProperty handle2Prop = serObj.FindProperty("_handle2");
|
||||
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
|
||||
if(GUILayout.Button("X", GUILayout.Width(20)))
|
||||
{
|
||||
Undo.RegisterSceneUndo("Remove Point");
|
||||
pointsProp.MoveArrayElement(curve.GetPointIndex(point), curve.pointCount - 1);
|
||||
pointsProp.arraySize--;
|
||||
DestroyImmediate(point.gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
EditorGUILayout.ObjectField(point.gameObject, typeof(GameObject), true);
|
||||
|
||||
if(index != 0 && GUILayout.Button(@"/\", GUILayout.Width(25)))
|
||||
{
|
||||
UnityEngine.Object other = pointsProp.GetArrayElementAtIndex(index - 1).objectReferenceValue;
|
||||
pointsProp.GetArrayElementAtIndex(index - 1).objectReferenceValue = point;
|
||||
pointsProp.GetArrayElementAtIndex(index).objectReferenceValue = other;
|
||||
}
|
||||
|
||||
if(index != pointsProp.arraySize - 1 && GUILayout.Button(@"\/", GUILayout.Width(25)))
|
||||
{
|
||||
UnityEngine.Object other = pointsProp.GetArrayElementAtIndex(index + 1).objectReferenceValue;
|
||||
pointsProp.GetArrayElementAtIndex(index + 1).objectReferenceValue = point;
|
||||
pointsProp.GetArrayElementAtIndex(index).objectReferenceValue = other;
|
||||
}
|
||||
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
EditorGUI.indentLevel++;
|
||||
EditorGUI.indentLevel++;
|
||||
|
||||
int newType = (int)((object)EditorGUILayout.EnumPopup("Handle Type", (BezierPoint.HandleStyle)handleStyleProp.enumValueIndex));
|
||||
|
||||
if(newType != handleStyleProp.enumValueIndex)
|
||||
{
|
||||
handleStyleProp.enumValueIndex = newType;
|
||||
if(newType == 0)
|
||||
{
|
||||
if(handle1Prop.vector3Value != Vector3.zero) handle2Prop.vector3Value = -handle1Prop.vector3Value;
|
||||
else if(handle2Prop.vector3Value != Vector3.zero) handle1Prop.vector3Value = -handle2Prop.vector3Value;
|
||||
else
|
||||
{
|
||||
handle1Prop.vector3Value = new Vector3(0.1f, 0, 0);
|
||||
handle2Prop.vector3Value = new Vector3(-0.1f, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
else if(newType == 1)
|
||||
{
|
||||
if(handle1Prop.vector3Value == Vector3.zero && handle2Prop.vector3Value == Vector3.zero)
|
||||
{
|
||||
handle1Prop.vector3Value = new Vector3(0.1f, 0, 0);
|
||||
handle2Prop.vector3Value = new Vector3(-0.1f, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
else if(newType == 2)
|
||||
{
|
||||
handle1Prop.vector3Value = Vector3.zero;
|
||||
handle2Prop.vector3Value = Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 newPointPos = EditorGUILayout.Vector3Field("Position : ", point.transform.localPosition);
|
||||
if(newPointPos != point.transform.localPosition)
|
||||
{
|
||||
Undo.RegisterUndo(point.transform, "Move Bezier Point");
|
||||
point.transform.localPosition = newPointPos;
|
||||
}
|
||||
|
||||
if(handleStyleProp.enumValueIndex == 0)
|
||||
{
|
||||
Vector3 newPosition;
|
||||
|
||||
newPosition = EditorGUILayout.Vector3Field("Handle 1", handle1Prop.vector3Value);
|
||||
if(newPosition != handle1Prop.vector3Value)
|
||||
{
|
||||
handle1Prop.vector3Value = newPosition;
|
||||
handle2Prop.vector3Value = -newPosition;
|
||||
}
|
||||
|
||||
newPosition = EditorGUILayout.Vector3Field("Handle 2", handle2Prop.vector3Value);
|
||||
if(newPosition != handle2Prop.vector3Value)
|
||||
{
|
||||
handle1Prop.vector3Value = -newPosition;
|
||||
handle2Prop.vector3Value = newPosition;
|
||||
}
|
||||
}
|
||||
|
||||
else if(handleStyleProp.enumValueIndex == 1)
|
||||
{
|
||||
EditorGUILayout.PropertyField(handle1Prop);
|
||||
EditorGUILayout.PropertyField(handle2Prop);
|
||||
}
|
||||
|
||||
EditorGUI.indentLevel--;
|
||||
EditorGUI.indentLevel--;
|
||||
|
||||
if(GUI.changed)
|
||||
{
|
||||
serObj.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(serObj.targetObject);
|
||||
}
|
||||
}
|
||||
|
||||
static void DrawPointSceneGUI(BezierPoint point)
|
||||
{
|
||||
Handles.Label(point.position + new Vector3(0, HandleUtility.GetHandleSize(point.position) * 0.4f, 0), point.gameObject.name);
|
||||
|
||||
Handles.color = Color.green;
|
||||
Vector3 newPosition = Handles.FreeMoveHandle(point.position, point.transform.rotation, HandleUtility.GetHandleSize(point.position)*0.1f, Vector3.zero, Handles.RectangleCap);
|
||||
|
||||
if(newPosition != point.position)
|
||||
{
|
||||
Undo.RegisterUndo(point.transform, "Move Point");
|
||||
point.transform.position = newPosition;
|
||||
}
|
||||
|
||||
if(point.handleStyle != BezierPoint.HandleStyle.None)
|
||||
{
|
||||
Handles.color = Color.cyan;
|
||||
Vector3 newGlobal1 = Handles.FreeMoveHandle(point.globalHandle1, point.transform.rotation, HandleUtility.GetHandleSize(point.globalHandle1)*0.075f, Vector3.zero, Handles.CircleCap);
|
||||
if(point.globalHandle1 != newGlobal1)
|
||||
{
|
||||
Undo.RegisterUndo(point, "Move Handle");
|
||||
point.globalHandle1 = newGlobal1;
|
||||
if(point.handleStyle == BezierPoint.HandleStyle.Connected) point.globalHandle2 = -(newGlobal1 - point.position) + point.position;
|
||||
}
|
||||
|
||||
Vector3 newGlobal2 = Handles.FreeMoveHandle(point.globalHandle2, point.transform.rotation, HandleUtility.GetHandleSize(point.globalHandle2)*0.075f, Vector3.zero, Handles.CircleCap);
|
||||
if(point.globalHandle2 != newGlobal2)
|
||||
{
|
||||
Undo.RegisterUndo(point, "Move Handle");
|
||||
point.globalHandle2 = newGlobal2;
|
||||
if(point.handleStyle == BezierPoint.HandleStyle.Connected) point.globalHandle1 = -(newGlobal2 - point.position) + point.position;
|
||||
}
|
||||
|
||||
Handles.color = Color.yellow;
|
||||
Handles.DrawLine(point.position, point.globalHandle1);
|
||||
Handles.DrawLine(point.position, point.globalHandle2);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawOtherPoints(BezierCurve curve, BezierPoint caller)
|
||||
{
|
||||
foreach(BezierPoint p in curve.GetAnchorPoints())
|
||||
{
|
||||
if(p != caller) DrawPointSceneGUI(p);
|
||||
}
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/Create Other/Bezier Curve")]
|
||||
public static void CreateCurve(MenuCommand command)
|
||||
{
|
||||
GameObject curveObject = new GameObject("BezierCurve");
|
||||
Undo.RegisterUndo(curveObject, "Undo Create Curve");
|
||||
BezierCurve curve = curveObject.AddComponent<BezierCurve>();
|
||||
|
||||
BezierPoint p1 = curve.AddPointAt(Vector3.forward * 0.5f);
|
||||
p1.handleStyle = BezierPoint.HandleStyle.Connected;
|
||||
p1.handle1 = new Vector3(-0.28f, 0, 0);
|
||||
|
||||
BezierPoint p2 = curve.AddPointAt(Vector3.right * 0.5f);
|
||||
p2.handleStyle = BezierPoint.HandleStyle.Connected;
|
||||
p2.handle1 = new Vector3(0, 0, 0.28f);
|
||||
|
||||
BezierPoint p3 = curve.AddPointAt(-Vector3.forward * 0.5f);
|
||||
p3.handleStyle = BezierPoint.HandleStyle.Connected;
|
||||
p3.handle1 = new Vector3(0.28f, 0, 0);
|
||||
|
||||
BezierPoint p4 = curve.AddPointAt(-Vector3.right * 0.5f);
|
||||
p4.handleStyle = BezierPoint.HandleStyle.Connected;
|
||||
p4.handle1 = new Vector3(0, 0, -0.28f);
|
||||
|
||||
curve.close = true;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 076c82ebe1ea6b249b4e66e85cdf269a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
|
||||
[CustomEditor(typeof(BezierPoint))]
|
||||
[CanEditMultipleObjects]
|
||||
public class BezierPointEditor : Editor {
|
||||
|
||||
BezierPoint point;
|
||||
|
||||
SerializedProperty handleTypeProp;
|
||||
SerializedProperty handle1Prop;
|
||||
SerializedProperty handle2Prop;
|
||||
|
||||
private delegate void HandleFunction(BezierPoint p);
|
||||
private HandleFunction[] handlers = new HandleFunction[] { HandleConnected, HandleBroken, HandleAbsent };
|
||||
|
||||
void OnEnable(){
|
||||
point = (BezierPoint)target;
|
||||
|
||||
handleTypeProp = serializedObject.FindProperty("handleStyle");
|
||||
handle1Prop = serializedObject.FindProperty("_handle1");
|
||||
handle2Prop = serializedObject.FindProperty("_handle2");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI (){
|
||||
|
||||
serializedObject.Update();
|
||||
|
||||
BezierPoint.HandleStyle newHandleType = (BezierPoint.HandleStyle)EditorGUILayout.EnumPopup("Handle Type", (BezierPoint.HandleStyle)handleTypeProp.intValue);
|
||||
|
||||
if(newHandleType != (BezierPoint.HandleStyle)handleTypeProp.intValue)
|
||||
{
|
||||
handleTypeProp.intValue = (int)newHandleType;
|
||||
|
||||
if((int)newHandleType == 0)
|
||||
{
|
||||
if(handle1Prop.vector3Value != Vector3.zero) handle2Prop.vector3Value = -handle1Prop.vector3Value;
|
||||
else if(handle2Prop.vector3Value != Vector3.zero) handle1Prop.vector3Value = -handle2Prop.vector3Value;
|
||||
else
|
||||
{
|
||||
handle1Prop.vector3Value = new Vector3(0.1f, 0, 0);
|
||||
handle2Prop.vector3Value = new Vector3(-0.1f, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
else if((int)newHandleType == 1)
|
||||
{
|
||||
if(handle1Prop.vector3Value == Vector3.zero && handle2Prop.vector3Value == Vector3.zero)
|
||||
{
|
||||
handle1Prop.vector3Value = new Vector3(0.1f, 0, 0);
|
||||
handle2Prop.vector3Value = new Vector3(-0.1f, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
else if((int)newHandleType == 2)
|
||||
{
|
||||
handle1Prop.vector3Value = Vector3.zero;
|
||||
handle2Prop.vector3Value = Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
if(handleTypeProp.intValue != 2)
|
||||
{
|
||||
Vector3 newHandle1 = EditorGUILayout.Vector3Field("Handle 1", handle1Prop.vector3Value);
|
||||
Vector3 newHandle2 = EditorGUILayout.Vector3Field("Handle 2", handle2Prop.vector3Value);
|
||||
|
||||
if(handleTypeProp.intValue == 0){
|
||||
if(newHandle1 != handle1Prop.vector3Value){
|
||||
handle1Prop.vector3Value = newHandle1;
|
||||
handle2Prop.vector3Value = -newHandle1;
|
||||
}
|
||||
|
||||
else if(newHandle2 != handle2Prop.vector3Value){
|
||||
handle1Prop.vector3Value = -newHandle2;
|
||||
handle2Prop.vector3Value = newHandle2;
|
||||
}
|
||||
}
|
||||
|
||||
else{
|
||||
handle1Prop.vector3Value = newHandle1;
|
||||
handle2Prop.vector3Value = newHandle2;
|
||||
}
|
||||
}
|
||||
|
||||
if(GUI.changed){
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(target);
|
||||
}
|
||||
}
|
||||
|
||||
void OnSceneGUI()
|
||||
{
|
||||
|
||||
Handles.color = Color.green;
|
||||
Vector3 newPosition = Handles.FreeMoveHandle(point.position, point.transform.rotation, HandleUtility.GetHandleSize(point.position)*0.2f, Vector3.zero, Handles.CubeCap);
|
||||
if(point.position != newPosition) point.position = newPosition;
|
||||
|
||||
handlers[(int)point.handleStyle](point);
|
||||
|
||||
Handles.color = Color.yellow;
|
||||
Handles.DrawLine(point.position, point.globalHandle1);
|
||||
Handles.DrawLine(point.position, point.globalHandle2);
|
||||
|
||||
BezierCurveEditor.DrawOtherPoints(point.curve, point);
|
||||
}
|
||||
|
||||
private static void HandleConnected(BezierPoint p){
|
||||
Handles.color = Color.cyan;
|
||||
|
||||
Vector3 newGlobal1 = Handles.FreeMoveHandle(p.globalHandle1, p.transform.rotation, HandleUtility.GetHandleSize(p.globalHandle1)*0.15f, Vector3.zero, Handles.SphereCap);
|
||||
|
||||
if(newGlobal1 != p.globalHandle1){
|
||||
Undo.RegisterUndo(p, "Move Handle");
|
||||
p.globalHandle1 = newGlobal1;
|
||||
p.globalHandle2 = -(newGlobal1 - p.position) + p.position;
|
||||
}
|
||||
|
||||
Vector3 newGlobal2 = Handles.FreeMoveHandle(p.globalHandle2, p.transform.rotation, HandleUtility.GetHandleSize(p.globalHandle2)*0.15f, Vector3.zero, Handles.SphereCap);
|
||||
|
||||
if(newGlobal2 != p.globalHandle2){
|
||||
Undo.RegisterUndo(p, "Move Handle");
|
||||
p.globalHandle1 = -(newGlobal2 - p.position) + p.position;
|
||||
p.globalHandle2 = newGlobal2;
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleBroken(BezierPoint p){
|
||||
Handles.color = Color.cyan;
|
||||
|
||||
Vector3 newGlobal1 = Handles.FreeMoveHandle(p.globalHandle1, Quaternion.identity, HandleUtility.GetHandleSize(p.globalHandle1)*0.15f, Vector3.zero, Handles.SphereCap);
|
||||
Vector3 newGlobal2 = Handles.FreeMoveHandle(p.globalHandle2, Quaternion.identity, HandleUtility.GetHandleSize(p.globalHandle2)*0.15f, Vector3.zero, Handles.SphereCap);
|
||||
|
||||
if(newGlobal1 != p.globalHandle1)
|
||||
{
|
||||
Undo.RegisterUndo(p, "Move Handle");
|
||||
p.globalHandle1 = newGlobal1;
|
||||
}
|
||||
|
||||
if(newGlobal2 != p.globalHandle2)
|
||||
{
|
||||
Undo.RegisterUndo(p, "Move Handle");
|
||||
p.globalHandle2 = newGlobal2;
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleAbsent(BezierPoint p)
|
||||
{
|
||||
p.handle1 = Vector3.zero;
|
||||
p.handle2 = Vector3.zero;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f40a14ecad007949831de4081a5a58e
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user