Advertisement
Staggart

Create Spline From Points

Apr 24th, 2024 (edited)
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Splines;
  5.  
  6. namespace sc.modeling.splines.runtime.auxiliary
  7. {
  8.     public class CreateSplineFromPoints : MonoBehaviour
  9.     {
  10.         [Header("Input")]
  11.         [Tooltip("Positional points in local-space")]
  12.         public List<Vector3> points = new List<Vector3>()
  13.         {
  14.             new Vector3(-1f, 0f, -5f),
  15.             new Vector3(1f, 0f, -2.5f),
  16.             new Vector3(-1f, 0f, 0),
  17.             new Vector3(1f, 0f, 2.5f),
  18.             new Vector3(-1f, 0f, 5f),
  19.         };
  20.        
  21.         [Space]
  22.        
  23.         [Header("Output")]
  24.         public SplineContainer splineContainer;
  25.         public bool smooth = true;
  26.        
  27.         private void Reset()
  28.         {
  29.             splineContainer = GetComponent<SplineContainer>();
  30.             if (!splineContainer) splineContainer = this.gameObject.AddComponent<SplineContainer>();
  31.         }
  32.        
  33.         private void OnValidate()
  34.         {
  35.             if (!splineContainer) return;
  36.  
  37.             int pointCount = points.Count;
  38.  
  39.             if (pointCount < 2) return;
  40.            
  41.             //First, delete all existing splines
  42.             for (int s = 0; s < splineContainer.Splines.Count; s++)
  43.             {
  44.                 splineContainer.RemoveSpline(splineContainer.Splines[s]);
  45.             }
  46.  
  47.             Spline spline = new Spline(pointCount);
  48.            
  49.             for (int i = 0; i < pointCount; i++)
  50.             {
  51.                 BezierKnot knot = new BezierKnot();
  52.                 knot.Position = points[i];
  53.                 knot.Rotation = Quaternion.identity;
  54.  
  55.                 spline.Add(knot, smooth ? TangentMode.AutoSmooth : TangentMode.Linear);
  56.             }
  57.            
  58.             //Automatically recalculate tangents
  59.             spline.SetTangentMode(new SplineRange(0, spline.Count), smooth ? TangentMode.AutoSmooth : TangentMode.Linear);
  60.    
  61.             //Adding a spline automatically rebuilds the mesh
  62.             splineContainer.AddSpline(spline);
  63.         }
  64.  
  65.         private void OnDrawGizmosSelected()
  66.         {
  67.             if (!splineContainer) return;
  68.  
  69.             Gizmos.matrix = splineContainer.transform.localToWorldMatrix;
  70.             foreach (Vector3 p in points)
  71.             {
  72.                 Gizmos.DrawSphere(p, 0.25f);
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement