gpt4 book ai didi

c# - 如何使用自定义编辑器在移动另一条贝塞尔曲线时更新一条贝塞尔曲线

转载 作者:IT王子 更新时间:2023-10-29 04:45:10 27 4
gpt4 key购买 nike

我正在使用从 here 获得的以下代码创建贝塞尔曲线.我还制作了一个 BezierPair 游戏对象,它有两条 Bézier 曲线作为子对象。

从下面的各个图像和 BezierPair,其中 points[0]...points[3] 表示为 P0.. .P3:

  1. 我希望每条贝塞尔曲线的 P0 在移动时始终保持不变。换句话说,我希望他们始终一起移动,并可选择关闭此移动。

enter image description here

  1. 假设两条曲线的 P1 是分开的。如何使每条曲线的 P1 沿相同方向移动,覆盖相同距离?

enter image description here

  1. 假设两条曲线的 P2 是分开的。我怎样才能使一条曲线的 P2 沿着连接 P0P3 的直线镜像另一条曲线的 P2?请注意,镜像线将取自下例中的曲线 1,因为 curve1P2 已移动。如果 curve2P2 被移动,那么镜像线将从 curve2P0P3 中获取。

enter image description here

我不想在运行时这样做。所以必须使用自定义编辑器。我尝试在下面的代码中解决 1. 但如果不在层次结构窗口中选择 BezierPair,第二条曲线的位置将不会更新

贝塞尔曲线:

public static class Bezier {

public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * oneMinusT * p0 +
3f * oneMinusT * oneMinusT * t * p1 +
3f * oneMinusT * t * t * p2 +
t * t * t * p3;
}

public static Vector3 GetFirstDerivative (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
3f * oneMinusT * oneMinusT * (p1 - p0) +
6f * oneMinusT * t * (p2 - p1) +
3f * t * t * (p3 - p2);
}
}

贝塞尔曲线:

[RequireComponent(typeof(LineRenderer))]
public class BezierCurve : MonoBehaviour {

public Vector3[] points;
LineRenderer lr;
public int numPoints = 49;
bool controlPointsChanged = false;

bool isMoving = false;

public void Reset () {
points = new Vector3[] {
new Vector3(1f, 0f, 0f),
new Vector3(2f, 0f, 0f),
new Vector3(3f, 0f, 0f),
new Vector3(4f, 0f, 0f)
};
}

void Start() {

lr = GetComponent<LineRenderer> ();
lr.positionCount = 0;
DrawBezierCurve ();

}
public Vector3 GetPoint (float t) {
return transform.TransformPoint(Bezier.GetPoint(points[0], points[1], points[2], points[3], t));
}

public void DrawBezierCurve () {
lr = GetComponent<LineRenderer> ();
lr.positionCount = 1;
lr.SetPosition(0, points[0]);

for (int i = 1; i < numPoints+1; i++) {
float t = i / (float)numPoints;
lr.positionCount = i+1;
lr.SetPosition(i, GetPoint(t));
}
}

public Vector3 GetVelocity (float t) {
return transform.TransformPoint(
Bezier.GetFirstDerivative(points[0], points[1], points[2], points[3], t)) - transform.position;
}

public Vector3 GetDirection (float t) {
return GetVelocity(t).normalized;
}
}

贝塞尔曲线编辑器:

[CustomEditor(typeof(BezierCurve))]
public class BezierCurveEditor : Editor {

private BezierCurve curve;
private Transform handleTransform;
private Quaternion handleRotation;

private const int lineSteps = 10;

private const float directionScale = 0.5f;

private void OnSceneGUI () {
curve = target as BezierCurve;
handleTransform = curve.transform;
handleRotation = Tools.pivotRotation == PivotRotation.Local ?
handleTransform.rotation : Quaternion.identity;

Vector3 p0 = ShowPoint(0);
Vector3 p1 = ShowPoint(1);
Vector3 p2 = ShowPoint(2);
Vector3 p3 = ShowPoint(3);

Handles.color = Color.gray;
Handles.DrawLine(p0, p1);
Handles.DrawLine(p2, p3);
Handles.DrawBezier(p0, p3, p1, p2, Color.white, null, 2f);

curve.DrawBezierCurve ();

if (GUI.changed) {
curve.DrawBezierCurve ();
EditorUtility.SetDirty( curve );
Repaint();
}

}


private void ShowDirections () {
Handles.color = Color.green;
Vector3 point = curve.GetPoint(0f);
Handles.DrawLine(point, point + curve.GetDirection(0f) * directionScale);
for (int i = 1; i <= lineSteps; i++) {
point = curve.GetPoint(i / (float)lineSteps);
Handles.DrawLine(point, point + curve.GetDirection(i / (float)lineSteps) * directionScale);
}
}

private Vector3 ShowPoint (int index) {
Vector3 point = handleTransform.TransformPoint(curve.points[index]);
EditorGUI.BeginChangeCheck();
point = Handles.DoPositionHandle(point, handleRotation);
if (EditorGUI.EndChangeCheck()) {
Undo.RecordObject(curve, "Move Point");
EditorUtility.SetDirty(curve);
curve.points[index] = handleTransform.InverseTransformPoint(point);
}
return point;
}
}

贝塞尔对:

public class BezierPair : MonoBehaviour {


public GameObject bez1;
public GameObject bez2;

public void setupCurves() {
bez1 = GameObject.Find("Bez1");
bez2 = GameObject.Find("Bez2");
}
}

贝塞尔对编辑器:

[CustomEditor(typeof(BezierPair))]
public class BezierPairEditor : Editor {

private BezierPair bezPair;

public override void OnInspectorGUI()
{
bezPair = target as BezierPair;

if (bezPair.bez1.GetComponent<BezierCurve>().points[0] != bezPair.bez2.GetComponent<BezierCurve>().points[0])
{
Vector3 assignPoint0 = bezPair.bez1.GetComponent<BezierCurve>().points[0];
bezPair.bez2.GetComponent<BezierCurve>().points[0] = assignPoint0;

}
if (GUI.changed)
{

EditorUtility.SetDirty(bezPair.bez1);
EditorUtility.SetDirty(bezPair.bez2);
Repaint();
}
}

最佳答案

已编辑:

我认为您不需要 BezierPair 类。我建议您在 BezierCurve 类 (paired) 上添加对要“配对”的其他 BezierCurve 对象的引用作为公共(public)字段.另一条曲线将与这条曲线“配对”。一旦配对,对运动的限制可能适用。您可以使用 3 个公共(public) bool 字段 behavior1behavior2behavior3 控制所需的行为。

注意#1:我没有从编辑器调用方法 DrawBezierCurve,而是将 [ExecuteInEditMode] 添加到组件类。这样,您就不会混淆组件和编辑器之间的职责:BezierCurve 组件在场景中自行绘制,而 BezierCurveEditor 仅管理编辑逻辑,例如应用约束和绘制处理程序。

贝塞尔曲线:

using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
[ExecuteInEditMode] // Makes Update() being called often even in Edit Mode
public class BezierCurve : MonoBehaviour
{

public Vector3[] points;
public int numPoints = 50;
// Curve that is paired with this curve
public BezierCurve paired;
public bool behavior1; // check on editor if you desired behavior 1 ON/OFF
public bool behavior2; // check on editor if you desired behavior 2 ON/OFF
public bool behavior3; // check on editor if you desired behavior 3 ON/OFF
LineRenderer lr;

void Reset()
{
points = new Vector3[]
{
new Vector3(1f, 0f, 0f),
new Vector3(2f, 0f, 0f),
new Vector3(3f, 0f, 0f),
new Vector3(4f, 0f, 0f)
};
}

void Start()
{
lr = GetComponent<LineRenderer>();
}

void Update()
{
// This component is the only responsible for drawing itself.
DrawBezierCurve();
}

// This method is called whenever a field is changed on Editor
void OnValidate()
{
// This avoids pairing with itself
if (paired == this) paired = null;
}

void DrawBezierCurve()
{
lr.positionCount = numPoints;
for (int i = 0; i < numPoints; i++)
{
// This corrects the "strange" extra point you had with your script.
float t = i / (float)(numPoints - 1);
lr.SetPosition(i, GetPoint(t));
}
}

public Vector3 GetPoint(float t)
{
return transform.TransformPoint(Bezier.GetPoint(points[0], points[1], points[2], points[3], t));
}

public Vector3 GetVelocity(float t)
{
return transform.TransformPoint(Bezier.GetFirstDerivative(points[0], points[1], points[2], points[3], t)) - transform.position;
}

public Vector3 GetDirection(float t)
{
return GetVelocity(t).normalized;
}
}

注意#2:所需的行为在处理程序绘制方法中进行了编码,因此您可以访问撤消和其他功能。

注意#3:EditorUtility.SetDirtyconsidered obsolete因为 Unity 5.3 用于将对象标记为脏的以供绘制,不应再用于修改场景中的对象Undo.RecordObject 完成这项工作。

贝塞尔曲线编辑器:

using UnityEngine;
using UnityEditor;

// This attribute allows you to select multiple curves and manipulate them all as a whole on Scene or Inspector
[CustomEditor(typeof(BezierCurve)), CanEditMultipleObjects]
public class BezierCurveEditor : Editor
{
BezierCurve curve;
Transform handleTransform;
Quaternion handleRotation;
const int lineSteps = 10;
const float directionScale = 0.5f;

BezierCurve prevPartner; // Useful later.

void OnSceneGUI()
{
curve = target as BezierCurve;
if (curve == null) return;
handleTransform = curve.transform;
handleRotation = Tools.pivotRotation == PivotRotation.Local ? handleTransform.rotation : Quaternion.identity;

Vector3 p0 = ShowPoint(0);
Vector3 p1 = ShowPoint(1);
Vector3 p2 = ShowPoint(2);
Vector3 p3 = ShowPoint(3);

Handles.color = Color.gray;
Handles.DrawLine(p0, p1);
Handles.DrawLine(p2, p3);
Handles.DrawBezier(p0, p3, p1, p2, Color.white, null, 2f);

// Handles multiple selection
var sel = Selection.GetFiltered(typeof(BezierCurve), SelectionMode.Editable);
if (sel.Length == 1)
{
// This snippet checks if you just attached or dettached another curve,
// so it updates the attached member in the other curve too automatically
if (prevPartner != curve.paired)
{
if (prevPartner != null) { prevPartner.paired = null; }
prevPartner = curve.paired;
}
}
if (curve.paired != null & curve.paired != curve)
{
// Pair the curves.
var partner = curve.paired;
partner.paired = curve;
partner.behavior1 = curve.behavior1;
partner.behavior2 = curve.behavior2;
partner.behavior3 = curve.behavior3;
}
}

// Constraints for a curve attached to back
// The trick here is making the object being inspected the "master" and the attached object is adjusted to it.
// This way, you avoid the conflict of one object trying to move the other.
// [ExecuteInEditMode] on component class makes it posible to have real-time drawing while editing.
// If you were calling DrawBezierCurve from here, you would only see updates on the other curve when you select it
Vector3 ShowPoint(int index)
{
var thisPts = curve.points;
Vector3 point = handleTransform.TransformPoint(thisPts[index]);
EditorGUI.BeginChangeCheck();
point = Handles.DoPositionHandle(point, handleRotation);
if (EditorGUI.EndChangeCheck())
{
if (curve.paired != null && curve.paired != curve)
{
Undo.RecordObjects(new Object[] { curve, curve.paired }, "Move Point " + index.ToString());
var pairPts = curve.paired.points;
var pairTransform = curve.paired.transform;
switch (index)
{
case 0:
{
if (curve.behavior1)
{
pairPts[0] = pairTransform.InverseTransformPoint(point);
}
break;
}
case 1:
{
if (curve.behavior2)
{
var p1 = handleTransform.TransformPoint(thisPts[1]);
pairPts[1] += pairTransform.InverseTransformVector(point - p1);
}
break;
}
case 2:
{
if (curve.behavior3)
{
var p0 = handleTransform.TransformPoint(thisPts[0]);
var p3 = handleTransform.TransformPoint(thisPts[3]);
var reflect = Vector3.Reflect(p3 - point, (p3 - p0).normalized);
pairPts[2] = pairTransform.InverseTransformPoint(p3 + reflect);
}
break;
}
default:
break;
}
}
else
{
Undo.RecordObject(curve, "Move Point " + index.ToString());
}
thisPts[index] = handleTransform.InverseTransformPoint(point);
}
return point;
}
}

要使其正常工作,请通过检查器将一个 BezierCurve 引用到另一个的配对字段,并设置 ON/OFF 您想要的行为。

提示:修改LineRenderer 的属性以获得炫酷的渐变或宽度变化(如笔触)。如果您有一个尖点节点并希望它看起来是连续的,请增加线渲染器上的 End Cap Vertices 的值。使用 Sprites-Default 作为 2D Material 。

关于c# - 如何使用自定义编辑器在移动另一条贝塞尔曲线时更新一条贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469571/

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