Simple LOD Manager

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
Line 64: Line 64:

Latest revision as of 20:52, 10 January 2012

Author: Tom Vogt

[edit] Overview

A simple script to automatically switch between LODs of a model based on distance to the active camera.

[edit] JavaScript

Automatically set LOD Levels of models
(C)2010 by Tom Vogt
Use and modify as you wish, but retain author credits (aka BSD license)
== Usage ==
* Create an empty gameobject
* Drag all LOD levels into it, so they become children of the LOD object
* Attach this script to the empty gameobject
* Set the number of meshes in the script, and drag them into the slots, from highest (closest) to lowest (far away)
* Set the number of distances to '''one less''' than the number of meshes (beyond the highest distance will use the most far away mesh automatically)
* Set the distances at which the script shall switch to the next lowest LOD. So the first number means "at this distance or less, use the highest LOD".
* Press Play and drag the camera around to check if your distances are fine.
public var Distances : float[];
public var Models : GameObject[];
private var current = -2;
function Start() {
	for (var i=0; i<Models.Length; i++) {
function Update() {
	var d = Vector3.Distance(camera.main.transform.position, transform.position);
	var stage = -1;
	for (var i=0; i<Distances.Length; i++) {
		if (d<Distances[i]) {
	if (stage==-1) stage=Distances.Length;
	if (current!=stage) {
function SetLOD(stage) {
	if (current>=0)	Models[current].SetActiveRecursively(false);
	current = stage;
Personal tools