- Home /
mesh into smaller triangles
Hi;
I'm having a tough time with my problem at the moment and I'm hoping anyone on here can help. I have a mesh that is made up of two triangles (see below):
I want to be able to split these triangles down into smaller triangles so that I can begin to play around with them. I'm working with a c# script, any help would be greatly appreciated. Thank you
(code below)
using UnityEngine;
using System.Collections;
public class CreatePlaneMesh : MonoBehaviour {
public float width = 50f; //width of terrain
public float height = 50f; //height of terrain
public Material mat; // material variable
// Use this for initialization
void Start () {
gameObject.AddComponent<MeshFilter>(); //add mesh filter to the gameobject (empty in this case)
MeshFilter mf = GetComponent<MeshFilter>(); //assign meshfilter to the 'mf' meshfilter variable
Mesh mesh = new Mesh(); //create a new mesh object
mf.mesh = mesh; //assign mesh to the 'mf' meshfilter variable
//vertices
Vector3[] vertices = new Vector3[4]; //create 4 slots to store vectors to create plane - local space
/** MESH
//
// v[2] v[3]
// |\ |
// | \ |
// | \ |
// v[0] v[1]
**/
vertices[0] = new Vector3 (width, 0, 0); //bottom left
vertices[1] = new Vector3 (width, 0, height); //bottom right
vertices[2] = new Vector3 (0, 0, height); //top left
vertices[3] = new Vector3 (0, 0, 0); //top right
mesh.vertices = vertices;
//get all vertex positions of the mesh object
mesh = GetComponent<MeshFilter>().mesh;
Vector3[] v = mesh.vertices;
int i = 0; while (i < v.Length){ //while loop to go through vertex positions and store then in the 'v' vector3 array
v[i]+= Vector3.up * Time.deltaTime; //same as v(0,1,0) - go along edges of mesh and mine position co-ordinates;
i++;
}
//convert local positions to in-game(world) positions
Vector3[] worldPOS = new Vector3[vertices.Length]; for(int j=0; j < vertices.Length; j++) {worldPOS[j] = transform.TransformPoint(vertices[j]);}
mesh.vertices = vertices; //assign vertices to mesh variable
mesh.RecalculateBounds(); //verify all is okay to auto-calculate bounds of the mesh from the vertices
//
int[] indices = new int[6]; //create boxes to store indices values
/** TRIANGLES TO FORM MESH
//
// 0----1 * NOTE. (T1) indices[0]+indices[1]+indices[2] -> triangle on bottom left
// |\ T2| (T2) indices[3]+indices[4]+indices[5] -> triangle on top right
// | \ |
// |T1\ |
// 3---\2
**/
indices[0] = 0; indices[1] = 3; indices[2] = 2; //one triangle
indices[3] = 0; indices[4] = 2; indices[5] = 1; //other triangle
mesh.SetIndices(indices, MeshTopology.Triangles, 0); //squish triangles together to form a plane like mesh
gameObject.AddComponent<MeshRenderer>(); //add meshrenderer object to the empty game object
MeshRenderer mr = GetComponent<MeshRenderer>(); //assign meshrenderer object to the 'mr' variable
mr.material.color = Color.green; //set the colour of the 'mr' meshrenderer
mesh.RecalculateNormals(); //verify all is okay to output to the screen
}
}
capture.png
(21.8 kB)
Comment
Not a direct answer, but you might take a look at the code in the Unity Wiki CreatePlane script.
http://wiki.unity3d.com/index.php?title=CreatePlane
It generates a mesh with whatever resolution you want. Basically it takes what you've already done and duplicates it across a 2D array of positions.