Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by lucaspiller · Aug 31, 2014 at 03:28 PM · procedural meshsplineruntime-generationroad

Generating road meshes from a spline

I'm building a game where you can edit a network of roads at runtime - basically a city builder. I watched Quill18's tutorial on creating editable roads which goes into the basics, but I'm stuck taking it further. I've used the Interpolate script on the wiki to create an editable Catmull-Rom spline, but I'm unsure of how to turn this into a mesh.

From the interpolate script I've got a list of points along the curve. This will end up being at the center of the road, so I can offset these by the road width to get the points at the edge. From this I just need to create the triangles for the mesh, which the Triangulator script appears to do (at least in 2D).

The problem is I'm not sure how to get the points at the edges. The Interpolate script just returns an array of Vector3s - there is no rotation/direction associated with them. I could use math to work out the points perpendicular to this line, however that still isn't quite what I'm after. I want a road to banked around a curve, so shouldn't the result from interpolating the spline return the rotation too?

(I'm interested in learning the math behind this, however if there are any plugins that do what I'm after, dynamically building a road network at runtime, I'd be interested in hearing about that too)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sirshelley · Feb 19, 2015 at 05:26 AM

yo i had this issue as well. First off have three splines: left ,middle and right of the track. Input verticies per length of spline update into your vertex array(s). On start set-up three triangles manually. After which a pattern occurs like so :

The last triangle data of your manual on start triangles will be : private int TriInd1 = 6; private int TriInd2 = 9; private int TriInd3 = 10;

Per ++Length: AddTriangle(TriInd1+=3, TriInd2+=3, TriInd3+=3); AddTriangle(TriInd1, TriInd3, TriInd1+1); AddTriangle(TriInd1+1, TriInd3, TriInd3+1); AddTriangle(TriInd1+1, TriInd3+1, TriInd1+2);

You can prove this with Paint. exe and many days of headaches. or take my word for it and plug it in :D GL!

P.s Proof: http://clip2net.com/s/3cJBVIc

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by robertbu · Aug 31, 2014 at 03:57 PM

I haven't got time for a highly detailed answer, but there is an outline. Walk the spline in steps. For each time through the loop you want a current position and the next position. Next you need to know what is consider 'u'p for your road. If it is perfectly flat, it will be Vector3.up, but if you are winding through a terrain, you will need to raycast. With this info, you can generate your two points:

 var dir = (Vector3.Cross(up, nextPos - currPos)).normalized;
 var point1 = currPos + dir * halfRoadWidth;
 var point2 = currPos - dir * halfRoadWidth;

Note if you are winding through a terrain, you may need to adjust the points 'y' height to match the terrain.

Note for the last point (which won't have a next point) on your spline, you can use the 'dir' from the previous point, or you can reverse the process and use:

  var dir = (Vector3.Cross(up, currPos - prevPos)).normalized;
 

 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Interact with procedural mesh hosted on a GameObject created at runtime 0 Answers

How do i do this type of spline?? 1 Answer

How do I change terrain generation settings smoothly? 0 Answers

Calculate Rotation based on mesh normals 1 Answer

Help needed: repairing the Spline Controller Script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges