using UnityEngine;
using System.Collections;
public class LOD : MonoBehaviour
{
//distances where LOD transitions occur.
public float[] lodLevels = new float[] { 1000f, 800f, 600f, 400f, 200f, 100f, 50f, 10f };
//These are the builtin LOD values for Unity shaders.
int[] shaderLOD = new int[] { 600, 500, 400, 300, 250, 200, 150, 100 };
void Start ()
{
StartCoroutine ("UpdateLOD");
}
void SetLOD (int LOD)
{
foreach (var i in renderer.sharedMaterials) {
i.shader.maximumLOD = shaderLOD[LOD];
}
}
IEnumerator UpdateLOD ()
{
var period = Random.value;
var cam = Camera.main.transform;
float[] levels = new float[lodLevels.Length];
int i = 0;
foreach (var level in lodLevels) {
levels[i++] = level * level;
}
while (true) {
var distance = (transform.position - cam.position).sqrMagnitude;
var LOD = levels.Length - 1;
foreach (var level in levels) {
if (distance > level) {
SetLOD (LOD);
break;
}
LOD--;
}
// make LOD checking non deterministic. avoids stampeding LOD calls?
yield return new WaitForSeconds (period);
}
}
}
Saturday, October 30, 2010
A Unity Shader LOD Script.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
These are the robots I've been working on for the last 12 months. They each weigh about 11 tonnes and have a 17 meter reach. The control...
-
This hard-to-see screenshot is a Generic Node Graph Editing framework I'm building. I'm hoping it can be used for any kind of node...
-
So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...
-
MiddleMan: A Pub/Sub and Request/Response server in Go. This is my first Go project. It is a rewrite of an existing Python server, based o...
-
Why would I ask that question? Python 3 has been available for some time now, yet uptake is slow. There aren't a whole lot of packages i...
-
It is about 8 degrees C this morning. So cold, especially when last week we had high twenties. To help solve the problem, a friend suggeste...
-
After my last post, I decided to benchmark the scaling properties of Stackless, Kamaelia, Fibra using the same hackysack algorithm. Left axi...
-
I'm now using bzr instead of svn. I'm pushing my repositories to: http://exactlysimilar.org/bzr/ I'm also auto publishing docume...
-
I've just read a newspaper article (courtesy of Kranzky ) from WA Business News documenting the malfeasance, gross negligence and misc...
-
Possibly slightly more correct lighting. The rim light is now only applied in the direction of the sun, rather than being purely based on vi...
3 comments:
NB: This is only useful for unique models (Eg boss models) which have their own unique material.
If you used .material instead of .sharedMaterial, couldn't you work around the unique requirement?
Can you explain how this script works please? I can't quite figure out shaderlod from the docs alone
Post a Comment