- Home /
How to render only the visible meshes of the blocks?
Hi, so my world generation script is working just fine but there is a problem that is causing lag and that is that when I instantiate too many objects it becomes laggy, I mean I have an area of about 40x40 blocks and it lags just then, and the reason to that is because I'm instantiating, but however, if there's any way to display only what's visible and not render all the other sides of the blocks then that would save a lot of performance power, but how do I do that? Do I make a script that always renders the visible content of the object and then attach that to the prefab that I'm instantiating? Or something like that?
Here's my world generation script:
var player : Transform;
var origin : Transform;
var width_x : int;
var width_z : int;
var depth : int;
var blocks : GameObject[];
function Start () {
for(var x : int = 0; x < width_x - 1; x++){
for(var y : int = 0; y > -depth - 1; y--){
for(var z : int = 0; z < width_z - 1; z++){
Instantiate(blocks[Random.Range(0, blocks.Length)], new Vector3(x, y, z), Quaternion.identity);
}
}
}
}
The problem you outline is typically solved by building multiple chunks of mesh, each representing many cubes. Search for Voxels for many posts and information. You can modify the mesh of each of your cubes to eli$$anonymous$$ate the sides not visible, but I doubt that it will give you the speedup you are seeking. A good deal of the overhead is just the number of game objects.
Have you considered using Application.LoadLevelAdditive and randomly generating the level in some way, either before the level is loaded or as the level is being loaded? ($$anonymous$$ight not be possible but would probably reduce lag)
Answer by Pendrokar · Jul 28, 2014 at 04:45 PM
You write a script that disables MeshRenders for GameObjects which are invisible to the camera/player.
Seems Unity already has something to detect visibility: http://docs.unity3d.com/ScriptReference/Renderer-isVisible.html
I bet the default use of this is to hide MeshRenders when a mesh is out of range for an active camera, not whenever an object without a transparent material is in front of another mesh. But shadows should be an independent element, wonder if they are.
OR
If there is no free space between these blocks/cubes, which there wouldn't be judging from the use of concussive integers in the posted script. Then create a dynamic object, the geometry of which is changed with added or removed blocks/cubes. I bet MineCraft clones have done it this way.
Your answer
Follow this Question
Related Questions
Create custom objects like in paper.io 2 at runtime 1 Answer
How to move the world/scene not the player - or not? 1 Answer
Confining / Constraining an object transform trajectory to a mesh 1 Answer
Unity adding random transform to mesh components 1 Answer
How can I "generate a mesh" for a projected image or decal? 0 Answers