Hi all,
I’ve been trying to set spline knot orientations, but not having predictable results.
I find that when rotating the splines manually, they can get into a weird orientation where the up vector starts to flip. I could stabilize this by switching to bezier and then back to catmul-rom. Some how this fixes it, but caused other issues (like the whole spline reorienting to another direction all together).
So I was trying to write a script that would calculate a new orientation pointing at the next knot, using world Up.
This didn’t work out, and it also appears there is a “normalization” process that happens after editing any knot. I’m not able to trigger this normalization by code.
So my questions are:
- Are there any examples of modifying a knots orientation by code?
- How can I run this internal normalization via code?
Thanks!
Any thoughts @kaarrrllll ?
So doing more digging, I’m not able to generate splines procedurally either. Are there any examples that demonstrate this?
Thanks!
Okay, here is a small example. The code below sets a some points, including the catmull-rom edit mode. Although, as you can see the spline doesn’t have a nice curvature. If I move any of the handles in the editor, or change a value in the inspector, some refresh mechanism is triggered. I didn’t see anything in the API that would allow me to “rebuild” or “re-evaluate” the curve. Is there anything Im missing here? Thanks!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;
public class Debug_SplineCreation : MonoBehaviour
{
void Start()
{
var testSpline = this.gameObject.AddComponent<SplineContainer>();
testSpline.Spline.EditType = SplineType.CatmullRom;
testSpline.Spline.Add(new BezierKnot(new Vector3(0, 0, 0)));
testSpline.Spline.Add(new BezierKnot(new Vector3(0, 5, 5)));
testSpline.Spline.Add(new BezierKnot(new Vector3(0, 10, 10)));
testSpline.Spline.Add(new BezierKnot(new Vector3(0, 5, 15)));
testSpline.Spline.Add(new BezierKnot(new Vector3(0, 0, 20)));
}
}
Just a quick update, it looks like in Splines 2.0 there are some new convenience functions that can do this.
https://docs.unity.cn/Packages/com.unity.splines@2.0/api/UnityEngine.Splines.SplineFactory.html
public static Spline CreateCatmullRom(IList<float3> positions, bool closed = false)
Case closed. Thanks everyone!