- Home /
Generate cylindrical mesh around 3D Catmull-rom spline
The spline is curve is defined by 4 points: p0,p1,p2,p3 and drawn between p1 and p2 with a for loop, looping through the number of segments I want. The point on the curve at time t can is being found with this function:
public Vector3 GetSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
Vector3 ret = new Vector3();
float t2 = t * t;
float t3 = t * t * t;
ret.x = 0.5f * ((2.0f * p1.x) +
(-p0.x + p2.x) * t +
(2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x) * t2 +
(-p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x) * t3);
ret.y = 0.5f * ((2.0f * p1.y) +
(-p0.y + p2.y) * t +
(2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y) * t2 +
(-p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y) * t3);
ret.z = 0.5f * ((2.0f * p1.z) +
(-p0.z + p2.z) * t +
(2.0f * p0.z - 5.0f * p1.z + 4 * p2.z - p3.z) * t2 +
(-p0.z + 3.0f * p1.z - 3.0f * p2.z + p3.z) * t3);
return ret;
}
I think I can figure out all the mesh generation stuff but I just can't figure out how to get a "normal" vector from the "direction vector" (segmentEnd-segmentStart). I feel like this is stupidly simple.. but my 3d maths isn't that great.
Any idea? Thanks!
Ins$$anonymous$$d of doing it with math I aligned an empty gameobject to the setmentsStart, and used go.transform.rotation = Quaternion.LookRotation(dirVec); Then used go.transform.right and go.transform.up to offset the mesh extrusion. It works, but it doesn't seem like a clean way to do it
Answer by MakeCodeNow · Jan 03, 2015 at 06:27 AM
Take your normalized direction/tangent vector and cross product it with any non-parallel vector (like global up). Normalize the result to get one of the spline local axes. Then, cross that axis with the original normalized direction/tangent vector and you have the other axis. Congratulations, you now have the three axes of a spline point local matrix.
Lots more info on Wikipedia
Wow man. You rock, I always forget about the cross product. This looks like exactly what I was looking for :)
Just tried it, works like a charm, and much neater than my hacky solution. Cheers mate.
Your answer
Follow this Question
Related Questions
exact vector reflection along collision normal vector 1 Answer
Custom gravity based on normal vector and rigidbody.addForce 2 Answers
Problem importing baked normal map from 3ds max 1 Answer
Tangent Space Normal Map to World Space Normal Vector in C# Script 0 Answers
Get normal direction to use as sign 1 Answer