using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using UnityEditor;
[ExecuteInEditMode]
public class Test : MonoBehaviour
{
public Transform pTransform, aTransform, bTransform, qTransform;//aTransform, bTransform为控制点
public Transform targetTransform;
public float t = 0;
// Use this for initialization
void Awake ()
{
}
// Update is called once per frame
void Update ()
{
t = Mathf.Clamp01(t);
Vector3 p = pTransform.position;
Vector3 a = aTransform.position;
Vector3 b = bTransform.position;
Vector3 q = qTransform.position;
targetTransform.position = Interpolation.CalculateBezier(p, a - p, b - q, q, t);
}
void OnDrawGizmos()
//void OnDrawGizmosSelected()
{
Vector3 p = pTransform.position;
Vector3 a = aTransform.position;
Vector3 b = bTransform.position;
Vector3 q = qTransform.position;
float width = HandleUtility.GetHandleSize(transform.position);
Handles.DrawBezier(p,
q,
a - p,
b - q,
Color.red,
null,
width);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public static class Interpolation
{
public static Vector3 CalculateBezier(Vector3 p0 /*startPosition*/, Vector3 p1/*startTangent*/, Vector3 p2/*endTangent*/, Vector3 p3/*endPosition*/, float t)
{
float t2 = t * t;
float t3 = t2 * t;
float u = 1.0f - t;
float u2 = u * u;
float u3 = u2 * u;
return u3 * p0 + 3 * u2 * t * p1 + 3 * u * t2 * p2 + t3 * p3;
}
}
```
![你需要知道的贝塞尔曲线.png](http://upload-images.jianshu.io/upload_images/1239281-4823e6cd40ca6a70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
[http://en.wikipedia.org/wiki/B%C3%A9zier_curve
你需要知道的贝塞尔曲线
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...