I was wondering if anyone has a script out there in the community that would effectively load and unload objects from memory as you get within and beyond a particular distance from them. Maybe even one that can be set for certain types of objects (eg. small distance 20-75m, medium distance 200-500m, large, 500-750, huge, 750-2000, etc).
Thanks for sharing. I assume from what I can make out, one would create 3 different meshes right? Attach the script to the mesh in the scene then fill in the Public Variables of the script with the appropriate info?
The principal of the script is to have three different LOD/Level of detail meshes/models.
Lets say you have a big castle model which is a drain on the performance of your game, so you develop three stages of this castle, the close, “detailed” one, which lets say has 5000 polygons, next you have the medium range model which could be 2500 polygons then finally you have the very distance 750 polygon model.
Now you will place these three mesh/model versions into the correct LOD variables:
public var lodMesh0 : Mesh;
public var lodMesh1 : Mesh;
public var lodMesh2 : Mesh;
Then you need to set the distance variables, the script checks the distance by using:
var distanceFromObject : float = Vector3.Distance(thisTransform.position, Camera.main.transform.position);
You just need to set the distance you want your models to be displayed at, here:
public var distanceLOD1 : float;
public var distanceLOD2 : float;
If your gameObject’s distance is under “distanceLOD1” then the script will display the “LOD0” mesh/model, your high detail model, next if your gameObject’s distance is between “distanceLOD1” and “distanceLOD2” then it will display “LOD1” or your medium detail mesh/model, then finally if your gameObject’s distance is larger then “distanceLOD2” it will display the “LOD2” mesh/model.
Much more on LOD/Level of detail here: Level of detail - Wikipedia
I also believe Unity is developing a built in LOD manager, well I read about it somewhere
I believe you attach it each object you use, obviously when using duplicate objects you will just set it once in a prefab. However I have not actually used this script, I only know of it
Thanks for the input. Now, would OC be standard even in the Indie license (non Pro)? I assume that it would just depend on the settings you put in the Camera info right?
OC doesn’t affect what is held in memory. Are you trying to improve performance or memory? These are completely separate. For example, an inactive game object with a large texture is still in memory, but it is not affecting performance because it is not being processed.
From what I gather neither occlusion culling or LOD will help you in saving memory. As Rafes mentioned they will help in rendering performance.
In the case of LOD you are rendering smaller textures for objects that are farther away, or not rendering anything if they are past your last LOD distance. You could setup a system that will load and unload textures that are not in use, but the cost of loading/unloading from memory as you change LODs would cost you much more in performance than you would gain in memory usage.
In the case of OC objects that are within your camera view but are obstructed by other objects are not rendered. This also helps in rendering performance. OC must be setup and baked onto your scene and I don’t think it comes with the free version of Unity.