138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
namespace PolyReduction
|
|
{
|
|
public class AutomaticLODEx : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
public float Percent = 0f;
|
|
public int flag = 0;
|
|
public void BeginLod()
|
|
{
|
|
SetData();
|
|
StartCoroutine(ComputeMeshWithVertices(Percent));
|
|
}
|
|
private void SetData()
|
|
{
|
|
string path = string.Format("SceneRoot/[LodRoot]/Lod_{0}", flag);
|
|
string name = string.Format("Lod_{0}", flag);
|
|
GameObject root = GameObject.Find(path);
|
|
if (root == null)
|
|
{
|
|
root = new GameObject(name);
|
|
var parent = GameObject.Find("SceneRoot/[LodRoot]").transform;
|
|
root.transform.parent = parent;
|
|
}
|
|
MeshSimplify meshSimplify = Object.Instantiate(GetComponent<MeshSimplify>());
|
|
meshSimplify.transform.position = transform.position;
|
|
meshSimplify.transform.rotation = transform.rotation;
|
|
meshSimplify.transform.parent = root.transform;
|
|
|
|
m_selectedMeshSimplify = meshSimplify;
|
|
|
|
m_objectMaterials = new Dictionary<GameObject, Material[]>();
|
|
AddMaterials(meshSimplify.gameObject, m_objectMaterials);
|
|
|
|
m_bWireframe = false;
|
|
}
|
|
|
|
private void AddMaterials(GameObject theGameObject, Dictionary<GameObject, Material[]> dicMaterials)
|
|
{
|
|
Renderer theRenderer = theGameObject.GetComponent<Renderer>();
|
|
MeshSimplify meshSimplify = theGameObject.GetComponent<MeshSimplify>();
|
|
|
|
if (theRenderer != null && theRenderer.sharedMaterials != null && meshSimplify != null)
|
|
{
|
|
dicMaterials.Add(theGameObject, theRenderer.sharedMaterials);
|
|
}
|
|
|
|
for (int i = 0; i < theGameObject.transform.childCount; i++)
|
|
{
|
|
AddMaterials(theGameObject.transform.GetChild(i).gameObject, dicMaterials);
|
|
}
|
|
}
|
|
|
|
private IEnumerator ComputeMeshWithVertices(float fAmount)
|
|
{
|
|
foreach (KeyValuePair<GameObject, Material[]> pair in m_objectMaterials)
|
|
{
|
|
MeshSimplify meshSimplify = pair.Key.GetComponent<MeshSimplify>();
|
|
MeshFilter meshFilter = pair.Key.GetComponent<MeshFilter>();
|
|
SkinnedMeshRenderer skin = pair.Key.GetComponent<SkinnedMeshRenderer>();
|
|
|
|
if (meshSimplify && (meshFilter != null || skin != null))
|
|
{
|
|
Mesh newMesh = null;
|
|
|
|
if (meshFilter != null)
|
|
{
|
|
newMesh = Mesh.Instantiate(meshFilter.sharedMesh);
|
|
}
|
|
else if (skin != null)
|
|
{
|
|
newMesh = Mesh.Instantiate(skin.sharedMesh);
|
|
}
|
|
|
|
if (meshSimplify.GetMeshSimplifier() != null)
|
|
{
|
|
meshSimplify.GetMeshSimplifier().CoroutineEnded = false;
|
|
|
|
StartCoroutine(meshSimplify.GetMeshSimplifier().
|
|
ComputeMeshWithVertexCount(pair.Key, newMesh,
|
|
Mathf.RoundToInt(fAmount * meshSimplify.GetMeshSimplifier().GetOriginalMeshUniqueVertexCount()), meshSimplify.name, Progress));
|
|
|
|
while (meshSimplify.GetMeshSimplifier().CoroutineEnded == false)
|
|
{
|
|
yield return null;
|
|
}
|
|
|
|
if (meshFilter != null)
|
|
{
|
|
meshFilter.mesh = newMesh;
|
|
}
|
|
else if (skin != null)
|
|
{
|
|
skin.sharedMesh = newMesh;
|
|
}
|
|
|
|
meshSimplify.m_simplifiedMesh = newMesh;
|
|
}
|
|
}
|
|
}
|
|
|
|
m_strLastTitle = "";
|
|
m_strLastMessage = "";
|
|
m_nLastProgress = 0;
|
|
}
|
|
|
|
void Progress(string strTitle, string strMessage, float fT)
|
|
{
|
|
int nPercent = Mathf.RoundToInt(fT * 100.0f);
|
|
|
|
if (nPercent != m_nLastProgress || m_strLastTitle != strTitle || m_strLastMessage != strMessage)
|
|
{
|
|
m_strLastTitle = strTitle;
|
|
m_strLastMessage = strMessage;
|
|
m_nLastProgress = nPercent;
|
|
}
|
|
}
|
|
|
|
Dictionary<GameObject, Material[]> m_objectMaterials;
|
|
MeshSimplify m_selectedMeshSimplify;
|
|
bool m_bWireframe;
|
|
float m_fRotationSpeed = 10.0f;
|
|
//float m_fLastMouseX;
|
|
|
|
//Mesh m_newMesh;
|
|
int m_nLastProgress = -1;
|
|
string m_strLastTitle = "";
|
|
string m_strLastMessage = "";
|
|
|
|
float m_fVertexAmount = 1.0f;
|
|
|
|
bool m_bGUIEnabled = true;
|
|
}
|
|
}
|