How to load large 3D map inside Unity3D. (Large Map Optimization Logic)

Why we need optimization?

As you know, it is hard to load a large map inside a game.For example, if you have play games like "GTA", those game has too big maps that the whole thing could not render at the same time.

So what they did was they disable all those parts which are not close to the player. So the game does not have to render tones of things.

How to optimize a game?


So let's set up our environment.

As you can see, I have taken nine square blocks as ground and a square block as a player. You can also use terrain.


(fig 1)


That's all for our environment setup.
Now let's see the process of disabling those ground block.


(fig 2)


These are the codes for the mechanism.

The important part of the code is


foreach(GameObject ground in grounds)
        {
            Vector3 groundSize = ground.transform.localScale;
            Vector3 groundDistance = ground.transform.position+(singleGrounddistance/5f);

            float xDistance = Mathf.Abs(groundDistance.x-Player.position.x);
            float zDistance = Mathf.Abs(groundDistance.z-Player.position.z);

            calculatedDistance = xDistance+zDistance;

            if(calculatedDistance>maxDistance)
            {
                ground.GetComponent<MeshRenderer>().enabled = false;
            }
            else
            {
                ground.GetComponent<MeshRenderer>().enabled = true;
            }
        }


So here we have just calculated the distance from the player to those block and we have set a parameter max distance.
If the distance between the player and the block is more than max distance, we disable those block.


That's all. Thank you.



(The fianal result)












Comments

Post a Comment

Popular Posts