/// <summary> /// Set the position of the tangent relatively to his point /// </summary> /// <param name="tangent"></param> /// <param name="newPos"></param> /// <param name="mirrorTangents"></param> /// <param name="mirrorScale"></param> public void SetPointSpaceTangent(PathTangentType tangent, Vector3 newPos, bool mirrorTangents, bool mirrorScale) { if (tangent == PathTangentType.In) { Vector3 deltaDirection = newPos - _inTangent; float deltaScale = deltaDirection.magnitude * Vector3.Dot(deltaDirection.normalized, _inTangent.normalized); _inTangent = newPos; if (mirrorTangents) { _outTangent = -_inTangent.normalized * _outTangent.magnitude; } if (mirrorScale) { _outTangent += _outTangent.normalized * deltaScale; } } else if (tangent == PathTangentType.Out) { Vector3 deltaDirection = newPos - _outTangent; float deltaScale = deltaDirection.magnitude * Vector3.Dot(deltaDirection.normalized, _outTangent.normalized); _outTangent = newPos; if (mirrorTangents) { _inTangent = -_outTangent.normalized * _inTangent.magnitude; } if (mirrorScale) { _inTangent += _inTangent.normalized * deltaScale; } } }
private static void DrawSelectedPointHandles(int selectedId, Vector3 handlePos, float handleSize) { PathPoint point = _currentPath.pathData[selectedId]; Handles.color = Color.yellow; if (Tools.current != Tool.Rotate) { //Draw tangents handles for (int i = 0; i < 2; i++) { Vector3 tangentPos = CurveSpaceToWorldSpace(point.GetObjectSpaceTangent((PathTangentType)i)); Handles.DrawAAPolyLine(6, tangentPos, handlePos); if (Handles.Button(tangentPos, Quaternion.identity, POINT_HANDLE_SIZE * handleSize, POINT_HANDLE_SIZE * 1.1f * handleSize, Handles.SphereHandleCap)) { _selectedTangent = (PathTangentType)i; } } } if (_selectedTangent < 0) //If no tangent is selected { switch (Tools.current) { case Tool.Rotate: //Rotate the point roll and tangents SetPointRotation(_currentPath.pathData[selectedId]); break; case Tool.Scale: //Scale the point SetPointScale(handlePos, _currentPath.pathData[selectedId]); break; default: //Moves curve the point point.position = MovePoint(handlePos); break; } } else { //Moves the curve tangent point PathTangentType oppositeTangent = (PathTangentType)(1 - ((int)_selectedTangent)); //Edits the position of the selected tangent Vector3 tangentPos = CurveSpaceToWorldSpace(point.GetObjectSpaceTangent((PathTangentType)_selectedTangent)); Vector3 newPos = MovePoint(tangentPos); bool mirrorScale = !Event.current.alt; _currentPath.pathData[selectedId].SetObjectSpaceTangent(_selectedTangent, newPos, true, mirrorScale); } }
/// <summary> /// Return the position of the tangent relatively to his point /// </summary> /// <param name="tangent"></param> public Vector3 GetPointSpaceTangent(PathTangentType tangent) { if (tangent == PathTangentType.In) { return(_inTangent); } else if (tangent == PathTangentType.Out) { return(_outTangent); } else { return(Vector3.zero); } }
/// <summary> /// Updates the scene editor /// </summary> public static void OnSceneUpdate(SceneView view) { Handles.color = Color.blue; for (int i = 0; i < _currentPath.pathData.Length; i++) { DrawPoint(i, view); } //Reset selection if press escape if (Event.current.keyCode == KeyCode.Escape && Event.current.type == EventType.KeyDown) { _selectedId = -1; _selectedTangent = (PathTangentType)(-1); DeselectPoint(); Event.current.Use(); } _currentPath.UpdatePath(); }
/// <summary> /// Return the position of the tangent relatively to the object /// </summary> /// <param name="tangent"></param> /// <param name="newPos"></param> /// <param name="mirrorTangents"></param> /// <param name="mirrorScale"></param> public void SetObjectSpaceTangent(PathTangentType tangent, Vector3 newPos, bool mirrorTangents, bool mirrorScale) { SetPointSpaceTangent(tangent, newPos - _position, mirrorTangents, mirrorScale); }
/// <summary> /// Return the position of the tangent relatively to the object /// </summary> /// <param name="tangent"></param> public Vector3 GetObjectSpaceTangent(PathTangentType tangent) { return(GetPointSpaceTangent(tangent) + _position); }
private static void SelectPoint(int index) { _selectedId = index; _selectedTangent = (PathTangentType)(-1); //Resets selected tangent index }