- Home /
Build plane on quad of mesh with same vertices
Hi! I want to build a plane by code on a quad of a existing mesh. So to simply generate a plane i use this code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlaneGenerator : MonoBehaviour
{
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
private Mesh mesh;
void Start ()
{
mesh = GetComponent<MeshFilter> ().mesh;
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
newVertices.Add( new Vector3 (x , y , z ));
newVertices.Add( new Vector3 (x + 1 , y , z ));
newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
newVertices.Add( new Vector3 (x , y-1 , z ));
newTriangles.Add(0);
newTriangles.Add(1);
newTriangles.Add(3);
newTriangles.Add(1);
newTriangles.Add(2);
newTriangles.Add(3);
newUV.Add(new Vector2 (0, 1));
newUV.Add(new Vector2 (1, 1));
newUV.Add(new Vector2 (1, 0));
newUV.Add(new Vector2 (0, 0));
}
void Update ()
{
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
}
}
The code obviously puts the vertices in the positions (0, 0, 0), (1, 0, 0), (1, -1, 0), (0, -1, 0). What I want is to generate the plane on a quad on a existing mesh. So the planes vertices will be at the position of the vertices of a random quad in the mesh. Therefore I think I need to get the vertices positions of two triangles (that build a quad thogether) of the mesh and put it's positions for the positions of the vertices of the plane. How could I code this?
Thank you so much!
So the planes vertices will be at the position of the vertices of a random quad
How are you finding this quad? Are all four vertices of this quad on the same plane?
No, they are not. I think finding the three vertices of a triangle is possible, but I don't know how to find the forth vertex for the quad. It certainly isn't the next one in the mesh.vertices-array. Would there be a way?
A triangle has three sides. You indicate the triangle is arbitrary. Any other triangle that shares one of these sides (two vertices the same or at least to Vector3 positions the same) is a potential triangle that forms quad with the original. So in order to solve this problem, you need to take advantage of some unique property of your mesh. If you can provide a description of how you are identifying the original triangle and any properties of the mesh, I might be able to narrow your selection process beyond sharing a side.
Answer by wolga2 · May 08, 2014 at 01:15 PM
I thought I could find every triangle and then it's vertices. I don't know, if that's even possible. I tried something like that, it was meant to build a triangle for each triangle of the mesh. It actually builds a triangle on the mesh, but only one:
using UnityEngine;
using System.Collections;
public class ExampleSpawn : MonoBehaviour
{
public GameObject spwanGO;
private Mesh spawnMesh;
public int[] spawnTris;
public Vector3[] spawnVecs;
void Start()
{
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
spawnMesh = spwanGO.GetComponent<MeshFilter>().mesh;
spawnTris = spawnMesh.triangles;
spawnVecs = spawnMesh.vertices;
for (int i = 0; i < spawnTris.Length; i += 3)
{
Vector3 p1 = spawnVecs[spawnTris[i + 0]];
Vector3 p2 = spawnVecs[spawnTris[i + 1]];
Vector3 p3 = spawnVecs[spawnTris[i + 2]];
mesh.vertices = new Vector3[] {transform.TransformPoint(p1), transform.TransformPoint(p2), transform.TransformPoint(p3)};
mesh.triangles = new int[] {i + 0, i + 1, i + 2};
}
}
}
Your answer
Follow this Question
Related Questions
Unidirectional slicing algorithm of a mesh 0 Answers
Plane: vertices and triangles 2 Answers
Generate terrain tutorial by brackeys not working 0 Answers