Advertisement
Staggart

Sample Spline Mesher Scale

Apr 1st, 2024 (edited)
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Splines;
  4.  
  5. namespace sc.modeling.splines.runtime.auxiliary
  6. {
  7.     //Sample the Spline Mesher's scale data
  8.     public class SampleSplineMesherScale : MonoBehaviour
  9.     {
  10.         public SplineMesher splineMesher;
  11.        
  12.         [Space]
  13.  
  14.         public float position;
  15.  
  16.         [Space]
  17.        
  18.         public Vector3 outputScale;
  19.  
  20.         private Vector3 splinePoint;
  21.        
  22.         private void OnValidate()
  23.         {
  24.             if (!splineMesher) return;
  25.  
  26.             //For this example, just reading from the 1st spline
  27.             int splineIndex = 0;
  28.  
  29.             //Disregard, this is just to calculate the position of the gizmo cube!
  30.             float splineLength = splineMesher.splineContainer.Splines[splineIndex].CalculateLength(splineMesher.splineContainer.transform.localToWorldMatrix);
  31.             splinePoint = splineMesher.splineContainer.Splines[splineIndex].EvaluatePosition(position / splineLength);
  32.             //Convert position to world-space
  33.             splinePoint = splineMesher.splineContainer.transform.TransformPoint(splinePoint);
  34.            
  35.  
  36.             //If there are no data points for the spline
  37.             if (splineMesher.scaleData[splineIndex].Count == 0)
  38.             {
  39.                 outputScale = Vector3.one;
  40.             }
  41.             else
  42.             {
  43.                 UnityEngine.Splines.Interpolators.LerpFloat3 float3Interpolator = new UnityEngine.Splines.Interpolators.LerpFloat3();
  44.                 outputScale = splineMesher.scaleData[splineIndex].Evaluate(splineMesher.splineContainer.Splines[splineIndex], position, splineMesher.scaleData[splineIndex].PathIndexUnit, float3Interpolator);
  45.             }
  46.         }
  47.  
  48.         private void OnDrawGizmosSelected()
  49.         {
  50.             Gizmos.DrawCube(splinePoint, outputScale);
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement