I would like to trim a track model by changing its mesh.
So far, I have tried iterating through the vertices and triangles, getting their Vector3 points, checking them against the cutoff Vector3 point (basically a point ahead of the current points on a spline path), and only letting those points into the array that aren’t beyond the cutoff point.
I have tried multiple implementations, some were more successful than others:
Full-length deformation:
Cutoff deformation attempts:
Sample, experimentation code trying to trim the mesh (note: I had multiple variations, this is the last one pictured)
void TrimMesh()
{
// VERTS
Vector3[] verts2 = new Vector3[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
// Get vertex point on spline
Vector3 meshVertex = vertices[i];
Vector3 splinePoint = path.GetPointAtDistance(meshVertex.z + targetObject.transform.position.z);
Vector3 futureSplinePoint = path.GetPointAtDistance(meshVertex.z + targetObject.transform.position.z + 0.01f);
Vector3 forwardVector = futureSplinePoint - splinePoint;
Quaternion imaginaryPlaneRotation = Quaternion.LookRotation(forwardVector, Vector3.up);
Vector3 pointWithinPlane = new Vector3(meshVertex.x, meshVertex.y, 0f);
Vector3 result = splinePoint + (imaginaryPlaneRotation * pointWithinPlane);
// Check vertex point against cutoff point
if (Vector3.Distance(result, path.GetPointAtDistance(DesiredLength)) > DesiredLength) continue;
verts2[i] = vertices[i];
mesh.vertices = verts2;
}
// TRIS
int[] triangles2 = new int[triangles.Count];
for (int i = 0; i < triangles.Count; i += 1)
{
// Get vertex point on spline
Vector3 meshVertex = vertices[triangles[i]];
Vector3 splinePoint = path.GetPointAtDistance(meshVertex.z + targetObject.transform.position.z);
Vector3 futureSplinePoint = path.GetPointAtDistance(meshVertex.z + targetObject.transform.position.z + 0.01f);
Vector3 forwardVector = futureSplinePoint - splinePoint;
Quaternion imaginaryPlaneRotation = Quaternion.LookRotation(forwardVector, Vector3.up);
Vector3 pointWithinPlane = new Vector3(meshVertex.x, meshVertex.y, 0f);
Vector3 result = splinePoint + (imaginaryPlaneRotation * pointWithinPlane);
// Check vertex point against cutoff point
if (Vector3.Distance(result, path.GetPointAtDistance(DesiredLength)) > DesiredLength) continue;
triangles2[i] = triangles[i];
mesh.triangles = triangles2;
}
}
Lengthening is not required, although I would be interested in knowing whether that would also be possible (perhaps through repeating the triangle/vertex pattern?)
I would also accept recommendations if there’s possibly a better way to trim models.
I’ve been looking into clip shaders, but looking into the future, I would need to trim the model from the other way around as well - clip shaders can only be used once.





