unity 贝塞尔曲线的制作

通过传入Vector3[]值,返回坐标点数组

public Vector3[] CreatPath(Vector3[] path,int SmoothSens) {

		Vector3[] vector3s = PathControlPointGenerator(path);
		Vector3 prevPt = Interp(vector3s, 0);

		int SmoothAmount = path.Length * SmoothSens;
		Vector3[] positons = new Vector3[SmoothAmount];
		for (int i = 1; i <= SmoothAmount; i++)
		{
			float pm = (float)i / SmoothAmount;
			Vector3 currPt = Interp(vector3s, pm);
			positons[i - 1] = currPt;
			if (i != SmoothAmount) {
				positons[i] = prevPt;
			}
			prevPt = currPt;
		}
		return positons;
	}

	private Vector3[] PathControlPointGenerator(Vector3[] path)
	{
		Vector3[] suppliedPath;
		Vector3[] vector3s;

		//create and store path points:
		suppliedPath = path;

		//populate calculate path;
		int offset = 2;
		vector3s = new Vector3[suppliedPath.Length + offset];
		Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);

		//populate start and end control points:
		//vector3s[0] = vector3s[1] - vector3s[2];
		vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
		vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);

		//is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
		if (vector3s[1] == vector3s[vector3s.Length - 2])
		{
			Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
			Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
			tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
			tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
			vector3s = new Vector3[tmpLoopSpline.Length];
			Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
		}
		return (vector3s);
	}

	public Vector3 Interp(Vector3[] pts, float t)
	{
		int numSections = pts.Length - 3;
		int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
		float u = t * (float)numSections - (float)currPt;

		Vector3 a = pts[currPt];
		Vector3 b = pts[currPt + 1];
		Vector3 c = pts[currPt + 2];
		Vector3 d = pts[currPt + 3];

		return .5f * (
			(-a + 3f * b - 3f * c + d) * (u * u * u)
			+ (2f * a - 5f * b + 4f * c - d) * (u * u)
			+ (-a + c) * u
			+ 2f * b
		);
	}




Unity制作贝塞尔曲线可以使用Unity自带的LineRenderer组件以及自定义脚本来实现。以下是一个简单的步骤: 1. 创建一个空物体作为贝塞尔曲线的父物体,并将LineRenderer组件添加到它上面。 2. 在脚本中定义贝塞尔曲线的起点、控制点和终点坐标,并根据这些点计算曲线上的点。 3. 将计算出来的曲线上的点赋值给LineRenderer组件的positions属性,使其显示出曲线。 这里是一个简单的例子代码,用于在Unity制作二次贝塞尔曲线: ```csharp using UnityEngine; [RequireComponent(typeof(LineRenderer))] public class BezierCurve : MonoBehaviour { public Vector3 startPoint; public Vector3 controlPoint; public Vector3 endPoint; public int vertexCount = 20; private LineRenderer lineRenderer; private void Start() { lineRenderer = GetComponent<LineRenderer>(); lineRenderer.positionCount = vertexCount; } private void Update() { for (int i = 0; i < vertexCount; i++) { float t = i / (float)(vertexCount - 1); Vector3 vertex = CalculateBezierPoint(startPoint, controlPoint, endPoint, t); lineRenderer.SetPosition(i, vertex); } } private Vector3 CalculateBezierPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t) { float u = 1 - t; float tt = t * t; float uu = u * u; Vector3 point = uu * p0; point += 2 * u * t * p1; point += tt * p2; return point; } } ``` 这个脚本可以添加到Unity场景中的一个空物体上,通过调整startPoint、controlPoint和endPoint的值来控制贝塞尔曲线的形状。vertexCount用于控制曲线上的点数,越大曲线越平滑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值