gpt4 book ai didi

unity-game-engine - 旋转贝塞尔曲线 handle 不起作用

转载 作者:行者123 更新时间:2023-12-02 12:18:11 24 4
gpt4 key购买 nike

我创建了一条用 handle 绘制的贝塞尔曲线。当我旋转容器游戏对象时,曲线会旋转,但 anchor 和控制点 handle 不会旋转。我怀疑问题出在我的 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);
}
}

路径编辑器:

[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. Path 中的所有点视为本地坐标(即相对于游戏对象本身)
  2. PathEditor 中,只要您想要设置从路径到场景的值,您就应该使用目标的 transform 组件来应用变换。例如,在 Input 方法中,您应该像这样更正鼠标位置:

    mousePos = Creator.transform.InverseTransformPoint(mousePos);

在您的 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;

关于unity-game-engine - 旋转贝塞尔曲线 handle 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50866262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com