- Home /
How do i create points along a spline???
Hello how do i create the Yellow dots(segmentPoints) all around the spline like in Pic1 and draw a blue line to each yellow dot. Instead of just creating the yellow dots in between the white dots(controlPoints) Pic2, would anyone know how to do this?????
-Pic1
-Pic2
Here is my code so you know what i have done.
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;
}
}
@b1gr4yn
capture99.png
(39.3 kB)
capture56.png
(33.6 kB)
Comment