-
Notifications
You must be signed in to change notification settings - Fork 66
/
ShaderUtils.cs
172 lines (161 loc) · 7.1 KB
/
ShaderUtils.cs
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
using System;
using UnityEngine;
using UnityEditor;
using ShaderPathID = UnityEngine.Rendering.Universal.ShaderPathID;
using UnityEditor.ShaderGraph;
using UnityEditor.Rendering.Universal.ShaderGraph;
using UnityEditor.Rendering.Universal.ShaderGUI;
namespace Unity.Rendering.Universal
{
/// <summary>
/// Various utility functions for shaders in URP.
/// </summary>
public static class ShaderUtils
{
internal enum ShaderID
{
Unknown = -1,
Lit = ShaderPathID.Lit,
SimpleLit = ShaderPathID.SimpleLit,
Unlit = ShaderPathID.Unlit,
TerrainLit = ShaderPathID.TerrainLit,
ParticlesLit = ShaderPathID.ParticlesLit,
ParticlesSimpleLit = ShaderPathID.ParticlesSimpleLit,
ParticlesUnlit = ShaderPathID.ParticlesUnlit,
BakedLit = ShaderPathID.BakedLit,
SpeedTree7 = ShaderPathID.SpeedTree7,
SpeedTree7Billboard = ShaderPathID.SpeedTree7Billboard,
SpeedTree8 = ShaderPathID.SpeedTree8,
SpeedTree9 = ShaderPathID.SpeedTree9,
ComplexLit = ShaderPathID.ComplexLit,
// ShaderGraph IDs start at 1000, correspond to subtargets
SG_Unlit = 1000, // UniversalUnlitSubTarget
SG_Lit, // UniversalLitSubTarget
SG_Decal, // UniversalDecalSubTarget
SG_SpriteUnlit, // UniversalSpriteUnlitSubTarget
SG_SpriteLit, // UniversalSpriteLitSubTarget
SG_SpriteCustomLit, // UniversalSpriteCustomLitSubTarget
SG_SixWaySmokeLit // UniversalSixWaySubTarget
}
internal static bool IsShaderGraph(this ShaderID id)
{
return ((int)id >= 1000);
}
// NOTE: this won't work for non-Asset shaders... (i.e. shadergraph preview shaders)
internal static ShaderID GetShaderID(Shader shader)
{
if (shader.IsShaderGraphAsset())
{
UniversalMetadata meta;
if (!shader.TryGetMetadataOfType<UniversalMetadata>(out meta))
return ShaderID.Unknown;
return meta.shaderID;
}
else
{
ShaderPathID pathID = UnityEngine.Rendering.Universal.ShaderUtils.GetEnumFromPath(shader.name);
return (ShaderID)pathID;
}
}
internal static bool HasMotionVectorLightModeTag(ShaderID id)
{
// Currently only these ShaderIDs have a pass with a { "LightMode" = "MotionVectors" } tag in URP
// (this is a more efficient check than looping over all sub-shaders and their passes and checking the
// "LightMode" tag value with FindPassTagValue)
switch(id)
{
case ShaderID.Lit:
case ShaderID.Unlit:
case ShaderID.SimpleLit:
case ShaderID.ComplexLit:
case ShaderID.BakedLit:
case ShaderID.SG_Unlit:
case ShaderID.SG_Lit:
return true;
}
return false;
}
internal enum MaterialUpdateType
{
CreatedNewMaterial,
ChangedAssignedShader,
ModifiedShader,
ModifiedMaterial
}
//Helper used by VFX, allow retrieval of ShaderID on another object than material.shader
//In case of ShaderGraph integration, the material.shader is actually pointing to VisualEffectAsset
internal static void UpdateMaterial(Material material, MaterialUpdateType updateType, UnityEngine.Object assetWithURPMetaData)
{
var currentShaderId = ShaderUtils.ShaderID.Unknown;
if (assetWithURPMetaData != null)
{
var path = AssetDatabase.GetAssetPath(assetWithURPMetaData);
foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(path))
{
if (asset is UniversalMetadata metadataAsset)
{
currentShaderId = metadataAsset.shaderID;
break;
}
}
}
UpdateMaterial(material, updateType, currentShaderId);
}
// this is used to update a material's keywords, applying any shader-associated logic to update dependent properties and keywords
// this is also invoked when a material is created, modified, or the material's shader is modified or reassigned
internal static void UpdateMaterial(Material material, MaterialUpdateType updateType, ShaderID shaderID = ShaderID.Unknown)
{
// if unknown, look it up from the material's shader
// NOTE: this will only work for asset-based shaders..
if (shaderID == ShaderID.Unknown)
shaderID = GetShaderID(material.shader);
switch (shaderID)
{
case ShaderID.Lit:
LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
break;
case ShaderID.SimpleLit:
SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
break;
case ShaderID.Unlit:
UnlitShader.SetMaterialKeywords(material);
break;
case ShaderID.ParticlesLit:
ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
break;
case ShaderID.ParticlesSimpleLit:
ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
break;
case ShaderID.ParticlesUnlit:
ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords);
break;
case ShaderID.SpeedTree8:
ShaderGraphLitGUI.UpdateMaterial(material, updateType);
break;
case ShaderID.SpeedTree9:
ShaderGraphLitGUI.UpdateMaterial(material, updateType);
break;
case ShaderID.SG_Lit:
ShaderGraphLitGUI.UpdateMaterial(material, updateType);
break;
case ShaderID.SG_Unlit:
ShaderGraphUnlitGUI.UpdateMaterial(material, updateType);
break;
case ShaderID.SG_Decal:
break;
case ShaderID.SG_SpriteUnlit:
break;
case ShaderID.SG_SpriteLit:
break;
case ShaderID.SG_SpriteCustomLit:
break;
case ShaderID.SG_SixWaySmokeLit:
ShaderGraphLitGUI.UpdateMaterial(material, updateType);
SixWayGUI.UpdateSixWayKeywords(material);
break;
default:
break;
}
}
}
}