给定一架飞机模型,需要分析该模型中,各个3D物体的面数和三角形数,目的是用于观察哪些物体面数过多,需要减面。
一、模型面数分析
二、脚本代码
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
/// <summary>
/// 计算面数:计算物体的面数,用于模型面数分析,有右键菜单进行
/// </summary>
public class JiSuanMianShu : MonoBehaviour
{
/// <summary>
/// 部件信息
/// </summary>
[Serializable]
public class PartInfo
{
/// <summary>
/// 部件对象
/// </summary>
[SerializeField]
[Header("部件对象")]
public GameObject part;
/// <summary>
/// 三角形面数
/// </summary>
[SerializeField]
[Header("三角形面数")]
public int trianglesNum;
/// <summary>
/// 顶点数
/// </summary>
[SerializeField]
[Header("顶点数")]
public int vertexNum;
}
/// <summary>
/// 物体的root
/// </summary>
[Header("物体的root")]
public GameObject root;
/// <summary>
/// 计算结果:部件信息表
/// </summary>
[Header("计算结果:部件信息表")]
public List<PartInfo> partsInfos = new List<PartInfo>();
#if UNITY_EDITOR
[ContextMenu("分析模型")]
#endif
void Load()
{
partsInfos.Clear();
//@@所有的物体
var allObjs = root.GetComponentsInChildren<Transform>().ToList();
Debug.Log($"【1】====子物体的数量:{allObjs.Count} 时间戳: {Time.realtimeSinceStartup}");
foreach (var go in allObjs)
{
if (go.GetComponent<MeshFilter>() == null) continue;
var pi = new PartInfo();
pi.part = go.gameObject;
pi.trianglesNum = go.GetComponent<MeshFilter>().sharedMesh.triangles.Length;
pi.vertexNum = go.GetComponent<MeshFilter>().sharedMesh.vertexCount;
partsInfos.Add(pi);
}
partsInfos.Sort((a,b)=>a.trianglesNum.CompareTo(b.trianglesNum));
}
}