Tanoda
UniversalRPMaterialMapper.cs
Go to the documentation of this file.
1using System;
2using TriLibCore.General;
4using UnityEngine;
5#if UNITY_EDITOR
6using UnityEditor;
7
8#endif
10{
12 [Serializable]
13 [CreateAssetMenu(menuName = "TriLib/Mappers/Material/Universal RP Material Mapper",
14 fileName = "UniversalRPMaterialMapper")]
15#if UNITY_EDITOR
16 [InitializeOnLoad]
17#endif
18 public class UniversalRPMaterialMapper : MaterialMapper
19 {
20 public override Material MaterialPreset => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRP");
21
22 public override Material AlphaMaterialPreset =>
23 Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlphaCutout");
24
25 public override Material AlphaMaterialPreset2 =>
26 Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlpha");
27
28 public override Material SpecularMaterialPreset =>
29 Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPSpecular");
30
31 public override Material SpecularAlphaMaterialPreset =>
32 Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlphaCutoutSpecular");
33
34 public override Material SpecularAlphaMaterialPreset2 =>
35 Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlphaSpecular");
36
37 public override Material LoadingMaterial =>
38 Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPLoading");
39
41 public override bool IsCompatible(MaterialMapperContext materialMapperContext)
42 {
43 return TriLibSettings.GetBool("UniversalRPMaterialMapper");
44 }
45
47 public override void Map(MaterialMapperContext materialMapperContext)
48 {
49 materialMapperContext.VirtualMaterial = new VirtualMaterial();
50
51 CheckDiffuseColor(materialMapperContext);
52 CheckDiffuseMapTexture(materialMapperContext);
53 CheckNormalMapTexture(materialMapperContext);
54 CheckEmissionColor(materialMapperContext);
55 CheckEmissionMapTexture(materialMapperContext);
56 CheckOcclusionMapTexture(materialMapperContext);
57 CheckParallaxMapTexture(materialMapperContext);
58
59 if (materialMapperContext.Material.MaterialShadingSetup == MaterialShadingSetup.Specular)
60 {
61 CheckMetallicValue(materialMapperContext);
62 CheckMetallicGlossMapTexture(materialMapperContext);
63 CheckGlossinessValue(materialMapperContext);
64 CheckSpecularTexture(materialMapperContext);
65 }
66 else
67 {
68 CheckGlossinessValue(materialMapperContext);
69 CheckSpecularTexture(materialMapperContext);
70 CheckMetallicValue(materialMapperContext);
71 CheckMetallicGlossMapTexture(materialMapperContext);
72 }
73
74 BuildMaterial(materialMapperContext);
75 }
76
77 private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
78 {
79 var diffuseTexturePropertyName =
80 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseTexture);
81 var texture = LoadTexture(materialMapperContext, TextureType.Diffuse,
82 materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName));
83 ApplyDiffuseMapTexture(materialMapperContext, TextureType.Diffuse, texture);
84 }
85
86 private void ApplyDiffuseMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
87 Texture texture)
88 {
89 materialMapperContext.VirtualMaterial.SetProperty("_BaseMap", texture);
90 }
91
92 private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
93 {
94 var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(
95 GenericMaterialProperty.Glossiness,
96 materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Glossiness));
97 materialMapperContext.VirtualMaterial.SetProperty("_Smoothness", value);
98 }
99
100 private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
101 {
102 var value = materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Metallic);
103 materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
104 }
105
106 private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
107 {
108 var emissionTexturePropertyName =
109 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionTexture);
110 var texture = LoadTexture(materialMapperContext, TextureType.Emission,
111 materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName));
112 ApplyEmissionMapTexture(materialMapperContext, TextureType.Emission, texture);
113 }
114
115 private void ApplyEmissionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
116 Texture texture)
117 {
118 materialMapperContext.VirtualMaterial.SetProperty("_EmissionMap", texture);
119 if (texture)
120 {
121 materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
122 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
123 MaterialGlobalIlluminationFlags.RealtimeEmissive;
124 }
125 else
126 {
127 materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
128 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
129 MaterialGlobalIlluminationFlags.EmissiveIsBlack;
130 }
131 }
132
133 private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
134 {
135 var normalMapTexturePropertyName =
136 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalTexture);
137 var texture = LoadTexture(materialMapperContext, TextureType.NormalMap,
138 materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName));
139 ApplyNormalMapTexture(materialMapperContext, TextureType.NormalMap, texture);
140 }
141
142 private void ApplyNormalMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
143 Texture texture)
144 {
145 materialMapperContext.VirtualMaterial.SetProperty("_BumpMap", texture);
146 if (texture != null)
147 {
148 materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
149 materialMapperContext.VirtualMaterial.SetProperty("_NormalScale",
150 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
151 GenericMaterialProperty.NormalTexture, 1f));
152 }
153 else
154 {
155 materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
156 }
157 }
158
159 private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
160 {
161 var specularTexturePropertyName =
162 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularTexture);
163 var texture = LoadTexture(materialMapperContext, TextureType.Specular,
164 materialMapperContext.Material.GetTextureValue(specularTexturePropertyName));
165 ApplySpecGlossMapTexture(materialMapperContext, TextureType.Specular, texture);
166 }
167
168 private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
169 Texture texture)
170 {
171 materialMapperContext.VirtualMaterial.SetProperty("_SpecGlossMap", texture);
172 if (texture != null)
173 materialMapperContext.VirtualMaterial.EnableKeyword("_METALLICSPECGLOSSMAP");
174 else
175 materialMapperContext.VirtualMaterial.DisableKeyword("_METALLICSPECGLOSSMAP");
176 }
177
178 private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
179 {
180 var occlusionMapTextureName =
181 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionTexture);
182 var texture = LoadTexture(materialMapperContext, TextureType.Occlusion,
183 materialMapperContext.Material.GetTextureValue(occlusionMapTextureName));
184 ApplyOcclusionMapTexture(materialMapperContext, TextureType.Occlusion, texture);
185 }
186
187 private void ApplyOcclusionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
188 Texture texture)
189 {
190 materialMapperContext.VirtualMaterial.SetProperty("_OcclusionMap", texture);
191 if (texture != null)
192 materialMapperContext.VirtualMaterial.EnableKeyword("_OCCLUSIONMAP");
193 else
194 materialMapperContext.VirtualMaterial.DisableKeyword("_OCCLUSIONMAP");
195 }
196
197 private void CheckParallaxMapTexture(MaterialMapperContext materialMapperContext)
198 {
199 var parallaxMapTextureName =
200 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.ParallaxMap);
201 var texture = LoadTexture(materialMapperContext, TextureType.Parallax,
202 materialMapperContext.Material.GetTextureValue(parallaxMapTextureName));
203 ApplyParallaxMapTexture(materialMapperContext, TextureType.Parallax, texture);
204 }
205
206 private void ApplyParallaxMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
207 Texture texture)
208 {
209 materialMapperContext.VirtualMaterial.SetProperty("_ParallaxMap", texture);
210 if (texture)
211 materialMapperContext.VirtualMaterial.EnableKeyword("_PARALLAXMAP");
212 else
213 materialMapperContext.VirtualMaterial.DisableKeyword("_PARALLAXMAP");
214 }
215
216 private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
217 {
218 var metallicGlossMapTextureName =
219 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicGlossMap);
220 var texture = LoadTexture(materialMapperContext, TextureType.Metalness,
221 materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName));
222 ApplyMetallicGlossMapTexture(materialMapperContext, TextureType.Metalness, texture);
223 }
224
225 private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
226 Texture texture)
227 {
228 materialMapperContext.VirtualMaterial.SetProperty("_MetallicGlossMap", texture);
229 if (texture != null)
230 materialMapperContext.VirtualMaterial.EnableKeyword("_METALLICGLOSSMAP");
231 else
232 materialMapperContext.VirtualMaterial.DisableKeyword("_METALLICGLOSSMAP");
233 }
234
235 private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
236 {
237 var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor) *
238 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
239 GenericMaterialProperty.EmissionColor, 1f);
240 materialMapperContext.VirtualMaterial.SetProperty("_EmissionColor", value);
241 if (value != Color.black)
242 {
243 materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
244 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
245 MaterialGlobalIlluminationFlags.RealtimeEmissive;
246 }
247 else
248 {
249 materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
250 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
251 MaterialGlobalIlluminationFlags.EmissiveIsBlack;
252 }
253 }
254
255 private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
256 {
257 var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.DiffuseColor) *
258 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
259 GenericMaterialProperty.DiffuseColor, 1f);
260 value.a *= materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.AlphaValue);
261 if (!materialMapperContext.VirtualMaterial.HasAlpha && value.a < 1f)
262 materialMapperContext.VirtualMaterial.HasAlpha = true;
263 materialMapperContext.VirtualMaterial.SetProperty("_BaseColor", value);
264 }
265 }
266}
UnityEngine.Color Color
Definition: TestScript.cs:32
Represents the TriLib project settings provider. You can override this behavior to store the settings...
static bool GetBool(string key)
Represents a Material Mapper that converts TriLib Materials into Unity UniversalRP Materials.
override bool IsCompatible(MaterialMapperContext materialMapperContext)
inheritdoc />
override void Map(MaterialMapperContext materialMapperContext)