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

150 lines
4.7 KiB
C#
Raw Permalink 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 System.IO;
using UnityEngine;
#region
/*****************************************************************
ForScripts
*****************************************************************/
#endregion
public class ProcessModelMatData : MonoBehaviour
{
//string regionMatDir = @"Assets\Resources\Models\Mat\Brain\";
//string organMatDir = @"Assets\Resources\Models\Mat\BodyAndOrgans\";
public List<Material> regionMat = new List<Material>();
public List<Material> organMat = new List<Material>();
private void Awake()
{
InitModelMatDict(ModelType.brain, regionMat);
InitModelMatDict(ModelType.organ, organMat);
}
private void Start()
{
SetMatColor();
}
void InitModelMatDict(ModelType type, List<Material> matList)
{
Dictionary<string, Material> tempDict = new Dictionary<string, Material>();
foreach (Material mat in matList)
{
string matName = mat.name;
tempDict[matName] = mat;
}
if (type == ModelType.brain)
DataSource.regionMatDict = tempDict;
else
DataSource.organMatDict = tempDict;
//Debug.Log(tempDict.Count);
}
public void SetMatColor()
{
// 设置脑区材质颜色
float brainAlpha = 0.75f;
foreach (Material mat in regionMat)
{
string matName = mat.name;
if (DataSource.regionColorDict.ContainsKey(matName))
{
Vector3 matRGB = DataSource.regionColorDict[matName];
Color matColor = new Color(matRGB.x, matRGB.y, matRGB.z, brainAlpha);
mat.color = matColor;
}
else
{
Debug.Log("material name: " + matName + " is not contained in keys list of " + "regionColorDict.");
}
}
// 设置器官材质颜色
foreach (Material mat in organMat)
{
string matName = mat.name;
if (DataSource.organColorDict.ContainsKey(matName))
{
// 为每个器官设置独立的透明度
float organAlpha = 1.0f; // 默认不透明
if (matName == "Body")
{
organAlpha = 0.6f; // Body设置为60%透明度比原来的40%更清晰
}
Vector3 matRGB = DataSource.organColorDict[matName];
Color matColor = new Color(matRGB.x, matRGB.y, matRGB.z, organAlpha);
mat.color = matColor;
// 根据材质名称设置合适的渲染模式
if (matName == "Body")
{
// Body使用特殊的渲染模式确保单独显示时也能看到
MaterialRenderModeHelper.SetBodyRenderMode(mat, organAlpha);
}
else
{
// 其他器官使用标准渲染模式
MaterialRenderModeHelper.AutoSetRenderMode(mat, organAlpha);
}
}
else
{
Debug.Log("material name: " + matName + " is not contained in keys list of " + "organColorDict.");
}
}
}
/// <summary>
/// 动态调整Body材质的透明度
/// </summary>
/// <param name="alpha">透明度值范围0-10为完全透明1为完全不透明</param>
public void SetBodyAlpha(float alpha)
{
alpha = Mathf.Clamp01(alpha); // 确保值在0-1范围内
foreach (Material mat in organMat)
{
string matName = mat.name;
if (matName == "Body" && DataSource.organColorDict.ContainsKey(matName))
{
Vector3 matRGB = DataSource.organColorDict[matName];
Color currentColor = mat.color;
// 保持当前颜色,只更改透明度
mat.color = new Color(currentColor.r, currentColor.g, currentColor.b, alpha);
// 为Body使用特殊的渲染模式确保单独显示时也能看到
MaterialRenderModeHelper.SetBodyRenderMode(mat, alpha);
break;
}
}
}
/// <summary>
/// 获取当前Body材质的透明度
/// </summary>
/// <returns>当前Body的透明度值</returns>
public float GetBodyAlpha()
{
foreach (Material mat in organMat)
{
string matName = mat.name;
if (matName == "Body")
{
return mat.color.a;
}
}
return 1.0f; // 如果未找到Body材质返回默认值
}
}