旋转Bezier曲线手柄不起作用

时间:2018-06-14 21:33:58

标签: unity3d

我创建了一个用手柄绘制的贝塞尔曲线。当我旋转容器游戏对象时,曲线会旋转,但锚点和控制点手柄不会旋转。我怀疑问题来自我的PathEditor类中的以下代码行。如何解决此轮换问题?

如果需要,请检查here以查找与PathEditor关联的其他类别,我决定不将它们放在此处以保持问题整洁。

PathEditor Draw()方法中的可疑行:

for (int i = 0; i < path.NumPoints; i++)
    {
        Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
        if (path[i] != newPos)
        {
            Undo.RecordObject(creator, "Move point");
            path.MovePoint(i, newPos);
        }
    }

PathEditor:

[CustomEditor(typeof(PathCreator))]
public class PathEditor : Editor {

PathCreator creator;
Path path;

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();
    EditorGUI.BeginChangeCheck();

    bool continuousControlPoints = GUILayout.Toggle(path.isContinuous, "Set Continuous Control Points");
    if (continuousControlPoints != path.isContinuous)
    {
        Undo.RecordObject(creator, "Toggle set continuous controls");
        path.isContinuous = continuousControlPoints;
    }

    if (EditorGUI.EndChangeCheck())
    {
        SceneView.RepaintAll();
    }
}

void OnSceneGUI()
{
    Input();
    Draw();
}

void Input()
 {
    Event guiEvent = Event.current;
    Vector2 mousePos = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).origin;

    if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift)
    {
        Undo.RecordObject(creator, "Add segment");
        path.AddSegment(mousePos);
    }
}

void Draw()
{

     Transform handleTransform = creator.transform;
    Quaternion handleRotation = Tools.pivotRotation == PivotRotation.Local ?
        handleTransform.rotation : Quaternion.identity;

    for (int i = 0; i < path.NumSegments; i++)
    {
        Vector2[] points = path.GetPointsInSegment(i);
        Handles.color = Color.black;

        Vector2 p0 = handleTransform.TransformPoint(points[0]);
        Vector2 p1 = handleTransform.TransformPoint(points[1]);
        Vector2 p2 = handleTransform.TransformPoint(points[2]);
        Vector2 p3 = handleTransform.TransformPoint(points[3]);

        Handles.DrawLine(p1, p0);
        Handles.DrawLine(p2, p3);
        Handles.DrawBezier(p0, p3, p1, p2, Color.green, null, 2);

    }


    Handles.color = Color.red;
    for (int i = 0; i < path.NumPoints; i++)
    {
        Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
        if (path[i] != newPos)
        {
            Undo.RecordObject(creator, "Move point");
            path.MovePoint(i, newPos);
        }
    }
}

void OnEnable()
{
    creator = (PathCreator)target;
    if (creator.path == null)
    {
        creator.CreatePath();
    }
    path = creator.path;
}
}

1 个答案:

答案 0 :(得分:2)

看起来您将点值存储在世界坐标中。当您转换对象时,这些点不会意识到这一点并继续绘制在相同的位置。

相反,将点存储在局部坐标中,并在每次绘制时应用矩阵变换。

编辑执行以下步骤以使其正常运行:

  1. Path中的所有点视为本地坐标(即相对于gameObject本身)
  2. PathEditor中,您应该使用目标的transform组件,以便在您希望从路径到场景设置值时随时应用转换。例如,在Input方法中,您应该像这样更正鼠标位置:

    mousePos = creator.transform.InverseTransformPoint(mousePos);

  3. Draw方法中,您实际上正在计算正确的handleTrasform矩阵和四元数handleRotation,但您没有应用它们:

    Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
    

    应该是:

    var pos = handleTransform.InverseTransformPoint(path[i]);
    Vector2 newPos = Handles.FreeMoveHandle(pos, handleRotation, .1f, Vector2.zero, Handles.CylinderHandleCap);
    

    您也可以在方法开头设置Handles.matrix,并自动处理比例。

    Handles.matrix = creator.transform.localToWorldMatrix;