1
This commit is contained in:
@@ -0,0 +1,421 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class CreatePointModule : PointModule
|
||||
{
|
||||
public enum AppendMode { Beginning = 0, End = 1}
|
||||
public enum PlacementMode { YPlane, XPlane, ZPlane, CameraPlane, Surface, Insert }
|
||||
public enum NormalMode { Default, LookAtCamera, AlignWithCamera, Calculate, Left, Right, Up, Down, Forward, Back }
|
||||
protected PlacementMode placementMode = PlacementMode.YPlane;
|
||||
public AppendMode appendMode = AppendMode.End;
|
||||
public float offset = 0f;
|
||||
public NormalMode normalMode = NormalMode.Default;
|
||||
public LayerMask surfaceLayerMask = new LayerMask();
|
||||
public float createPointSize = 1f;
|
||||
public Color createPointColor = Color.white;
|
||||
protected Spline visualizer;
|
||||
protected Camera editorCamera;
|
||||
protected Vector3 createPoint = Vector3.zero, createNormal = Vector3.up;
|
||||
protected SplineSample evalResult = new SplineSample();
|
||||
protected int lastCreated = -1;
|
||||
|
||||
public CreatePointModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return IconContent("+", "add", "Add Points");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return IconContent("+", "add_on", "Add Points");
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
normalMode = (NormalMode)LoadInt("normalMode");
|
||||
placementMode = (PlacementMode)LoadInt("placementMode");
|
||||
appendMode = (AppendMode)LoadInt("appendMode", 1);
|
||||
offset = LoadFloat("offset");
|
||||
surfaceLayerMask = LoadInt("surfaceLayerMask", ~0);
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveInt("normalMode", (int)normalMode);
|
||||
SaveInt("placementMode", (int)placementMode);
|
||||
SaveInt("appendMode", (int)appendMode);
|
||||
SaveFloat("offset", offset);
|
||||
SaveInt("surfaceLayerMask", surfaceLayerMask);
|
||||
}
|
||||
|
||||
public override void Deselect()
|
||||
{
|
||||
base.Deselect();
|
||||
GUIUtility.hotControl = -1;
|
||||
if (Event.current != null)
|
||||
{
|
||||
Event.current.Use();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
placementMode = (PlacementMode)EditorGUILayout.EnumPopup("Placement Mode", placementMode);
|
||||
if (placementMode != PlacementMode.Insert)
|
||||
{
|
||||
normalMode = (NormalMode)EditorGUILayout.EnumPopup("Normal Mode", normalMode);
|
||||
appendMode = (AppendMode)EditorGUILayout.EnumPopup("Append To", appendMode);
|
||||
}
|
||||
string offsetLabel = "Grid Offset";
|
||||
if (placementMode == PlacementMode.CameraPlane) offsetLabel = "Far Plane";
|
||||
if (placementMode == PlacementMode.Surface) offsetLabel = "Surface Offset";
|
||||
offset = EditorGUILayout.FloatField(offsetLabel, offset);
|
||||
if (placementMode == PlacementMode.Surface)
|
||||
{
|
||||
surfaceLayerMask = DreamteckEditorGUI.LayermaskField("Surface Mask", surfaceLayerMask);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
editorCamera = SceneView.currentDrawingSceneView.camera;
|
||||
bool canCreate = false;
|
||||
if (placementMode == PlacementMode.CameraPlane)
|
||||
{
|
||||
GetCreatePointOnPlane(-editorCamera.transform.forward, editorCamera.transform.position + editorCamera.transform.forward * offset, out createPoint);
|
||||
Handles.color = new Color(1f, 0.78f, 0.12f);
|
||||
DrawGrid(createPoint, editorCamera.transform.forward, Vector2.one * 10, 2.5f);
|
||||
Handles.color = Color.white;
|
||||
canCreate = true;
|
||||
createNormal = -editorCamera.transform.forward;
|
||||
}
|
||||
|
||||
if (placementMode == PlacementMode.Surface)
|
||||
{
|
||||
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(ray, out hit, Mathf.Infinity, surfaceLayerMask))
|
||||
{
|
||||
canCreate = true;
|
||||
createPoint = hit.point + hit.normal * offset;
|
||||
Handles.color = Color.blue;
|
||||
Handles.DrawLine(hit.point, createPoint);
|
||||
SplineEditorHandles.DrawRectangle(createPoint, Quaternion.LookRotation(-editorCamera.transform.forward, editorCamera.transform.up), HandleUtility.GetHandleSize(createPoint) * 0.1f);
|
||||
Handles.color = Color.white;
|
||||
createNormal = hit.normal;
|
||||
}
|
||||
}
|
||||
|
||||
if (placementMode == PlacementMode.XPlane)
|
||||
{
|
||||
canCreate = AxisGrid(Vector3.right, new Color(0.85f, 0.24f, 0.11f, 0.92f), out createPoint);
|
||||
createNormal = Vector3.right;
|
||||
}
|
||||
|
||||
if (placementMode == PlacementMode.YPlane)
|
||||
{
|
||||
canCreate = AxisGrid(Vector3.up, new Color(0.6f, 0.95f, 0.28f, 0.92f), out createPoint);
|
||||
createNormal = Vector3.up;
|
||||
}
|
||||
|
||||
if (placementMode == PlacementMode.ZPlane)
|
||||
{
|
||||
canCreate = AxisGrid(Vector3.forward, new Color(0.22f, 0.47f, 0.97f, 0.92f), out createPoint);
|
||||
createNormal = Vector3.back;
|
||||
}
|
||||
|
||||
if (placementMode == PlacementMode.Insert)
|
||||
{
|
||||
canCreate = true;
|
||||
if (points.Length < 2)
|
||||
{
|
||||
placementMode = PlacementMode.YPlane;
|
||||
}
|
||||
else
|
||||
{
|
||||
InsertMode(Event.current.mousePosition);
|
||||
}
|
||||
}
|
||||
else if (eventModule.mouseLeftDown && canCreate && !eventModule.mouseRight && !eventModule.alt)
|
||||
{
|
||||
CreateSplinePoint(createPoint, createNormal);
|
||||
}
|
||||
|
||||
if (lastCreated >= 0 && lastCreated < points.Length && editor.eventModule.mouseLeft)
|
||||
{
|
||||
Vector3 tangent = points[lastCreated].position - createPoint;
|
||||
if (appendMode == AppendMode.End)
|
||||
{
|
||||
tangent = createPoint - points[lastCreated].position;
|
||||
}
|
||||
points[lastCreated].SetTangent2Position(points[lastCreated].position + tangent);
|
||||
RegisterChange();
|
||||
}
|
||||
else if (!editor.eventModule.mouseLeft)
|
||||
{
|
||||
lastCreated = -1;
|
||||
}
|
||||
|
||||
|
||||
if (!canCreate) DrawMouseCross();
|
||||
UpdateVisualizer();
|
||||
SplineDrawer.DrawSpline(visualizer, color);
|
||||
Repaint();
|
||||
}
|
||||
|
||||
protected virtual void CreateSplinePoint(Vector3 position, Vector3 normal)
|
||||
{
|
||||
GUIUtility.hotControl = -1;
|
||||
AddPoint();
|
||||
}
|
||||
|
||||
protected void AddPoint()
|
||||
{
|
||||
SplinePoint newPoint = new SplinePoint(createPoint, createPoint);
|
||||
newPoint.size = createPointSize;
|
||||
newPoint.color = createPointColor;
|
||||
SplinePoint[] newPoints = editor.GetPointsArray();
|
||||
if (appendMode == AppendMode.End)
|
||||
{
|
||||
Dreamteck.ArrayUtility.Add(ref newPoints, newPoint);
|
||||
lastCreated = newPoints.Length - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Dreamteck.ArrayUtility.Insert(ref newPoints, 0, newPoint);
|
||||
lastCreated = 0;
|
||||
}
|
||||
|
||||
editor.SetPointsArray(newPoints);
|
||||
SetPointNormal(lastCreated, createNormal);
|
||||
SelectPoint(lastCreated);
|
||||
RegisterChange();
|
||||
}
|
||||
|
||||
protected void SetPointNormal(int index, Vector3 defaultNormal)
|
||||
{
|
||||
if (editor.is2D)
|
||||
{
|
||||
points[index].normal = Vector3.back;
|
||||
return;
|
||||
}
|
||||
if (normalMode == NormalMode.Default) points[index].normal = defaultNormal;
|
||||
else
|
||||
{
|
||||
Camera editorCamera = SceneView.lastActiveSceneView.camera;
|
||||
switch (normalMode)
|
||||
{
|
||||
case NormalMode.AlignWithCamera: points[index].normal = editorCamera.transform.forward; break;
|
||||
case NormalMode.LookAtCamera: points[index].normal = Vector3.Normalize(editorCamera.transform.position - points[index].position); break;
|
||||
case NormalMode.Calculate: PointNormalModule.CalculatePointNormal(points, index, isClosed); break;
|
||||
case NormalMode.Left: points[index].normal = Vector3.left; break;
|
||||
case NormalMode.Right: points[index].normal = Vector3.right; break;
|
||||
case NormalMode.Up: points[index].normal = Vector3.up; break;
|
||||
case NormalMode.Down: points[index].normal = Vector3.down; break;
|
||||
case NormalMode.Forward: points[index].normal = Vector3.forward; break;
|
||||
case NormalMode.Back: points[index].normal = Vector3.back; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void InsertMode(Vector3 screenCoordinates)
|
||||
{
|
||||
|
||||
double percent = ProjectScreenSpace(screenCoordinates);
|
||||
editor.evaluate(percent, ref evalResult);
|
||||
if (editor.eventModule.mouseRight)
|
||||
{
|
||||
SplineEditorHandles.DrawCircle(evalResult.position, Quaternion.LookRotation(editorCamera.transform.position - evalResult.position), HandleUtility.GetHandleSize(evalResult.position) * 0.2f);
|
||||
return;
|
||||
}
|
||||
if (SplineEditorHandles.CircleButton(evalResult.position, Quaternion.LookRotation(editorCamera.transform.position - evalResult.position), HandleUtility.GetHandleSize(evalResult.position) * 0.2f, 1.5f, color))
|
||||
{
|
||||
SplinePoint newPoint = new SplinePoint(evalResult.position, evalResult.position);
|
||||
newPoint.size = evalResult.size;
|
||||
newPoint.color = evalResult.color;
|
||||
newPoint.normal = evalResult.up;
|
||||
double floatIndex = (points.Length - 1) * percent;
|
||||
int pointIndex = Mathf.Clamp(DMath.FloorInt(floatIndex), 0, points.Length - 2);
|
||||
editor.AddPointAt(pointIndex + 1);
|
||||
points[pointIndex + 1].SetPoint(newPoint);
|
||||
SelectPoint(pointIndex);
|
||||
RegisterChange();
|
||||
}
|
||||
}
|
||||
|
||||
protected double ProjectScreenSpace(Vector2 screenPoint)
|
||||
{
|
||||
float closestDistance = (screenPoint - HandleUtility.WorldToGUIPoint(points[0].position)).sqrMagnitude;
|
||||
double closestPercent = 0.0;
|
||||
double moveStep = 1.0 / ((editor.points.Length - 1) * sampleRate);
|
||||
double add = moveStep;
|
||||
if (splineType == Spline.Type.Linear) add /= 2.0;
|
||||
int count = 0;
|
||||
for (double i = add; i < 1.0; i += add)
|
||||
{
|
||||
editor.evaluate(i, ref evalResult);
|
||||
Vector2 point = HandleUtility.WorldToGUIPoint(evalResult.position);
|
||||
float dist = (point - screenPoint).sqrMagnitude;
|
||||
if (dist < closestDistance)
|
||||
{
|
||||
closestDistance = dist;
|
||||
closestPercent = i;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return closestPercent;
|
||||
}
|
||||
|
||||
bool GetCreatePointOnPlane(Vector3 normal, Vector3 origin, out Vector3 result)
|
||||
{
|
||||
Plane plane = new Plane(normal, origin);
|
||||
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
|
||||
float rayDistance;
|
||||
if (plane.Raycast(ray, out rayDistance))
|
||||
{
|
||||
result = ray.GetPoint(rayDistance);
|
||||
return true;
|
||||
}
|
||||
else if (normal == Vector3.zero)
|
||||
{
|
||||
result = origin;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = ray.GetPoint(0f);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool AxisGrid(Vector3 axis, Color color, out Vector3 origin)
|
||||
{
|
||||
float dot = Vector3.Dot(editorCamera.transform.position.normalized, axis);
|
||||
if (dot < 0f) axis = -axis;
|
||||
Plane plane = new Plane(axis, Vector3.zero);
|
||||
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
|
||||
float rayDistance;
|
||||
if (plane.Raycast(ray, out rayDistance))
|
||||
{
|
||||
origin = ray.GetPoint(rayDistance) + axis * offset;
|
||||
Handles.color = color;
|
||||
float distance = 1f;
|
||||
ray = new Ray(editorCamera.transform.position, -axis);
|
||||
if (!editorCamera.orthographic && plane.Raycast(ray, out rayDistance)) distance = Vector3.Distance(editorCamera.transform.position + axis * offset, origin);
|
||||
else if (editorCamera.orthographic) distance = 2f * editorCamera.orthographicSize;
|
||||
DrawGrid(origin, axis, Vector2.one * distance * 0.3f, distance * 2.5f * 0.03f);
|
||||
Handles.DrawLine(origin, origin - axis * offset);
|
||||
Handles.color = Color.white;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
origin = Vector3.zero;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawGrid(Vector3 center, Vector3 normal, Vector2 size, float scale)
|
||||
{
|
||||
Vector3 right = Vector3.Cross(Vector3.up, normal).normalized;
|
||||
if (Mathf.Abs(Vector3.Dot(Vector3.up, normal)) >= 0.9999f) right = Vector3.Cross(Vector3.forward, normal).normalized;
|
||||
Vector3 up = Vector3.Cross(normal, right).normalized;
|
||||
Vector3 startPoint = center - right * size.x * 0.5f + up * size.y * 0.5f;
|
||||
float i = 0f;
|
||||
float add = scale;
|
||||
while (i <= size.x)
|
||||
{
|
||||
Vector3 point = startPoint + right * i;
|
||||
Handles.DrawLine(point, point - up * size.y);
|
||||
i += add;
|
||||
}
|
||||
|
||||
i = 0f;
|
||||
add = scale;
|
||||
while (i <= size.x)
|
||||
{
|
||||
Vector3 point = startPoint - up * i;
|
||||
Handles.DrawLine(point, point + right * size.x);
|
||||
i += add;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawMouseCross()
|
||||
{
|
||||
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
|
||||
Vector3 origin = ray.GetPoint(1f);
|
||||
float size = 0.4f * HandleUtility.GetHandleSize(origin);
|
||||
Vector3 a = origin + editorCamera.transform.up * size - editorCamera.transform.right * size;
|
||||
Vector3 b = origin - editorCamera.transform.up * size + editorCamera.transform.right * size;
|
||||
Handles.color = Color.red;
|
||||
Handles.DrawLine(a, b);
|
||||
a = origin - editorCamera.transform.up * size - editorCamera.transform.right * size;
|
||||
b = origin + editorCamera.transform.up * size + editorCamera.transform.right * size;
|
||||
Handles.DrawLine(a, b);
|
||||
Handles.color = Color.white;
|
||||
}
|
||||
|
||||
private void UpdateVisualizer()
|
||||
{
|
||||
if(visualizer == null) visualizer = new Spline(splineType);
|
||||
visualizer.type = splineType;
|
||||
visualizer.sampleRate = sampleRate;
|
||||
if(placementMode == PlacementMode.Insert)
|
||||
{
|
||||
visualizer.points = editor.GetPointsArray();
|
||||
if (isClosed) visualizer.Close();
|
||||
else if (visualizer.isClosed) visualizer.Break();
|
||||
return;
|
||||
}
|
||||
|
||||
if (visualizer.points.Length != points.Length + 1)
|
||||
{
|
||||
visualizer.points = new SplinePoint[points.Length + 1];
|
||||
}
|
||||
|
||||
SplinePoint newPoint = new SplinePoint(createPoint, createPoint, createNormal, 1f, Color.white);
|
||||
if (appendMode == AppendMode.End)
|
||||
{
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
visualizer.points[i] = points[i].CreateSplinePoint();
|
||||
}
|
||||
visualizer.points[visualizer.points.Length - 1] = newPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1; i < visualizer.points.Length; i++)
|
||||
{
|
||||
visualizer.points[i] = points[i - 1].CreateSplinePoint();
|
||||
}
|
||||
visualizer.points[0] = newPoint;
|
||||
}
|
||||
|
||||
if (isClosed && !visualizer.isClosed)
|
||||
{
|
||||
if(visualizer.points.Length >= 3)
|
||||
{
|
||||
visualizer.Close();
|
||||
} else
|
||||
{
|
||||
visualizer.Break();
|
||||
}
|
||||
}
|
||||
else if (!isClosed && visualizer.isClosed)
|
||||
{
|
||||
visualizer.Break();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dcb9fe180ed013a44a474b2a4ece59fc
|
||||
timeCreated: 1476219856
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,87 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class DeletePointModule : PointModule
|
||||
{
|
||||
public float deleteRadius = 50f;
|
||||
Vector2 lastMousePos = Vector2.zero;
|
||||
|
||||
|
||||
public DeletePointModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return IconContent("-", "remove", "Delete Points");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return IconContent("-", "remove_on", "Delete Points");
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
deleteRadius = LoadFloat("deleteRadius", 50f);
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveFloat("deleteRadius", deleteRadius);
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
deleteRadius = EditorGUILayout.FloatField("Brush Radius", deleteRadius);
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (selectedPoints.Count > 0) ClearSelection();
|
||||
Handles.BeginGUI();
|
||||
Handles.color = Color.red;
|
||||
Handles.DrawWireDisc(Event.current.mousePosition, -Vector3.forward, deleteRadius);
|
||||
Handles.color = Color.white;
|
||||
Handles.EndGUI();
|
||||
if (!eventModule.alt && SceneView.currentDrawingSceneView.camera.pixelRect.Contains(Event.current.mousePosition)) {
|
||||
if (editor.eventModule.mouseLeftDown) GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
|
||||
if (editor.eventModule.mouseLeft && lastMousePos != Event.current.mousePosition)
|
||||
{
|
||||
lastMousePos = Event.current.mousePosition;
|
||||
RunDeleteMethod();
|
||||
}
|
||||
}
|
||||
Repaint();
|
||||
}
|
||||
|
||||
void RunDeleteMethod()
|
||||
{
|
||||
Camera cam = SceneView.currentDrawingSceneView.camera;
|
||||
Vector3 mousPos = Event.current.mousePosition;
|
||||
Rect mouseRect = new Rect(mousPos.x - deleteRadius, mousPos.y - deleteRadius, deleteRadius * 2f, deleteRadius * 2f);
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
Vector3 localPos = cam.transform.InverseTransformPoint(points[i].position);
|
||||
if (localPos.z < 0f) continue;
|
||||
Vector2 screenPos = HandleUtility.WorldToGUIPoint(points[i].position);
|
||||
if (mouseRect.Contains(screenPos))
|
||||
{
|
||||
if (Vector2.Distance(mousPos, screenPos) <= deleteRadius)
|
||||
{
|
||||
DeletePoint(i);
|
||||
editor.ApplyModifiedProperties(true);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 031571ebca44b8a47a9af1984caeeff9
|
||||
timeCreated: 1476219856
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,350 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
public class MainPointModule : PointModule
|
||||
{
|
||||
public bool excludeSelected = false;
|
||||
public int minimumRectSize = 5;
|
||||
private Vector2 _rectStart = Vector2.zero;
|
||||
private Vector2 _rectEnd = Vector2.zero;
|
||||
private Rect _dragRect;
|
||||
private bool _drag = false;
|
||||
private bool _finalizeDrag = false;
|
||||
private bool _pointsMoved = false;
|
||||
private bool _tangentMode = false;
|
||||
private Color _bgColor = Color.black;
|
||||
|
||||
public static bool isSelecting => __isDragging;
|
||||
private static bool __holdInteraction = false;
|
||||
private static bool __isDragging = false;
|
||||
|
||||
|
||||
public bool isDragging
|
||||
{
|
||||
get
|
||||
{
|
||||
return _drag && _dragRect.width >= minimumRectSize && _dragRect.height >= minimumRectSize;
|
||||
}
|
||||
}
|
||||
|
||||
public bool tangentMode => _tangentMode;
|
||||
|
||||
public MainPointModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
_bgColor = Color.Lerp(color, Color.black, 0.75f);
|
||||
_bgColor.a = 0.75f;
|
||||
}
|
||||
|
||||
public static void HoldInteraction()
|
||||
{
|
||||
__holdInteraction = true;
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
string[] options = new string[points.Length + 4];
|
||||
options[0] = "- - -";
|
||||
if (selectedPoints.Count > 1) options[0] = "- Multiple -";
|
||||
options[1] = "All";
|
||||
options[2] = "None";
|
||||
options[3] = "Inverse";
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
options[i + 4] = "Point " + (i + 1);
|
||||
if (splineType == Spline.Type.Bezier)
|
||||
{
|
||||
switch (points[i].type)
|
||||
{
|
||||
case SplinePoint.Type.Broken: options[i + 4] += " - Broken"; break;
|
||||
case SplinePoint.Type.SmoothFree: options[i + 4] += " - Smooth Free"; break;
|
||||
case SplinePoint.Type.SmoothMirrored: options[i + 4] += " - Smooth Mirrored"; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
int option = 0;
|
||||
if (selectedPoints.Count == 1) {
|
||||
option = selectedPoints[0] + 4;
|
||||
}
|
||||
option = EditorGUILayout.Popup("Select", option, options);
|
||||
switch (option)
|
||||
{
|
||||
case 1:
|
||||
ClearSelection();
|
||||
for (int i = 0; i < points.Length; i++) AddPointSelection(i);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
ClearSelection();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
InverseSelection();
|
||||
break;
|
||||
}
|
||||
if(option >= 4)
|
||||
{
|
||||
SelectPoint(option - 4);
|
||||
}
|
||||
|
||||
if (isDragging)
|
||||
{
|
||||
if (!eventModule.mouseLeft)
|
||||
{
|
||||
FinishDrag();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (eventModule.v) return;
|
||||
Transform camTransform = SceneView.currentDrawingSceneView.camera.transform;
|
||||
if (!_drag)
|
||||
{
|
||||
if (_finalizeDrag)
|
||||
{
|
||||
if (_dragRect.width > 0f && _dragRect.height > 0f)
|
||||
{
|
||||
if (!eventModule.control) ClearSelection();
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
Vector2 guiPoint = HandleUtility.WorldToGUIPoint(points[i].position);
|
||||
if (_dragRect.Contains(guiPoint))
|
||||
{
|
||||
Vector3 local = camTransform.InverseTransformPoint(points[i].position);
|
||||
if (local.z >= 0f)
|
||||
{
|
||||
AddPointSelection(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_finalizeDrag = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (__holdInteraction)
|
||||
{
|
||||
CancelDrag();
|
||||
}
|
||||
else
|
||||
{
|
||||
_rectEnd = Event.current.mousePosition;
|
||||
_dragRect = new Rect(Mathf.Min(_rectStart.x, _rectEnd.x), Mathf.Min(_rectStart.y, _rectEnd.y), Mathf.Abs(_rectEnd.x - _rectStart.x), Mathf.Abs(_rectEnd.y - _rectStart.y));
|
||||
if (_dragRect.width >= minimumRectSize && _dragRect.height >= minimumRectSize)
|
||||
{
|
||||
Color col = highlightColor;
|
||||
col.a = 0.4f;
|
||||
Handles.BeginGUI();
|
||||
EditorGUI.DrawRect(_dragRect, col);
|
||||
Handles.EndGUI();
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
TextAnchor originalAlignment = GUI.skin.label.alignment;
|
||||
Color originalColor = GUI.skin.label.normal.textColor;
|
||||
|
||||
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
|
||||
GUI.skin.label.normal.textColor = color;
|
||||
|
||||
if (selectedPoints.Count > 1)
|
||||
{
|
||||
_tangentMode = false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
bool isSelected = selectedPoints.Contains(i);
|
||||
Vector3 lastPos = points[i].position;
|
||||
|
||||
if (splineType == Spline.Type.Bezier && isSelected)
|
||||
{
|
||||
Handles.color = color;
|
||||
|
||||
|
||||
if (Event.current.type == EventType.Repaint)
|
||||
Handles.DrawDottedLine(points[i].position, points[i].tangent, 4f);
|
||||
if (Event.current.type == EventType.Repaint)
|
||||
Handles.DrawDottedLine(points[i].position, points[i].tangent2, 4f);
|
||||
|
||||
if (_tangentMode && selectedPoints.Count == 1)
|
||||
{
|
||||
Handles.color = highlightColor;
|
||||
}
|
||||
|
||||
Vector3 lastTangentPos = points[i].tangent;
|
||||
|
||||
Vector3 newPos = SplineEditorHandles.FreeMoveCircle(points[i].tangent, HandleUtility.GetHandleSize(points[i].tangent) * 0.22f);
|
||||
if (lastTangentPos != newPos)
|
||||
{
|
||||
points[i].SetTangentPosition(newPos);
|
||||
RegisterChange();
|
||||
}
|
||||
lastTangentPos = points[i].tangent2;
|
||||
newPos = SplineEditorHandles.FreeMoveCircle(points[i].tangent2, HandleUtility.GetHandleSize(points[i].tangent2) * 0.22f);
|
||||
if (!__holdInteraction && lastTangentPos != newPos)
|
||||
{
|
||||
points[i].SetTangent2Position(newPos);
|
||||
RegisterChange();
|
||||
}
|
||||
|
||||
Handles.color = color;
|
||||
}
|
||||
|
||||
Handles.color = Color.clear;
|
||||
|
||||
if (showPointNumbers && camTransform.InverseTransformPoint(points[i].position).z > 0f)
|
||||
{
|
||||
if(Event.current.type == EventType.Repaint)
|
||||
{
|
||||
Handles.Label(points[i].position + Camera.current.transform.up * HandleUtility.GetHandleSize(points[i].position) * 0.3f, (i + 1).ToString());
|
||||
}
|
||||
}
|
||||
if (!eventModule.alt && !__holdInteraction)
|
||||
{
|
||||
if (excludeSelected && isSelected)
|
||||
{
|
||||
|
||||
SplineEditorHandles.FreeMoveRectangle(points[i].position, HandleUtility.GetHandleSize(points[i].position) * 0.1f);
|
||||
}
|
||||
else
|
||||
{
|
||||
points[i].SetPosition(SplineEditorHandles.FreeMoveRectangle(points[i].position, HandleUtility.GetHandleSize(points[i].position) * 0.1f));
|
||||
}
|
||||
}
|
||||
|
||||
if (!__holdInteraction && lastPos != points[i].position)
|
||||
{
|
||||
_tangentMode = false;
|
||||
_pointsMoved = true;
|
||||
if (isSelected)
|
||||
{
|
||||
for (int n = 0; n < selectedPoints.Count; n++)
|
||||
{
|
||||
if (selectedPoints[n] == i) continue;
|
||||
points[selectedPoints[n]].SetPosition(points[selectedPoints[n]].position + (points[i].position - lastPos));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectPoint(i);
|
||||
}
|
||||
RegisterChange();
|
||||
}
|
||||
|
||||
if (!_pointsMoved && !eventModule.alt && editor.eventModule.mouseLeftUp)
|
||||
{
|
||||
if(SplineEditorHandles.HoverArea(points[i].position, 0.12f))
|
||||
{
|
||||
if (eventModule.control && selectedPoints.Contains(i))
|
||||
{
|
||||
DeselectPoint(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (eventModule.shift) ShiftSelect(i, points.Length);
|
||||
else if (eventModule.control) AddPointSelection(i);
|
||||
else SelectPoint(i);
|
||||
}
|
||||
_tangentMode = false;
|
||||
} else if(splineType == Spline.Type.Bezier)
|
||||
{
|
||||
if (SplineEditorHandles.HoverArea(points[i].tangent, 0.23f))
|
||||
{
|
||||
if (eventModule.shift) ShiftSelect(i, points.Length);
|
||||
else if (eventModule.control) AddPointSelection(i);
|
||||
else SelectPoint(i);
|
||||
_tangentMode = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!excludeSelected || !isSelected)
|
||||
{
|
||||
if (Event.current.type == EventType.Repaint)
|
||||
{
|
||||
SplineEditorHandles.DrawPoint(points[i].position, isSelected && (!_tangentMode || selectedPoints.Count != 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
GUI.skin.label.alignment = originalAlignment;
|
||||
GUI.skin.label.normal.textColor = originalColor;
|
||||
|
||||
if (isDragging && Event.current.type == EventType.MouseDrag)
|
||||
{
|
||||
bool mouseIsOutside = false;
|
||||
#if UNITY_2022_1_OR_NEWER
|
||||
Vector2 mousePos = Event.current.mousePosition;
|
||||
Vector2 viewportSize = new Vector2(_currentSceneView.position.width, _currentSceneView.position.height);
|
||||
mouseIsOutside = mousePos.x <= 0 || mousePos.y <= 0f || mousePos.x >= viewportSize.x || mousePos.y >= viewportSize.y;
|
||||
#else
|
||||
mouseIsOutside = !SceneView.currentDrawingSceneView.camera.pixelRect.Contains(Event.current.mousePosition);
|
||||
#endif
|
||||
if (eventModule.alt || mouseIsOutside || !eventModule.mouseLeft)
|
||||
{
|
||||
FinishDrag();
|
||||
}
|
||||
}
|
||||
|
||||
if (eventModule.mouseLeftUp)
|
||||
{
|
||||
_pointsMoved = false;
|
||||
}
|
||||
|
||||
__holdInteraction = false;
|
||||
|
||||
__isDragging = isDragging;
|
||||
}
|
||||
|
||||
void ShiftSelect(int index, int pointCount)
|
||||
{
|
||||
if (selectedPoints.Count == 0)
|
||||
{
|
||||
AddPointSelection(index);
|
||||
return;
|
||||
}
|
||||
int minSelected = pointCount-1, maxSelected = 0;
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
if (minSelected > selectedPoints[i]) minSelected = selectedPoints[i];
|
||||
if (maxSelected < selectedPoints[i]) maxSelected = selectedPoints[i];
|
||||
}
|
||||
|
||||
if(index > maxSelected)
|
||||
{
|
||||
for (int i = maxSelected + 1; i <= index; i++) AddPointSelection(i);
|
||||
} else if(index < minSelected)
|
||||
{
|
||||
for (int i = minSelected-1; i >= index; i--) AddPointSelection(i);
|
||||
} else
|
||||
{
|
||||
for (int i = minSelected + 1; i <= index; i++) AddPointSelection(i);
|
||||
}
|
||||
}
|
||||
|
||||
public void StartDrag(Vector2 position)
|
||||
{
|
||||
if (__holdInteraction) return;
|
||||
_rectStart = position;
|
||||
_drag = true;
|
||||
_finalizeDrag = false;
|
||||
}
|
||||
|
||||
public void FinishDrag()
|
||||
{
|
||||
if (!_drag) return;
|
||||
_drag = false;
|
||||
_finalizeDrag = true;
|
||||
}
|
||||
|
||||
public void CancelDrag()
|
||||
{
|
||||
_drag = false;
|
||||
_finalizeDrag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 222164afd4e1e1845bca6b747eca74fb
|
||||
timeCreated: 1476960765
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,371 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
|
||||
public class PointMirrorModule : PointTransformModule
|
||||
{
|
||||
public enum Axis { X, Y, Z }
|
||||
public Axis axis = Axis.X;
|
||||
public bool flip = false;
|
||||
public float weldDistance = 0f;
|
||||
Vector3 mirrorCenter = Vector3.zero;
|
||||
|
||||
|
||||
private SplinePoint[] mirrored = new SplinePoint[0];
|
||||
|
||||
|
||||
public PointMirrorModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
LoadState();
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return IconContent("||", "mirror", "Mirror Path");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return IconContent("||", "mirror_on", "Mirror Path");
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
axis = (Axis)LoadInt("axis");
|
||||
flip = LoadBool("flip");
|
||||
weldDistance = LoadFloat("weldDistance");
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveInt("axis", (int)axis);
|
||||
SaveBool("flip", flip);
|
||||
SaveFloat("weldDistance", weldDistance);
|
||||
}
|
||||
|
||||
public override void Select()
|
||||
{
|
||||
base.Select();
|
||||
ClearSelection();
|
||||
DoMirror();
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
public override void Deselect()
|
||||
{
|
||||
if (IsDirty())
|
||||
{
|
||||
if (EditorUtility.DisplayDialog("Unapplied Mirror Operation", "There is an unapplied mirror operation. Do you want to apply the changes?", "Apply", "Revert"))
|
||||
{
|
||||
Apply();
|
||||
}
|
||||
else
|
||||
{
|
||||
Revert();
|
||||
}
|
||||
}
|
||||
base.Deselect();
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
if (selectedPoints.Count > 0) ClearSelection();
|
||||
EditorGUI.BeginChangeCheck();
|
||||
axis = (Axis)EditorGUILayout.EnumPopup("Axis", axis);
|
||||
flip = EditorGUILayout.Toggle("Flip", flip);
|
||||
weldDistance = EditorGUILayout.FloatField("Weld Distance", weldDistance);
|
||||
mirrorCenter = EditorGUILayout.Vector3Field("Center", mirrorCenter);
|
||||
if (EditorGUI.EndChangeCheck()) DoMirror();
|
||||
if (IsDirty())
|
||||
{
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
if (GUILayout.Button("Apply")) Apply();
|
||||
if (GUILayout.Button("Revert")) Revert();
|
||||
EditorGUILayout.EndHorizontal();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (selectedPoints.Count > 0) ClearSelection();
|
||||
Vector3 worldCenter = TransformPosition(mirrorCenter);
|
||||
Vector3 lastCenter = worldCenter;
|
||||
worldCenter = Handles.PositionHandle(worldCenter, rotation);
|
||||
mirrorCenter = InverseTransformPosition(worldCenter);
|
||||
DrawMirror();
|
||||
if (lastCenter != worldCenter) DoMirror();
|
||||
selectedPoints.Clear();
|
||||
}
|
||||
|
||||
public void DoMirror()
|
||||
{
|
||||
List<int> half = GetHalf(ref originalPoints);
|
||||
int welded = -1;
|
||||
if (half.Count > 0)
|
||||
{
|
||||
if (flip)
|
||||
{
|
||||
if (IsWeldable(originalPoints[half[0]]))
|
||||
{
|
||||
welded = half[0];
|
||||
half.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (IsWeldable(originalPoints[half[half.Count - 1]]))
|
||||
{
|
||||
welded = half[half.Count - 1];
|
||||
half.RemoveAt(half.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
int offset = welded >= 0 ? 1 : 0;
|
||||
int mirroredLength = half.Count * 2 + offset;
|
||||
if(mirrored.Length != mirroredLength) mirrored = new SplinePoint[mirroredLength];
|
||||
for (int i = 0; i < half.Count; i++)
|
||||
{
|
||||
if (flip)
|
||||
{
|
||||
mirrored[i] = new SplinePoint(originalPoints[half[(half.Count - 1) - i]]);
|
||||
mirrored[i + half.Count + offset] = GetMirrored(originalPoints[half[i]]);
|
||||
SwapTangents(ref mirrored[i]);
|
||||
SwapTangents(ref mirrored[i + half.Count + offset]);
|
||||
}
|
||||
else
|
||||
{
|
||||
mirrored[i] = new SplinePoint(originalPoints[half[i]]);
|
||||
mirrored[i + half.Count + offset] = GetMirrored(originalPoints[half[(half.Count - 1) - i]]);
|
||||
}
|
||||
}
|
||||
if (welded >= 0)
|
||||
{
|
||||
mirrored[half.Count] = new SplinePoint(originalPoints[welded]);
|
||||
if (flip) SwapTangents(ref mirrored[half.Count]);
|
||||
MakeMiddlePoint(ref mirrored[half.Count]);
|
||||
}
|
||||
|
||||
if (isClosed && mirrored.Length > 0)
|
||||
{
|
||||
MakeMiddlePoint(ref mirrored[0]);
|
||||
mirrored[mirrored.Length - 1] = new SplinePoint(mirrored[0]);
|
||||
}
|
||||
}
|
||||
else mirrored = new SplinePoint[0];
|
||||
editor.SetPointsArray(mirrored);
|
||||
RegisterChange();
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
void SwapTangents(ref SplinePoint point)
|
||||
{
|
||||
Vector3 temp = point.tangent;
|
||||
point.tangent = point.tangent2;
|
||||
point.tangent2 = temp;
|
||||
}
|
||||
|
||||
void MakeMiddlePoint(ref SplinePoint point)
|
||||
{
|
||||
point.type = SplinePoint.Type.Broken;
|
||||
InverseTransformPoint(ref point);
|
||||
Vector3 newPos = point.position;
|
||||
switch (axis)
|
||||
{
|
||||
case Axis.X:
|
||||
|
||||
newPos.x = mirrorCenter.x;
|
||||
point.SetPosition(newPos);
|
||||
if ((point.tangent.x >= mirrorCenter.x && flip) || (point.tangent.x <= mirrorCenter.x && !flip))
|
||||
{
|
||||
point.tangent2 = point.tangent;
|
||||
point.SetTangent2X(point.position.x + (point.position.x - point.tangent.x));
|
||||
}
|
||||
else
|
||||
{
|
||||
point.tangent = point.tangent2;
|
||||
point.SetTangentX(point.position.x + (point.position.x - point.tangent2.x));
|
||||
}
|
||||
break;
|
||||
case Axis.Y:
|
||||
newPos.y = mirrorCenter.y;
|
||||
point.SetPosition(newPos);
|
||||
if ((point.tangent.y >= mirrorCenter.y && flip) || (point.tangent.y <= mirrorCenter.y && !flip))
|
||||
{
|
||||
point.tangent2 = point.tangent;
|
||||
point.SetTangent2Y(point.position.y + (point.position.y - point.tangent.y));
|
||||
}
|
||||
else
|
||||
{
|
||||
point.tangent = point.tangent2;
|
||||
point.SetTangentY(point.position.y + (point.position.y - point.tangent2.y));
|
||||
}
|
||||
break;
|
||||
case Axis.Z:
|
||||
newPos.z = mirrorCenter.z;
|
||||
point.SetPosition(newPos);
|
||||
if ((point.tangent.z >= mirrorCenter.z && flip) || (point.tangent.z <= mirrorCenter.z && !flip))
|
||||
{
|
||||
point.tangent2 = point.tangent;
|
||||
point.SetTangent2Z(point.position.z + (point.position.z - point.tangent.z));
|
||||
}
|
||||
else
|
||||
{
|
||||
point.tangent = point.tangent2;
|
||||
point.SetTangentZ(point.position.z + (point.position.z - point.tangent2.z));
|
||||
}
|
||||
break;
|
||||
}
|
||||
TransformPoint(ref point);
|
||||
}
|
||||
|
||||
bool IsWeldable(SplinePoint point)
|
||||
{
|
||||
switch (axis)
|
||||
{
|
||||
case Axis.X:
|
||||
if (Mathf.Abs(point.position.x - mirrorCenter.x) <= weldDistance) return true;
|
||||
break;
|
||||
case Axis.Y:
|
||||
if (Mathf.Abs(point.position.y - mirrorCenter.y) <= weldDistance) return true;
|
||||
break;
|
||||
case Axis.Z:
|
||||
if (Mathf.Abs(point.position.z - mirrorCenter.z) <= weldDistance) return true;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DrawMirror()
|
||||
{
|
||||
Vector3[] points = new Vector3[4];
|
||||
Color color = Color.white;
|
||||
Vector3 worldCenter = TransformPosition(mirrorCenter);
|
||||
float size = HandleUtility.GetHandleSize(worldCenter);
|
||||
Vector3 forward = rotation * Vector3.forward * size;
|
||||
Vector3 back = -forward;
|
||||
Vector3 right = rotation * Vector3.right * size;
|
||||
Vector3 left = -right;
|
||||
Vector3 up = rotation * Vector3.up * size;
|
||||
Vector3 down = -up;
|
||||
switch (axis)
|
||||
{
|
||||
case Axis.X:
|
||||
points[0] = back + up;
|
||||
points[1] = forward + up;
|
||||
points[2] = forward + down;
|
||||
points[3] = back + down;
|
||||
color = Color.red;
|
||||
break;
|
||||
case Axis.Y:
|
||||
points[0] = back + left;
|
||||
points[1] = forward + left;
|
||||
points[2] = forward + right;
|
||||
points[3] = back + right;
|
||||
color = Color.green;
|
||||
break;
|
||||
case Axis.Z:
|
||||
points[0] = left + up;
|
||||
points[1] = right + up;
|
||||
points[2] = right + down;
|
||||
points[3] = left + down;
|
||||
color = Color.blue;
|
||||
break;
|
||||
}
|
||||
Handles.color = color;
|
||||
Handles.DrawLine(worldCenter + points[0], worldCenter + points[1]);
|
||||
Handles.DrawLine(worldCenter + points[1], worldCenter + points[2]);
|
||||
Handles.DrawLine(worldCenter + points[2], worldCenter + points[3]);
|
||||
Handles.DrawLine(worldCenter + points[3], worldCenter + points[0]);
|
||||
Handles.color = Color.white;
|
||||
}
|
||||
|
||||
SplinePoint GetMirrored(SplinePoint source)
|
||||
{
|
||||
SplinePoint newPoint = new SplinePoint(source);
|
||||
InverseTransformPoint(ref newPoint);
|
||||
switch (axis)
|
||||
{
|
||||
case Axis.X:
|
||||
newPoint.SetPositionX(mirrorCenter.x - (newPoint.position.x - mirrorCenter.x));
|
||||
newPoint.SetNormalX(-newPoint.normal.x);
|
||||
newPoint.SetTangentX(mirrorCenter.x - (newPoint.tangent.x - mirrorCenter.x));
|
||||
newPoint.SetTangent2X(mirrorCenter.x - (newPoint.tangent2.x - mirrorCenter.x));
|
||||
break;
|
||||
case Axis.Y:
|
||||
newPoint.SetPositionY(mirrorCenter.y - (newPoint.position.y - mirrorCenter.y));
|
||||
newPoint.SetNormalY(-newPoint.normal.y);
|
||||
newPoint.SetTangentY(mirrorCenter.y - (newPoint.tangent.y - mirrorCenter.y));
|
||||
newPoint.SetTangent2Y(mirrorCenter.y - (newPoint.tangent2.y - mirrorCenter.y));
|
||||
break;
|
||||
case Axis.Z:
|
||||
newPoint.SetPositionZ(mirrorCenter.z - (newPoint.position.z - mirrorCenter.z));
|
||||
newPoint.SetNormalZ(-newPoint.normal.z);
|
||||
newPoint.SetTangentZ(mirrorCenter.z - (newPoint.tangent.z - mirrorCenter.z));
|
||||
newPoint.SetTangent2Z(mirrorCenter.z - (newPoint.tangent2.z - mirrorCenter.z));
|
||||
break;
|
||||
}
|
||||
SwapTangents(ref newPoint);
|
||||
TransformPoint(ref newPoint);
|
||||
return newPoint;
|
||||
}
|
||||
|
||||
|
||||
|
||||
List<int> GetHalf(ref SplinePoint[] points)
|
||||
{
|
||||
List<int> found = new List<int>();
|
||||
switch (axis)
|
||||
{
|
||||
case Axis.X:
|
||||
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
if (flip)
|
||||
{
|
||||
if (InverseTransformPosition(points[i].position).x >= mirrorCenter.x) found.Add(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (InverseTransformPosition(points[i].position).x <= mirrorCenter.x) found.Add(i);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Axis.Y:
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
if (flip)
|
||||
{
|
||||
if (InverseTransformPosition(points[i].position).y >= mirrorCenter.y) found.Add(i);
|
||||
else
|
||||
{
|
||||
if (InverseTransformPosition(points[i].position).y <= mirrorCenter.y) found.Add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Axis.Z:
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
if (flip)
|
||||
{
|
||||
if (InverseTransformPosition(points[i].position).z >= mirrorCenter.z) found.Add(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (InverseTransformPosition(points[i].position).z <= mirrorCenter.z) found.Add(i);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0562a8339f410c04180d98ba21fdafb3
|
||||
timeCreated: 1476814299
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,344 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEditor;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PointModule : EditorModule
|
||||
{
|
||||
protected bool isClosed
|
||||
{
|
||||
get { return editor.GetSplineClosed(); }
|
||||
}
|
||||
protected int sampleRate
|
||||
{
|
||||
get { return editor.GetSplineSampleRate(); }
|
||||
}
|
||||
protected Spline.Type splineType
|
||||
{
|
||||
get { return editor.GetSplineType(); }
|
||||
}
|
||||
protected Color color {
|
||||
get { return editor.drawColor; }
|
||||
}
|
||||
protected SplineEditor editor;
|
||||
|
||||
protected SerializedSplinePoint[] points {
|
||||
get { return editor.points; }
|
||||
set { editor.points = value; }
|
||||
}
|
||||
|
||||
protected List<int> selectedPoints
|
||||
{
|
||||
get { return editor.selectedPoints; }
|
||||
set { editor.selectedPoints = value; }
|
||||
}
|
||||
|
||||
public Vector3 center
|
||||
{
|
||||
get
|
||||
{
|
||||
Vector3 avg = Vector3.zero;
|
||||
if (points.Length == 0) return avg;
|
||||
for (int i = 0; i < points.Length; i++) avg += points[i].position;
|
||||
return avg / points.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 selectionCenter
|
||||
{
|
||||
get
|
||||
{
|
||||
Vector3 avg = Vector3.zero;
|
||||
if (selectedPoints.Count == 0) return avg;
|
||||
for (int i = 0; i < selectedPoints.Count; i++) avg += points[selectedPoints[i]].position;
|
||||
return avg / selectedPoints.Count;
|
||||
}
|
||||
}
|
||||
|
||||
protected EditorGUIEvents eventModule;
|
||||
|
||||
public delegate void UndoHandler(string title);
|
||||
public delegate void EmptyHandler();
|
||||
public delegate void IntHandler(int value);
|
||||
public delegate void IntArrayHandler(int[] values);
|
||||
|
||||
public Spline.Direction duplicationDirection = Spline.Direction.Forward;
|
||||
public Color highlightColor = Color.white;
|
||||
public bool showPointNumbers = false;
|
||||
|
||||
public event EmptyHandler onBeforeDeleteSelectedPoints;
|
||||
public event EmptyHandler onSelectionChanged;
|
||||
public event IntArrayHandler onDuplicatePoint;
|
||||
|
||||
private bool movePivot = false;
|
||||
private Vector3 idealPivot = Vector3.zero;
|
||||
|
||||
|
||||
|
||||
public PointModule(SplineEditor editor)
|
||||
{
|
||||
this.editor = editor;
|
||||
eventModule = editor.eventModule;
|
||||
}
|
||||
|
||||
protected override void RecordUndo(string title)
|
||||
{
|
||||
if (editor.undoHandler != null) editor.undoHandler(title);
|
||||
}
|
||||
|
||||
protected override void Repaint()
|
||||
{
|
||||
if (editor.repaintHandler != null) editor.repaintHandler();
|
||||
}
|
||||
|
||||
public override void BeforeSceneDraw(SceneView current)
|
||||
{
|
||||
base.BeforeSceneDraw(current);
|
||||
Event e = Event.current;
|
||||
|
||||
if (movePivot)
|
||||
{
|
||||
SceneView.lastActiveSceneView.pivot = Vector3.Lerp(SceneView.lastActiveSceneView.pivot, idealPivot, 0.02f);
|
||||
if (e.type == EventType.MouseDown || e.type == EventType.MouseUp) movePivot = false;
|
||||
if (Vector3.Distance(SceneView.lastActiveSceneView.pivot, idealPivot) <= 0.05f)
|
||||
{
|
||||
SceneView.lastActiveSceneView.pivot = idealPivot;
|
||||
movePivot = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Delete && HasSelection())
|
||||
{
|
||||
DeleteSelectedPoints();
|
||||
e.Use();
|
||||
}
|
||||
|
||||
if(e.type == EventType.ExecuteCommand && Tools.current == Tool.None)
|
||||
{
|
||||
switch (e.commandName)
|
||||
{
|
||||
case "FrameSelected":
|
||||
if (points.Length > 0)
|
||||
{
|
||||
e.commandName = "";
|
||||
FramePoints();
|
||||
e.Use();
|
||||
}
|
||||
break;
|
||||
case "SelectAll":
|
||||
e.commandName = "";
|
||||
ClearSelection();
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
AddPointSelection(i);
|
||||
}
|
||||
e.Use();
|
||||
break;
|
||||
|
||||
case "Duplicate":
|
||||
if (points.Length > 0 && selectedPoints.Count > 0)
|
||||
{
|
||||
e.commandName = "";
|
||||
DuplicateSelected();
|
||||
e.Use();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void DuplicateSelected()
|
||||
{
|
||||
if (selectedPoints.Count == 0) return;
|
||||
SplinePoint[] newPoints = new SplinePoint[points.Length + selectedPoints.Count];
|
||||
SplinePoint[] duplicated = new SplinePoint[selectedPoints.Count];
|
||||
editor.SetPointsCount(newPoints.Length);
|
||||
int index = 0;
|
||||
for (int i = 0; i < selectedPoints.Count; i++) duplicated[index++] = points[selectedPoints[i]].CreateSplinePoint();
|
||||
int min = points.Length - 1, max = 0;
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
if (selectedPoints[i] < min) min = selectedPoints[i];
|
||||
if (selectedPoints[i] > max) max = selectedPoints[i];
|
||||
}
|
||||
int[] selected = selectedPoints.ToArray();
|
||||
selectedPoints.Clear();
|
||||
if (duplicationDirection == Spline.Direction.Backward)
|
||||
{
|
||||
for (int i = 0; i < min; i++) newPoints[i] = points[i].CreateSplinePoint();
|
||||
for (int i = 0; i < duplicated.Length; i++)
|
||||
{
|
||||
newPoints[i + min] = duplicated[i];
|
||||
selectedPoints.Add(i + min);
|
||||
}
|
||||
for (int i = min; i < points.Length; i++) newPoints[i + duplicated.Length] = points[i].CreateSplinePoint();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i <= max; i++) newPoints[i] = points[i].CreateSplinePoint();
|
||||
for (int i = 0; i < duplicated.Length; i++)
|
||||
{
|
||||
newPoints[i + max + 1] = duplicated[i];
|
||||
selectedPoints.Add(i + max + 1);
|
||||
}
|
||||
for (int i = max + 1; i < points.Length; i++) newPoints[i + duplicated.Length] = points[i].CreateSplinePoint();
|
||||
}
|
||||
editor.SetPointsArray(newPoints);
|
||||
RegisterChange();
|
||||
if (onDuplicatePoint != null) onDuplicatePoint(selected);
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
}
|
||||
|
||||
public bool HasSelection()
|
||||
{
|
||||
return selectedPoints.Count > 0;
|
||||
}
|
||||
|
||||
public void ClearSelection()
|
||||
{
|
||||
selectedPoints.Clear();
|
||||
Repaint();
|
||||
if (editor.selectionChangeHandler != null) editor.selectionChangeHandler();
|
||||
if (onSelectionChanged != null) onSelectionChanged();
|
||||
}
|
||||
|
||||
protected void DeleteSelectedPoints()
|
||||
{
|
||||
if (onBeforeDeleteSelectedPoints != null)
|
||||
{
|
||||
onBeforeDeleteSelectedPoints();
|
||||
}
|
||||
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
DeletePoint(selectedPoints[i]);
|
||||
for (int n = i; n < selectedPoints.Count; n++)
|
||||
{
|
||||
selectedPoints[n]--;
|
||||
}
|
||||
}
|
||||
ClearSelection();
|
||||
RegisterChange();
|
||||
editor.ApplyModifiedProperties(true);
|
||||
}
|
||||
|
||||
protected void DeletePoint(int index)
|
||||
{
|
||||
editor.DeletePoint(index);
|
||||
RegisterChange();
|
||||
}
|
||||
|
||||
|
||||
public void InverseSelection()
|
||||
{
|
||||
List<int> inverse = new List<int>();
|
||||
for (int i = 0; i < (isClosed ? points.Length - 1 : points.Length); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (int j = 0; j < selectedPoints.Count; j++)
|
||||
{
|
||||
if (selectedPoints[j] == i)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) inverse.Add(i);
|
||||
}
|
||||
selectedPoints = new List<int>(inverse);
|
||||
Repaint();
|
||||
if (editor.selectionChangeHandler != null) editor.selectionChangeHandler();
|
||||
if (onSelectionChanged != null) onSelectionChanged();
|
||||
}
|
||||
|
||||
protected void SelectPoint(int index)
|
||||
{
|
||||
if (selectedPoints.Count == 1 && selectedPoints[0] == index) return;
|
||||
selectedPoints.Clear();
|
||||
selectedPoints.Add(index);
|
||||
Repaint();
|
||||
if (editor.selectionChangeHandler != null) editor.selectionChangeHandler();
|
||||
if (onSelectionChanged != null) onSelectionChanged();
|
||||
}
|
||||
|
||||
protected void DeselectPoint(int index)
|
||||
{
|
||||
if (selectedPoints.Contains(index))
|
||||
{
|
||||
selectedPoints.Remove(index);
|
||||
Repaint();
|
||||
if (editor.selectionChangeHandler != null) editor.selectionChangeHandler();
|
||||
if (onSelectionChanged != null) onSelectionChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected void SelectPoints(List<int> indices)
|
||||
{
|
||||
selectedPoints.Clear();
|
||||
for (int i = 0; i < indices.Count; i++)
|
||||
{
|
||||
selectedPoints.Add(indices[i]);
|
||||
}
|
||||
Repaint();
|
||||
if (editor.selectionChangeHandler != null) editor.selectionChangeHandler();
|
||||
if (onSelectionChanged != null) onSelectionChanged();
|
||||
}
|
||||
|
||||
protected void AddPointSelection(int index)
|
||||
{
|
||||
if (selectedPoints.Contains(index)) return;
|
||||
selectedPoints.Add(index);
|
||||
Repaint();
|
||||
if (editor.selectionChangeHandler != null) editor.selectionChangeHandler();
|
||||
if (onSelectionChanged != null) onSelectionChanged();
|
||||
}
|
||||
|
||||
protected void FramePoints()
|
||||
{
|
||||
if (points.Length == 0) return;
|
||||
Vector3 center = Vector3.zero;
|
||||
Camera camera = SceneView.lastActiveSceneView.camera;
|
||||
Transform cam = camera.transform;
|
||||
Vector3 min = Vector3.zero, max = Vector3.zero;
|
||||
if (HasSelection())
|
||||
{
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
center += points[selectedPoints[i]].position;
|
||||
Vector3 local = cam.InverseTransformPoint(points[selectedPoints[i]].position);
|
||||
if (local.x < min.x) min.x = local.x;
|
||||
if (local.y < min.y) min.y = local.y;
|
||||
if (local.z < min.z) min.z = local.z;
|
||||
if (local.x > max.x) max.x = local.x;
|
||||
if (local.y > max.y) max.y = local.y;
|
||||
if (local.z > max.z) max.z = local.z;
|
||||
}
|
||||
center /= selectedPoints.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
center += points[i].position;
|
||||
Vector3 local = cam.InverseTransformPoint(points[i].position);
|
||||
if (local.x < min.x) min.x = local.x;
|
||||
if (local.y < min.y) min.y = local.y;
|
||||
if (local.z < min.z) min.z = local.z;
|
||||
if (local.x > max.x) max.x = local.x;
|
||||
if (local.y > max.y) max.y = local.y;
|
||||
if (local.z > max.z) max.z = local.z;
|
||||
}
|
||||
center /= points.Length;
|
||||
}
|
||||
movePivot = true;
|
||||
idealPivot = center;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b030b7d8eb93a004185129c729549ba8
|
||||
timeCreated: 1476220001
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,162 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PointMoveModule : PointTransformModule
|
||||
{
|
||||
public bool snap = false;
|
||||
public float snapGridSize = 1f;
|
||||
public bool surfaceMode = false;
|
||||
public float surfaceOffset = 0f;
|
||||
public LayerMask surfaceLayerMask = ~0;
|
||||
|
||||
private bool useTangentHandles => editor.mainModule.tangentMode || editor.selectedPoints.Count != 1;
|
||||
|
||||
public PointMoveModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return EditorGUIUtility.IconContent("MoveTool");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return EditorGUIUtility.IconContent("MoveTool On");
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
snap = LoadBool("snap");
|
||||
snapGridSize = LoadFloat("snapGridSize", 0.5f);
|
||||
surfaceOffset = LoadFloat("surfaceOffset", 0f);
|
||||
surfaceMode = LoadBool("surfaceMode");
|
||||
surfaceLayerMask = LoadInt("surfaceLayerMask", ~0);
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveBool("snap", snap);
|
||||
SaveFloat("snapGridSize", snapGridSize);
|
||||
SaveFloat("surfaceOffset", surfaceOffset);
|
||||
SaveBool("surfaceMode", surfaceMode);
|
||||
SaveInt("surfaceLayerMask", surfaceLayerMask);
|
||||
}
|
||||
|
||||
public override void BeforeSceneDraw(SceneView current)
|
||||
{
|
||||
base.BeforeSceneDraw(current);
|
||||
if (Event.current.type == EventType.MouseUp) GetRotation();
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
editSpace = (EditSpace)EditorGUILayout.EnumPopup("Edit Space", editSpace);
|
||||
surfaceMode = EditorGUILayout.Toggle("Move On Surface", surfaceMode);
|
||||
if (surfaceMode)
|
||||
{
|
||||
surfaceLayerMask = DreamteckEditorGUI.LayermaskField("Surface Mask", surfaceLayerMask);
|
||||
surfaceOffset = EditorGUILayout.FloatField("Surface Offset", surfaceOffset);
|
||||
}
|
||||
snap = EditorGUILayout.Toggle("Snap to Grid", snap);
|
||||
if (snap)
|
||||
{
|
||||
snapGridSize = EditorGUILayout.FloatField("Grid Size", snapGridSize);
|
||||
if (snapGridSize < 0.0001f) snapGridSize = 0.0001f;
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 SurfaceMoveHandle(Vector3 inputPosition, float size = 0.2f)
|
||||
{
|
||||
Vector3 lastPosition = inputPosition;
|
||||
inputPosition = SplineEditorHandles.FreeMoveHandle(inputPosition, HandleUtility.GetHandleSize(inputPosition) * size, Vector3.zero, Handles.CircleHandleCap);
|
||||
if (lastPosition != inputPosition)
|
||||
{
|
||||
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(ray, out hit, Mathf.Infinity, surfaceLayerMask))
|
||||
{
|
||||
inputPosition = hit.point + hit.normal * surfaceOffset;
|
||||
Handles.DrawLine(hit.point, hit.point + hit.normal * HandleUtility.GetHandleSize(hit.point) * 0.5f);
|
||||
}
|
||||
}
|
||||
return inputPosition;
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (selectedPoints.Count == 0) return;
|
||||
Vector3 c = selectionCenter;
|
||||
Vector3 lastPos = c;
|
||||
if (surfaceMode)
|
||||
{
|
||||
c = SurfaceMoveHandle(c, 0.2f);
|
||||
}
|
||||
else
|
||||
{
|
||||
c = Handles.PositionHandle(c, rotation);
|
||||
}
|
||||
if (lastPos != c)
|
||||
{
|
||||
RegisterChange();
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
points[selectedPoints[i]].SetPosition(points[selectedPoints[i]].position + (c - lastPos));
|
||||
if (snap) points[selectedPoints[i]].SetPosition(SnapPoint(points[selectedPoints[i]].position));
|
||||
}
|
||||
}
|
||||
|
||||
if (splineType == Spline.Type.Bezier && selectedPoints.Count == 1 && useTangentHandles)
|
||||
{
|
||||
int index = selectedPoints[0];
|
||||
lastPos = points[index].tangent;
|
||||
Vector3 newPos = Vector3.zero;
|
||||
if (surfaceMode)
|
||||
{
|
||||
newPos = SurfaceMoveHandle(points[index].tangent, 0.15f);
|
||||
} else
|
||||
{
|
||||
newPos = Handles.PositionHandle(points[index].tangent, rotation);
|
||||
}
|
||||
|
||||
if (snap) newPos = SnapPoint(newPos);
|
||||
if (newPos != lastPos)
|
||||
{
|
||||
RegisterChange();
|
||||
}
|
||||
points[index].SetTangentPosition(newPos);
|
||||
|
||||
lastPos = points[index].tangent2;
|
||||
if (surfaceMode)
|
||||
{
|
||||
newPos = SurfaceMoveHandle(points[index].tangent2, 0.15f);
|
||||
} else
|
||||
{
|
||||
newPos = Handles.PositionHandle(points[index].tangent2, rotation);
|
||||
}
|
||||
|
||||
if (snap) newPos = SnapPoint(newPos);
|
||||
if (newPos != lastPos)
|
||||
{
|
||||
RegisterChange();
|
||||
}
|
||||
points[index].SetTangent2Position(newPos);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 SnapPoint(Vector3 point)
|
||||
{
|
||||
point.x = Mathf.RoundToInt(point.x / snapGridSize) * snapGridSize;
|
||||
point.y = Mathf.RoundToInt(point.y / snapGridSize) * snapGridSize;
|
||||
point.z = Mathf.RoundToInt(point.z / snapGridSize) * snapGridSize;
|
||||
return point;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 37e5af750f2ba6a49bd760d3a39d1309
|
||||
timeCreated: 1476219856
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,290 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
public class PointNormalModule : PointModule
|
||||
{
|
||||
public enum NormalMode { Auto, Free }
|
||||
public NormalMode normalMode = NormalMode.Auto;
|
||||
SplineSample evalResult = new SplineSample();
|
||||
|
||||
private string[] _normalOperations = new string[0];
|
||||
private int _normalOperation = 0;
|
||||
|
||||
private NormalRotationWindow _rotationWindow;
|
||||
|
||||
public PointNormalModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
_normalOperations = new string[] { "Flip",
|
||||
"Look At Camera",
|
||||
"Align with Camera",
|
||||
"Calculate",
|
||||
"Look Left",
|
||||
"Look Right",
|
||||
"Look Up",
|
||||
"Look Down",
|
||||
"Look Forward",
|
||||
"Look Back",
|
||||
"Look At Avg. Center",
|
||||
"Perpendicular to Spline",
|
||||
"Rotate Degrees"
|
||||
};
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
normalMode = (NormalMode)LoadInt("normalMode");
|
||||
_normalOperation = LoadInt("normalOperation");
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveInt("normalMode", (int)normalMode);
|
||||
SaveInt("normalOperation", (int)_normalOperation);
|
||||
if (_rotationWindow != null)
|
||||
{
|
||||
_rotationWindow.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return IconContent("N", "normal", "Set Point Normals");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return IconContent("N", "normal_on", "Set Point Normals");
|
||||
}
|
||||
|
||||
private void OnNormalRotationApplied()
|
||||
{
|
||||
editor.ApplyModifiedProperties(true);
|
||||
RegisterChange();
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
|
||||
void SetNormals(int mode)
|
||||
{
|
||||
if (mode == 12)
|
||||
{
|
||||
_rotationWindow = EditorWindow.GetWindow<NormalRotationWindow>(true);
|
||||
_rotationWindow.Init(this, OnNormalRotationApplied);
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 avg = Vector3.zero;
|
||||
for (int i = 0; i < selectedPoints.Count; i++) avg += points[selectedPoints[i]].position;
|
||||
if (selectedPoints.Count > 1) avg /= selectedPoints.Count;
|
||||
Camera editorCamera = SceneView.lastActiveSceneView.camera;
|
||||
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case 0: points[selectedPoints[i]].normal *= -1; break;
|
||||
case 1: points[selectedPoints[i]].normal = Vector3.Normalize(editorCamera.transform.position - points[selectedPoints[i]].position); break;
|
||||
case 2: points[selectedPoints[i]].normal = editorCamera.transform.forward; break;
|
||||
case 3: points[selectedPoints[i]].normal = CalculatePointNormal(points, selectedPoints[i], isClosed); break;
|
||||
case 4: points[selectedPoints[i]].normal = Vector3.left; break;
|
||||
case 5: points[selectedPoints[i]].normal = Vector3.right; break;
|
||||
case 6: points[selectedPoints[i]].normal = Vector3.up; break;
|
||||
case 7: points[selectedPoints[i]].normal = Vector3.down; break;
|
||||
case 8: points[selectedPoints[i]].normal = Vector3.forward; break;
|
||||
case 9: points[selectedPoints[i]].normal = Vector3.back; break;
|
||||
case 10: points[selectedPoints[i]].normal = Vector3.Normalize(avg - points[selectedPoints[i]].position); break;
|
||||
case 11:
|
||||
SplineSample result = new SplineSample();
|
||||
editor.evaluateAtPoint(selectedPoints[i], ref result);
|
||||
points[selectedPoints[i]].normal = Vector3.Cross(result.forward, result.right).normalized;
|
||||
break;
|
||||
}
|
||||
}
|
||||
RegisterChange();
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
|
||||
public static Vector3 CalculatePointNormal(SerializedSplinePoint[] points, int index, bool isClosed)
|
||||
{
|
||||
if (points.Length < 3)
|
||||
{
|
||||
Debug.Log("Spline needs to have at least 3 control points in order to calculate normals");
|
||||
return Vector3.zero;
|
||||
}
|
||||
Vector3 side1 = Vector3.zero;
|
||||
Vector3 side2 = Vector3.zero;
|
||||
if (index == 0)
|
||||
{
|
||||
if (isClosed)
|
||||
{
|
||||
side1 = points[index].position - points[index + 1].position;
|
||||
side2 = points[index].position - points[points.Length - 2].position;
|
||||
}
|
||||
else
|
||||
{
|
||||
side1 = points[0].position - points[1].position;
|
||||
side2 = points[0].position - points[2].position;
|
||||
}
|
||||
}
|
||||
else if (index == points.Length - 1)
|
||||
{
|
||||
side1 = points[points.Length - 1].position - points[points.Length - 3].position;
|
||||
side2 = points[points.Length - 1].position - points[points.Length - 2].position;
|
||||
}
|
||||
else
|
||||
{
|
||||
side1 = points[index].position - points[index + 1].position;
|
||||
side2 = points[index].position - points[index - 1].position;
|
||||
}
|
||||
return Vector3.Cross(side1.normalized, side2.normalized).normalized;
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
if (editor.is2D)
|
||||
{
|
||||
EditorGUILayout.LabelField("Normal editing unavailable in 2D Mode", EditorStyles.centeredGreyMiniLabel);
|
||||
return;
|
||||
}
|
||||
normalMode = (NormalMode)EditorGUILayout.EnumPopup("Normal Mode", normalMode);
|
||||
|
||||
|
||||
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUILayout.LabelField("Normal Operations");
|
||||
|
||||
EditorGUILayout.BeginVertical();
|
||||
|
||||
_normalOperation = EditorGUILayout.Popup(_normalOperation, _normalOperations);
|
||||
if (GUILayout.Button("Apply"))
|
||||
{
|
||||
SetNormals(_normalOperation);
|
||||
}
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
EditorGUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (editor.is2D) return;
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
if (normalMode == NormalMode.Free) FreeNormal(selectedPoints[i]);
|
||||
else AutoNormal(selectedPoints[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void AutoNormal(int index)
|
||||
{
|
||||
editor.evaluateAtPoint(index, ref evalResult);
|
||||
Handles.color = highlightColor;
|
||||
Handles.DrawWireDisc(points[index].position, evalResult.forward, HandleUtility.GetHandleSize(points[index].position) * 0.5f);
|
||||
Handles.color = color;
|
||||
Matrix4x4 matrix = Matrix4x4.TRS(points[index].position, evalResult.rotation, Vector3.one);
|
||||
Vector3 pos = points[index].position + points[index].normal * HandleUtility.GetHandleSize(points[index].position) * 0.5f;
|
||||
Handles.DrawLine(points[index].position, pos);
|
||||
Vector3 lastPos = pos;
|
||||
Vector3 lastLocalPos = matrix.inverse.MultiplyPoint(pos);
|
||||
pos = SplineEditorHandles.FreeMoveHandle(pos, HandleUtility.GetHandleSize(pos) * 0.1f, Vector3.zero, Handles.CircleHandleCap);
|
||||
if (pos != lastPos)
|
||||
{
|
||||
pos = matrix.inverse.MultiplyPoint(pos);
|
||||
Vector3 delta = pos - lastLocalPos;
|
||||
for (int n = 0; n < selectedPoints.Count; n++)
|
||||
{
|
||||
if (selectedPoints[n] == index) continue;
|
||||
editor.evaluateAtPoint(selectedPoints[n], ref evalResult);
|
||||
Matrix4x4 localMatrix = Matrix4x4.TRS(points[selectedPoints[n]].position, evalResult.rotation, Vector3.one);
|
||||
Vector3 localPos = localMatrix.inverse.MultiplyPoint(points[selectedPoints[n]].position + points[selectedPoints[n]].normal * HandleUtility.GetHandleSize(points[selectedPoints[n]].position) * 0.5f);
|
||||
localPos += delta;
|
||||
localPos.z = 0f;
|
||||
points[selectedPoints[n]].normal = (localMatrix.MultiplyPoint(localPos) - points[selectedPoints[n]].position).normalized;
|
||||
}
|
||||
pos.z = 0f;
|
||||
pos = matrix.MultiplyPoint(pos);
|
||||
points[index].normal = (pos - points[index].position).normalized;
|
||||
RegisterChange();
|
||||
}
|
||||
}
|
||||
|
||||
void FreeNormal(int index)
|
||||
{
|
||||
Handles.color = highlightColor;
|
||||
Handles.DrawWireDisc(points[index].position, points[index].normal, HandleUtility.GetHandleSize(points[index].position) * 0.25f);
|
||||
Handles.DrawWireDisc(points[index].position, points[index].normal, HandleUtility.GetHandleSize(points[index].position) * 0.5f);
|
||||
Handles.color = color;
|
||||
Handles.DrawLine(points[index].position, points[index].position + HandleUtility.GetHandleSize(points[index].position) * points[index].normal);
|
||||
Vector3 normalPos = points[index].position + points[index].normal * HandleUtility.GetHandleSize(points[index].position);
|
||||
Vector3 lastNormal = points[index].normal;
|
||||
normalPos = SplineEditorHandles.FreeMoveCircle(normalPos, HandleUtility.GetHandleSize(normalPos) * 0.1f);
|
||||
normalPos -= points[index].position;
|
||||
normalPos.Normalize();
|
||||
if (normalPos == Vector3.zero) normalPos = Vector3.up;
|
||||
if (lastNormal != normalPos)
|
||||
{
|
||||
Debug.Log(Random.Range(0, 10000));
|
||||
points[index].normal = normalPos;
|
||||
Quaternion delta = Quaternion.FromToRotation(lastNormal, normalPos);
|
||||
for (int n = 0; n < selectedPoints.Count; n++)
|
||||
{
|
||||
if (selectedPoints[n] == index) continue;
|
||||
points[selectedPoints[n]].normal = delta * points[selectedPoints[n]].normal;
|
||||
}
|
||||
RegisterChange();
|
||||
}
|
||||
}
|
||||
|
||||
private class NormalRotationWindow : EditorWindow
|
||||
{
|
||||
private float _angle = 0f;
|
||||
private PointNormalModule _normalModule;
|
||||
private System.Action _onRotationApplied;
|
||||
|
||||
public void Init(PointNormalModule module, System.Action onRotationApplied)
|
||||
{
|
||||
_normalModule = module;
|
||||
_onRotationApplied = onRotationApplied;
|
||||
titleContent = new GUIContent("Rotate Normal");
|
||||
minSize = maxSize = new Vector2(240, 90);
|
||||
_angle = EditorPrefs.GetFloat("Dreamteck.Splines.Editor.PointNormalModule.NormalRotationWindow.angle", 0f);
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
|
||||
{
|
||||
ApplyRotationAndClose();
|
||||
}
|
||||
|
||||
_angle = EditorGUILayout.FloatField("Angle", _angle);
|
||||
if (GUILayout.Button("Rotate"))
|
||||
{
|
||||
ApplyRotationAndClose();
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyRotationAndClose()
|
||||
{
|
||||
SplineSample sample = new SplineSample();
|
||||
for (int i = 0; i < _normalModule.selectedPoints.Count; i++)
|
||||
{
|
||||
int pointIndex = _normalModule.selectedPoints[i];
|
||||
_normalModule.editor.evaluateAtPoint(pointIndex, ref sample);
|
||||
Quaternion rotation = Quaternion.AngleAxis(-_angle, sample.forward);
|
||||
_normalModule.points[pointIndex].normal = rotation * _normalModule.points[pointIndex].normal;
|
||||
_normalModule.points[pointIndex].changed = true;
|
||||
}
|
||||
if (_onRotationApplied != null)
|
||||
{
|
||||
_onRotationApplied();
|
||||
}
|
||||
EditorPrefs.SetFloat("Dreamteck.Splines.Editor.PointNormalModule.NormalRotationWindow.angle", _angle);
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9bb1bdae9d59a748a61453d1696598f
|
||||
timeCreated: 1476219856
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,79 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PointRotateModule : PointTransformModule
|
||||
{
|
||||
public bool rotateNormals = true;
|
||||
public bool rotateTangents = true;
|
||||
|
||||
public PointRotateModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return EditorGUIUtility.IconContent("RotateTool");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return EditorGUIUtility.IconContent("RotateTool On");
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
rotateNormals = LoadBool("rotateNormals");
|
||||
rotateTangents = LoadBool("rotateTangents");
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveBool("rotateNormals", rotateNormals);
|
||||
SaveBool("rotateTangents", rotateTangents);
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
editSpace = (EditSpace)EditorGUILayout.EnumPopup("Edit Space", editSpace);
|
||||
rotateNormals = EditorGUILayout.Toggle("Rotate Normals", rotateNormals);
|
||||
rotateTangents = EditorGUILayout.Toggle("Rotate Tangents", rotateTangents);
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (selectedPoints.Count == 0) return;
|
||||
if (rotateNormals)
|
||||
{
|
||||
Handles.color = new Color(Color.yellow.r, Color.yellow.g, Color.yellow.b, 0.4f);
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
Vector3 normal = points[selectedPoints[i]].normal;
|
||||
normal *= HandleUtility.GetHandleSize(points[selectedPoints[i]].position);
|
||||
Handles.DrawLine(points[selectedPoints[i]].position, points[selectedPoints[i]].position + normal);
|
||||
SplineEditorHandles.DrawArrowCap(points[selectedPoints[i]].position + normal, Quaternion.LookRotation(normal), HandleUtility.GetHandleSize(points[selectedPoints[i]].position));
|
||||
}
|
||||
}
|
||||
Handles.color = Color.white;
|
||||
Quaternion lastRotation = rotation;
|
||||
rotation = Handles.RotationHandle(lastRotation, selectionCenter);
|
||||
if (lastRotation != rotation)
|
||||
{
|
||||
PrepareTransform();
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
var point = localPoints[selectedPoints[i]];
|
||||
TransformPoint(ref point, rotateNormals, rotateTangents);
|
||||
points[selectedPoints[i]].SetPoint(point);
|
||||
}
|
||||
RegisterChange();
|
||||
SetDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 241d10a7b944cc44e9a68f8a66f3b04d
|
||||
timeCreated: 1476219856
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,73 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PointScaleModule : PointTransformModule
|
||||
{
|
||||
public bool scaleSize = true;
|
||||
public bool scaleTangents = true;
|
||||
|
||||
|
||||
public PointScaleModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOff()
|
||||
{
|
||||
return EditorGUIUtility.IconContent("ScaleTool");
|
||||
}
|
||||
|
||||
public override GUIContent GetIconOn()
|
||||
{
|
||||
return EditorGUIUtility.IconContent("ScaleTool On");
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
scaleSize = LoadBool("scaleSize");
|
||||
scaleTangents = LoadBool("scaleTangents");
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveBool("scaleSize", scaleSize);
|
||||
SaveBool("scaleTangents", scaleTangents);
|
||||
}
|
||||
|
||||
protected override void OnDrawInspector()
|
||||
{
|
||||
editSpace = (EditSpace)EditorGUILayout.EnumPopup("Edit Space", editSpace);
|
||||
scaleSize = EditorGUILayout.Toggle("Scale Sizes", scaleSize);
|
||||
scaleTangents = EditorGUILayout.Toggle("Scale Tangents", scaleTangents);
|
||||
}
|
||||
|
||||
protected override void OnDrawScene()
|
||||
{
|
||||
if (selectedPoints.Count == 0) return;
|
||||
if (eventModule.mouseLeftUp)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
Vector3 lastScale = scale;
|
||||
Vector3 c = selectionCenter;
|
||||
scale = Handles.ScaleHandle(scale, c, rotation, HandleUtility.GetHandleSize(c));
|
||||
if (lastScale != scale)
|
||||
{
|
||||
PrepareTransform();
|
||||
for (int i = 0; i < selectedPoints.Count; i++)
|
||||
{
|
||||
var point = localPoints[selectedPoints[i]];
|
||||
TransformPoint(ref point, false, scaleTangents, scaleSize);
|
||||
points[selectedPoints[i]].SetPoint(point);
|
||||
}
|
||||
RegisterChange();
|
||||
SetDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2efaeffa226557a4183cdcf4cf6142a9
|
||||
timeCreated: 1476219856
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,177 @@
|
||||
namespace Dreamteck.Splines.Editor
|
||||
{
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PointTransformModule : PointModule
|
||||
{
|
||||
public enum EditSpace { World, Transform, Spline }
|
||||
public EditSpace editSpace = EditSpace.World;
|
||||
public Vector3 scale = Vector3.one, offset = Vector3.zero;
|
||||
protected Quaternion rotation = Quaternion.identity;
|
||||
protected Vector3 origin = Vector3.zero;
|
||||
protected SplinePoint[] originalPoints = new SplinePoint[0];
|
||||
protected SplinePoint[] localPoints = new SplinePoint[0];
|
||||
|
||||
private Matrix4x4 matrix = new Matrix4x4();
|
||||
private Matrix4x4 inverseMatrix = new Matrix4x4();
|
||||
private bool _unapplied = true;
|
||||
SplineSample evalResult = new SplineSample();
|
||||
|
||||
public PointTransformModule(SplineEditor editor) : base(editor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
GetRotation();
|
||||
origin = selectionCenter;
|
||||
scale = Vector3.one;
|
||||
matrix.SetTRS(origin, rotation, Vector3.one);
|
||||
inverseMatrix = matrix.inverse;
|
||||
localPoints = editor.GetPointsArray();
|
||||
for (int i = 0; i < localPoints.Length; i++) InverseTransformPoint(ref localPoints[i]);
|
||||
}
|
||||
|
||||
protected void GetRotation()
|
||||
{
|
||||
switch (editSpace)
|
||||
{
|
||||
case EditSpace.World: rotation = Quaternion.identity; break;
|
||||
case EditSpace.Transform: rotation = TransformUtility.GetRotation(editor.matrix); break;
|
||||
case EditSpace.Spline:
|
||||
if (editor.evaluate == null)
|
||||
{
|
||||
Debug.LogError("Unassigned handler evaluate for Spline Editor.");
|
||||
break;
|
||||
}
|
||||
if (selectedPoints.Count == 1)
|
||||
{
|
||||
editor.evaluate((double)selectedPoints[0] / (points.Length - 1), ref evalResult);
|
||||
rotation = evalResult.rotation;
|
||||
}
|
||||
else rotation = Quaternion.identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override void LoadState()
|
||||
{
|
||||
base.LoadState();
|
||||
editSpace = (EditSpace)LoadInt("editSpace");
|
||||
}
|
||||
|
||||
public override void SaveState()
|
||||
{
|
||||
base.SaveState();
|
||||
SaveInt("editSpace", (int)editSpace);
|
||||
}
|
||||
|
||||
protected void SetDirty()
|
||||
{
|
||||
RegisterChange();
|
||||
_unapplied = true;
|
||||
}
|
||||
|
||||
protected bool IsDirty()
|
||||
{
|
||||
return _unapplied;
|
||||
}
|
||||
|
||||
public virtual void Revert()
|
||||
{
|
||||
editor.SetPointsArray(originalPoints);
|
||||
editor.ApplyModifiedProperties();
|
||||
_unapplied = false;
|
||||
}
|
||||
|
||||
public virtual void Apply()
|
||||
{
|
||||
RegisterChange();
|
||||
CacheOriginalPoints();
|
||||
_unapplied = false;
|
||||
}
|
||||
|
||||
public override void Select()
|
||||
{
|
||||
base.Select();
|
||||
CacheOriginalPoints();
|
||||
}
|
||||
|
||||
public override void Deselect()
|
||||
{
|
||||
base.Deselect();
|
||||
_unapplied = false;
|
||||
}
|
||||
|
||||
private void CacheOriginalPoints()
|
||||
{
|
||||
originalPoints = editor.GetPointsArray();
|
||||
}
|
||||
|
||||
protected void PrepareTransform()
|
||||
{
|
||||
matrix.SetTRS(origin + offset, rotation, scale);
|
||||
}
|
||||
|
||||
protected Vector3 TransformPosition(Vector3 position)
|
||||
{
|
||||
return matrix.MultiplyPoint3x4(position);
|
||||
}
|
||||
|
||||
protected Vector3 InverseTransformPosition(Vector3 position)
|
||||
{
|
||||
return inverseMatrix.MultiplyPoint3x4(position);
|
||||
}
|
||||
|
||||
protected Vector3 TransformDirection(Vector3 direction)
|
||||
{
|
||||
return matrix.MultiplyVector(direction);
|
||||
}
|
||||
|
||||
protected Vector3 InverseTransformDirection(Vector3 direction)
|
||||
{
|
||||
return inverseMatrix.MultiplyVector(direction);
|
||||
}
|
||||
|
||||
protected void TransformPoint(ref SplinePoint point, bool normals = true, bool tangents = true, bool size = false)
|
||||
{
|
||||
if (tangents)
|
||||
{
|
||||
point.position = TransformPosition(point.position);
|
||||
point.tangent = TransformPosition(point.tangent);
|
||||
point.tangent2 = TransformPosition(point.tangent2);
|
||||
}
|
||||
else
|
||||
{
|
||||
point.SetPosition(TransformPosition(point.position));
|
||||
}
|
||||
if(normals) point.normal = TransformDirection(point.normal).normalized;
|
||||
if (size)
|
||||
{
|
||||
float avg = (scale.x + scale.y + scale.z) / 3f;
|
||||
point.size *= avg;
|
||||
}
|
||||
}
|
||||
|
||||
protected void InverseTransformPoint(ref SplinePoint point, bool normals = true, bool tangents = true, bool size = false)
|
||||
{
|
||||
if (tangents)
|
||||
{
|
||||
point.position = InverseTransformPosition(point.position);
|
||||
point.tangent = InverseTransformPosition(point.tangent);
|
||||
point.tangent2 = InverseTransformPosition(point.tangent2);
|
||||
} else point.SetPosition(TransformPosition(point.position));
|
||||
|
||||
if (normals) point.normal = InverseTransformDirection(point.normal).normalized;
|
||||
if (size)
|
||||
{
|
||||
float avg = (scale.x + scale.y + scale.z) / 3f;
|
||||
point.size /= avg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d24553f5bce60b458dabe3b6bca0158
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user