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

SplineMesh, the plugin to create curved content

Discussion in 'Assets and Asset Store' started by methusalah999, Dec 14, 2017.

  1. zagreekie

    zagreekie

    Joined:
    Nov 20, 2017
    Posts:
    29
    I've run into an interesting problem, and I'm wondering if you have any ideas on how to solve it. Ideally without needing to bake the model into a mesh, and keeping it in its workable spline form.

    Ive been working on baked lighting, and i noticed it works fine in all situations except "repeating" in that situation it seems like the way the spline is potentially dealing with the UV for the lightmap data is forcing all segments to use the data of the last segment (origin node is at the bottom in the below screenshot, and you can see the first pipe segment of the repeat is using the lightmap of the second and final segment.)

    upload_2020-11-6_13-1-4.png
     
  2. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
  3. zagreekie

    zagreekie

    Joined:
    Nov 20, 2017
    Posts:
    29
    oh sorry, I missed that. I'll be able to test it on Monday when my programming partner comes back, I'm just the dumb artist.
     
    methusalah999 likes this.
  4. zagreekie

    zagreekie

    Joined:
    Nov 20, 2017
    Posts:
    29
    I gave your fix a try, and now they are not all repeating the same lightmap uv however they are not lighting correctly.

    hard to explain but here is my test you can see the differences between the stretched pipe lighting and the repeating pipe lighting. upload_2020-11-9_1-9-10.png
     
  5. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Ok I knew there would be some issues with this method, even if I didn't observe it myself. That's why I didn't push it on the store.

    I can think of another solution that may work. First, revet the changes I proposed earlier, then you can try to use the method
    Unwrapping.GenerateSecondaryUVSet(result);
    at the end of the same method, after the last line
    MeshUtility.Update(...);
    . This way, Unity will do the job itself, allowing some space on the lightmap for each of the generated meshes.

    This method is probably time consuming and you should NOT use this modification for a mesh that requires to be updated every frame. If you have both baked and dynamic bent meshes in your scene (and this fix works), I will integrate the fix correctly in the SplineMesh.
     
    Last edited: Nov 10, 2020
  6. Razelis

    Razelis

    Joined:
    Sep 29, 2016
    Posts:
    2
    Greetings,

    First of all, awesome asset keep up the good work! I have a question and wanted you ask about it. I'm sorry if this has been asked before, going through 500 replies is kinda hard.

    So, I'm trying to create a rollercoaster track, it was going alright until, MeshTiling started to go wild. On the first screenshot, current track is fine without any problems.
    upload_2020-11-9_11-37-36.png
    But when I try to extend the track, with or without new nodes it turns into second screenshot, instead of extending the track.
    upload_2020-11-9_11-39-5.png
    And in the third screenshot it shows me the current extension goes into the start of the path, like this.
    upload_2020-11-9_11-40-14.png
    And my current settings are below.
    upload_2020-11-9_11-41-51.png

    Is this caused by the model itself or am I missing something around here? Worked with the asset before but didn't got this issue, so any ideas would be appreciated.
     
  7. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    tl:dr you will have to write your own script to cut the final mesh into smaller parts.

    I think it is caused by your generated mesh having more than 65535 vertices, which is the max supported by Unity. Could you please confirm that by going down into the hierarchy of the spline object, select the generated mesh and check the vertex count?

    In repeat mode, SplineMeshTiling will create a single mesh, merging as many repetitions as possible. This is clearly a limitation of this script but having a giant mesh of millions of vertices would not be good either, as the whole track would always be drawn by the engine, even when most of it is outside the camera field of view, as long as you see any part of it.

    To enable a correct frustrum culling (avoid drawing things that are outside the camera fiel of view), you will have to cut your meshes in smaller parts. It is no big deal but you will have to write some code.

    First, please understand that SplineMeshTiling is a very convenient script that allows for 90% of the spline common uses. It is totally normal that it fails in some situations like yours. I've designed SplineMesh with that in mind and made this script quite easy to expand.

    Thinking about it, I think the SplineMeshTiling script could be doing the cutting itself, by avoiding to create a repeted mesh with more than 65535 vertices, but I wont have time to do it myself right now.

    Start by copy/pasting the SplineMeshTiling script code to your own (or start with a simpler one with less parameters), and go into the CreateMesh method. This line
    go.GetComponent<MeshBender>().SetInterval(spline, 0);
    is the culprit. It creates only one mesh bender with the repeat mode, resulting in the giant mesh.

    What you will want to do in your case, is to create more objects in a "for" loop. For example, if you want to have a maximum of 100 mesh repetitions per group, you could have a "for" loop that finds the space occupied by 100 meshes, then iterate from zero to spline.Length, and for each space, create an object and set the correct spline interval (0 to 100, 100 to 200, etc.).

    If you go down this road, I'm interested in seeing the result!
     
  8. Razelis

    Razelis

    Joined:
    Sep 29, 2016
    Posts:
    2
    Thank you very much for your fast response. Didn't know about the mesh generation limit, it is good to know, thanks!

    I'll try the for loop solution, if I can manage to make it work, I'll post the results. Else, I'll check some other solutions like making the road as a 3D model altogether.
     
    methusalah999 likes this.
  9. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    you can also split the spline in many splines, use a longer track model in curve space, that kind of things.
     
  10. francescobrischetto97

    francescobrischetto97

    Joined:
    Nov 10, 2020
    Posts:
    1
    Is there a way to draw bezier surfaces using this plugin?
     
  11. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Nope.

    You can still expand it. It is open-source, has strong architecture and already has most of the Bézier UI and math required for surfaces.
     
  12. zagreekie

    zagreekie

    Joined:
    Nov 20, 2017
    Posts:
    29
    I have found when trying to load prefabs with mesh generating splines in them at run time, they do not have their mesh. is there any way to save their mesh data in a prefab the same way it does in a scene?
     
  13. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Generated objets are supposed to be only generated in the scene, or in play mode, not in the prefab editor. The main reason for it comes from the prefab refactor of Unity 2018, that prevents degenerating changes in the prefab hierarchy.

    If you allow your spline scripts to generate content inside the prefab editor it will cause issues. In the scene, on many occasions, the scripts will try to generate the content again but will fail to delete the previous generated content because it is bound to the prefab.

    My suggestion is to always remove the generated content before saving your prefab, and let it be generated in the scene only. Removing the generated content is an annoying step and SplineMesh could use some refactor here.

    That should resolve any generation issue both in editor and during playmode. If it doesn't please tell me with a more detailled scenario.
     
  14. zagreekie

    zagreekie

    Joined:
    Nov 20, 2017
    Posts:
    29
    I am working on a side on a colony builder style game, where the placable rooms are instantiated prefabs. (image attached below) the issue I am having is when we load one of these prefabs all of the spline meshes are gone.

    toggling on the "update in play mode" option fixes this, however, it does mean that I can no longer bake lighting to the splines. which I would like to do. (using bakery you can bake lighting data inside of prefabs.)

    upload_2020-11-11_14-19-41.png
     
  15. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Ok I understand. I didn't thought about the possibility of baking the lighting into the prefab.

    To solve this, you just have to deactivate, or even delete completly the spline script in your prefabs. So you generate your content, bake the lighting then you don't generate it again, neither in the scene nor in the game.
     
  16. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,401
    @methusalah999 I was searching for an asset that can connect 2 points with a cable which moves at runtime. Yours looks very promising for that. Actually it looks awesome, thank you very much!

    However the latest version on the asset store doesn't seem to work in runtime. I see the colliders move in the editor when I hit play, but the mesh isn't visualized there. Looks like this:

    cable.jpg
    For my project I currently use a different asset which uses a line renderer. Which is fast, but comes with the flaws of a line renderer. Can your asset connect 2 points and make some kind of a dynamically wobbling mesh? Here's an example what I'd like to do:

    lr.jpg

    I'd like to connect the points on the left and the right side with a cable.
     
  17. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    The bug you see is due to the static batching. It's my fault and I need to push a fix. In the meantime, you can deactivate the static batching completly in project settings -> player -> other. The mesh should update correctly at run time.

    About your use case, it is totaly doable with SplineMesh. The method depends on the quality you need to obtain.

    The best looking solution is to create a chain with joints, like the one you mentionned from the demo scene. You will have a spline with as many nodes as the chain joints, and move the nodes each frame so they match with the physic simulation.

    If you want to avoid the physic calculation, you can also create a script that will just place and update an intermediate point wobbling between the two anchors to simulate a weight. You will then have a three nodes spline, one on each extremity and one on the weight and move them accordingly each frame.

    There may be many other solutions of course.

    Hope it helps !
     
    Rowlan likes this.
  18. BAB4K

    BAB4K

    Joined:
    Sep 4, 2020
    Posts:
    1
    Hello. I am a beginner. Would it be possible for you to help me getting this to run and work. Having difficulty figuring it out myself. Thanks.




     
  19. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    I can provide help using SplineMesh, but I will need at least one question.
     
  20. canmujde

    canmujde

    Joined:
    Oct 16, 2018
    Posts:
    4
    Hi, I have this issue. When i even clicking on node does not get selected on the inspector panel. So I can't modify it's x,y properties etc. manually. Also because of this i can't add another node after selected...

    I'm having this issue on Unity Editor 2019.3.9f1 and 2020.1.2f1

    upload_2020-11-30_1-28-10.png
     
    Last edited: Nov 29, 2020
  21. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    This bug has been reported many times, but I have never been able to reproduce it myself. Most of the time, reseting the layout to default solved the issue (a button in the upper right).

    If you have any information about what caused the issue, I'm interested to know!
     
    canmujde likes this.
  22. canmujde

    canmujde

    Joined:
    Oct 16, 2018
    Posts:
    4
    Wow, thanks for very fast reply and for the solution! Unfortunately, I could not recognize the source of the problem at that time. I have no idea when it happened because I noticed it later. But I'll be more careful from now on, if it happens again I'll report back on when it happened.
     
    methusalah999 likes this.
  23. ConanTheDeveloper

    ConanTheDeveloper

    Joined:
    May 8, 2011
    Posts:
    9
    First of all thank you for the incredible work;
    If possible I would like some suggestion on what's the best approach to use the plugin to create a branch-like element, let's say like the tentacle example, but at runtime;

    from the examples I found how to add nodes at runtime, but I am not understanding the logic behind the parameters direction and up in order to create a more or less contorted branch, following a general direction.

    when moving the tangent nodes what node property am I altering ? n1 and n2 ?
     
  24. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    I don't know exactly what is your issue so here is some random info that may help.

    A SplineNode is:
    - a position where the curve pass,
    - a control point that control both the tangent of the curve and the curvature.

    In SplineMesh the control point is unintuitively named Direction. This vector is NOT a directionnal one. Instead, the actual direction is the vector between node.Position and node.Direction. It should be called controlPoint but I've never renamed it for backward compatibility purpose.

    The vector (node.Direction - node.Position) give two information. Its direction is the tangent of the curve at the node position, and its length is the persistance of this tangent, that defines the curvature of the curve.

    If you want to place a node using a direction, you will typically do
    new Node(Position: nodePosition, Direction: nodePosition + direction * curvature)
    .

    Also, note that a curve is define by two nodes n1 and n2. n1.Direction is used to define the tangent at the curve start, and the tangent for the end is the mirror of n2.Direction. SplineMesh only support mirrored tangents between curves, this is a limitation.

    The curvature will depend on the scale you are working at. It is typicaly defined by a portion of le length between n1 and n2 but other solutions are possible.

    Finaly, note that SplineMesh includes a component to automaticaly smooth a spline. You just set your nodes' position and this component will find a correct Direction for you. You can specify a curvature. You can also look at the source code of this component to get a better understanding.

    Have fun bending things!

    Ben
     
    ConanTheDeveloper likes this.
  25. ConanTheDeveloper

    ConanTheDeveloper

    Joined:
    May 8, 2011
    Posts:
    9
    Again, as a first thing I'd like to thank you again for your support.

    I had two objectives and with your description and the examples / video provided I solved the first:
    I also need to split the spline between two nodes, without altering its shape;

    While i do understand the code, I still am quite green on the math behind it.

    do you think that I could split by using GetSampleAtDistance and/or any other methods already available?

    or do you think that the best approach is to actually calculate the point at a certain distance and then recreating it ?


    thank you
     
  26. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    You can find a point on the spline with three methods :
    - GetSample(float time) where time is a value from 0 to node count, and increase without taking the curvature into account,
    - GetSample at distance(float dist), dist is the distance from the strat of the spline, in world meter,
    - GetProjectionSample(Vector3 pointToProject), this will give the closest point on the spline from the given point.

    Now you have the position on the spline, you will want to insert a node between the previous and the next one. That will require to iterate through the node list to find the border nodes.

    The real challenge will be to find the control point (Direction) of the new node, so you can get the exact same curve as before the insertion. The tangent and up vectors are already present in the curve sample but for the curvature, there is no data and you will have to experiment. It may be as simple as using the average length of the previous and next nodes' direction vectors, but I doubt it will be that easy.

    I would appreciate some feedback on the subject, though.
     
    ConanTheDeveloper likes this.
  27. ConanTheDeveloper

    ConanTheDeveloper

    Joined:
    May 8, 2011
    Posts:
    9
    Ok, allow me to ramble for a bit, I think there is a quick way to implement this;
    all comes to the following question:
    given 4 Vector3 coords (start,end +2 control points) is it possible via script to create the curve ?

    are the control points splinenode.direction ?
    ---

    Any cubic bezier is defined by 4 points: P0...P3 where P0=start, P3 = end, P1, P2 are the control points.

    these points define 3 segments (see attached image)
    - (P0,P1)
    - (P1,P2)
    - (P2,P3)


    Splitting before or at the middle point (ex: middle point)

    We need to create a curve that is defined by:
    • Start = P0
    • End = split point of the curve => getSample(0.5f) ??
    • UP1 = Unknown control point 1
    • UP2 = Unknown control point 2
    we look for the following elements:
    - m0 = point at 50% of segment (P0,P1). (50% because we are splitting at 50% of the original curve)
    - m1 = point at 50% of segment (P1,P2) (50% because we are splitting at 50% of the original curve)
    - q0 = point at 50% of segment (m0, m1)

    Well if you check the image you have that
    UP1 = m0
    UP2 = q0

    so we now have all the coordinates to create the first half of the original bezier

    Splitting after the middle point (ex: 80% of the curve):
    the reasoning is identical, we just need to find m1, m2, q1 where
    - m1 = point at 80% of segment (P1,P2) (80% because we are splitting at 80% of the original curve)
    - m2 = point at 80% of the segment (P2,P3) (80% because we are splitting at 80% of the original curve)
    - q1 = point at 80% of the segment (m1, m2)
    • Start = separation point of the curve => getSample(0.8f) ??
    • End = End point of the original curve
    • UP1 = q1
    • UP2 = m2






    source:
     

    Attached Files:

  28. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Well it seems that you got it cover. Just in case, here is the equivalent of the quadratic bezier curve nomenclature, applied to SplineMesh:
    - p0 = startNode.Position
    - p1 = startNode.Direction
    - p2 = endNode.Position - (endNode.Direction - endNode.Position) (mirror of the next node Direction)
    - p3 = endNode.Position

    Also, you may have to work with both distances and time for that feature. You can easily get both these value in the curve space, as it is stored in any CurveSample. For example, spline.GetSampleAtDistance(3).timeInCurve will give you the time of this sample in the curve define by the previous and next node around the sample. Remember that the time, in the bezier glossary, is not growing linearily over the distance as the curvature is not taken into account. It in fact a rate going from a control point to the following one (p0 to p1 for example).
     
  29. ConanTheDeveloper

    ConanTheDeveloper

    Joined:
    May 8, 2011
    Posts:
    9
    I believe I hit an obstacle that I cannot solve immediatly.

    In order to add the point between two nodes , the control point of the end node must be altered.

    So even if the first segment is now been altered with a further node, and is identical to the original segment, modifying the final node direction to obtain this, causes the next attached segment to be altered; this is because the node has a single tangent (direction) used for both when the node is the beginning point of the segment and when it is the end point ,instead of having two independent tangents.

    I suppose the splinenode object should be modified,along with splineEditor, i think, to correctly position the handles, but I'm not sure of what other effects it could have
     
  30. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    I understand the issue.

    Mirrored direction is old limitation of SplineMesh and it could be a great addition to break it. As far as I know, there should be three modes for each nodes : mirrored, aligned, and free.
    - The mirrored mode is the one currently implemented. The in and out tangent vectors are symetrical around the node.
    - In aligned mode, the in and out tangent vectors are also opposed but they can have different lengths, hence different curvature. This is the mode you need.
    - In free mode, the in and out vectors can be completly independant.

    You are right to say that you will need to change both the SplineNode and the editor.

    Spline node will acquire a "mode" enum field, and Direction will be split in two InDirection and OutDirection. It would be a great time to rename that property to ControlPoint. Doing so, your Visual Studio will point out were you need to update the code to ensure everything works fine. It won't be a lot, but you will have to carefully think about each case.

    About the editor, it's no big deal but it's more work, to get an efficient UI.

    If you go down this road and wish to share commit your work to the repository, donc't hesitate to contact me via PM for any questions.
     
  31. miles24

    miles24

    Joined:
    Feb 1, 2019
    Posts:
    3
    So I have a problem that happens only on my job computer on Unity Pro. So when I create a spline in my scene, we have by default 2 spline nodes.

    Here is the result on my personal computer on Unity Free Version (2019.2.19f1):

    upload_2020-12-15_12-13-54.png

    On the computer at my job, I also have 2 spline nodes but I see each node (elements) 2 times (Unity Pro version 2019.4.13.f1)

    upload_2020-12-15_12-15-1.png

    I can't modify any node except the selected node. If I try to change the position of the Element 1 for exemple, the default value come back instantly.

    Do you know what is going on? Restarting Unity doesn't solve the problem.
     
    Last edited: Dec 15, 2020
  32. BParnell

    BParnell

    Joined:
    Aug 11, 2017
    Posts:
    2
    So how do I animate the tentacle? I have it growing in editor based on the growing roots demo scene however I want it to grow in timeline, or as a Unity animation based on the nodes I'd placed and then stop growing at the end. Is that possible? Also, can I export out the shape or convert it to an FBX so it's a static mesh?
    Thanks
    B
     
  33. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    I don't know what the problem is, but it seems that other user have some trouble with the spline editor. Usually, reseting the layout to default solve the problem (using the button in the top left corner of Unity). Can you try that?
     
  34. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Animating the spline or the tentacle growing is definitly possible with SplineMesh. To animate the spline itself, you can use empty game objects in your key frames and have a script that move and rotate the spline nodes according to these objects movement (you can't animate spline nodes directly as they are not unity objects).

    To animate the growing, you can create your own script based on the growing tentacle example, and change the code so that the growing ratio is not increasing over time but a simple public field instead. Then, you can change its value in your animation key frames.

    To export the result mesh of the mesh bender, there isn't any built-in tool in SplineMesh but it's pretty trivial:
    Code (CSharp):
    1. AssetDatabase.CreateAsset(myMeshBender.result, "myMeshPath.mesh");
    2. AssetDatabase.SaveAssets();
     
  35. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    Hi, first of all awesome asset and thanks for sharing it with everyone.

    I'm trying to update the first two nodes and last two nodes of my spline through code. Which is working fine, except I want the moved nodes to maintain their curve when moving these. What is the recommended way of doing this, should I simple apply the delta of the old position and new position to the direction as well instead of just updating the position?

    What I mean:
    https://i.imgur.com/wuIkiwL.mp4

    Notice how the red handle remains fixed. The nodes follow the player in this example.
     
  36. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    You indeed have to move node.Direction as well. Note that in SplineMesh, SplineNode.Direction is not a directionnal vector from position, but a control point in the same space (p1 in cubic bezier curve vocabulary). The direction vector is therefore
    node.Direction - node.Position
    (the length of this vector is the curvature).

    You can also simply use the SplineSmoother component to compute smooth directions automatically. You then only have to worry about the node positions.
     
  37. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    Thanks for the response. I'll go for the first solution I think. The SplineSmoother looks pretty sweet and mostly does exactly what I want it to, however there are two points in the Spline that basically have to make a 90 degree turn (wrapping around an object) and so wouldn't want to smooth these. Maybe I'll look into excluding certain nodes from the smoothing.
     
  38. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    This. Spline are a very generic tool in games, so most SplineMesh features have been designed to be simple to duplicate and modify.

    Have fun bending things !
     
  39. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    Yeah when I started I was certain I had to make some changes for my use case. But that's no problem at all, it's a fantastic toolbox. I ended up with the exclusion direction, because it makes more sense to do it this way and could help me with other use cases in the future.

    upload_2020-12-17_18-39-47.png
     
    methusalah999 likes this.
  40. miles24

    miles24

    Joined:
    Feb 1, 2019
    Posts:
    3
    I did Window - Layouts - Default but the problem is still there. I also tried to comment this section of the code (SplineEditor class, InspectorGUI function). It fixes the dupplication of elements but I'm still not able to modify the parameters of these elements.

    upload_2020-12-17_14-17-35.png

    SplineMeshBug.gif
     
  41. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    Thanks for the detailled report, I've been able to reproduce the bug.

    I will have to look into it, but in the meantime, you can use a simple workaround. The nodes in the list are indeed not editable, but the selected node, at the bottom of the list, is. You could just select the node that you want to edit, and consider the list as read only.
     
  42. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    Small QOL suggestion. In SplineEditor I register what the last active tool was in OnEnable and I restore this in OnDisable. Quite helpful when you have to switch a lot between objects.

    upload_2020-12-18_12-23-43.png
     
    methusalah999 likes this.
  43. afterdark1973

    afterdark1973

    Joined:
    Nov 25, 2019
    Posts:
    10
    This is an awesome asset and I am really hoping its just my lack of understanding that is stopping me from doing what I need. Would you be able to create a quick youtube tutorial showing how to create and animate a tank track as I just cant seem to get it to work... I have a mesh for a single track segment that I created in blender which I need to instantiate along a looped spline and then animate at various speeds corresponding to the velocity of the tank - any assistance very much appreciated -attached is a model of the tank for reference. I will need to parent the spline to the individual wheel pods as they can rotate up and down to accommodate the slope of the underlying terrain. Many thanks!
    upload_2020-12-27_11-27-35.png
     
  44. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    I won't be available to create a tutorial for it, but I can give some hints

    First, you will have to choose between two solutions :
    - sowing the track segments along the spline and move them. With this solution, track segments may not appear perfectly connected to each other.
    - bending track segments along the spline in a repeat mode. With this solution, segments will be deformed and it may be odd where the track is the most curved.

    For the first solution, you will start creating your own script using the ExampleSower as a base.
    - spawn as much segments as required along the spline
    - each segment will have a
    sowingDistance
    along the spline from 0 to
    spline.length

    - a variable "offset" will start from zero and be incremented with
    Time.deltaTime * tankSpeed
    each frame
    - keep the offset in the interval with
    if(offset > spline.length) offset -= spline.length;

    - for each segment, you will find the current distance with
    segmentCurrentDistance = segment.sowingDistance + offset
    and keep it in the interval the same way.
    - now you only have to place the segment on the spline with
    segment.transform.position = spline.GetSampleAtDistance(segmentCurrentDistance).Position
    . Same goes for rotation.

    For the second solution, you will have to use a trick where the spline loops because SplineMesh does not support bending a part of a mesh. Start creating your own script using SplineMeshTiling as a base.
    - use repeat mode and spline space,
    - have an "offset" variable like in the sowing version, but going from 0 to
    segmentLength

    - at line 95 or so
    go.GetComponent<MeshBender>().SetInterval(spline, 0);
    , replace "0" by your offset. This will cause the first mesh to be translated along the spline and simulate the motion.
    - place the end node of the spline beyond the start node, so the last mesh will occupy the empty place left by the first mesh.
    - adjust the scale on X, so an exact number of segment fits in the spline length, and the first and last one appear connected without a hole or overlap.

    In both solution, your spline will make a vertical loop, so you will have to adjust the up vector of the nodes to avoid a gimbal lock when the spline gets exactly vertical. There is a checkbox to draw and change the up vector of the node in the Spline component.

    In both solution, if you change the position or direction of the node, you will introduce a change in the length of the spline. It could be a great effect to see the track affected by gravity or bumps, but you will then have to change the scale of the segments dynamically so they continue to appear connected whatever the spline length is.

    Note that the first solution will be much faster to compute than the second one, because no mesh deformation is required.

    I hope some of this helps, and I would be happy to see the result!
     
    Last edited: Dec 27, 2020
  45. afterdark1973

    afterdark1973

    Joined:
    Nov 25, 2019
    Posts:
    10
    Sorry for the slow reply - I was really ill over Christmas :-(

    I went for option one and it seems to work really well!! Once I have the proper high poly track segment in place I will put a clip to the result on my games YouTube channel, a link to which can be found here: www.Titan-RTS.com

    A minor tweak I made was to track the track segments that got instantiated in a list of game objects (segmentGOs) so I didn't have to keep creating ad destroying objects

    If anyone else come across this thread trying to do similar, this is the function I created and called in the Update loop

    private void UpdateTrackPosition()
    {
    float distance = 0;
    trackOffset += tankVelocity * Time.deltaTime; // How much do we need to advance the track this frame
    if(trackOffset>spline.Length)
    {
    trackOffset -= spline.Length; // Make sure we are always in range of the length of the spline
    }
    foreach (GameObject go in segmentGOs)
    {
    float segmentOffset = distance + trackOffset;
    if (segmentOffset > spline.Length)
    {
    segmentOffset -= spline.Length; // Make sure we are always in range of the length of the spline
    }
    CurveSample sample = spline.GetSampleAtDistance(segmentOffset);
    go.transform.position = sample.location; // Set thje new segment position along the spline
    go.transform.rotation = sample.Rotation; // Set its rotation to match the spline
    distance += spacing; // Move on to the next segment position
    }
    }

    I don't seem to be able to modify the spline curve points by grabbing the handles though - is this by design?
     
  46. afterdark1973

    afterdark1973

    Joined:
    Nov 25, 2019
    Posts:
    10
    Sorry - one more question.. an you expand on how to fix the gimble lock please? I have tried various values in the up direction but cant get rid of it.... In the picture below, I need the spike to always be pointing outwards (this is a simple looped spline with just two points upload_2021-1-4_15-11-27.png
     
  47. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    645
    You can go into the showcase scene and look at the loop example to see how it's done. The easiest way is to have spline nodes where the spline is vertical. Defining a custom up vector where the spline is the most vertical is the better solution usualy. Also, to understand what's going on, don't try and set up vector from the inspector. You can actually draw the up vector handle on the scene view by checking the "draw up vector" box, then you just have to move the handle around the node to find the correct configuration.

    upload_2021-1-6_0-3-21.png
     
  48. afterdark1973

    afterdark1973

    Joined:
    Nov 25, 2019
    Posts:
    10
    cool! sorted it! thanks for all the help!
    upload_2021-1-6_12-14-40.png
     
    methusalah999 likes this.
  49. miles24

    miles24

    Joined:
    Feb 1, 2019
    Posts:
    3
    Thanks. Let me know when the problem will be fixed.
     
    methusalah999 likes this.
  50. Roboserg

    Roboserg

    Joined:
    Jun 3, 2018
    Posts:
    83
    Very nice asset thanks. I can't figure out how to set the nodes of the spline to align itself automatically, without using the handlers?