Utils.cs
5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using UnityEngine;
using UnityEngine.Rendering;
namespace UniGLTF.UniUnlit
{
public enum UniUnlitRenderMode
{
Opaque = 0,
Cutout = 1,
Transparent = 2,
}
public enum UniUnlitCullMode
{
Off = 0,
// Front = 1,
Back = 2,
}
public enum UniUnlitVertexColorBlendOp
{
None = 0,
Multiply = 1,
}
public static class Utils
{
public const string PropNameMainTex = "_MainTex";
public const string PropNameColor = "_Color";
public const string PropNameCutoff = "_Cutoff";
public const string PropNameBlendMode = "_BlendMode";
public const string PropNameCullMode = "_CullMode";
public const string PropeNameVColBlendMode = "_VColBlendMode";
public const string PropNameSrcBlend = "_SrcBlend";
public const string PropNameDstBlend = "_DstBlend";
public const string PropNameZWrite = "_ZWrite";
public const string PropNameStandardShadersRenderMode = "_Mode";
public const string KeywordAlphaTestOn = "_ALPHATEST_ON";
public const string KeywordAlphaBlendOn = "_ALPHABLEND_ON";
public const string KeywordVertexColMul = "_VERTEXCOL_MUL";
public const string TagRenderTypeKey = "RenderType";
public const string TagRenderTypeValueOpaque = "Opaque";
public const string TagRenderTypeValueTransparentCutout = "TransparentCutout";
public const string TagRenderTypeValueTransparent = "Transparent";
public static void SetRenderMode(Material material, UniUnlitRenderMode mode)
{
material.SetInt(PropNameBlendMode, (int)mode);
}
public static void SetCullMode(Material material, UniUnlitCullMode mode)
{
material.SetInt(PropNameCullMode, (int) mode);
}
public static UniUnlitRenderMode GetRenderMode(Material material)
{
return (UniUnlitRenderMode)material.GetInt(PropNameBlendMode);
}
public static UniUnlitCullMode GetCullMode(Material material)
{
return (UniUnlitCullMode)material.GetInt(PropNameCullMode);
}
/// <summary>
/// Validate target material's UniUnlitRenderMode, UniUnlitVertexColorBlendOp.
/// Set appropriate hidden properites & keywords.
/// This will change RenderQueue independent to UniUnlitRenderMode if isRenderModeChagedByUser is true.
/// </summary>
/// <param name="material">Target material</param>
/// <param name="isRenderModeChangedByUser">Is changed by user</param>
public static void ValidateProperties(Material material, bool isRenderModeChangedByUser = false)
{
SetupBlendMode(material, (UniUnlitRenderMode)material.GetFloat(PropNameBlendMode),
isRenderModeChangedByUser);
SetupVertexColorBlendOp(material, (UniUnlitVertexColorBlendOp)material.GetFloat(PropeNameVColBlendMode));
}
private static void SetupBlendMode(Material material, UniUnlitRenderMode renderMode,
bool isRenderModeChangedByUser = false)
{
switch (renderMode)
{
case UniUnlitRenderMode.Opaque:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueOpaque);
material.SetInt(PropNameSrcBlend, (int)BlendMode.One);
material.SetInt(PropNameDstBlend, (int)BlendMode.Zero);
material.SetInt(PropNameZWrite, 1);
SetKeyword(material, KeywordAlphaTestOn, false);
SetKeyword(material, KeywordAlphaBlendOn, false);
if (isRenderModeChangedByUser) material.renderQueue = -1;
break;
case UniUnlitRenderMode.Cutout:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueTransparentCutout);
material.SetInt(PropNameSrcBlend, (int)BlendMode.One);
material.SetInt(PropNameDstBlend, (int)BlendMode.Zero);
material.SetInt(PropNameZWrite, 1);
SetKeyword(material, KeywordAlphaTestOn, true);
SetKeyword(material, KeywordAlphaBlendOn, false);
if (isRenderModeChangedByUser) material.renderQueue = (int)RenderQueue.AlphaTest;
break;
case UniUnlitRenderMode.Transparent:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueTransparent);
material.SetInt(PropNameSrcBlend, (int)BlendMode.SrcAlpha);
material.SetInt(PropNameDstBlend, (int)BlendMode.OneMinusSrcAlpha);
material.SetInt(PropNameZWrite, 0);
SetKeyword(material, KeywordAlphaTestOn, false);
SetKeyword(material, KeywordAlphaBlendOn, true);
if (isRenderModeChangedByUser) material.renderQueue = (int)RenderQueue.Transparent;
break;
}
}
private static void SetupVertexColorBlendOp(Material material, UniUnlitVertexColorBlendOp vColBlendOp)
{
switch (vColBlendOp)
{
case UniUnlitVertexColorBlendOp.None:
SetKeyword(material, KeywordVertexColMul, false);
break;
case UniUnlitVertexColorBlendOp.Multiply:
SetKeyword(material, KeywordVertexColMul, true);
break;
}
}
private static void SetKeyword(Material mat, string keyword, bool required)
{
if (required)
mat.EnableKeyword(keyword);
else
mat.DisableKeyword(keyword);
}
}
}