How do i calculate mesh on a spline??
Hello i was wondering how do i calculate mesh in the middle of a catmullSpline like this in Pic1, does anyone know how to do this?? I would be very thankful if some one could help me.
Here is what i want to achieve.
-Pic1
Here is my Catmullspline (White wireSpheres = controlPointsList, Yellow Spheres = segmentPoints)
-Pic2
And here is the code
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class CatmullSpline : MonoBehaviour {
public List<Vector3> controlPointsList = new List<Transform>(); List<Vector3> segmentPoints = new List<Vector3>(); public bool isLooping = true; public int SEGMENT_COUNT; void Start() { CreateSegmentList(); } void OnDrawGizmos() { for (int i = 0; i < controlPointsList.Count; i++) { if ((i == 0 || i == controlPointsList.Count - 2 || i == controlPointsList.Count - 1) && !isLooping) { continue; } DisplayCatmullRomSpline(i); } Gizmos.color = Color.white; for (int i = 0; i < controlPointsList.Count; i++) { Gizmos.DrawWireSphere(controlPointsList[i], 0.3f); } } Vector3 ReturnCatmullRom(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) { Vector3 a = 0.5f * (2f * p1); Vector3 b = 0.5f * (p2 - p0); Vector3 c = 0.5f * (2f * p0 - 5f * p1 + 4f * p2 - p3); Vector3 d = 0.5f * (-p0 + 3f * p1 - 3f * p2 + p3); Vector3 pos = a + (b * t) + (c * t * t) + (d * t * t * t); return pos; } void DisplayCatmullRomSpline(int pos) { Vector3 p0 = controlPointsList[ClampListPos(pos - 1)]; Vector3 p1 = controlPointsList[pos].position; Vector3 p2 = controlPointsList[ClampListPos(pos + 1)]; Vector3 p3 = controlPointsList[ClampListPos(pos + 2)]; Vector3 lastPos = Vector3.zero; for (float i = 0; i < SEGMENT_COUNT; i++) { float t = (float)i / SEGMENT_COUNT; Vector3 newPos = ReturnCatmullRom(t, p0, p1, p2, p3); if (t == 0) { lastPos = newPos; continue; } Gizmos.color = Color.white; Gizmos.DrawLine(lastPos, newPos); Gizmos.color = Color.yellow; var tan = ReturnCatmullRomTangent(t, p0, p1, p2, p3); Gizmos.DrawLine(newPos, newPos + tan * 3); Gizmos.DrawSphere(newPos, 0.3f); lastPos = newPos; } Gizmos.DrawLine(lastPos, p2); } void CreateSegmentList() { for (int i = 0; i < controlPoints.Count; i++) { Vector3 p0 = controlPoints[ClampListPos(i - 1)]; Vector3 p1 = controlPoints[i]; Vector3 p2 = controlPoints[ClampListPos(i + 1)]; Vector3 p3 = controlPoints[ClampListPos(i + 2)]; for (int j = 1; j < SEGMENT_COUNT; j++) { float t = (float)j / (float)SEGMENT_COUNT; segmentPoints.Add(ReturnCatmullRom(t, p0, p1, p2, p3)); } } } int ClampListPos(int pos) { if (pos < 0) { pos = controlPointsList.Count - 1; } if (pos > controlPointsList.Count) { pos = 1; } else if (pos > controlPointsList.Count - 1) { pos = 0; } return pos; } }
capture81.png
(48.1 kB)
capture56.png
(33.6 kB)
Comment