Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Official Unity Splines

Discussion in 'World Building' started by gabrielw_unity, Oct 5, 2022.

  1. yarilo

    yarilo

    Joined:
    Dec 14, 2012
    Posts:
    5
    - Do you have an official source code for the spline package ( I found https://github.com/needle-mirror/com.unity.splines )?
    - Is there any way to play SplineAnimate in reverse order? I created a loop animation and my object moves counterclockwise by default. I suppose it depends on the order of knots created. Can I change it somehow?
     
    graskovi, Kamyker and shikhrr like this.
  2. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    700
    The first post in this thread shows a road following a spline.


    Was this done with the Splines Package? If so, how was this done? Spline extrude only makes tubes and spline instantiate doesn't bend objects. If not, can you please add this functionality? It is needed to make things like roads.
     
  3. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    Agreed. In the next update we added a "quickstart" to the API landing page. In the meantime, I'd suggest importing the samples and referencing the SplineCommonAPI class for a substitute.

    In case you're still looking, the evaluation functions are in SplineUtility:
    https://docs.unity3d.com/Packages/c...tility_EvaluatePosition__1___0_System_Single_
     
  4. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    The source code is not available in a public repo, but is included when you import the package. Right click the Splines folder in Unity and "Show in Finder/Explorer" to view.

    You can reverse a Spline in the editor using the Scene View "Knot Inspector" overlay.
     
    yarilo likes this.
  5. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    This is an example scene included in the Samples, which can be imported from the Splines entry in the Package Manager window.
     
    Wolfos likes this.
  6. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
  7. Acissathar

    Acissathar

    Joined:
    Jun 24, 2011
    Posts:
    677
    Hi,

    A few issues I have run into:

    1. Spline Animator doesn't show all fields (notably Movement related settings) until you deselect the game object and then reselect it.
    • Adding Component
      upload_2023-1-19_9-7-52.png
    • Deselecting and reselecting
      upload_2023-1-19_9-8-16.png

    2. Occasionally, selecting any Spline object causes a bunch of Look Rotation messages to spam the console as long as any spline object is selected. It seems to be random, and resets for a little upon restarting the Editor, but I have not been able to consistently replicate it, but it only occurs on Spline objects. The log itself also doesn't give much info about where it's coming from upload_2023-1-19_9-10-11.png

    3. Although you can select multiple objects with Spline Animators and the component doesn't give the usual Multi-Editing is not Allowed message, pressing Play for preview will only apply it to the most recently selected object. De-selecting an object will stop the Preview as well.

    4. Max Speed of 0 causes the time to recalc to NaN, but changing Max Speed to non-zero does not recalculate out of that. The time remains NaN until Restart is called. A workaround is to clamp to something like 0.001 I suppose and then Pause.
     
    Last edited: Jan 19, 2023
    Shizola likes this.
  8. Marcos-Elias

    Marcos-Elias

    Joined:
    Nov 1, 2014
    Posts:
    159
    Does it work with floating origin?
    Currently I’m using Dreamteck Splines, it uses local positions so we can move the entire spline and everything just continues working. I would like to confirm that before upgrading my projects.
     
  9. gabrielw_unity

    gabrielw_unity

    Unity Technologies

    Joined:
    Feb 19, 2018
    Posts:
    970
    If I understand correctly - yes. The Spline object can be moved anywhere, result will be the same as moving a parent gameobject - all the child objects stay with it.
     
    Marcos-Elias likes this.
  10. JonathanMeaney

    JonathanMeaney

    Unity Technologies

    Joined:
    Jan 28, 2022
    Posts:
    4
    We're tentatively scheduling a release for the end of February.
     
  11. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    Any chance to get an early release for some people? Is there somewhere where one can contribute and give feedback before a release?
     
  12. Nblues

    Nblues

    Joined:
    Aug 26, 2014
    Posts:
    1
    Great job on the spline system! Its very nice to use and the spline painter is good!

    I have read through the documentation to see if there is a function to get the distance between two points on a spline (follwing the flow) but I cant seem to see any easy fix hehe. Am I missing something in the documentation or could someone maybe help point me in the right direction of how to find this? :)
     
  13. JonathanMeaney

    JonathanMeaney

    Unity Technologies

    Joined:
    Jan 28, 2022
    Posts:
    4
    For this release, there aren't plans for organizing anything like that. We really like sharing pre-release versions for feedback, but we're not set up for it right now.

    However, we do appreciate any documentation feedback you have at any time. You can always click the Report a problem on this page button to leave feedback on the documentation. We read it!
     
  14. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    I figured, since I saw that you guys also added instant selection and dragging as I showed how I needed it in the other thread.

    However we based our asset on the Unity Splines. Really, really promising spline framework of yours. But if you guys release all of a sudden something that doesn't work properly anymore we are in a support hell with negative reviews.

    Some don't like beta-testing, I however do. It shapes the way. If you guys want early feedback and testing, I'm in :)
     
    gabrielw_unity likes this.
  15. chrpetry

    chrpetry

    Joined:
    Mar 7, 2018
    Posts:
    65
    First thing I did when using Splines package here is writing extensions...

    Can you please add convenience methods
    • Convert float3 to Vector3 (and back)
    • Get a list of Vector3 from Spline ("GetPositions()")
    • Spline.SetKnot(-1, ...) // -1 to fetch last knot
    And I guess I will stumble on many other stuff that would be nice to have.
     
    Last edited: Feb 3, 2023
    Novack likes this.
  16. MaxWitsch

    MaxWitsch

    Joined:
    Jul 6, 2015
    Posts:
    114
    I was wondering why the spline.changed delegate has been deprecated and insted the static method UnityEditor.Splines.Spline.OnSplineChanged is implemented.
    Is there any advantage by using this workflow?
     
  17. Balours

    Balours

    Joined:
    Nov 27, 2013
    Posts:
    59
    How precise is the EvaluatePosition on the spline?
    I'm placing X objects on the curve based on the Length of it (example if length = 10, 10 objects, 1 at every unit), and I want them to move all together along the spline, but It's not accurate at all. Even when spawning I can see weird spacing between objects (in this example spline length = 120, with 120 objects):
    upload_2023-2-8_12-6-26.png

    Then if I try to move them using varioug methods it always end ups with inaccurate offset building over time.
    Is there a spline resolution somewhere? Or something else I should use instead of EvaluatePosition?
    Thanks!
     
  18. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    802
    float3 has an implicit conversion operator for Vector3, so a toVector3() method would be redundant.
     
    Novack likes this.
  19. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    802
    Its implicit, so you don't need to cast,

    Code (CSharp):
    1. float3 myFloat3;
    2. Vector3 myVector = myFloat3;
    3.  
    You can pass float3 to all methods that take Vector3 as a parameter without casting. and it works both ways.

    Code (CSharp):
    1. float3 position = transform.position;
    2. // to some stuff
    3. transform.position = position;
     
    Novack likes this.
  20. imblue4d

    imblue4d

    Joined:
    May 27, 2016
    Posts:
    110
    You'll still need to cast when using some math operators the add implicit is missing for example
    e.g (my use case)
    Code (CSharp):
    1. myVar += (Vector3)MySpline.EvaluatePosition(0.5f);
     
    Novack likes this.
  21. Balours

    Balours

    Joined:
    Nov 27, 2013
    Posts:
    59
    Any chance to have informations on that? Just so I know if I wait for an update or move onto another spline system.
     
  22. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,780
    Can unity spline methods be called from Jobs?
     
  23. osgseb

    osgseb

    Joined:
    Jan 17, 2022
    Posts:
    26
    Hello,

    I just tried the package and take a look at the extrude example but it seems that it's hard-coded and can only extrude a tube and not any kind of mesh. Is there plan to create a more generalized version of this script ? It could be very useful in many scenarios, like create roads from a prefab.

    Taking example from the Sprite Shape Controller it could be very cool to be able to specify different meshes depending to the curvature of the spline in order to have more control of the result.

    Anyway, the package it's cool and I'm looking forward to see what else is coming.

    Good job.
     
  24. unity_8p6oRXXqiWbRTQ

    unity_8p6oRXXqiWbRTQ

    Joined:
    Dec 20, 2018
    Posts:
    22
    This would be such a basic needed tool, i cant stress enough how important this would be. There is a free spline tool in the unity asset store, called spline mesh, which sadly isnt updated anymore, but maybe could be used as an inspiration.
     
  25. adslitw

    adslitw

    Joined:
    Aug 23, 2012
    Posts:
    276
    Hi @gabrielw_unity - I've noticed that SplineUtility.GetPointAtLinearDistance returns based on the scale of the spline - e.g. if the spline gameobject is scaled to 2 and you ask for a point 1 unit away, it will return a point 2 units away. Is this expected behaviour? It's easy enough to work around at uniform scalings, but a bit tricker if the scale is non-uniform.

    Edit: this is my current hack, but it relies on the forward vector of the previous point, so I guess it's not completely accurate -

    Code (CSharp):
    1. float localSpaceDistance = transform.InverseTransformVector(currentForwardVector.normalized * distance).magnitude * Mathf.Sign((distance));
     
    Last edited: Feb 27, 2023
  26. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    I'm using 2021 lts. Can't find official github for Splines. Should i install 2022, download via package manager and copypaste to 2021 project, or is there some easier way?

    edit: i guess this is the closest one for official: https://github.com/needle-mirror/com.unity.splines
     
    Last edited: Mar 3, 2023
    FerdowsurAsif likes this.
  27. JonathanMeaney

    JonathanMeaney

    Unity Technologies

    Joined:
    Jan 28, 2022
    Posts:
    4
    Hey!

    Splines is compatible with 2021.3, but not supported. In 2021,3, you can find it in the Package Manager if you search for the package by name:
    1. In the Package Manager, click the + sign dropdown and select Add package by name...
    2. In the Name field, enter com.unity.splines.

    upload_2023-3-3_10-11-21.gif
     
    FerdowsurAsif and chelnok like this.
  28. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    Yes, in fact there are some evaluation jobs included in the `SplineJobs` class.

    On the question of overloads vs. out params, we did it this way because it's very common to check if a point is closer than x to a spline, then do something with the out parameters. It's a matter of preference, and there certainly is merit to an alternative that would return a struct with a data payload.

    On out float3 not working with out Vector3, yeah that's an annoyance for sure. Unfortunately I think we're stuck on this one unless we move all our math to UnityEngine. Being inconsistent between float3 and vector3 would be significantly more annoying in my opinion.

    What doesn't work when passing `Spline`?

    GetPointAtLinearDistance doesn't apply any transformations. I'd need to see the full code to see where the scale is being introduced.
     
  29. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    @kaarrrllll is there an ETA for the next spline update? I thought end of Feb, but there's nothing yet. Just asking if I should continue with current version or still wait for next
     
  30. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    I don't have an exact date, but I can tell you that we have a last few PRs in review before release QA starts. From that point, probably another week or two before the updates are published to the public repo.
     
  31. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    Nice, thanks. I know it's hardly an option, but if you guys need some testing or feedback before you roll it out to the masses, just hit me up. Or I guess @JasonBooth as well, since his asset is one which uses your splines extensively and it's an excellent showcase.
     
    kaarrrllll likes this.
  32. tafkams

    tafkams

    Joined:
    Oct 5, 2021
    Posts:
    46
    Hello, and first all of thank you for the amazing spline package. However, I stumbled upon an issue with the UVs if I extrude the spline. I would expect the uvs to span the entire extruded spline, but if you consider the below spline, where
    if(uv.y =1) col.rgb = float4(1,1,1,1);
    , you see that the uv space is restricted between the first and second knot of the spline.



    Do you have any suggestions or timeline on how to fix this?
     
  33. Image3d

    Image3d

    Joined:
    Jun 20, 2008
    Posts:
    155
    How can I attach a Knot to an GameObject, so when this GameObject moves the knot also move ?
     
  34. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    Bug in 2.2.0: When you add a new knot to a spline it goes straight into Bezier instead of Auto.

    Why?
     
  35. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    `SplineRange` was introduced in 2.0 and generally we try to use it as a replacement for indices or `Spline` where-ever it makes sense. GetLength is a good suggestion, I'll add it to the backlog.

    This is to make it easier to register code to listen for any spline events in the editor without keeping track of when splines are created/destroyed.

    As of 2.2, the `Spline.Changed` signature looks like this:

    Code (csharp):
    1.  
    2. /// <summary>
    3. /// Invoked any time a spline is modified.
    4. /// </summary>
    5. /// <remarks>
    6. /// First parameter is the target Spline that the event is raised for, second parameter is
    7. /// the knot index and the third parameter represents the type of change that occured.
    8. /// If the event does not target a specific knot, the second parameter will have the value of -1.
    9. ///
    10. /// In the editor this callback can be invoked many times per-frame.
    11. /// Prefer to use <see cref="UnityEditor.Splines.EditorSplineUtility.AfterSplineWasModified"/> when
    12. /// working with splines in the editor.
    13. /// </remarks>
    14. /// <seealso cref="SplineModification"/>
    15. public static event Action<Spline, int, SplineModification> Changed;
    16.  
    UVs on the extruded mesh are wrapping on the Y axis. That is, they span from 0 to 1 on X, but will span from 0 to Length on the Y axis.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Splines;
    4.  
    5. [RequireComponent(typeof(SplineContainer))]
    6. class SplineFromChildren : MonoBehaviour
    7. {
    8.     Spline spline;
    9.    
    10.     void Start()
    11.     {
    12.          spline = GetComponent<SplineContainer>().Spline;
    13.          for (int i = 0, c = transform.childCount; i < c; ++i)
    14.             spline.Add(new BezierKnot(transform.GetChild(i).localPosition));
    15.     }
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         for (int i = 0, c = transform.childCount; i < c; ++i)
    21.             spline[i] = new BezierKnot(transform.GetChild(i).localPosition);
    22.     }
    23. }
    24.  
    Assuming you mean when adding from the Inspector, I'm not sure why that is. It feels like it should take the previous knot type there.
     
  36. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    How can I always set everything on the Spline to Auto? Bezier is so annoying and can never get it right. It might be nice for fine-tuning in advanced mode, but generally it's not for simple use cases.
     
  37. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
    I toyed around with combining MicroVerse with Easy Roads and using Unity Splines as bridge:



    In case anyone wants the code, you can get it here.
     
    Gokcan likes this.
  38. eliasmi

    eliasmi

    Joined:
    Apr 15, 2018
    Posts:
    1
    Hi! First of all, thank you for making this package. I truely see the potential using it.

    I'd like to ask for a tip or two, but I'm not sure if I'm taking the spline system a bit out of intended use case:
    I'm sampling a spline for the nearest position from a Rigidbody to set its position via rb.MovePosition along with evaluating the tangent to get a forward rotation. Also evaluating spline data to set the boundaries of the possible movement perpendicular to the spline. The velocities I'm targeting are in the hundereds thus the movement via RB.

    The problem I'm having is when the spline is relatively curved my horizontal movement is either slowed to a crawl or absolutely catapulted on the set boundaries. Is my vector logic flawed? No problems on a straight spline no matter the direction.

    Code (CSharp):
    1. private void FixedUpdate()
    2.         {
    3.             _position = rb.position;
    4.             var d = SplineUtility.GetNearestPoint(_container.Spline, _position, out _nearest, out var t, SplineUtility.PickResolutionMax, SplineUtility.PickResolutionMax);
    5.             // horizontal offset limit
    6.             var width = zone.WidthData.Width.Evaluate(_container.Spline, t, new Interpolators.LerpFloat());
    7.  
    8.             // evaluate rotation
    9.             var fwdRotation = Quaternion.LookRotation(_container.Spline.EvaluateTangent(t), _container.Spline.EvaluateUpVector(t));
    10.             rb.rotation = fwdRotation;
    11.             _fwdDir = fwdRotation * Vector3.forward;
    12.             _targetForward = _fwdDir * (Time.fixedDeltaTime * speed);
    13.  
    14.             // get horizontal input
    15.             var rawHorizontal = fwdRotation * Vector3.right * (inputHandler.Horizontal * Time.fixedDeltaTime * horizontalSpeed);
    16.             var futureHorizontal = Vector3.ClampMagnitude(_horizontalOffset + rawHorizontal, width);
    17.             _horizontalOffset = futureHorizontal;
    18.          
    19.             var nextPos = Vector3.ClampMagnitude(_targetForward + _horizontalOffset, speed);
    20.  
    21.             rb.MovePosition((Vector3)_nearest + nextPos);
    22. }
    The input is from the mouse screen position form -1 to 1.
     
  39. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,383
  40. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    Hi -

    1) Is there no way we can reverse the order that the SplineAnimate component is played? I was hoping I could set MaxSpeed to a negative speed (or to manually set a direction) to get it to go in reverse (like other third party solutions), but since you clamp it to >= 0 this is not an intended use case.

    The way I had to solve this was to duplicate the spline at run-time by pulling out a subset of the spline reversal code from EditorSplineUtility.ReverseFlow () and make an extension method out of it so I could use it at run-time. And then reference the reverse spline when going in the reverse direction. This isn't an ideal workflow.

    2) There are also no published events for reaching the end of the path, so I had to poll the normalized position myself using the Updated event that you provide to know when the path was finished using SplineAnimate.
     
  41. taimex

    taimex

    Joined:
    Feb 20, 2022
    Posts:
    13
    C# keyword 'new' not 'New'. Use it in front of the constructor BezierKnot() and Spline().
     
  42. M4XXX

    M4XXX

    Joined:
    Aug 27, 2017
    Posts:
    10
    I am trying to use the splines as a basis for my level editor.
    My aim is to take a number of Prefabs each of which will be a flat road section (one may be tarmac, another dirt etc.) and modify these prefabs to fit along the spline.

    In order to do this, I am taking the length of my prefab and, for each point in its mesh, finding the equivalent point along the spline, then moving the mesh point an appropriate amount so the mesh bends to follow the spline.

    At the end of that mesh, I position the next prefab's mesh and so on .

    To make life simple I am doing this in 2 dimensions - so my spline is confined to Z=0

    My track pieces are simple planes (at the moment) scaled by 2 along the x axis.

    So as I iterate over each vertex in the track's mesh, I need to find the equivalent point along the spline, at that distance along the spline.

    I had thought that GetPointAtLinearDistance() would give me this - but it appears not.

    Using this code

    Code (CSharp):
    1.         container = GetComponent<SplineContainer>();
    2.         spline = container.Spline;
    3.         float d;
    4.         for (int i = 0; i < spline.GetLength(); i++)
    5.         {
    6.             Vector3 splinePoint = spline.GetPointAtLinearDistance(0, i, out d);
    7.             Debug.Log($"At {i} d is {d} and point is {splinePoint}");
    8.         }
    My spline's length is 228

    But any value of i greater than 200 gives d as 1 and the point of (200,0,0) - where my last knot is at (200,0,0)
    e.g.
    Code (CSharp):
    1. At 217 d is 1 and point is (200.00, 0.00, 0.00)
    2. UnityEngine.Debug:Log (object)
    3. SplineExperiments:Start () (at Assets/SplineExperiments.cs:26)
    4.  
    5.  

    So it looks as if the linear distance is measuring along the x axis, and not along the spline - which sounds wrong?


    Attached image is for context so you can see the spline.


    This is all a long-winded way of asking if there is a way of finding the point a certain distance along a spline?
     

    Attached Files:

  43. ThomasLopez

    ThomasLopez

    Unity Technologies

    Joined:
    Jun 8, 2020
    Posts:
    163
    Hi Dirty Hippy!

    Here are some questions for you and answers to yours :D
    Regarding 1) There is no way to animate in the reverse direction indeed. However, it's possible to go back and forth using the loop mode "Ping Pong".
    The reason reverse is not a possibility for now is that for simplicity here we are using a unique Time/Speed parameter to define the motion along the curve, and a negative speed would result here in a negative time which does not make sense here. This could indeed be feasible to add this, but we thought that forward and pingpong should cover most usecases. Do you have a special one that cannot be covered by that ?

    Regarding 2), There is no event indeed, using the Updated event is a possiblity, you could also check in a script of yours the value of IsPlaying which will change to false when reaching the end of the spline, this might be faster and allow less calcullation. The reason we didn't implemted that yet is that usecases can be really different depending on the animation (one time, loop, pingpong, on close or open splines), so this is tricky in term of events as the "end" can be different in these usecases, it would also require to add an event for the begining of the spline (for ping pong).
     
    JonathanMeaney likes this.
  44. ThomasLopez

    ThomasLopez

    Unity Technologies

    Joined:
    Jun 8, 2020
    Posts:
    163
    Hey M4XXX!

    Thanks for reaching out!
    Out of curiosity, did you have a look at the samples provided with the package ?
    We have a sample that does a road mesh creation if you look at the "SplineData and Generation" scene.

    To answer your question then. So yes the method GetPointAtLinearDistance() computes a linear distance, linear being "in a straight line" from the first position (here 0 for you) to the one you demand (here i).
    This is used to place non deformable objects along the spline, for instance fences (have a look at the SplineInstantiate samples for more details :D)

    In your case, you just want a position that is at a certain distance d from the origin of the spline, so you can just use the Evaluate function to get that and you'll get the up and normal vectors (the get the direction perpendicular to the spline direction) for free as well.

    Code (CSharp):
    1.    
    2. container = GetComponent<SplineContainer>();
    3. // use NativeSpline to work with SplineContainers for which the GameObject as scale and/or rotation
    4. using (var nativeSpline = new NativeSpline(container.Spline, container.transform.localToWorldMatrix
    5. ))
    6. {
    7.         var length = nativeSpline.GetLength();
    8.         for (int i = 0; i < length; i++)
    9.             nativeSpline.Evaluate((i / length), out var splinePoint, out var tangent, out var upVector);
    10. }
     
    Last edited: Apr 11, 2023
    M4XXX likes this.
  45. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,937
    Building fails and this compiler error is reported. At a high level has anyone else experienced this error?
    Assets\Samples\Splines\2.2.0\Spline Examples (requires Shader Graph package)\Runtime\LoftRoadBehaviour.cs(158,21): error CS0103: The name 'Undo' does not exist in the current context
     
  46. ThomasLopez

    ThomasLopez

    Unity Technologies

    Joined:
    Jun 8, 2020
    Posts:
    163
    We logged that and will be fixing it soon! Thanks
     
    JamesArndt likes this.
  47. taimex

    taimex

    Joined:
    Feb 20, 2022
    Posts:
    13
    I've been trying to use SplineData to pass in more than just floats. Example cases:
    Case 1:
    Code (CSharp):
    1.  
    2. public class WidthsSplineData : MonoBehaviour {
    3.         [SerializeField]
    4.         SplineData<int2> i2 = new();
    5.         public SplineData<int2> Widths {
    6.             get {
    7.                 return i2;
    8.             }
    9.         }
    10.         public int Count => i2.Count;
    11.         SplineContainer splineContainer;
    12. ...
    13. }
    Case 2:
    Code (CSharp):
    1.  
    2. public class MaterialSplineData : MonoBehaviour {
    3.     [SerializeField]
    4.     SplineData<Material> material = new();
    5.     public SplineData<Material> Material {
    6.         get {
    7.             if (material.DefaultValue == null)
    8.                 material.DefaultValue = Resources.Load<Material>("Road");
    9.             return material;
    10.         }
    11.     }
    12.     public int Count => material.Count;
    13.     SplineContainer splineContainer;
    14. ...
    15. }

    Case 3:

    Code (CSharp):
    1.  
    2. public struct Combo : IComparable<Combo> {
    3.     public float scale;
    4.     public int mat;
    5.     public float offset;
    6.     public int CompareTo( Combo other ) {
    7.         return 0;
    8.     }
    9. }
    10.  
    11. public class ComboSplineData : MonoBehaviour {
    12.         [SerializeField]
    13.         SplineData<Combo> combo = new();
    14.         public SplineData<Combo> Combo {
    15.             get {
    16.                 if (combo.DefaultValue.mat == 0) {
    17.                     Combo defCombo = new() {
    18.                         mat = 7,
    19.                         offset = .2f,
    20.                         scale = 7.4f
    21.                     };
    22.                     combo.DefaultValue = defCombo;
    23.                 }
    24.                 return combo;
    25.             }
    26.         }
    27.         public int Count => combo.Count;
    28.         SplineContainer splineContainer;
    29. ...
    30. }

    Case 1 when the inspector draws the Data Values for a Data Point, the two integer input fields when expanded overlaying on tope of the +- buttons

    Case 2 works as it fits on one line and confirmed I can use other non-numeric types

    Case 3 throws a NullReferenceException in SplineDtaropertyDrawer.cs:31

    I would really like to be able to have a combination of data used for SplineData. Nothing in the documentation implies I can not except for the PropertyDrawer not handling it correctly. Use case: change a set of properties at different points on a spline.

    Are there constraints as to the TDataType used for SplineData?

    In all use cases, it seems that the "Default Value" are never used when a new Data Point is added with the + button. Even your example WidthSplineData does not do this. Why call it Default Value if it isn't used by default?
    Is there something more in code that needs to be done to support it?

    Also, can you help me understand when DataPoint<TDataType>.CompareTo( TDataType other ) is called?
    I assume CompareTo( float other ) is used to sort the Data Points array. But I'm not sure if I need to implement it for TDataType, nor how to do it if I don't know why/when it is called?
     
  48. M4XXX

    M4XXX

    Joined:
    Aug 27, 2017
    Posts:
    10
    Thanks you so much for the response - and sorry it took so long for me to reply - for some reason the forum didn't email me to tell me there was a response.

    I did look briefly at the samples - but the road on looked like it was extruding a 2d shape along the spline, whereas I'm bending a 3d shape around it - so (in my head, at least) it is my mesh driving the computation whereas with the road sample, it is the spline deciding where each point should be generated - but perhaps I should have looked more closely!

    I'm working on changing my editor now following your advice - so thanks, once again.
     
    ThomasLopez likes this.
  49. M4XXX

    M4XXX

    Joined:
    Aug 27, 2017
    Posts:
    10
    It's late here so I may be missing something - but the constructor forNativeSpline takes a float4x4 as the transform parameter, but the transform property of container is a Transform?
     
  50. abeltoy

    abeltoy

    Joined:
    Feb 27, 2023
    Posts:
    2
    How do I add a point in the middle of an existing spline without the tool automatically creating a new spline instead?