- Home /
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));
}
}
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.
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
I tried what you told me but I did not get it, my friend ... @Remy_Unity