- Home /
How to generate a curve from a model ?
I have a road model which is a long looped curve made in Blender then exported to FBX.
To make this model, I have created a path and then repeated a portion of road all over it.
Now for my game, I need to get this path back so I can create two lanes for my IAs and player to follow.
I would like to know if it would be possible to extract this path from the model topology via script and if so how ?
That would be ideal so that when I make changes to my model, the paths can be recreated again automatically.
If not possible I'd like to know how I could create those paths otherwise visually in the editor ?
This is the model I'm talking about :
Answer by stepan-stulov · Aug 11, 2015 at 07:56 AM
In order for it to be possible the mesh would have to follow some very strict conventions about order of vertices, how they're paired, etc. So that a pattern could be recognised and you could somehow iterate over mesh sections and take sections' mid-points as you spline's key points. Something tells me this mesh is not one of such. And analysing ambiguously modelled mesh — you don't want to be there with your game, unless you're working on a next PhD about topologies etc.
What I advise instead is to flip you workflow. Don't base an abstract mathematical model on a fuzzy human-made mesh. Create the spline first and generate the road mesh (perhaps by "skinning" the spline with mesh segments) from it then. This will solve the problem of analytical representation of your road as well as will dramatically improve iterations on adjusting curvatures etc. In fact such approach is very "traditional".
Not the worst scenario (looking at the complexity of your track) would also be to create the spline by hand with the road being under it as a visual guide.
Hope this helps.
So what you're saying is I should create the spline in Unity or have a mathemathical representation of it anyway and then import this into blender to generate the mesh ?
(1) Auto-generating the track: I'm not sure if blender can do that, I actually have next to no idea about it. I implied you would create the curve in Unity (or maybe import it from any convenient file format) and also generate the final visual object in Unity (Editor script?) based on the curve and by decorating it with segment meshes from Blender. The problem is there might be much tweaking involved into making the auto-generated result look good, not have seams etc.
(2) Hand-supplying the spline: This approach should produce the best visuals, because you can make a nicely looking track mesh and also highly optimised spline on top of it. The problem of this approach is a much longer iteration of updating the track. First you need to update the mesh, then you need to go back and sync the spline with it. The question is how many tracks you have. If it's 5 — go for it. If it's 1000 — probably auto-generate.
What I would not recommend is to reverse-engineer the mesh, unless you really have to.
Update: what you can do as an option (3) is to reverse-engineer the mesh but then correct all the errors by hand. Could actually be a viable option.
Answer by Bunny83 · Aug 11, 2015 at 08:44 AM
Since you probably have unwrapped your mesh it should be relatively simple to follow an edge loop along your road by looking at the UV coordinates to determine which neighbor vertex is the next one. So depending on the orientation of your UV map you have to look for a connected edge which second point has a greater / smaller UV value along the road direction.
For this approach all you have to do to start is to pick a start-vertex and move along the edges. That start point could be determined by doing a raycast against a meshcollider. However it also depends on your actual topography of your mesh and where you have edges.
This seems a good technique but I'm afraid I can't do that since the UV is unwrapped only on a portion of road. So if I'm not mistaken, each portion will have the same UVs along the curve right ? But maybe I could try to collect all the UVs in the center and get all vertices that match those coordinates.