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...
-
So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...
-
Dear Lazyweb. Imagine a nice RESTful interface for working with Tags. The URL: /tags/ will return a list of all the tags. The URL: /tags/fo...
-
Pyglet 1.1 comes with a shiny new event loop. I've always liked to control my own mainloop, so that I can implement things like fixed t...
-
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...
-
Often, when building a game, you need to test if objects are colliding. The objects could be spaceships, rocks, mouse pointers, laser beams....
-
We're almost done with our Giant Robot Project. Realising we had only a few hours before the robots would be deconstructed and sent awa...
-
I went to the pulse expo this evening, where one of the speakers mentioned that Ubisoft are in town (Perth, Western Australia), looking to...
-
I've built sites with Django, TurboGears and Pylons. I've come to prefer Pylons. Why? Pylons gets out of the way, and stays out of t...
-
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...
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