- Home /
Create a plane from points
Hi Guys,
Please, I need help: I have n points (x, y, z), where n> 2
From these points, i need to build a plane. Any idea where I can start? This is possible? Thank you
You'll want to look into creating mesh from scratch, there should be enough tutorials and examples on the web to get you started on this. Like, http://answers.unity3d.com/questions/8023/creating-a-mesh-procedurally.html , for example
I should add that given n>3 points, it's not always possible to build a plane - They would have to be guaranteed to lie in the same spacial plane in order to build a geometric plane out of them.
Answer by robertbu · Oct 01, 2013 at 04:58 PM
I'm assuming you are talking about building a mesh in the shape defined by the vertices. If your polygon is axes aligned, and if it is convex, then it is fairly easy to do. If it is not axes aligned, then I'd likely rotate it to match an the axes, build the mesh, then rotate the vertices again. If it is not convex, then it becomes a much harder problem and you'll likely have to dig around in some CompSci posts for the algorithm.
I would approach the problem by finding a center point and then defining the triangles in terms of this center point:
Here is a bit of source that will build polygons. Attach the script to an empty game object, define your polygon shape in the 'poly' array in the Inspector, and attach a material to display to the 'mat' parameter. The polygon is built on the XY plane and any 'z' components in the 'poly' Vector3s are ignored.
using UnityEngine;
using System.Collections;
public class PlaneFromPoly : MonoBehaviour {
public Material mat;
public Vector3[] poly; // Initialized in the inspector
void Start () {
if (poly == null || poly.Length < 3) {
Debug.Log ("Define 2D polygon in 'poly' in the the Inspector");
return;
}
MeshFilter mf = gameObject.AddComponent<MeshFilter>();
Mesh mesh = new Mesh();
mf.mesh = mesh;
Renderer rend = gameObject.AddComponent<MeshRenderer>();
rend.material = mat;
Vector3 center = FindCenter ();
Vector3[] vertices = new Vector3[poly.Length+1];
vertices[0] = Vector3.zero;
for (int i = 0; i < poly.Length; i++) {
poly[i].z = 0.0f;
vertices[i+1] = poly[i] - center;
}
mesh.vertices = vertices;
int[] triangles = new int[poly.Length*3];
for (int i = 0; i < poly.Length-1; i++) {
triangles[i*3] = i+2;
triangles[i*3+1] = 0;
triangles[i*3+2] = i + 1;
}
triangles[(poly.Length-1)*3] = 1;
triangles[(poly.Length-1)*3+1] = 0;
triangles[(poly.Length-1)*3+2] = poly.Length;
mesh.triangles = triangles = triangles;
mesh.uv = BuildUVs(vertices);
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
Vector3 FindCenter() {
Vector3 center = Vector3.zero;
foreach (Vector3 v3 in poly) {
center += v3;
}
return center / poly.Length;
}
Vector2[] BuildUVs(Vector3[] vertices) {
float xMin = Mathf.Infinity;
float yMin = Mathf.Infinity;
float xMax = -Mathf.Infinity;
float yMax = -Mathf.Infinity;
foreach (Vector3 v3 in vertices) {
if (v3.x < xMin)
xMin = v3.x;
if (v3.y < yMin)
yMin = v3.y;
if (v3.x > xMax)
xMax = v3.x;
if (v3.y > yMax)
yMax = v3.y;
}
float xRange = xMax - xMin;
float yRange = yMax - yMin;
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < vertices.Length; i++) {
uvs[i].x = (vertices[i].x - xMin) / xRange;
uvs[i].y = (vertices[i].y - yMin) / yRange;
}
return uvs;
}
}
The uvs are defined in terms of the minimum and maximum points and result in the shape cookie cutting out the texture attached to a material (assuming your shader has textures and deals with them in a standard way). Try a material using Unlit/Texture to see what I mean.
I'm not sure that the algorithm that I am using will find a center point within the bounds of the polygon for all convex polygons. If this is a problem, you can have the center specified as a parameter (rather than calculated), or you can elect to implement some other center finding method:
Wow! robertbu, you are amazing! really, really thank you!
Your script is basically what I need, because all my polygons are convex and axes are aligned.
I studied your script, and now i know more about mesh and polygons in Unity 3D.
Thank you again!
As a side note, you are likely better off special casing triangles and building a single triangle rather than using this algorithm.
your script is good, but do you know rewrite him? i mean, if i will change one of all x,y,z axis, then this point change position in playmode? because this script work only one ("Start") so i need Update, or something like that. Thank you
your script is good, but do you know rewrite him? i mean, if i will change one of all x,y,z axis, then this point change position in playmode? because this script work only one ("Start") so i need Update, or something like that. Thank you
@Raynoko - I don't understand what you are asking. What does, "if i will change one of all x,y,z axis, then this point change position in playmode" means? Note that after the mesh is built, you can move the vertices in the $$anonymous$$esh.vertices array at runtime and the mesh will change.
Your answer
Follow this Question
Related Questions
Sprite creation problem 1 Answer
create game object on the fly 2 Answers
Create plane from 2 Vectors of a symmetry line segment 1 Answer
Create a plane on Voronoi 1 Answer
Create scenes at runtime 2 Answers