lod with animted hierarchy,question about lod implementation on animated hierarchy with animator
Hi. i was searching for some assets to make lod on animated characters, but i could not find any thing working on unity 2018.2. so i was thinking on write my own custom script to do that. the idea is basically replace every meshfilter mesh with the corresponding on other hierarchy (from the current lod level). i post the code so if somebody can tell me if this is a good idea or not.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LOD_alex : MonoBehaviour
{
[System.Serializable] // sin esto el array objetos no aparecerá en el inspector
public struct _datosObjeto
{
public GameObject Plantillalod;
[HideInInspector]
public GameObject instantiatedLod;
[Tooltip("no asignar. se asigna dinamicamente")]
public float distancia;
}
public _datosObjeto[] lods;
[System.Serializable] // sin esto el array correspondencias no aparecerá en el inspector
public struct _datosGizmos
{
public GameObject original;
public int index;
}
public _datosGizmos[] correspondencias;
public manager m; // manager is a global class storing particular game data, such as the player camera
private void Awake()
{
m = Object.FindObjectOfType<manager>(); // through the manager we access the players camera (see update method below)
// DEBEMOS INSTANCIAR CADA PLANTILLALOD Y PONERLA EN INSTANTIATEDLOD PARA USARLA DESPUES Y O ESTAR CREANDO INSTANCIAS A CADA MOMENTO
for (int i=0;i<lods.Length; i++)
{
if (lods[i].Plantillalod != null)
lods[i].instantiatedLod = Instantiate(lods[i].Plantillalod);
}
}
// Use this for initialization
void Start ()
{
}
float timeRemaining=0;
public float timeInterval=0.5f;
int indiceLodActual = 0;
// Update is called once per frame
void Update ()
{
timeRemaining -= Time.deltaTime;
if (timeRemaining > 0)
return;
timeRemaining = timeInterval;
// here calculates the distance from the players camera to this animated object
float sqrDist=(m.cameraFPC.transform.position- transform.position).sqrMagnitude;
for (int i=lods.Length-1;i>=0;i--) // traverse the lods on iverse order to determine the correct lod to select
{
if (sqrDist >= lods[i].distancia * lods[i].distancia && indiceLodActual!=i) // if the current lod is not the same that the lod to use, we change it
{
// este es el lod que necesitamos colocar
reemplazarMeshLod(i);
indiceLodActual = i;
}
}
}
void reemplazarMeshLod(int indLod)
{
MeshFilter meshFilt=null;
MeshFilter meshFilt2=null;
for (int i=0;i<correspondencias.Length;i++)
{
meshFilt = correspondencias[i].original.GetComponentInChildren<MeshFilter>();
if (i < lods[indLod].instantiatedLod.transform.childCount)
meshFilt2 = lods[indLod].instantiatedLod.transform.GetChild(i).GetComponent<MeshFilter>();
//meshFilt2=lods[indLod].instantiatedLod.transform.GetChild(i).GetComponentInChildren<MeshFilter>();
if (meshFilt!=null && meshFilt2!=null)
{
meshFilt.mesh = meshFilt2.mesh;
}
}
}
}
Thanks in advance.
,Hi. i was searching for some assets to make lod on animated characters, but i could not find any thing working on unity 2018.2. so i was thinking on write my own custom script to do that. the idea is basically replace every meshfilter mesh with the corresponding on other hierarchy (from the current lod level). i post the code so if somebody can tell me if this is a good idea or not.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LOD_alex : MonoBehaviour
{
[System.Serializable] // sin esto el array objetos no aparecerá en el inspector
public struct _datosObjeto
{
public GameObject Plantillalod;
[HideInInspector]
public GameObject instantiatedLod;
[Tooltip("no asignar. se asigna dinamicamente")]
public float distancia;
}
public _datosObjeto[] lods;
[System.Serializable] // sin esto el array correspondencias no aparecerá en el inspector
public struct _datosGizmos
{
public GameObject original;
public int index;
}
public _datosGizmos[] correspondencias;
public manager m; // manager is a global class storing particular game data, such as the player camera
private void Awake()
{
m = Object.FindObjectOfType<manager>(); // through the manager we access the players camera (see update method below)
// DEBEMOS INSTANCIAR CADA PLANTILLALOD Y PONERLA EN INSTANTIATEDLOD PARA USARLA DESPUES Y O ESTAR CREANDO INSTANCIAS A CADA MOMENTO
for (int i=0;i<lods.Length; i++)
{
if (lods[i].Plantillalod != null)
lods[i].instantiatedLod = Instantiate(lods[i].Plantillalod);
}
}
// Use this for initialization
void Start ()
{
}
float timeRemaining=0;
public float timeInterval=0.5f;
int indiceLodActual = 0;
// Update is called once per frame
void Update ()
{
timeRemaining -= Time.deltaTime;
if (timeRemaining > 0)
return;
timeRemaining = timeInterval;
// here calculates the distance from the players camera to this animated object
float sqrDist=(m.cameraFPC.transform.position- transform.position).sqrMagnitude;
for (int i=lods.Length-1;i>=0;i--) // traverse the lods on iverse order to determine the correct lod to select
{
if (sqrDist >= lods[i].distancia * lods[i].distancia && indiceLodActual!=i) // if the current lod is not the same that the lod to use, we change it
{
// este es el lod que necesitamos colocar
reemplazarMeshLod(i);
indiceLodActual = i;
}
}
}
void reemplazarMeshLod(int indLod)
{
MeshFilter meshFilt=null;
MeshFilter meshFilt2=null;
for (int i=0;i<correspondencias.Length;i++)
{
meshFilt = correspondencias[i].original.GetComponentInChildren<MeshFilter>();
if (i < lods[indLod].instantiatedLod.transform.childCount)
meshFilt2 = lods[indLod].instantiatedLod.transform.GetChild(i).GetComponent<MeshFilter>();
//meshFilt2=lods[indLod].instantiatedLod.transform.GetChild(i).GetComponentInChildren<MeshFilter>();
if (meshFilt!=null && meshFilt2!=null)
{
meshFilt.mesh = meshFilt2.mesh;
}
}
}
}
Thanks in advance and every suggestion will be very good received.