1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 05:15:43 +00:00
X10D/X10D.Unity/MaterialExtensions.cs

83 lines
3.3 KiB
C#
Raw Normal View History

2019-07-07 12:23:48 +01:00
namespace X10D.Unity
{
#region Using Directives
using System.Diagnostics.CodeAnalysis;
using UnityEngine;
#endregion
/// <summary>
/// A set of extension methods for <see cref="Material"/>.
/// </summary>
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");
/// <summary>
/// An enumeration of blend modes.
/// </summary>
public enum BlendMode
{
Opaque,
Cutout,
Fade,
Transparent
}
/// <summary>
/// Changes the material's blend mode.
/// </summary>
/// <param name="material">The material.</param>
/// <param name="blendMode">The blend mode.</param>
[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;
}
}
}
}