Download

Deform and Move mesh along spline

Hey all, I’m trying to figure out if anyone knows how (or if it’s possible) to both deform a mesh along a spline and also move it along that spline at the same time? I know how to do each separately, but not together.
I can get the mesh to deform along the spline, but as soon as I try to move it, it no longer deforms along the spline, it simply holds the initial deformation and then moves along the spline without deforming anymore.

Here’s a video of this code in action:

No one has any ideas on how to do this?

You would need to Set Start and End of the Spline Mesh Component during timeline’s update, too. The deformation does not happen automatically, you explicitly do it once when creating the SMC. If the spline deforms (and from the perspective of the sliding mesh it has deformed!), you will need to update its shape again.

Yeah, that’s what I thought, I just didn’t know how to do it, but now that I’m more awake, I’m thinking I could plug a custom node in there and run that on update along with the movement. Thanks for the help, I’ll post if it works.

hmm… Yeah, this is my problem with being an artist and not understanding the math. I don’t quite know what needs to be done to get the correct information from the spline. I’ve removed the construction script side of things and just doing everything at runtime now.

2nd Attempt:

And the results:

At least it’s updating the shape now :stuck_out_tongue:

Anyone with some better Blueprint and Math skills than me able to help figure this out?

Here’s a crude example using time which you can treat like percentage:

https://i.gyazo.com/b47c3eb79a1d5626…cf24119701.mp4

The shape event runs in the Construction Script (for debugging purposes). By default spline duration is equal 1, no matter how many points it has, this can be adjusted on the spline. If I set *TimeStart *to 0.0 and *TimeEnd *to 0.1, the SMC will deform onto the first 10% of the spline. By adjusting the Offset, I can move that 10% around along the duration of the spline.

You will want to use Constant Velocity here, most likely - this will give you even distribution regardless of how closely points are packed together. Otherwise the time (and space) will become compressed where points are close; you can, of course, take advantage of that, too.

You can make it work with length in a similar fashion, I’m just used to working with spline duration.

Hope it helps.

Oh you’re awesome! Thanks so much. I’ll see if I can get this working with my setup. Oh, that’s something I didn’t consider at all, that it would stretch between points. I definitely want to make sure it maintains the original shape of the mesh as close as possible while moving along the spline. Thanks!

Oh, this is so close to being perfect, thank you so much.

Here’s my latest code:

And Latest Results:

As you can see, it’s not quite updating the tangent properly as the back half gets bent oddly and then there’s the weird squish when it loops which isn’t a dealbreaker for my purposes, but it would be nice if it didn’t squish.

Also, if the time end is determining the length of the mesh, is it possible to actually make this the correct length of the mesh as opposed to a percentage value?

Oh, I just discovered a slight issue… your method is only causing the start and end points to follow the spline, but the midsection is no longer conforming to the spline, it’s just averaged between the two points. I discovered this when I adjusted the End Time to 0.2 instead of 0.1 for example. So if the mesh is really long, it won’t work the way I’d hoped.

This is what I’m ultimately hoping to achieve:

I actually never tested this properly with SMCs covering more than 2 points, true :expressionless: Sorry about that. Never occurred to me it might not work. I’ll try to have a look into this today.

edit: on the other hand, I can swear I had long sliding text on a spline that deformed perfectly, I’ll do some digging.

You’re awesome. Thanks so much!

so do you have the final BluePrint please?

So yeah, no. Can’t crack it, sorry.

There is no way in BP to apply tangents to a spline mesh component apart from its start and end. And this is where things break apart. I tried to approximate it by mapping a shorter spline onto the long one and using 2 SMCs but that was still way too wonky. Atm, I feel you can’t pull it off in BPs, I can’t for sure :expressionless:

The wonky bit:

https://i.gyazo.com/63f48beaef1c146c…5022ae9b2f.mp4

Dead end: every letter was a separate mesh so not really it.

Yeah, I thought that might be the case. It’s for a background traffic simulation and at the moment, I’ve got each vehicle moving along the spline separately but this is killing the frame rates. Dropping by 15-20fps due to the sheer volume of vehicles being calculated at once. I think I’ll just export the roads into Max and make an animation by hand there for an entire cluster of vehicles. Hopefully that will make things more performant.

Thanks so much for your help though Everynone :slight_smile:

Not sure if I follow you here. If it’s supposed to simulate traffic, why deform spline meshes in realtime anyway?

What’s sheer volume here? 10k? More?

Are you using Instanced Static Meshes for this?

I’m not using spline meshes for it, this was just a thought that I could join all the vehicles together into one mesh and then deform them along the spline so that way I only have one instance going for each road. I decided to test out if it’s possible before attempting to implement it.

So here’s my original setup:

BP_TrafficSpawner:

With a Function inside the Traffic Spawner for randomizing the vehicle mesh:

And this is the Vehicle that I spawn:
BP_Traffic_Vehicle:

So essentially for each spline, I set a number of vehicles to spawn - in my current level the average is 50 vehicles per spline and I only have 6 splines so 300 vehicles. To be honest, I have no idea how to use instanced meshes. I thought this happened automatically with UE4 but apparently not?

It’s likely due to the fact that I’m an artist mostly and still figuring out code, and really have no idea how to optimize with UE4 yet.

OH, it could be that my performance is tanking because of a glitch that I was experiencing where the CurrentNumberOfCars just wouldn’t stop counting even though I created a bool to test when the number of cars hit its max and then stop spawning after that - which appears to work, and yet the counter just keeps going.

I started a discussion about it but never got a suggestion as to what might have been going wrong…
https://forums.unrealengine.com/deve…crement-glitch

So I tried changing the static mesh to an instanced static mesh inside my TrafficVehicle blueprint and it made the frame rate worse for whatever reason. Without traffic, I average 65fps. With the non-instanced meshes it was around 42 and with instanced meshes it’s 39. Not sure how that works but…

Instanced Static Meshes are insanely fast. You can have 10s of thousands on screen at once, and it will run smooth in a blueprint-only project. There’s also Hierarchical Instanced Static Meshes that swap LOD groups automatically and can be culled efficiently, below:

400 above, 4000 below:

There is a catch - updating their transforms is expensive. You can move the actor that owns them easily enough but then they’ll all follow since it’s just a single component with a lot of instances. You can update them in batches, or update only the ones that are visible. It would take some scripting to pull it off, though.

Updating all 4000 to slide along their splines:

updating.jpg

You can strip them of collision, update them less frequently, apply more aggressive LODs, all help a little.

Here’s a quick example setup used above:

Wow, Awesome! Thanks so much for this. I’ll definitely have to figure out why my system with instanced meshes didn’t work. It’s probably because I’m not using the instance meshes correctly. I watched a quick tutorial on the subject but I haven’t had a chance to devote enough time to it. Granted though, with 4,000 meshes your frame rate more than halved so that’s a pretty significant hit. Though I guess with 300 it’d be a cakewalk.

You’ve been a huge help so thank you :slight_smile:

Hi! I know I’m a little late to this party, but I’ve been trying to use this method for swimming fish and I’m running into the same problem shown in the video here (the squish on loop, which seems like it’s just the back end catching up to the front end?). Judging by this thread it looks like you moved on to other methods, but I was just curious if you had any idea how to fix this. The fish are relatively short meshes that don’t need to perfectly deform along the spline, so this method is well-suited to my needs in almost every other way and it would be fantastic if I could get this working.