Metadata

Sometimes you want to store additional information to your spline, e.g. a max speed or other track information for your racing track, additional Curvy Generator data etc..

This is done by creating your own data classes and attaching them to Control Points. You then can use Curvy API to access and interpolate that data.

Creating the Metadata class

Create a new MonoBehaviour and inherit one of the following interfaces:

Interface Use when
ICurvyMetadata Data that needs no interpolated, e.g. simple booleans
ICurvyInterpolatableMetadata Interpolatable data, e.g. float or Vector3
ICurvyInterpolatableMetadata<U> Generic version of the above

Next, you'll need to implement the following methods/properties depending on the interface:

Member Purpose ICurvyInterpolatableMetadata ICurvyInterpolatableMetadata<T>
object Value { get; } Gets the uninterpolated metadata x x
object InterpolateObject(ICurvyMetadata b, float f) Gets interpolated metadata x x
U Interpolate(ICurvyMetadata b, float f) Generic version of the above x

Here is an example (used by the Metadata example scene) that provides a simple float value that can be interpolated:

public class HeightMetadata : MonoBehaviour, ICurvyInterpolatableMetadata<float>
    {
        [SerializeField]
        float m_Height;

        // Gets the plain, non-interpolated value
        public object Value
        {
            get { return m_Height; }
        }

        // Interpolate between this value and another value here
        public object InterpolateObject(ICurvyMetadata b, float f)
        {
            var mdb = b as HeightMetadata;
            return (mdb != null) ? Mathf.Lerp((float)Value, (float)mdb.Value, f) : Value;
        }
        // Generic variant of the above
        public float Interpolate(ICurvyMetadata b, float f)
        {
            return (float)InterpolateObject(b, f);
        }
    }

Assigning Metadata to Control Points

As metadata classes are regular MonoBehaviours, simply add them to the desired Control Points using the Component menu.

Retrieving Metadata

Like almost all other Curvy API methods you can access metadata from Splines, Spline Groups and Control Points using the following API members:

Member Purpose
GetMetadata (Type, tf) Gets the metadata class itself
InterpolateMetadata (type, tf) Gets interpolated data
InterpolateMetadata<T,U> (tf) Gets interpolated data of type U from metadata class T
MetaData {get; } Gets a list of all attached metadata (CurvySplineSegment only)

For example, the MetadataController script from the example scene mentioned above uses the following code to access the height value:

// Get the interpolated Metadata value for the current position (for SplineController, RelativePosition means TF)
// If values can't be interpolated (no next value), current value (if present) or default type value (for float that's 0) is returned
var v = Spline.InterpolateMetadata<HeightMetadata,float>(RelativePosition);
Last modified: 2015/08/11 20:45 by Jake