Hi there,
I wanna create a Mesh at runtime from x,y,z coordinates which I read from a file. I read all the float point into an Vector Array and then I maked a VERTICE Array like
"vertices = new Vector3(x, y, z);"
The next work is to make the triangles like this:
- var tri: int[ ] = new int[18];*
- tri[0] = 0;*
- tri[1] = 2;*
- tri[2] = 1;*
- tri[3] = 2;*
- tri[4] = 3;*
- tri[5] = 1;*
- tri[6] = 1;*
- tri[7] = 3;*
- tri[8] = 4;*
- tri[9] = 3;*
- tri[10] = 5;*
- tri[11] = 4;*
And that is the position I’m a little bit confused how to make this Array in a FOR i …
I attached a file where I draw what I mean. This concept function but to get this in a FOR …NEXT
I should have continued numbers, but only the beginning is for me confused.
Have someone some infos or a tut which better explain how to make a mesh from coordinates x,y,z
I usually do it this way
int numberOfQuads = ...;
var indices = new List<int>();
for (int i = 0; i < numberOfQuads; i++)
{
indices.Add(i * 2 + 0);
indices.Add(i * 2 + 1);
indices.Add(i * 2 + 2);
indices.Add(i * 2 + 2);
indices.Add(i * 2 + 1);
indices.Add(i * 2 + 3);
}
or
int numberOfQuads = ...;
var indices = new int[numberOfQuads * 6];
for (int i = 0; i < numberOfQuads; i++)
{
indices[i * 6 + 0] = i * 2 + 0;
indices[i * 6 + 1] = i * 2 + 1;
indices[i * 6 + 2] = i * 2 + 2;
indices[i * 6 + 3] = i * 2 + 2;
indices[i * 6 + 4] = i * 2 + 1;
indices[i * 6 + 5] = i * 2 + 3;
}
Thanks,
better structered code, but one problem I have, cause the first 6 triangles must I declare manuel out of the FOR…
cause If I take yours my meshes lays down in the x-z axis.
So if I take this manually like above, my mesh lay in the x-y or z-y axis…see screenshot.
So I wondering how to find out how I start with the Triangles.
I have all the Points from your picture i xyz-coordinates,
1 = (0,1,0)
3 = (3,2,2)
5 = (5,1,3)
and so on…but I don’t know the 2,4,6,8…
I wanna make the mesh in x-y / z-y
The vertices I set so:
// Set first 4 Vectors manually, I don’t know why…
vertices[0] = new Vector3(GPS_daten[0].x * 1,0,GPS_daten[0].z * 1);
vertices[1] = new Vector3(GPS_daten[1].x * 1,0,GPS_daten[1].z * 1);
vertices[2] = GPS_daten[0];
vertices[3] = GPS_daten[1];
for (var j : int = 4; j < MaxVertices+1; j++) {
vertices[j] = new Vector3(GPS_daten[j/2].x * 1,0,GPS_daten[j/2].z * 1);
j++;
vertices[j] = GPS_daten[(j-1)/2];
}
mesh.vertices = vertices;
That gives my screenshot I attached. Do you have some more informations on create meshes at runtime, a tutorial or something else an example?
I think the first definition of the vertices and then the triangle gives the orientation of the mesh, I think…
You can easily skip first 3 quads if you want:
int numberOfQuads = ...;
var indices = new List<int>();
// add first 3 quads manually
for (int i = 3; i < numberOfQuads; i++)
{
indices.Add(i * 2 + 0);
indices.Add(i * 2 + 1);
indices.Add(i * 2 + 2);
indices.Add(i * 2 + 2);
indices.Add(i * 2 + 1);
indices.Add(i * 2 + 3);
}
int numberOfQuads = ...;
var indices = new int[numberOfQuads * 6];
// add first 3 quads manually
for (int i = 3; i < numberOfQuads; i++)
{
indices[i * 6 + 0] = i * 2 + 0;
indices[i * 6 + 1] = i * 2 + 1;
indices[i * 6 + 2] = i * 2 + 2;
indices[i * 6 + 3] = i * 2 + 2;
indices[i * 6 + 4] = i * 2 + 1;
indices[i * 6 + 5] = i * 2 + 3;
}
Hi alexzzzz,
Many thanks, i also find your way to create the mesh logical and I think its the better way.
but in my scene it isn’t functioned, so that I might set the first two triangles to
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
tri[6] = 1;
tri[7] = 3;
tri[8] = 4;
tri[9] = 3;
tri[10] = 5;
tri[11] = 4;
not 0,1,2 and so on…cause when I take 0,1,2…then all my meshes lays down in the x-z axis.
So now it function with my method above but Ican’t figure out why. I think I should look at this detail in a few days. My brain is full of code and other thinks so maybe i can’t see the other better solution.
Are the vertices #1 and #2 on the picture attached to the first post are swapped on purpose or by accident?
Hi alex,
no not really an accident. Its on purpose I changed the triangles cause if i don’t do that I get meshes that layed dawn in the x-z axis.
if I don’t do this like i decriped I get this: see screenshot.
And that is I wonder why. To understand create meshes at runtime I can’t find any tut which explain me how to make correct way.
I make an empty gameobject in the scene and attached my Java script
Principle if I was UNITY I should ask one question: In which direction should I begin to draw the meshes of my user at the emptyGameObject.