- Home /
Adding segments in missing spots
Hello how would i fill in the wireSphere(controlPoint) with a yellow Sphere (segmentPoints)?
Here is my code, sing 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;
}
}
capture56.png
(33.6 kB)
Comment
Answer by ellioman · Sep 15, 2016 at 07:25 PM
By changing this:
Gizmos.color = Color.white;
for (int i = 0; i < controlPointsList.Count; i++)
{
Gizmos.DrawWireSphere(controlPointsList[i], 0.3f);
}
to
Gizmos.color = Color.yellow;
for (int i = 0; i < controlPointsList.Count; i++)
{
Gizmos.DrawSphere(controlPointsList[i], 0.3f);
}
Gizmos.color = Color.white;
No. how do i use the SegmentsPoints to calculate all around the spline filling in the gaps?
Just to be clear, you need help with filling the area inside the shape that you create with these points?
@ellioman Not really(we'll talk about that later since i i find it interesting after you said that)i'm trying to do this, kinda like this in the picture, creating the segmentPoints in "Order" around the spline. http://i.imgur.com/qCrOAfa.png