LOD switching - SOLVED!

Hello,

I’m trying to figure out how to activate/deactivate a mesh inside a gameComponent in order to implement an “automatic” LOD switch inside one object.

I have edited Ara’s LOD script for use with 3 LODs:

var HLOD : Mesh;
var MLOD : Mesh;
var LLOD : Mesh;
var distance1 = 10.0;
var distance2 = 20.0;

function Update ()
{
    var campos = Camera.main.transform.position;
    var meshFilter : MeshFilter = GetComponent(MeshFilter);
	
    if( (transform.position - campos).sqrMagnitude < distance1 * distance1 )
    {
        // use High LOD
        if( meshFilter.sharedMesh != HLOD )
            meshFilter.sharedMesh = HLOD;
    }
   else if ((transform.position - campos).sqrMagnitude < distance2 * distance2)
    {
         // use Medium LOD
        if( meshFilter.sharedMesh != MLOD )
            meshFilter.sharedMesh = MLOD;	
    }
    else
    {
        // use Low LOD
        if( meshFilter.sharedMesh != LLOD )
            meshFilter.sharedMesh = LLOD;
    }
}

It works great if I have 3 different models (in my case .dae), include one model (it does´t matter which one) into the world and attach the script to the mesh so that I can setup the mesh switching.

Now, what I would like it to do is to create just one model with 3 meshes (I usually call them HLOD, MLOD and LLOD) and edit the original script in order to get the same LOD switching but inside just one model. I mean, I would like to attach the script to the model (gameObject) and that the script recognize the name of the 3 meshes and activate/deactivate them depending on the distance.
This is my example:

This way creating LODs is much more easy because I only will have one object and because the script will recognize the corresponding LOD that should be rendered.

I have tried a lot of ways but none is printable here because they are really crap…
Can anyone help me?

Thanks in advance

you basically have a single mesh there. you just have to see where your mesh renderer is. if it is on the root object, then you can rewrite / replace below script to just enable the whole game object and make the HLOD etc variable take a GameObject reference instead of a mesh one (thats actually simpler than your switching there)

thats the theoretical approach, potentially we migt need to see the model should that not work

Thanks for the fast answer.

Well, inside the model I have three meshes with their own renderers, these meshes are called HLOD, MLOD and LLOD so what I would like to do into the script is something like that:

(this is not code, only a sketch)
if (dist <= distance1 ) // that means I’m very near
{
thisgameObject.HLOD.mesh renderer = true;
thisgameObject.MLOD.mesh renderer = false;
thisgameObject.LLOD.mesh renderer = false;
}

This way the script understand which mesh it should activate or not.
But my problem is that I don’t know how to write this code and as I’m trying it since this morning I would like you to ask for how to write this piece of code.

Thanks again

you don’t need to even access the renderer, it would be like the code below.
it hasn’t been tested but I think it should work

var HLOD : Transform;
var MLOD : Transform;
var LLOD : Transform;
var distance1 = 10.0;
var distance2 = 20.0;

function Update ()
{
    var campos = Camera.main.transform.position;
   
    if( (transform.position - campos).sqrMagnitude < distance1 * distance1 )
    {
        // use High LOD
        HLOD.active = true;
        MLOD.active = false;
        LLOD.active = false;
    }
   else if ((transform.position - campos).sqrMagnitude < distance2 * distance2)
    {
         // use Medium LOD
        HLOD.active = false;
        MLOD.active = true;
        LLOD.active = false; 
    }
    else
    {
        // use Low LOD
        HLOD.active = false;
        MLOD.active = false;
        LLOD.active = true;
    }
}

That’s great!
and as you said it was easier than mine…
OK, last thing: instead of writing:

var HLOD : Transform;
var MLOD : Transform;
var LLOD : Transform;

what I would like the script to do is to “read” the gameObject where it is attached and apply the 3 LODs automatically, example:

(this is not a working code)
HLOD = thisGameObject.HLOD.mesh;
MLOD = thisGameObject.MLOD.mesh;
LLOD = thisGameObject.LLOD.mesh;

So if my gameobject is called house and it has 3 children (meshes) called HLOD, MLOD and LLOD, the script will create on its own the next:

(this is not a working code)
HLOD = house.HLOD.mesh;
MLOD = house.MLOD.mesh;
LLOD = house.LLOD.mesh;

This way the only thing should be done is to apply the script to the object and change the distances in case it is necessary.
I hope I’m explaining myself correctly and once again thank you very much.

you don’t need mesh, nowhere as you will not work on meshes any longer if you have distinct objects.

What you are actually looking for is using

transform.Find(“”);

which should work by adding

function Start () {
  HLOD = transform.Find("HLOD");
  MLOD = transform.Find("MLOD");
  LLOD = transform.Find("LLOD");
}

Thank you again, I think you are right, this is the way.
Anyway it is not working for me and as, may be, there are people reading this post I have packed the assets I’m using in order to let you try or check what I’m trying to do.

So why do you think it’s still not working?
Thanks

226431–8161–$lods_in_progress_v10_272.unitypackage (19.4 KB)

I’m still trying to access the childs in order to activate/deactivate them…

So, once again… :sweat_smile:
I have the following hierarchy:

I would like to access the gameObject “house” and then activate or deactivate their children “HLOD”, “MLOD” or “LLOD” (they are meshes), but not at the same time, in the way I will only have one of them active depending on the distance from the gameObject to the active camera…

Any ideas?
Thank you very much.

Check out this post:

http://forum.unity3d.com/viewtopic.php?p=230891#230891

for more ideas.

strange where did my post go?
Guess I didn’t click send then.

All you have to do is to fill the content of the HLOD, MLOD, LLOD with the content of

transform.Find()

simplest way is using the start function for that

function Start() {
  HLOD = transform.Find("HLOD");
  MLOD = transform.Find("MLOD");
  LLOD = transform.Find("LLOD");
}

this will search in the childs of the game object this component is assigned to and assign the corresponding child when found

here you go

“LODS_notworking” (its just the name it works now):

var distance1 = 10.0;
var distance2 = 20.0;
var distance3 = 30.0;
var  HLOD : GameObject;
var  MLOD : GameObject;
var  LLOD : GameObject;
private var HLODmesh : GameObject;
private var MLODmesh : GameObject;
private var LLODmesh : GameObject;
private var Hactive = true;
private var Mactive = true;
private var Lactive = true;
private var player : Transform;

function Start ()
{
	player = GameObject.FindWithTag("Player").transform;
}

function Update ()
{
    var campos = Vector3.Distance(transform.position, player.position);
   
    if (campos < distance1)
    {
        // use High LOD
		if (!HLODmesh  Hactive)
			HLODmesh = Instantiate(HLOD, transform.position, transform.rotation);
						
	   	   Hactive = false;
		   
    }
	else
	{
		if (HLODmesh  !Hactive)
		Destroy (HLODmesh);
		Hactive = true;
	}
   if (campos < distance2  campos > distance1)
    {
         // use Medium LOD
		 if (!MLODmesh  Mactive)
			 MLODmesh = Instantiate(MLOD, transform.position, transform.rotation);
		 
	     Mactive = false;
	    		 
    }
	else
	{
		if (MLODmesh  !Mactive)
		Destroy (MLODmesh);
		Mactive = true;
	}
    if (campos < distance3  campos > distance2)
    {
        // use Low LOD
		if (!LLODmesh  Lactive)
			LLODmesh = Instantiate(LLOD, transform.position, transform.rotation);
		 
	     Lactive = false;
    }
	else
	{
		if (LLODmesh  !Lactive)
		Destroy (LLODmesh);
		Lactive = true;
	}
}

// Called by the Unity Editor GUI every update cycle, but only when the object is selected.
// Draws a sphere showing visually.
function OnDrawGizmosSelected ()
{
	Gizmos.color = Color(0, 1, 1);
	Gizmos.DrawWireSphere(transform.position, distance1);
	
	Gizmos.color = Color(1, 0, 1);
	Gizmos.DrawWireSphere(transform.position, distance3);
	
	Gizmos.color = Color(1, 1, 0);
	Gizmos.DrawWireSphere(transform.position, distance2);
}

and i made a few changes tho the other one
“LoDS_working”

var HLOD : GameObject;
var MLOD : GameObject;
var LLOD : GameObject;
var distance1 = 10.0;
var distance2 = 20.0;
private var player : Transform;

function Start ()
{
	player = GameObject.FindWithTag("Player").transform;
}

function Update ()
{
    var campos = Vector3.Distance(transform.position, player.position);
   
    if (campos < distance1)
    {
        // use High LOD
        HLOD.active = true;
        MLOD.active = false;
        LLOD.active = false;
    }
   else if (campos < distance2)
    {
         // use Medium LOD
        HLOD.active = false;
        MLOD.active = true;
        LLOD.active = false;
    }
    else
    {
        // use Low LOD
        HLOD.active = false;
        MLOD.active = false;
        LLOD.active = true;
    }
}
function OnDrawGizmosSelected ()
{
	Gizmos.color = Color(0, 1, 1);
	Gizmos.DrawWireSphere(transform.position, distance1);
	
	Gizmos.color = Color(1, 1, 0);
	Gizmos.DrawWireSphere(transform.position, distance2);
}

all i did was pretty much copy and paste form an other script, as you can see now there will be Gizmos drawn in the shape of a Sphere to know visually where the objects appear and disappear,
also the is the player = GameObject.FindWithTag("Player").transform;
you have to select the object that is going to be the “player” and set its Tag to “player”

well if there is something you don’t understand just PM me hope i make myself clear, the code i used is from the tutorial Lerpz Scape.

Hello again,

I finally got it working…

dreamora, you were right, I’m very sorry, the first time I didn’t understand you: I added your “function start” code but deleted the variables declarations so the compiler always asked for them and I thought your solution was the problem, but it was my error.

Anglis, thanks also for your solution, it couldn’t make it work but as dreamora solution worked for me I used part of your code that I really liked a lot: the part of the gizmos. I didn’t understand the reason for “Player” tag, why it is used? is it necessary?

So, finally, I got this working LOD system:

// This script is made to be applied to an unique gameObject that has 3 different meshes called HLOD, MLOD and LLOD that will correspond with its different versions.

var distance1 = 5.0; //Jump from HLOD to MLOD
var distance2 = 10.0; //Jump from MLOD to LLOD
private var HLOD : Transform;
private var MLOD : Transform;
private var LLOD : Transform;

function Start() {
  HLOD = transform.Find("HLOD");
  MLOD = transform.Find("MLOD");
  LLOD = transform.Find("LLOD");
}
function Update ()
{
    //The camera should have applied "MainCamera" tag in order to correctly switch LODs
    var campos = Camera.main.transform.position;
   
    if ((transform.position - campos).sqrMagnitude < distance1 * distance1 )
    {
        // use High LOD
        HLOD.active = true;
        MLOD.active = false;
        LLOD.active = false;
    }
   else if ((transform.position - campos).sqrMagnitude < distance2 * distance2)
    {
         // use Medium LOD
        HLOD.active = false;
        MLOD.active = true;
        LLOD.active = false;
    }
    else
    {
        // use Low LOD
        HLOD.active = false;
        MLOD.active = false;
        LLOD.active = true;
    }
}

// Called by the Unity Editor GUI every update cycle, but only when the object is selected.
// Draws a sphere showing visually. 
function OnDrawGizmosSelected ()
{
   Gizmos.color = Color(0, 1, 1);
   Gizmos.DrawWireSphere(transform.position, distance1);
   
   Gizmos.color = Color(1, 1, 0);
   Gizmos.DrawWireSphere(transform.position, distance2);
}

If you still can optimize this code or make it better it will be great but as you can see it works and can be used by any person who likes this way of modelling (the 3 LODs inside an unique model).

Thank you very much.
=)