我正在寻找使用Catmull-Rom算法弯曲网格物体,但我不知道从哪里开始。我可以获得用于形成网格对象的每个三角形的位置(感谢Heisenbug)。
我已经开始写一些基本的数学来做弯曲,但我不确定它是否正确。有没有人可以指出我正确的方向?我希望能够在C#脚本中执行此操作,到目前为止我只有一个网格对象。
public static Vector3 PointOnCurve(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
Vector3 result = new Vector3();
float t0 = ((-t + 2f) * t - 1f) * t * 0.5f;
float t1 = (((3f * t - 5f) * t) * t + 2f) * 0.5f;
float t2 = ((-3f * t + 4f) * t + 1f) * t * 0.5f;
float t3 = ((t - 1f) * t * t) * 0.5f;
result.x = p0.x * t0 + p1.x * t1 + p2.x * t2 + p3.x * t3;
result.y = p0.y * t0 + p1.y * t1 + p2.y * t2 + p3.y * t3;
result.z = p0.z * t0 + p1.z * t1 + p2.z * t2 + p3.z * t3;
return result;
}
public Mesh CreateMesh(float width, float height)
{
Mesh m = new Mesh();
m.name = "ScriptedMesh";
m.vertices = new Vector3[] {
new Vector3(-width, -height, 0.01f),
new Vector3(width, -height, 0.01f),
new Vector3(width, height, 0.01f),
new Vector3(-width, height, 0.01f)
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
// -- retrieve mesh filter component that contains mesh data (points of each triangle)
Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
//
return m;
}