- Home /
Procedurally generated mesh is invisible.
First of all, it has a MeshRenderer, so that´s not the problem. The vertices of the mesh go from left to right per row, from below to above in a grid. The order of triangles was clockwise, so I simpy don´t know what´s wrong. Here´s my code:
Mesh mesh;
Vector3[] orderedVertices;
int[] orderedTriangles;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
List<float> rows = new List<float>();
List<float> columns = new List<float>();
//Here am skipping code that gets the values of the rows (z coordinates of vertices) and columns (x coordinates). Am using Contains() to avoid repetition. This is is fine: I already checked.
rows.Sort();
columns.Sort();
orderedVertices = new Vector3 [rows.Count * columns.Count];
for (int i = 0, r = 0; r < rows.Count; r++)
{
for (int c = 0; c < columns.Count; c++, i++)
{
orderedVertices[i] = Vector3.zero;
orderedVertices[i].z = rows[r];
orderedVertices[i].x = columns[c];
orderedVertices[i].y = bottomFormer.y; //the order is fine as well.
}
}
int xSize = rows.Count - 1; //number of squares per row
int zSize = columns.Count - 1;
orderedTriangles = new int[(xSize * zSize) * 6];
//I originally had code here to set the triangles, but it didn´t worked and I moved on to something more simple... that still doesn´t work:
orderedTriangles[0] = 0;
orderedTriangles[1] = columns.Count; //clockwise order
orderedTriangles[2] = 1;
orderedTriangles[3] = 1;
orderedTriangles[4] = columns.Count;
orderedTriangles[5] = 1 + columns.Count;
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = orderedVertices;
mesh.triangles = orderedTriangles;
for (int i = 0; i < mesh.triangles.Length; i++)
{
Vector3 vert = mesh.vertices[mesh.triangles[i]];
print("error vertex[triangles]: " + vert.x + ", " + vert.y + ", " + vert.z); //this seems to be fine. Is the supposed order and all of the vertices are in their respective places
}
mesh.RecalculateNormals();
//mesh.RecalculateBounds(); I didn´t knew if it was this, but nothing.
}
At least that I know, am doing everything right, but the mesh is still invisible.
Thanks for your help and time.
Answer by Bunny83 · Apr 26, 2021 at 06:23 PM
Well, it probably is just this line:
GetComponent<MeshFilter>().mesh = mesh;
try this one instead:
GetComponent<MeshFilter>().sharedMesh = mesh;
The "mesh" property will ensure that this MeshFilter has it's own instance of the mesh. So since you modify the mesh after you assigned it to your MeshFilter, the meshfilter will be stuck with a copied empty mesh.
Note that the vertex array creation could be simplified and optimised like that: Vector3 v = new Vector3(0,bottomFormer.y,0); for (int i = 0, r = 0; r < rows.Count; r++) { v.z = rows[r]; for (int c = 0; c < columns.Count; c++, i++) { v.x = columns[c];
orderedVertices[i] = v; } }
Is this supposed to create some sort of table? Though it seems a bit strange that you sort the rows and columns like that.
Thanks for your reply. sharedMesh doesn´t solve the problem. Am creating a plane that is more like a grid. I have a group of baseObjects on this plane. The vertices at the bottom most part are supposed to create vertices on the plane, then build the rows and columns with that. I don´t understand exactly what is wrong with my code (from what you said), but if it´s weird the way I set vertices, that´s because am trying to avoid the keyword "new" from new Vector3. I always do that because I think that this word generates garbage. Am not even sure if it´s better the way I do it, but at nay case I doubt is worst. Anyway, here is the skipped code if it´s useful:
Mesh mesh;
Vector3[] orderedVertices;
int[] orderedTriangles;
public GameObject[] baseObjects;
Mesh[] baseMeshes;
public GameObject _bottomFormer; //deter$$anonymous$$e the corners of the plane
public GameObject _topNext;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().sharedMesh = mesh;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
baseMeshes = new Mesh[baseObjects.Length];
List<float> rows = new List<float>();
List<float> columns = new List<float>();
Vector3 bottomFormer = _bottomFormer.transform.position;
Vector3 topNext = _topNext.transform.position;
rows.Add (bottomFormer.z);
rows.Add (topNext.z);
columns.Add (bottomFormer.x);
columns.Add (topNext.x);
for (int i = 0; i < baseObjects.Length; i++)
{
baseMeshes[i] = baseObjects[i].GetComponent<MeshFilter>().mesh;
for (int j = 0; j < baseMeshes[i].vertices.Length; j++)
{
if (Mathf.Abs (baseMeshes[i].vertices[j].y) < 0.000005) //the origin, (0, 0, 0), so is the bottom most part
{
Vector3 globalBaseVertex = baseMeshes[i].vertices[j] + baseObjects[i].transform.position;
if (!rows.Contains (globalBaseVertex.z))
{
rows.Add (globalBaseVertex.z);
}
if (!columns.Contains (globalBaseVertex.x))
{
columns.Add (globalBaseVertex.x);
}
}
}
}
rows.Sort();
columns.Sort();
orderedVertices = new Vector3 [rows.Count * columns.Count];
for (int i = 0, r = 0; r < rows.Count; r++)
{
for (int c = 0; c < columns.Count; c++, i++)
{
orderedVertices[i] = Vector3.zero;
orderedVertices[i].z = rows[r];
orderedVertices[i].x = columns[c];
orderedVertices[i].y = bottomFormer.y;
}
}
}
Ok, there's a lot to clear up here ^^. First of all a Vector3 is a struct and therefore a value type. The new keyword does not automatically allocate memory. Actually it does not for value types. The new keyword is just the syntax for calling the constructor. So you're misguided here. However on the other hand the code you've posted creates tons of garbage:
First you should stay away from the "mesh" property unless you really want a duplicated mesh. In your for loop where you iterate through your base meshes, you access the mesh property. This will actually instantiate the mesh of each of those objects. Reading or writing to the mesh property will ensure that this gameobject has it's own unique mesh. You should always use sharedMesh unless your intention is to create a seperate mesh for this specific gameobject only.
The next issue where you create a huge about of garbage is your usage of the vertices and triangles properties of the mesh class. Reading those properties will create new arrays each time you read it. In your "j" loop you access the vertices property 3 times per iteraion. So you allocate memory that is worth 12*3*v² bytes where "v" is the number of vertices you have. So if you have a mesh with 1000 vertices you will allocate 36MB of memory / garbage. Note that the size grows quadratically. So if you have 10000 vertices you allocate 3.6 GB of garbage.
Likewise in your UpdateMesh method you allocate 2 triangles arrays and one vertices array each iteration. So you allocate t*(4*2*t + 12*v) bytes where t is the length of the triangle array and v the length of the vertices array.
When working with meshes, you should always store the vertices, triangles, normals, ... arrays in a local variable once before the loop.
I don't quite understand this if statement. At least the comment does not really match what the if statement does:
if (Mathf.Abs (baseMeshes[i].vertices[j].y) < 0.000005) //the origin, (0, 0, 0), so is the bottom most part
You take the absolute value of the y position and only use vertices that are smaller than your threshold. So you only grab vertices which are literally at y==0, nothing more. Depending on where those vertices are, those may not be the bottom most vertices as a vertex at for example "-1" would not be considered as the absolute value is larger than the threshold. If you know your vertices are never below 0, why take the absolute value? You essentially just pick a very very small slice around y==0.
I'm still not quite sure why you rip those vertices apart into seperate x and z components, but hey, if it does what you want, go with it.
ran out of characters ^^
You originally said you don't see your mesh at all. Are you sure the object with the MeshFilter has a MeshRenderer with a material attached? Otherwise nothing would be rendered.
Do you see anything if you switch out the mesh of the MeshFilter at runtime with the default cube mesh?
Are you sure you don't have some incredible large scales that the mesh is simply huge and maybe 100 units above you? Have you tried pressing "f" to focus the object during runtime after selecting your object in the hierarchy and moving the mouse over the sceneview?
Over the years I have created countless scripts which generate all sorts of meshes and I never had any issue that is did not display (for example a procedural Utah Teapot). At least if it wasn't visible, I know i did something wrong (wrong material, wrong winding order, disabled component, maybe camera culling mask and some dodgy layer set on your object).
Well after all we can't do the debugging for you. Just systematically rule out possible causes one by one.
"First of all, It has a Mesh Renderer" (I mean, it´s literally the first line of my question). Anyway, regarding this line: if (Mathf.Abs (baseMeshes[i].vertices[j].y) < 0.000005) //sorry if I didn´t explained properly. It is indeed checking if the y == 0, but the absolute was to avoid floating precision errors (I actually tried == 0 originally and didn´t worked). The origin is (0, 0, 0) and is fixed to be at the bottom most part of the mesh, so there aren´t any negative vertices.y (honestly, this was the only way that I could think of getting the "base" vertices);
And the whole idea was to get these vertices and place rows and columns on them to create the shape of the plane. I wanted to get the rows and columns, then set the vertices on them to avoid repetition and control the order in which they are called, from left to right, from bottom to top (I originally took all of the vertices in a List, but my code was much longer and I didn´t even knew the order of the vertices, that I will use to create the triangles in code that I haven´t shown that is still not finished).
Also, being honest, I don´t know that much about garbage generation. I remember what I read in a really long article and am constantly trying to apply that, but I didn´t knew anything of what you´ve told me. Please, could you post some code doing what you told me to do? I didn´t quite understood how to avoid that garbage generation.
And finally, I think I´ve found the error, kind of. I knew that the vertices and triangles were right since I was debugging them. I even pressed f to focus the object just in case the shown vertices were wrong, but nothing. That´s why I assumed my code was wrong and asked for help, but there is one more thing. Actually, THE MESH IS INDEED RENDERED. It´s just that, in the wrong place and with the wrong rotation (despite the accurate positioning of vertices and triangles) and I couldn´t see it before because is a flat plane and was hidden to the camera form my top-down perspective. And I didn´t even considered changing the perspective because the vertices were shown right and focusing the object gave me the, aparent, same location, and somehow they managed to stay hidden from my view, even when I was moving the camera. Anyway, I know is indeed the right object because of the selection outline. It´s just somewhere else and with another rotantion. Any ideas?
Edit: apparently, the empty gameObject with the script was rotated (for some reason) and now is solved, but it´s still at the wrong location.