DCS/ruiyiweiUX/Assets/Scripts/Components/AddMaterialToBrainAndOrgans.cs

194 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region
/*****************************************************************
"Canvas/BodyAndOrgans
"Canvas/BrainRegions
*****************************************************************/
#endregion
public class AddMaterialToBrainAndOrgans : MonoBehaviour
{
public ModelType modelType;
// 声明为静态变量便于在别的类中调用因为大脑的材质需要随着BIS值的变化而变化
public Material brainMat;
public void Update()
{
float bisValue = 0;
brainMat.color = Color.green;
// if (!MySqlDataSource.curFeatureTableDataDict.ContainsKey("BIS"))
// {
// return;
// }
// bool isBisValueParsed = float.TryParse(MySqlDataSource.curFeatureTableDataDict["BIS"], out bisValue);
bool isBisValueParsed = true;
if (!isBisValueParsed)
{
// 如果无法解析 BIS 值,您可能需要处理这种异常情况
return;
}
if (bisValue <= 0)
{
brainMat.color = Color.green;
}
if (bisValue >=40 && bisValue <= 60)
{
brainMat.color = Color.green;
}
else
{
brainMat.color = Color.red;
}
}
private void Awake()
{
InitModelsList();
}
private void Start()
{
AddMatToModel1();
}
/// <summary>
/// 因为每个脑区的颜色不同,需要为不同脑区模型添加对应的材质,器官模型也一样
/// </summary>
void AddMatToModel()
{
List<Transform> models = new List<Transform>();
Dictionary<string, Material> tempMatDict = new Dictionary<string, Material>();
if (modelType == ModelType.brain)
{
models = DataSource.regions;
tempMatDict = DataSource.regionMatDict;
}
else
{
models = DataSource.organs;
tempMatDict = DataSource.organMatDict;
}
foreach (Transform obj in models)
{
string objName = obj.gameObject.name;
if (tempMatDict.ContainsKey(objName))
{
obj.GetComponent<Renderer>().material = tempMatDict[objName];
}
else
{
Debug.Log(objName + " add Mat failed, model name is not matching with ModelMaterialDict!");
}
}
}
/// <summary>
/// 让所有脑区模型使用一个材质(材质的颜色随着麻醉深度的变化而变化-->在其他函数中实现)
/// 人体器官模型还是不同器官使用不同材质
/// </summary>
void AddMatToModel1()
{
List<Transform> models = new List<Transform>();
if (modelType == ModelType.brain)
{
models = DataSource.regions;
brainMat = Resources.Load<Material>("Models/Mat/BrainMat");
if(brainMat != null)
{
foreach (Transform obj in models)
{
if(obj != null)
{
obj.GetComponent<Renderer>().material = brainMat;
}
}
}
}
else
{
Dictionary<string, Material> tempMatDict = new Dictionary<string, Material>();
models = DataSource.organs;
tempMatDict = DataSource.organMatDict;
foreach (Transform obj in models)
{
if (obj != null)
{
string objName = obj.gameObject.name;
if (tempMatDict.ContainsKey(objName)) obj.GetComponent<Renderer>().material = tempMatDict[objName];
else
Debug.Log(objName + " add Mat failed, model name is not matching with ModelMaterialDict!");
}
}
}
}
#region
void InitModelsList()
{
if (modelType == ModelType.brain)
{
int regionCount = transform.childCount;
//遍历脑区模型,添加到列表中
for (int idx = 0; idx < regionCount; idx++)
{
Transform region = transform.GetChild(idx); // 获取脑区模型
DataSource.regions.Add(region);
}
}
else
{
// 人体器官模型有两层目录,第一层目录是大的器官种类,如消化系统,神经系统等
// 第二层目录是具体的器官如盲肠尿管等所以需要两层for循环来提取所有模型
int chdCount = transform.childCount;
//遍历第一层目录
for (int i = 0; i < chdCount; i++)
{
Transform curChild = transform.GetChild(i);
if (curChild.name != "Location")
{
int chdCount2 = curChild.childCount;
// 如果当前目录中包含子物体,则继续遍历目录下的子物体
if (chdCount2 > 0)
{
for (int j = 0; j < chdCount2; j++)
{
// 把提取到的模型添加到列表中
Transform curChild2 = curChild.GetChild(j);
DataSource.organs.Add(curChild2);
}
}
else
{
DataSource.organs.Add(curChild);
}
}
}
}
}
#endregion
}