#if UNITY_BURST_ENABLED
using Unity.Burst;
#endif
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
namespace UnityEngine.Splines
{
///
/// Provides methods to calculate, in parallel, the positions along .
///
#if UNITY_BURST_ENABLED
[BurstCompile]
#endif
public struct GetPosition : IJobParallelFor
{
///
/// The to be evaluated.
///
///
/// Must be allocated with a Allocator.Persistent or Allocator.TempJob.
///
[ReadOnly]
public NativeSpline Spline;
///
/// A NativeArray of float3 to be written. The size of this array determines how many positions are
/// evaluated.
///
[WriteOnly]
public NativeArray Positions;
///
/// Called by the job system to evaluate a position at an index. The interpolation value is calculated as
/// `index / positions.Length - 1`.
///
/// The index of the positions array to evaluate.
public void Execute(int index)
{
Positions[index] = Spline.EvaluatePosition(index / (Positions.Length-1f));
}
}
///
/// A job struct for calculating in parallel the position, tangent, and normal (up) vectors along a
/// .
///
public struct GetPositionTangentNormal : IJobParallelFor
{
///
/// The to be evaluated.
///
///
/// Must be allocated with a Allocator.Persistent or Allocator.TempJob.
///
[ReadOnly]
public NativeSpline Spline;
///
/// A NativeArray of float3 to be written. The size of this array determines how many positions are
/// evaluated.
///
[WriteOnly]
public NativeArray Positions;
///
/// A NativeArray of float3 to be written. The size of this array must match the length of .
///
[WriteOnly]
public NativeArray Tangents;
///
/// A NativeArray of float3 to be written. The size of this array must match the length of .
///
[WriteOnly]
public NativeArray Normals;
///
/// Called by the job system to evaluate position, tangent, and normal at an index. The interpolation value is
/// calculated as `index / positions.Length - 1`.
///
/// The index of the positions array to evaluate.
public void Execute(int index)
{
Spline.Evaluate(index / (Positions.Length - 1f), out var p, out var t, out var n);
Positions[index] = p;
Tangents[index] = t;
Normals[index] = n;
}
}
///
/// The SplineJobs class contains utility methods for evaluating spline data using the Jobs system.
///
public static class SplineJobs
{
///
/// Populate a preallocated NativeArray with position data from a spline.
///
/// The spline to evaluate. If you pass a NativeSpline, it must be allocated
/// with the Persistent or TempJob allocator. Temp is invalid for use with the Jobs system.
/// A preallocated array of float3 to be populated with evenly interpolated positions
/// from a spline.
/// The type of ISpline.
public static void EvaluatePosition(T spline, NativeArray positions) where T : ISpline
{
using var native = new NativeSpline(spline, Allocator.TempJob);
EvaluatePosition(native, positions);
}
///
/// Populate a preallocated NativeArray with position data from a spline.
///
/// The spline to evaluate. The NativeSpline must be allocated with a Persistent
/// or TempJob allocator. Temp is invalid for use in the Jobs system.
/// A preallocated array of float3 to be populated with evenly interpolated positions
/// from a spline.
public static void EvaluatePosition(NativeSpline spline, NativeArray positions)
{
var job = new GetPosition()
{
Spline = spline,
Positions = positions
};
var handle = job.Schedule(positions.Length, 1);
handle.Complete();
}
///
/// Populate a set of pre-allocated NativeArray with position, tangent, and normal data from a spline.
///
///
/// To apply a transform to the results of this method, pass a new NativeSpline constructed with the desired
/// transformation matrix.
///
/// This method creates a temporary NativeSpline copy of the spline to be evaluated. In some cases, this can
/// be more resource intensive than iterating and evaluating a spline on a single thread. For the best performance,
/// pass an existing NativeSpline instance to the
/// parameter.
///
/// The spline to evaluate. If you pass a NativeSpline, it must be allocated
/// with the Persistent or TempJob allocator. Temp is invalid for use with the Jobs system.
/// A preallocated array of float3 to be populated with evenly interpolated positions
/// from a spline.
/// A preallocated array of float3 to be populated with evenly interpolated tangents
/// from a spline. Must be the same size as the positions array.
/// A preallocated array of float3 to be populated with evenly interpolated normals
/// from a spline. Must be the same size as the positions array.
/// The type of ISpline.
public static void EvaluatePositionTangentNormal(
T spline,
NativeArray positions,
NativeArray tangents,
NativeArray normals) where T : ISpline
{
using var native = new NativeSpline(spline, Allocator.TempJob);
EvaluatePositionTangentNormal(native, positions, tangents, normals);
}
///
/// Populate a set of preallocated NativeArray with position, tangent, and normal data from a spline.
///
///
/// To apply a transform to the results of this method, pass a new NativeSpline constructed with the desired
/// transformation matrix.
///
/// The spline to evaluate. The NativeSpline must be allocated with a Persistent
/// or TempJob allocator. Temp is invalid for use in the Jobs system.
/// A preallocated array of float3 to be populated with evenly interpolated positions
/// from a spline.
/// A preallocated array of float3 to be populated with evenly interpolated tangents
/// from a spline. Must be the same size as the positions array.
/// A preallocated array of float3 to be populated with evenly interpolated normals
/// from a spline. Must be the same size as the positions array.
public static void EvaluatePositionTangentNormal(NativeSpline spline,
NativeArray positions,
NativeArray tangents,
NativeArray normals)
{
var job = new GetPositionTangentNormal()
{
Spline = spline,
Positions = positions,
Tangents = tangents,
Normals = normals
};
var handle = job.Schedule(positions.Length, 1);
handle.Complete();
}
}
}