Filling gaps between two parts

Hello,

I’m currently working on a procedurally generated road system. The road is comprised of various individual segments with some rotation to make up a windy-looking road.

I’m struggling with filling in the gaps between two rotated road pieces. It’s easy enough in studio with a plugin like GapFill, but I want them to be filled at runtime and it seems like forking GapFill for this is way too complicated.

If I were to make the road pieces rotate from the opposite side point, I wouldn’t have to deal with filling in the gaps, but instead have to deal with the resulting texture clashing and Z-fighting.

Here’s what GapFill does, a beautiful two wedges positioned in a way that connects the two segments:

I’m hoping for someone to help me replicate the above result through script. I tried myself but I really suck at math so I’m at a loss here. The furthest I got from examining GapFill results was this:

The rotation of a given wedge is of course roughly half of the rotation of the road segment before it. If a road segment is rotated 30 degrees, the wedges in the middle will be rotated 15 degrees. This formula is easy enough to understand and apply.

The width of the wedge just almost matches the width of the road segments. In one example, a road segment is 50 studs wide and rotated 30 degrees. The resulting wedge is 48.295 studs wide.

I’m not sure how to get the length of the wedge (how far it goes out to meet the other wedge in the middle.)

Thanks in advance.