如何使用代码更换模型材质球的渲染模式?
众所周知,Unity的材质球有四种不同的渲染模式,在编辑器状态下,我们可以根据点击材质球上的RenderingMode来进行不同状态的选择。
渲染模式 | 意思 | 适用对象 | 说明 |
---|---|---|---|
Opaque | 不透明 | 石头 | 适用于所有不透明的物体 |
Cutout | 镂空 | 破布 | 透明度不是0%就是100%,不存在半透明的区域 |
Fade | 隐现 | 物体逐渐消失 | 与Transparent的区别为高光反射会随着透明度而消失 |
Transparent | 透明 | 玻璃 | 适用于彩色玻璃一样的半透明物体,高光反射下不会随透明而消失 |
那么,我们怎么通过代码修改它们的值呢?
- 首先我们需要创建一个枚举类型
pubLic enum RenderingMode
{
Opaque,
Cutout,
Fade,
Transparent
}
- 然后写一个更换渲染模式的方法
<summary>
///更换物体材质球染模式
/// </summary>
ii<param name="material"></param>
/// <param name="renderingMode"></param>
public static void SetMaterialRenderinglode(Material material, RenderingMode renderingMode)
{
switch (renderingMode)
case RenderingMode.Opaque:
material.SetInt("SrcBlend",(int)UnityEngine.Rendering.BlendMode.One);
material.SetInt(” DstBlend",(int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt(” Zwrite",1);
material.DisableKeyword(” ALPHATEST ON”);material.DisableKeyword(” ALPHABLEND ON");
material.DisableKeyword(” ALPHAPREMULTIPLY ON");material.renderQueue = -1;
break;
case RenderingMode.Cutout:
material.SetInt("SrcBlend",(int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("DstBlend",(int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt(” Zwrite",1);
material.EnableKeyword(” ALPHATEST ON”);
material.DisableKeyword(” ALPHABLEND ON”);
material.DisableKeyword(”ALPHAPREMULTIPLY ON");
material.renderQueue = 2458:
break;
case RenderingMode.Fade:
material.SetInt(" SrcBlend"(int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt(" DstBlend",(int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt(" Zwrite",@);
material.DisableKeyword(”ALPHATEST ON");material.EnableKeyword(” ALPHABLEND ON”);
material.DisableKeyword(”ALPHAPREMULTIPLY ON");material.renderQueue = 3008:
break;
case RenderingMode.Transparent:
material.SetInt(" SrcBlend"(int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("DstBlend",(int)UnityEngine.Rendering.BlendMode.neMinus5rcAlpha);
material.SetInt(" ZWrite",@);
material.DisableKeyword(” ALPHATEST ON”);material.DisableKeyword(” ALPHABLEND ON");
material.EnableKeyword(”ALPHAPREMULTIPLY_ON");material.renderOueue = 3008:
break;
default:
break;
}