Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by $$anonymous$$ · Apr 09, 2018 at 06:25 PM · script.planecreate

Create a plane on Voronoi

I am trying to create a plane to simulate a road or a street with the Voronoi library that is in Github. So, I want to create a plan on the red lines generated by the Voronoi code, how can I do it? I do not have much idea of Unity.

The Voronoi code:

 using UnityEngine;
 using System.Collections.Generic;
 using Delaunay;
 using Delaunay.Geo;
 
 public class VoronoiDemo : MonoBehaviour
 {
     private int
         m_pointCount = 10;
 
     private List<Vector2> m_points;
     private float m_mapWidth = 50;
     private float m_mapHeight = 50;
     private List<LineSegment> m_edges = null;
     public Vector3[] poly;  // Initialized in the inspector
         
 
     void Awake ()
     {
         Demo ();
 
      }
 
     private void Demo ()
     {        
         List<uint> colors = new List<uint> ();
         m_points = new List<Vector2> ();
             
         for (int i = 0; i < m_pointCount; i++) {
             colors.Add (0);
             m_points.Add (new Vector2 (
                     UnityEngine.Random.Range (0, m_mapWidth),
                     UnityEngine.Random.Range (0, m_mapHeight))
             );
         }
         Delaunay.Voronoi v = new Delaunay.Voronoi (m_points, colors, new Rect (0, 0, m_mapWidth, m_mapHeight));
         m_edges = v.VoronoiDiagram ();
     }
 
     void OnDrawGizmos ()
     {
         Gizmos.color = Color.red;
         if (m_points != null) {
             for (int i = 0; i < m_points.Count; i++) {
                 Gizmos.DrawSphere (m_points [i], 0.2f);
             }
         }
 
         if (m_edges != null) {
             // I think that i must generate de plane in this part.
             Gizmos.color = Color.red;
             for (int i = 0; i< m_edges.Count; i++) {
                 Vector2 left = (Vector2)m_edges [i].p0;
                 Vector2 right = (Vector2)m_edges [i].p1;
                 Gizmos.DrawLine ((Vector3)left, (Vector3)right);
             }
         }
         Gizmos.color = Color.yellow;
         Gizmos.DrawLine (new Vector2 (0, 0), new Vector2 (0, m_mapHeight));
         Gizmos.DrawLine (new Vector2 (0, 0), new Vector2 (m_mapWidth, 0));
         Gizmos.DrawLine (new Vector2 (m_mapWidth, 0), new Vector2 (m_mapWidth, m_mapHeight));
         Gizmos.DrawLine (new Vector2 (0, m_mapHeight), new Vector2 (m_mapWidth, m_mapHeight));
     }
 }


alt text

Comment
Add comment · Show 1
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 $$anonymous$$ · Apr 09, 2018 at 06:46 PM 0
Share

Does anyone know how it is done?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Remy_Unity · Apr 10, 2018 at 08:08 AM

What you want to do is to create "streets" based on the red edges of your generated result, right ?

Logic wise, the easiest solution is to place a plane at the center of each edge, rotate it so that say the X axis is pointing toward one of the edge end, and finally scale it to match the edge length.

Other solution, but a bit more difficult, is to generate you road topology using a procedural mesh : https://docs.unity3d.com/Manual/GeneratingMeshGeometryProcedurally.html

And finally, no, you don't wan to do this where you've pu the comment, OnDrawGizmos is called at each frame in the editor, and you definitively don't want to generate your roads so often.

Best would be to do it at the end of your Demo function.

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Apr 10, 2018 at 08:33 AM 0
Share

And where and how i can do this men?

avatar image Remy_Unity $$anonymous$$ · Apr 10, 2018 at 09:42 AM 0
Share

Looking at the code you posted :

This code :

 for (int i = 0; i< m_edges.Count; i++) {
                  Vector2 left = (Vector2)m_edges [i].p0;
                  Vector2 right = (Vector2)m_edges [i].p1;
                  Gizmos.DrawLine ((Vector3)left, (Vector3)right);
              }



It iterates on each edge you want and draws a red line. You can see that you can access each edge extremity with (Vector2)m_edges [i].p0 and (Vector2)m_edges [i].p1

You can use them to find the center of the edge : center = ( p0 + p1 ) / 2;

With this, you create plane : https://docs.unity3d.com/ScriptReference/GameObject.CreatePrimitive.html

Then place it at the edge center : https://docs.unity3d.com/ScriptReference/Transform-position.html

$$anonymous$$ake it "look at" one of the edge extremity : https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

And finally scale it on Z (because it's the Z axis that will be looking at the point) to match the edge size : https://docs.unity3d.com/ScriptReference/Transform-localScale.html

Note that the default plane is of size 10x10, so you will need to divide your scale by 10. So you will have :

x = width / 10

y = 1 (plane is flat anyway)

z = Vector3.Distance( p0, p1) / 10

avatar image $$anonymous$$ Remy_Unity · Apr 10, 2018 at 10:24 AM 0
Share

I tried what you told me but I did not get it, my friend ... @Remy_Unity

Show more comments

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

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find closest point in triangle to point? 1 Answer

Create a plane from points 1 Answer

create plane .cs from wiki not working on Unity4 ? 1 Answer

Create plane from 2 Vectors of a symmetry line segment 1 Answer

How do I stop my player from going through the platform? 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