summaryrefslogtreecommitdiff
path: root/Assets/Samples/XR Hands/1.6.0/HandVisualizer/Scripts/MaterialPipelineHandler.cs
blob: c673fa9de1b1fec8172e55a66b170c5cc4c7841c (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using UnityEngine.Rendering;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UnityEngine.XR.Hands.Samples.VisualizerSample
{
#if UNITY_EDITOR
    [InitializeOnLoad]
    static class RenderPipelineValidation
    {
        static RenderPipelineValidation()
        {
            foreach (var pipelineHandler in GetAllInstances())
                pipelineHandler.AutoRefreshPipelineShaders();
        }

        static List<MaterialPipelineHandler> GetAllInstances()
        {
            var instances = new List<MaterialPipelineHandler>();

            // Find all GUIDs for objects that match the type MaterialPipelineHandler
            var guids = AssetDatabase.FindAssets("t:MaterialPipelineHandler");
            for (int i = 0; i < guids.Length; i++)
            {
                string path = AssetDatabase.GUIDToAssetPath(guids[i]);
                var asset = AssetDatabase.LoadAssetAtPath<MaterialPipelineHandler>(path);
                if (asset != null)
                    instances.Add(asset);
            }

            return instances;
        }
    }
#endif

    /// <summary>
    /// Serializable class that contains the shader information for a material.
    /// </summary>
    [System.Serializable]
    public class ShaderContainer
    {
        public Material material;
        public bool useSRPShaderName = true;
        public string scriptableRenderPipelineShaderName = "Universal Render Pipeline/Lit";
        public Shader scriptableRenderPipelineShader;
        public bool useBuiltinShaderName = true;
        public string builtInPipelineShaderName = "Standard";
        public Shader builtInPipelineShader;
    }

    /// <summary>
    /// Scriptable object that allows for setting the shader on a material based on the current render pipeline.
    /// Will run automatically OnEnable in the editor to set the shaders on project bootup. Can be refreshed manually with editor button.
    /// This exists because while objects render correctly using shadergraph shaders, others do not and using the standard shader resolves various rendering issues.
    /// </summary>
    [CreateAssetMenu(fileName = "MaterialPipelineHandler", menuName = "XR/MaterialPipelineHandler", order = 0)]
    public class MaterialPipelineHandler : ScriptableObject
    {
        [SerializeField]
        [Tooltip("List of materials and their associated shaders.")]
        List<ShaderContainer> m_ShaderContainers;

        [SerializeField]
        [Tooltip("If true, the shaders will be refreshed automatically when the editor opens and when this scriptable object instance is enabled.")]
        bool m_AutoRefreshShaders = true;

#if UNITY_EDITOR
        void OnEnable()
        {
            if (Application.isPlaying)
                return;
            AutoRefreshPipelineShaders();
        }
#endif

        public void AutoRefreshPipelineShaders()
        {
            if (m_AutoRefreshShaders)
                SetPipelineShaders();
        }

        /// <summary>
        /// Applies the appropriate shader to the materials based on the current render pipeline.
        /// </summary>
        public void SetPipelineShaders()
        {
            if (m_ShaderContainers == null)
                return;

            bool isBuiltinRenderPipeline = GraphicsSettings.currentRenderPipeline == null;

            foreach (var info in m_ShaderContainers)
            {
                if (info.material == null)
                    continue;

                // Find the appropriate shaders based on the toggle
                Shader birpShader = info.useBuiltinShaderName ? Shader.Find(info.builtInPipelineShaderName) : info.builtInPipelineShader;
                Shader srpShader = info.useSRPShaderName ? Shader.Find(info.scriptableRenderPipelineShaderName) : info.scriptableRenderPipelineShader;

                // Determine current shader for comparison
                Shader currentShader = info.material.shader;

                // Update shader for the current render pipeline only if necessary
                if (isBuiltinRenderPipeline && birpShader != null && currentShader != birpShader)
                {
                    info.material.shader = birpShader;
                    MarkMaterialModified(info.material);
                }
                else if (!isBuiltinRenderPipeline && srpShader != null && currentShader != srpShader)
                {
                    info.material.shader = srpShader;
                    MarkMaterialModified(info.material);
                }
            }
        }

        static void MarkMaterialModified(Material material)
        {
#if UNITY_EDITOR
            EditorUtility.SetDirty(material);
#endif
        }
    }

#if UNITY_EDITOR
    /// <summary>
    /// Custom property drawer for the shader container class.
    /// </summary>
    [CustomPropertyDrawer(typeof(ShaderContainer))]
    public class ShaderContainerDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            float singleLineHeight = EditorGUIUtility.singleLineHeight;
            float verticalSpacing = EditorGUIUtility.standardVerticalSpacing;

            SerializedProperty materialProp = property.FindPropertyRelative("material");
            SerializedProperty useSRPShaderNameProp = property.FindPropertyRelative("useSRPShaderName");
            SerializedProperty scriptableShaderNameProp = property.FindPropertyRelative("scriptableRenderPipelineShaderName");
            SerializedProperty scriptableShaderProp = property.FindPropertyRelative("scriptableRenderPipelineShader");
            SerializedProperty useShaderNameProp = property.FindPropertyRelative("useBuiltinShaderName");
            SerializedProperty builtInNameProp = property.FindPropertyRelative("builtInPipelineShaderName");
            SerializedProperty builtInShaderProp = property.FindPropertyRelative("builtInPipelineShader");

            // Draw Material without the header.
            position.height = singleLineHeight;
            EditorGUI.PropertyField(position, materialProp);
            position.y += singleLineHeight + verticalSpacing;

            // SRP Shader header and fields.
            EditorGUI.LabelField(position, "Scriptable Render Pipeline Shader", EditorStyles.boldLabel);
            position.y += EditorGUIUtility.singleLineHeight + verticalSpacing;

            EditorGUI.PropertyField(position, useSRPShaderNameProp);
            position.y += singleLineHeight + verticalSpacing;

            if (useSRPShaderNameProp.boolValue)
            {
                EditorGUI.PropertyField(position, scriptableShaderNameProp);
                position.y += singleLineHeight + verticalSpacing;
            }
            else
            {
                EditorGUI.PropertyField(position, scriptableShaderProp);
                position.y += singleLineHeight + verticalSpacing;
            }

            // Built-in Shader header and fields.
            EditorGUI.LabelField(position, "Built-In Render Pipeline Shader", EditorStyles.boldLabel);
            position.y += singleLineHeight + verticalSpacing;

            EditorGUI.PropertyField(position, useShaderNameProp);
            position.y += singleLineHeight + verticalSpacing;

            if (useShaderNameProp.boolValue)
            {
                EditorGUI.PropertyField(position, builtInNameProp);
                position.y += singleLineHeight + verticalSpacing;
            }
            else
            {
                EditorGUI.PropertyField(position, builtInShaderProp);
                position.y += singleLineHeight + verticalSpacing;
            }

            // Draw a separator line at the end.
            position.y += verticalSpacing / 2; // Extra space for the line.
            position.height = 1;
            EditorGUI.DrawRect(new Rect(position.x, position.y, position.width, 1), Color.gray);

            EditorGUI.EndProperty();
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            const int baseFieldCount = 4; // The Material field, the two toggles, and one for an optional field.
            int extraLineCount = property.FindPropertyRelative("useBuiltinShaderName").boolValue ? 0 : 1;
            extraLineCount += property.FindPropertyRelative("useSRPShaderName").boolValue ? 0 : 1;

            float singleLineHeight = EditorGUIUtility.singleLineHeight;
            float verticalSpacing = EditorGUIUtility.standardVerticalSpacing;
            float headerHeight = EditorGUIUtility.singleLineHeight; // No longer need extra height for headers.

            // Calculate height for fields and headers
            float fieldsHeight = baseFieldCount * singleLineHeight + (baseFieldCount - 1 + extraLineCount) * verticalSpacing;

            // Allow space for header, separator line, and a bit of padding before the line.
            float headersHeight = 2 * (headerHeight + verticalSpacing);
            float separatorSpace = verticalSpacing / 2 + 1; // Additional vertical spacing and line height.

            return fieldsHeight + headersHeight + separatorSpace + singleLineHeight * 1.5f;
        }
    }

    /// <summary>
    /// Custom editor MaterialPipelineHandler
    /// </summary>
    [CustomEditor(typeof(MaterialPipelineHandler)), CanEditMultipleObjects]
    public class MaterialPipelineHandlerEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            // Draw the "Refresh Shaders" button
            if (GUILayout.Button("Refresh Shaders"))
            {
                foreach (var t in targets)
                {
                    var handler = (MaterialPipelineHandler)t;
                    handler.SetPipelineShaders();
                }
            }
        }
    }
#endif
}