Hey guys,
I tried to create a flat mesh on my own, just like the terrain, but so i can change its vertices height in each Update()-Call (to create something wave-like).
I’m pretty sure I created the rectangles right (there are no errors anymore) and even the pivot in Scene-View moves when I hit start.
But the mesh turns out to be invisible, even in “Wireframe”-Mode, from all directions (so i guess its not the normals). Also i got a material ‘Diffuse’ with a Texture on it.
I have the feeling I’m missing something simple, any suggestions?
Here’s the code, still in Start(), because I don’t need to transform it yet. I just want it visible. Possibly the uv’s?
public class Waves : MonoBehaviour {
private int width = 4;
private int maxRows = 1; //Minimum 1
private float distance = 2f;
public bool showInformation = true;
public bool showDetailed = true;
void Start() {
if(maxRows < 1)
maxRows = 1;
//Building Vertex-Grid
List<Vector3> vertices = new List<Vector3>();
for (int row = 0; row <= maxRows; row++){
if(row%2 == 0){
for (int i = 1; i <= width ; i++ ){
vertices.Add(new Vector3(i*distance, 0f, row* distance));
}
} else {
for (int i = 1; i <= width ; i++ ){
vertices.Add(new Vector3(i*distance + distance*0.5f, 0f, row* distance));
}
}
}
//Convert List<Vector3> to Vector3[]
Vector3 [] allVertices = new Vector3[vertices.Count];
for (int i = 0; i < vertices.Count; i++) {
allVertices _= vertices*;*_
* if(showDetailed)*
_ Debug.Log(allVertices*);
}*_
* if(showInformation)*
* Debug.Log (“There are " + allVertices.Length + " vertices known”);*
* //Building Triangles*
* List triangles = new List();*
* for (int row = 0; row < maxRows; row++){*
* //Green Part*
* for (int i = 1; i < width ; i++ ){*
* int a = computeGreenA(row,width,i)-1;*
* int b = computeGreenB(row,width,a)-1;*
* int c = computeGreenC(row,width,a)-1;*
* triangles.Add (a);*
* triangles.Add (b);*
* triangles.Add (c);*
* }*
* //Orange Part*
* for (int i = 1; i < width ; i++ ){*
* int a = computeOrangeA(row,width,i)-1;*
* int b = computeOrangeB(row,width,a)-1;*
* int c = computeOrangeC(row,width,a)-1;*
* triangles.Add (a);*
* triangles.Add (b);*
* triangles.Add (c);*
* }*
* //The “-1” exists, because the Array goes from, say, 0-7, while the saved Vertices are numbered 1-8!*
* if(showInformation) *
* Debug.Log (“We are at row " + row + " and currently got " + triangles.Count / 3 + " Triangles”);*
* }*
* //Convert List to int[]*
* int[] allTriangles = new int[triangles.Count];*
* for (int i = 0; i < triangles.Count; i++) {*
allTriangles = triangles*;*
* if(showDetailed)*
_ Debug.Log(allTriangles*);
}
if(showInformation)
Debug.Log(“allTriangles holds " + allTriangles.Length + " entries”);*_
* //Create UV*
* Vector2[] uv = new Vector2[allVertices.Length];*
* for(int i = 0; i < uv.Length; i++) {*
uv = new Vector2 (allVertices_.x, allVertices*.z);
if(showDetailed)
Debug.Log(uv);
}*_
* //Create Mesh*
* Mesh mesh = new Mesh();*
* //mesh.MarkDynamic();*
* mesh.vertices = allVertices;*
* mesh.triangles = allTriangles;*
* mesh.uv = uv;*
* mesh.RecalculateNormals();*
* mesh.RecalculateBounds();*
* GetComponent().mesh = mesh; *
* }*
* //Green Computations*
* int computeGreenA(int row,int width,int i){*
_ return rowwidth+i;
}*_
* int computeGreenB(int row, int width, int a){*
* return a+1;*
* }*
* int computeGreenC(int row, int width, int a){*
* if(row%2 == 0){*
* return a+width; *
* } else {*
* return a+width+1; *
* }*
* }*
* //Orange Computations*
* int computeOrangeA(int row,int width,int i){*
_ return (row+1)width+i;
}*_
* int computeOrangeB(int row, int width, int a){*
* if(row%2 == 0){*
* return a-width+1; *
* } else {*
* return a-width; *
* }*
* }*
* int computeOrangeC(int row, int width, int a){*
* return a+1;*
* }*
}
I also uploaded the concept for creation of the triangles:
Triangles in a field of 7 vertices wide and 4 deep:
*http://imageshack.us/a/img40/267/wk61.jpg*_
Triangles in a field of 4 vertices wide and 5 deep:
*http://imageshack.us/a/img62/6407/zvwv.jpg*_
The formulas i got from my ‘research’:
_*http://imageshack.us/a/img96/1368/82sd.jpg*_