namespace X10D.Unity
{
#region Using Directives
using System.Diagnostics.CodeAnalysis;
using UnityEngine;
#endregion
///
/// A set of extension methods for .
///
public static class MaterialExtensions
{
private static readonly int srcBlend = Shader.PropertyToID("_SrcBlend");
private static readonly int dstBlend = Shader.PropertyToID("_DstBlend");
private static readonly int zWrite = Shader.PropertyToID("_ZWrite");
///
/// An enumeration of blend modes.
///
public enum BlendMode
{
Opaque,
Cutout,
Fade,
Transparent
}
///
/// Changes the material's blend mode.
///
/// The material.
/// The blend mode.
[SuppressMessage("ReSharper", "StringLiteralTypo")]
public static void ChangeRenderMode(this Material material, BlendMode blendMode)
{
switch (blendMode)
{
case BlendMode.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 BlendMode.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 = 2450;
break;
case BlendMode.Fade:
material.SetInt(srcBlend, (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt(dstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt(zWrite, 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
break;
case BlendMode.Transparent:
material.SetInt(srcBlend, (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt(dstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt(zWrite, 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
break;
}
}
}
}