Class SplineUtils
- Namespace
- WitShells.SplineRuntime
public static class SplineUtils
- Inheritance
-
SplineUtils
- Inherited Members
Methods
AddPointsToSpline(SplineContainer, Vector3[], int, bool)
Adds a sequence of points to an existing spline
public static void AddPointsToSpline(SplineContainer splineContainer, Vector3[] positions, int splineIndex = 0, bool autoSmooth = true)
Parameters
splineContainerSplineContainerThe spline container to modify
positionsVector3[]Positions to add
splineIndexintIndex of the spline to modify
autoSmoothboolWhether to auto-smooth tangents
AutoSmoothTangents(Spline, float)
Automatically smooths the tangents of a spline
public static void AutoSmoothTangents(Spline spline, float tangentScale = 0.33)
Parameters
splineSplineThe spline to smooth
tangentScalefloatScale factor for tangent length (default: 0.33)
ContainerChildrenToPositionList(Transform, bool, bool)
Converts all child positions of a container transform to a list of Vector3 positions
public static List<Vector3> ContainerChildrenToPositionList(Transform container, bool includeInactive = false, bool localPositions = true)
Parameters
containerTransformThe parent transform containing child objects
includeInactiveboolWhether to include inactive child objects (default: false)
localPositionsbool
Returns
- List<Vector3>
List of Vector3 positions of all children
CreateArcBetweenPoints(Vector3, Vector3, float, int, Transform)
Creates a path between two points with an arc
public static SplineContainer CreateArcBetweenPoints(Vector3 start, Vector3 end, float arcHeight, int pointCount = 5, Transform parent = null)
Parameters
startVector3Start position
endVector3End position
arcHeightfloatHeight of the arc
pointCountintNumber of points to generate (min 3)
parentTransformOptional parent transform
Returns
- SplineContainer
SplineContainer with an arcing path
CreateClosedLoopSpline(Vector3[], Transform)
Creates a closed loop spline from positions
public static SplineContainer CreateClosedLoopSpline(Vector3[] positions, Transform parent = null)
Parameters
positionsVector3[]Positions to form the loop
parentTransformOptional parent transform
Returns
- SplineContainer
A new SplineContainer with a closed loop
CreateElevatedRoadMesh(SplineContainer, float, float, float, int, int, Transform)
Creates an elevated road mesh with side walls
public static Mesh CreateElevatedRoadMesh(SplineContainer splineContainer, float width, float height, float materialTilingMultiplier = 1, int splineIndex = 0, int segments = 100, Transform transform = null)
Parameters
splineContainerSplineContainerThe spline to follow
widthfloatWidth of the road
heightfloatHeight of the side walls
materialTilingMultiplierfloatMultiplier for texture tiling
splineIndexintIndex of the spline to use
segmentsintNumber of segments for the road mesh
transformTransformOptional transform to convert to local space
Returns
- Mesh
A generated elevated road mesh with walls
CreateRibbonMesh(SplineContainer, float, float, float, int, int, bool, float, Transform)
Creates a ribbon mesh following a spline, useful for rivers, streams, or paths
public static Mesh CreateRibbonMesh(SplineContainer splineContainer, float startWidth, float endWidth, float materialTilingMultiplier = 1, int splineIndex = 0, int segments = 100, bool conformToTerrain = false, float terrainOffset = 0.1, Transform transform = null)
Parameters
splineContainerSplineContainerThe spline to follow
startWidthfloatWidth at the start of the spline
endWidthfloatWidth at the end of the spline
materialTilingMultiplierfloatTexture tiling multiplier
splineIndexintIndex of the spline to use
segmentsintNumber of segments along the spline
conformToTerrainboolWhether to conform to terrain height
terrainOffsetfloatHeight offset from terrain
transformTransformOptional transform to convert to local space
Returns
- Mesh
A ribbon mesh following the spline with varying width
CreateRoadMesh(SplineContainer, float, float, bool, int, int, Transform)
Creates a road mesh from a spline path
public static Mesh CreateRoadMesh(SplineContainer splineContainer, float width, float materialTilingMultiplier = 1, bool flipDirection = false, int splineIndex = 0, int segments = 100, Transform transform = null)
Parameters
splineContainerSplineContainerThe spline to follow
widthfloatWidth of the road
materialTilingMultiplierfloatMultiplier for texture tiling (higher value = more tiling)
flipDirectionboolWhether to flip the road direction
splineIndexintIndex of the spline to use
segmentsintNumber of segments for the road mesh (higher = smoother)
transformTransformOptional transform to convert to local space
Returns
- Mesh
A generated road mesh
CreateSpiralSpline(Vector3, float, float, float, float, int, Transform)
Creates a spiral spline
public static SplineContainer CreateSpiralSpline(Vector3 center, float startRadius, float endRadius, float height, float rotations, int segments, Transform parent = null)
Parameters
centerVector3Center of the spiral
startRadiusfloatStarting radius
endRadiusfloatEnding radius
heightfloatHeight difference from start to end
rotationsfloatNumber of full rotations
segmentsintNumber of segments per rotation (higher = smoother)
parentTransformOptional parent transform
Returns
- SplineContainer
SplineContainer with a spiral shape
CreateSplineFromLocalPositions(Vector3[], bool)
Creates a Spline object directly from a list of positions
public static Spline CreateSplineFromLocalPositions(Vector3[] positions, bool autoSmooth = true)
Parameters
positionsVector3[]Array of positions in local space
autoSmoothboolWhether to automatically smooth the tangents
Returns
- Spline
A new Spline object
CreateSplineFromPositions(Vector3[], Transform, bool)
Creates a new SplineContainer with a spline from a list of world positions
public static SplineContainer CreateSplineFromPositions(Vector3[] positions, Transform parent = null, bool autoSmooth = true)
Parameters
positionsVector3[]List of world positions to form the spline
parentTransformOptional parent transform
autoSmoothboolWhether to automatically smooth the tangents
Returns
- SplineContainer
A new SplineContainer GameObject
CreateSplineFromPositionsList(List<Vector3>, bool)
Creates a spline from a List of Vector3 positions
public static Spline CreateSplineFromPositionsList(List<Vector3> positionsList, bool autoSmooth = true)
Parameters
positionsListList<Vector3>List of positions in local space
autoSmoothboolWhether to auto-smooth the tangents
Returns
- Spline
A new Spline object
CreateSplineFromWorldPositions(Vector3[], Transform, bool)
Creates a Spline object directly from a list of world positions
public static Spline CreateSplineFromWorldPositions(Vector3[] worldPositions, Transform referenceTransform, bool autoSmooth = true)
Parameters
worldPositionsVector3[]Array of positions in world space
referenceTransformTransformTransform used to convert world to local positions
autoSmoothboolWhether to automatically smooth the tangents
Returns
- Spline
A new Spline object
CreateSplineGrid(Vector3, float, float, int, int, Transform)
Creates a grid of splines forming a network
public static List<SplineContainer> CreateSplineGrid(Vector3 origin, float width, float length, int rows, int columns, Transform parent = null)
Parameters
originVector3Bottom-left corner of the grid
widthfloatWidth of the grid (X axis)
lengthfloatLength of the grid (Z axis)
rowsintNumber of rows
columnsintNumber of columns
parentTransformOptional parent transform
Returns
- List<SplineContainer>
List of SplineContainer GameObjects forming the grid
CreateTerrainFollowingSpline(Vector3, Vector3, float, int, Transform)
Creates a spline that follows terrain height
public static SplineContainer CreateTerrainFollowingSpline(Vector3 startPoint, Vector3 endPoint, float heightOffset, int segments, Transform parent = null)
Parameters
startPointVector3Start position in world space
endPointVector3End position in world space
heightOffsetfloatOffset from terrain height
segmentsintNumber of segments (higher = better terrain following)
parentTransformOptional parent transform
Returns
- SplineContainer
SplineContainer that follows terrain height
CreateTunnelMesh(SplineContainer, float, int, int, float, int, Transform)
Creates a tunnel mesh along a spline
public static Mesh CreateTunnelMesh(SplineContainer splineContainer, float radius, int segments = 50, int circleVertices = 12, float materialTilingMultiplier = 1, int splineIndex = 0, Transform transform = null)
Parameters
splineContainerSplineContainerThe spline to follow
radiusfloatRadius of the tunnel
segmentsintNumber of segments along the spline
circleVerticesintNumber of vertices around the circumference
materialTilingMultiplierfloatTexture tiling multiplier
splineIndexintIndex of the spline to use
transformTransformOptional transform to convert to local space
Returns
- Mesh
A generated tunnel mesh
DistributePooledObjectsAlongSpline(SplineContainer, Func<GameObject>, Action<GameObject>, Transform, int, List<GameObject>, int, bool, Vector3, Vector3, Vector3)
Distributes GameObjects along a spline using object pooling
public static List<GameObject> DistributePooledObjectsAlongSpline(SplineContainer spline, Func<GameObject> getPooledObject, Action<GameObject> releasePooledObject, Transform container, int objectCount, List<GameObject> existingObjects = null, int splineIndex = 0, bool alignToSpline = true, Vector3 rotationOffset = null, Vector3 forwardAxis = null, Vector3 upAxis = null)
Parameters
splineSplineContainerThe spline to distribute objects along
getPooledObjectFunc<GameObject>Function to get an object from a pool
releasePooledObjectAction<GameObject>Function to release an object back to the pool
containerTransformContainer transform to parent the objects to
objectCountintNumber of objects to distribute along the spline
existingObjectsList<GameObject>Optional list of existing objects to release back to the pool
splineIndexintIndex of the spline to use (default: 0)
alignToSplineboolWhether to align objects to the spline direction
rotationOffsetVector3Additional rotation to apply (in degrees)
forwardAxisVector3Which local axis should point forward along the spline
upAxisVector3Which local axis should align with the spline's up direction
Returns
- List<GameObject>
List of positioned GameObjects from the pool
DistributePooledPrefabsAlongSpline(SplineContainer, GameObject, Transform, int, List<GameObject>, int, bool, Vector3)
Example method showing how to use DistributePooledObjectsAlongSpline with ObjectPoolPattern
public static List<GameObject> DistributePooledPrefabsAlongSpline(SplineContainer spline, GameObject prefab, Transform container, int objectCount, List<GameObject> existingObjects = null, int splineIndex = 0, bool alignToSpline = true, Vector3 rotationOffset = null)
Parameters
splineSplineContainerprefabGameObjectcontainerTransformobjectCountintexistingObjectsList<GameObject>splineIndexintalignToSplineboolrotationOffsetVector3
Returns
- List<GameObject>
DistributePooledPrefabsAlongSpline(SplineContainer, GameObject, Transform, int, int, bool, Vector3)
public static List<GameObject> DistributePooledPrefabsAlongSpline(SplineContainer spline, GameObject prefab, Transform container, int objectCount, int splineIndex = 0, bool alignToSpline = true, Vector3 rotationOffset = null)
Parameters
splineSplineContainerprefabGameObjectcontainerTransformobjectCountintsplineIndexintalignToSplineboolrotationOffsetVector3
Returns
- List<GameObject>
FlipNormals(Mesh)
Flips the normals of a mesh
public static void FlipNormals(Mesh mesh)
Parameters
meshMeshThe mesh to modify
GetClosestPointOnSpline(SplineContainer, Vector3, int, int)
Gets the closest point on a spline to a world position
public static float GetClosestPointOnSpline(SplineContainer splineContainer, Vector3 worldPosition, int splineIndex = 0, int resolution = 100)
Parameters
splineContainerSplineContainerThe spline container
worldPositionVector3The world position to find closest point to
splineIndexintIndex of the spline
resolutionintHow many points to check (higher = more accurate but slower)
Returns
- float
Normalized t value (0-1) of the closest point on the spline
GetPositionsFromSpline(Spline)
Extracts a list of all knot positions from a spline
public static Vector3[] GetPositionsFromSpline(Spline spline)
Parameters
splineSplineThe spline to extract positions from
Returns
- Vector3[]
Array of Vector3 positions
MergeSplines(List<SplineContainer>, bool, Transform)
Merges multiple splines into a single spline container
public static SplineContainer MergeSplines(List<SplineContainer> splines, bool connectEndpoints = false, Transform parent = null)
Parameters
splinesList<SplineContainer>List of spline containers to merge
connectEndpointsboolWhether to connect the endpoints of consecutive splines
parentTransformOptional parent transform
Returns
- SplineContainer
SplineContainer containing all the merged splines
NeedToFlipNormals(Mesh, Transform)
Helper method to check if mesh normals need to be flipped
public static bool NeedToFlipNormals(Mesh mesh, Transform transform = null)
Parameters
meshMeshThe mesh to check
transformTransformOptional transform to convert normals to world space
Returns
- bool
True if normals should be flipped
OffsetSpline(SplineContainer, float, int, int, Transform)
Offsets a spline by a given distance perpendicular to its direction
public static SplineContainer OffsetSpline(SplineContainer sourceSpline, float offset, int splineIndex = 0, int sampleCount = 20, Transform parent = null)
Parameters
sourceSplineSplineContainerThe source spline container
offsetfloatDistance to offset (positive = right, negative = left)
splineIndexintIndex of the spline to offset
sampleCountintNumber of samples for accuracy
parentTransformOptional parent transform
Returns
- SplineContainer
New SplineContainer with the offset path
PlaceObjectAtSplinePoint(SplineContainer, GameObject, Transform, bool, float, Vector3, Vector3, Vector3, int)
Places a single object at a specific point along a spline with precise rotation control
public static GameObject PlaceObjectAtSplinePoint(SplineContainer spline, GameObject prefab, Transform parent, bool normalizeDistance, float distance, Vector3 rotationOffset, Vector3 forwardAxis, Vector3 upAxis, int splineIndex = 0)
Parameters
splineSplineContainerThe spline container
prefabGameObjectThe object to spawn
parentTransformParent transform for the spawned object
normalizeDistanceboolWhether the distance is normalized (0-1) or in world units
distancefloatDistance along the spline
rotationOffsetVector3Additional rotation to apply (in degrees)
forwardAxisVector3Which local axis should point forward along the spline
upAxisVector3Which local axis should align with the spline's up direction
splineIndexintIndex of the spline to use (default: 0)
Returns
- GameObject
The instantiated GameObject or null if failed
ReverseSpline(SplineContainer, int)
Reverses the direction of a spline
public static void ReverseSpline(SplineContainer splineContainer, int splineIndex = 0)
Parameters
splineContainerSplineContainerThe spline container to modify
splineIndexintIndex of the spline to reverse
SamplePointsAlongSpline(SplineContainer, float, int)
Samples points along a spline at regular distance intervals
public static Vector3[] SamplePointsAlongSpline(SplineContainer splineContainer, float spacing, int splineIndex = 0)
Parameters
splineContainerSplineContainerThe spline container to sample
spacingfloatDistance between samples
splineIndexintIndex of the spline to sample
Returns
- Vector3[]
Array of sampled world positions
SpawnObjectsAlongSpline(SplineContainer, GameObject, Transform, LayerMask, bool, bool, float, float, int)
Spawns objects along a spline at regular intervals
public static void SpawnObjectsAlongSpline(SplineContainer spline, GameObject prefab, Transform objectContainer, LayerMask groundLayers, bool snapToGround, bool alignToSpline, float groundOffset, float spacing, int splineIndex = 0)
Parameters
splineSplineContainerThe spline container to follow
prefabGameObjectThe object to spawn
objectContainerTransformgroundLayersLayerMasksnapToGroundboolalignToSplineboolgroundOffsetfloatspacingfloatDistance between objects
splineIndexintIndex of the spline to use (default: 0)
SpawnObjectsAlongSplineAdvanced(SplineContainer, GameObject, Transform, LayerMask, bool, bool, float, float, Vector3, int)
Spawns objects along a spline at regular intervals with advanced rotation control
public static void SpawnObjectsAlongSplineAdvanced(SplineContainer spline, GameObject prefab, Transform objectContainer, LayerMask groundLayers, bool snapToGround, bool alignToSpline, float groundOffset, float spacing, Vector3 rotationOffset, int splineIndex = 0)
Parameters
splineSplineContainerThe spline container to follow
prefabGameObjectThe object to spawn
objectContainerTransformContainer to parent spawned objects to
groundLayersLayerMaskLayers to raycast against for ground snapping
snapToGroundboolWhether to snap objects to ground
alignToSplineboolWhether to align objects to spline direction
groundOffsetfloatHeight offset from ground when snapping
spacingfloatDistance between objects
rotationOffsetVector3Additional rotation to apply to objects (in degrees)
splineIndexintIndex of the spline to use (default: 0)
SpawnObjectsAlongSplineAdvanced(SplineContainer, ObjectPool<GameObject>, Transform, LayerMask, bool, bool, float, float, Vector3, int)
public static void SpawnObjectsAlongSplineAdvanced(SplineContainer spline, ObjectPool<GameObject> prefabPool, Transform objectContainer, LayerMask groundLayers, bool snapToGround, bool alignToSpline, float groundOffset, float spacing, Vector3 rotationOffset, int splineIndex = 0)
Parameters
splineSplineContainerprefabPoolObjectPool<GameObject>objectContainerTransformgroundLayersLayerMasksnapToGroundboolalignToSplineboolgroundOffsetfloatspacingfloatrotationOffsetVector3splineIndexint
SubdivideSpline(SplineContainer, int, int, bool)
Subdivides a spline by adding additional points between existing knots
public static void SubdivideSpline(SplineContainer splineContainer, int subdivisions, int splineIndex = 0, bool preserveTangents = false)
Parameters
splineContainerSplineContainerThe spline container to subdivide
subdivisionsintNumber of points to add between each existing pair of knots
splineIndexintIndex of the spline to subdivide
preserveTangentsboolWhether to preserve tangent information (true) or auto-smooth (false)
TransformsToPositionList(List<Transform>)
Converts a list of transforms to a list of their world positions
public static List<Vector3> TransformsToPositionList(List<Transform> transforms)
Parameters
transformsList<Transform>List of transforms to convert
Returns
- List<Vector3>
List of Vector3 positions
TransformsToPositionList(Transform[])
Converts an array of transforms to a list of their world positions
public static List<Vector3> TransformsToPositionList(Transform[] transforms)
Parameters
transformsTransform[]Array of transforms to convert
Returns
- List<Vector3>
List of Vector3 positions
UpdateObjectsAlongSpline(List<Vector3>, List<GameObject>, bool, Vector3, bool, Vector3, Vector3)
Updates the positions and rotations of objects based on a spline created from updated positions
public static SplineContainer UpdateObjectsAlongSpline(List<Vector3> positions, List<GameObject> objects, bool alignToSpline = true, Vector3 rotationOffset = null, bool distributeEvenly = true, Vector3 forwardAxis = null, Vector3 upAxis = null)
Parameters
positionsList<Vector3>List of Vector3 positions to create the spline from
objectsList<GameObject>List of GameObjects to update positions and rotations for
alignToSplineboolWhether to align objects to the spline direction
rotationOffsetVector3Additional rotation to apply (in degrees)
distributeEvenlyboolIf true, distributes objects evenly along spline; if false, tries to match positions to closest spline points
forwardAxisVector3Which local axis should point forward along the spline
upAxisVector3Which local axis should align with the spline's up direction
Returns
- SplineContainer
The temporary SplineContainer created for the update (null if failed)
UpdateObjectsAlongSplineAndCleanup(List<Vector3>, List<GameObject>, bool, Vector3, bool, Vector3, Vector3)
Updates the positions and rotations of objects based on a spline created from updated positions, and automatically cleans up the temporary spline
public static void UpdateObjectsAlongSplineAndCleanup(List<Vector3> positions, List<GameObject> objects, bool alignToSpline = true, Vector3 rotationOffset = null, bool distributeEvenly = true, Vector3 forwardAxis = null, Vector3 upAxis = null)
Parameters
positionsList<Vector3>List of Vector3 positions to create the spline from
objectsList<GameObject>List of GameObjects to update positions and rotations for
alignToSplineboolWhether to align objects to the spline direction
rotationOffsetVector3Additional rotation to apply (in degrees)
distributeEvenlyboolIf true, distributes objects evenly along spline; if false, tries to match positions to closest spline points
forwardAxisVector3Which local axis should point forward along the spline
upAxisVector3Which local axis should align with the spline's up direction