- Home /
Rendering invisible objects
Hi,
I have a cube that i rendering the faces of based off of weather or not there is a cube next to it (im attempting to make a mine craft clone). Now, I have gotten this visibly working, however, it is still running rather slow. To test, i hid all the faces on all the cubes. This visually renders nothing, however, the lag is still there and the CPU profiler is suggesting that the renderer calls are what is slowing everything down.
I am still new to modifying meshes, so is there possibly a step i have missed when modifying the mesh? Or is there something I need to set in the unity editor etc? Or is this just expected when rendering each cube separately, as opposed to proper voxel "chunk" like rendering?
i have attached the script I am using to modify the mesh. This code is only being called once on startup.
Please let me know if any more information is needed.
Thanks in advance
public void UpdateBlockRender()
{
//From list of touching blocks, figure out which sides to render
Mesh mesh = GetComponent<MeshFilter>().mesh;
List<Vector3> VertToAdd = new List<Vector3>();
if (visibleFaces.ShowNorth) VertToAdd.AddRange(Misc.FaceVerticiesOrderedToMatch[BlockDirection.North]);
if (visibleFaces.ShowSouth) VertToAdd.AddRange(Misc.FaceVerticiesOrderedToMatch[BlockDirection.South]);
if (visibleFaces.ShowTop) VertToAdd.AddRange(Misc.FaceVerticiesOrderedToMatch[BlockDirection.Top]);
if (visibleFaces.ShowBottom) VertToAdd.AddRange(Misc.FaceVerticiesOrderedToMatch[BlockDirection.Bottom]);
if (visibleFaces.ShowWest) VertToAdd.AddRange(Misc.FaceVerticiesOrderedToMatch[BlockDirection.West]);
if (visibleFaces.ShowEast) VertToAdd.AddRange(Misc.FaceVerticiesOrderedToMatch[BlockDirection.East]);
List<int> TriToAdd = new List<int>();
int faceCounter = 0;
if (visibleFaces.ShowNorth)
{
TriToAdd.AddRange(new int[] { 2,3,0,3,1,0 }.Select(x => x + faceCounter*4));
faceCounter++;
}
if (visibleFaces.ShowSouth)
{
TriToAdd.AddRange(new int[] { 0, 2, 1, 1, 2, 3 }.Select(x => x + faceCounter * 4));
faceCounter++;
}
if (visibleFaces.ShowTop)
{
TriToAdd.AddRange(new int[] { 0, 2, 1, 1, 2, 3 }.Select(x => x + faceCounter * 4));
faceCounter++;
}
if (visibleFaces.ShowBottom)
{
TriToAdd.AddRange(new int[] { 1,2,0,2,3,0 }.Select(x => x + faceCounter * 4));
faceCounter++;
}
if (visibleFaces.ShowWest)
{
TriToAdd.AddRange(new int[] { 2, 0, 1, 2, 3, 0 }.Select(x => x + faceCounter * 4));
faceCounter++;
}
if (visibleFaces.ShowEast)
{
TriToAdd.AddRange(new int[] { 2,3,1,3,0,1 }.Select(x => x + faceCounter * 4));
//faceCounter++;
}
mesh.Clear();
mesh.SetVertices(VertToAdd);
mesh.triangles = (TriToAdd.ToArray());
mesh.RecalculateBounds();
//mesh = newMesh;
Vector2[] UVs = new Vector2[mesh.vertices.Length];
int index = 0;
if (visibleFaces.ShowNorth) // Front
{
UVs[index++] = new Vector2(0.0f, 0.0f);
UVs[index++] = new Vector2(0.333f, 0.0f);
UVs[index++] = new Vector2(0.0f, 0.333f);
UVs[index++] = new Vector2(0.333f, 0.333f);
}
if (visibleFaces.ShowSouth) // Back //For some Reason, back renders upside down. values have been changed to reflect that
{
UVs[index++] = new Vector2(1.0f, 0.333f);
UVs[index++] = new Vector2(0.667f, 0.333f);
UVs[index++] = new Vector2(1.0f, 0.0f);
UVs[index++] = new Vector2(0.667f, 0.0f);
}
if (visibleFaces.ShowTop) // Top //For some reason, top renders right to left, values have been changed to reflect that
{
UVs[index++] = new Vector2(0.666f, 0.333f);
UVs[index++] = new Vector2(0.334f, 0.333f);
UVs[index++] = new Vector2(0.666f, 0.0f);
UVs[index++] = new Vector2(0.334f, 0.0f);
}
if (visibleFaces.ShowBottom) // Bottom //For some Reason, Bottom renders upside down. values have been changed to reflect that //Might actually render right, who knows
{
UVs[index++] = new Vector2(0.333f, 0.666f);
UVs[index++] = new Vector2(0.333f, 0.334f);
UVs[index++] = new Vector2(0.0f, 0.334f);
UVs[index++] = new Vector2(0.0f, 0.666f);
}
if (visibleFaces.ShowWest) // Left
{
UVs[index++] = new Vector2(0.334f, 0.334f);
UVs[index++] = new Vector2(0.334f, 0.666f);
UVs[index++] = new Vector2(0.666f, 0.666f);
UVs[index++] = new Vector2(0.666f, 0.334f);
}
if (visibleFaces.ShowEast) // Right
{
UVs[index++] = new Vector2(0.667f, 0.334f);
UVs[index++] = new Vector2(0.667f, 0.666f);
UVs[index++] = new Vector2(1.0f, 0.666f);
UVs[index++] = new Vector2(1.0f, 0.334f);
}
mesh.uv = UVs;
mesh.Optimize();
}
Answer by JonPQ · Sep 04, 2019 at 09:21 PM
if all the faces are removed from cube, you can turn off the whole gameobject for that cube. process nothing. at a minimum, disable the meshRenderer component. Also make sure you only construct the mesh when something changes... not every frame.
You might want to try combining many cubes in an area into one mesh, so you can send them all in one draw-call, one mesh together. to reduce overhead. Maybe combine them by material type... use same shader / material on all/most of the cubes if you can. but Just with different uv's / vertex color
So even if the are no sides rendered, I.E 0 vertices, 0 triangles etc, the mesh renderer component still creates overhead just by being enabled? Yes i probably will eventually end up doing a proper voxel solution by joining meshes together, but I'm just trying to understand why it is going so slow. For instance, I am generating around 44000 cubes, with only about 3000 cubes visible (side and bottom blocks of the "chunk" aren't being rendered at the moment) but still getting 20 fps when i put all of them in the camera frame at once
pretty much anythign in the scene is going to cost some kind of overhead. only an empty scene with no cameras could be considered to have 0 overhead. So.... 40,000 objects in a scene is a huge number of objects.... even if they have nothing much in their mesh. if its all enabled, that forces unity to transform them all, and do a frustrum check for each camera (per object) to see if it is on screen. Thats a lot of math... Then for each object on screen, process the meshes to do polygon culling... granted yours are empty... but if you have 3000, empty cubes it will still do 3000 checks to deter$$anonymous$$e they are empty... for the cubes that have meshes in them... Unity will try to batch and draw them (if you have batching enabled) it does all this every frame.... If your cubes are all individual meshes or with many materials materials etc... they may not batch well, (and its also a lot of objects to batch)... before drawing anything. Try turning on the "stats" window in top right corner... for mobile 3d.. I usually aim for <100 draw calls, and < 50,000 vertices total. (aim for <20k verts if you want to support older devices.) What do you have ?
Try to further arrange your cubes in a hierarchy, so that you can completely turn off large sections of the hierarchy, or know if they are obscured.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can you manually set the layer order for triangles inside a mesh? 2 Answers
Why The shareMesh isn't full load when i load a prefab with SkinnedMeshRenderer in unity? 1 Answer
Why does adding cloth component to skinned meshrenderer move the bounds away from the mesh? 2 Answers
My mesh isnt creating any shadows 1 Answer
Mesh Resolution Problem 1 Answer