-
November 9th, 2013, 12:47 PM
#1
Creating PLane Mesh via code
Hello,
Im trying to learn how to create a plane mesh via code.
so far i got this.
"Im trying to learn how to create plane meshes via code. I was able to draw my vertices and and half my triangle.
"using UnityEngine;
using System.Collections;
public class DrawPlaneMesh : MonoBehaviour
{
private Mesh myMesh;
private Vector3[] newVertices;
private int[] newTriangles;
public int xSize;
public int zSize;
public float seperationX;
public float seperationZ;
private void Start()
{
newVertices = new Vector3[xSize * zSize];
newTriangles = new int[(xSize - 1) * (zSize - 1) * 6];
myMesh = new Mesh();
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
int counter = 0;
for(int i = 0; i < xSize; i++){
for(int j = 0; j < zSize; j++){
GenerateSphere(i * seperationX,0,j * seperationZ);
int count = j+zSize * i;
newVertices[ConvertToArrayIndex(i,j,xSize)] = new Vector3(i * seperationX,0,j * seperationZ);
Debug.Log(ConvertToArrayIndex(i,j,xSize ));
Debug.Log(count);
if((i < xSize-1) && j < (zSize-1)) {
newTriangles[counter] = ConvertToArrayIndex(i,j,xSize);
counter++;
newTriangles[counter] = ConvertToArrayIndex(i + 1,j,xSize);
counter++;
newTriangles[counter] = ConvertToArrayIndex(i,j + 1,xSize);
counter++;
newTriangles[counter] = ConvertToArrayIndex(i,j,xSize);
counter++;
newTriangles[counter] = ConvertToArrayIndex(i + 1,j,xSize);
counter++;
newTriangles[counter] = ConvertToArrayIndex(i,j + 1,xSize);
counter++;
}
}
}
myMesh.vertices = newVertices;
myMesh.triangles = newTriangles;
GetComponent<MeshFilter>().mesh = myMesh;
}
void GenerateSphere(float x,float y,float z){
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.localPosition = new Vector3(x,y,z);
sphere.transform.localScale = new Vector3(0.1f,0.1f,0.1f) ;
}
int ConvertToArrayIndex(int i, int j, int size){
return j+size * i;
}
}"
this makes the vertices and triangles. but the triangles are only half.
Meaning that
Triangles go
Lets say if im gonna make a smal plane with 0,1,2,3
The triangle is only showing from 0,1,2.
How can i make it so it also shows the 1,2,3 triangle?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|