summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/VRM/VRMShaders/UniUnlit/Scripts/Utils.cs
blob: 74c474054b3dc94c83118435532fa766fdaec746 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
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 ShaderName = "UniGLTF/UniUnlit";
        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";
        [Obsolete("Use PropNameVColBlendMode")]
        public const string PropeNameVColBlendMode = PropNameVColBlendMode;
        public const string PropNameVColBlendMode = "_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 void SetVColBlendMode(Material material, UniUnlitVertexColorBlendOp mode)
        {
            material.SetInt(PropNameVColBlendMode, (int)mode);
        }

        public static UniUnlitRenderMode GetRenderMode(Material material)
        {
            return (UniUnlitRenderMode)material.GetInt(PropNameBlendMode);
        }

        public static UniUnlitCullMode GetCullMode(Material material)
        {
            return (UniUnlitCullMode)material.GetInt(PropNameCullMode);
        }

        public static UniUnlitVertexColorBlendOp GetVColBlendMode(Material material)
        {
            return (UniUnlitVertexColorBlendOp)material.GetInt(PropNameVColBlendMode);
        }

        /// <summary>
        /// Validate target material's UniUnlitRenderMode, UniUnlitVertexColorBlendOp.
        /// Set appropriate hidden properties & keywords.
        /// This will change RenderQueue independent to UniUnlitRenderMode if isRenderModeChangedByUser 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(PropNameVColBlendMode));
        }

        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);
        }
    }
}