Tanoda
HDRPMaterialMapper.cs
Go to the documentation of this file.
1using System;
2using TriLibCore.General;
4using UnityEngine;
5using UnityEngine.Experimental.Rendering;
6#if UNITY_EDITOR
7using UnityEditor;
8
9#endif
11{
13 [Serializable]
14 [CreateAssetMenu(menuName = "TriLib/Mappers/Material/HDRP Material Mapper", fileName = "HDRPMaterialMapper")]
15#if UNITY_EDITOR
16 [InitializeOnLoad]
17#endif
18 public class HDRPMaterialMapper : MaterialMapper
19 {
20 public override Material MaterialPreset => Resources.Load<Material>("Materials/HDRP/TriLibHDRP");
21
22 public override Material AlphaMaterialPreset =>
23 Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlphaCutout");
24
25 public override Material AlphaMaterialPreset2 => Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlpha");
26
27 public override Material SpecularMaterialPreset => Resources.Load<Material>("Materials/HDRP/TriLibHDRP");
28
29 public override Material SpecularAlphaMaterialPreset =>
30 Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlphaCutout");
31
32 public override Material SpecularAlphaMaterialPreset2 =>
33 Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlpha");
34
35 public override Material LoadingMaterial => Resources.Load<Material>("Materials/HDRP/TriLibHDRPLoading");
36
38 public override bool IsCompatible(MaterialMapperContext materialMapperContext)
39 {
40 return TriLibSettings.GetBool("HDRPMaterialMapper");
41 }
42
44 public override void Map(MaterialMapperContext materialMapperContext)
45 {
46 materialMapperContext.VirtualMaterial = new HDRPVirtualMaterial();
47
48 CheckDiffuseColor(materialMapperContext);
49 CheckDiffuseMapTexture(materialMapperContext);
50 CheckNormalMapTexture(materialMapperContext);
51 CheckEmissionColor(materialMapperContext);
52 CheckEmissionMapTexture(materialMapperContext);
53 CheckOcclusionMapTexture(materialMapperContext);
54
55 if (materialMapperContext.Material.MaterialShadingSetup == MaterialShadingSetup.Specular)
56 {
57 CheckMetallicValue(materialMapperContext);
58 CheckMetallicGlossMapTexture(materialMapperContext);
59 CheckGlossinessValue(materialMapperContext);
60 CheckSpecularTexture(materialMapperContext);
61 }
62 else
63 {
64 CheckGlossinessValue(materialMapperContext);
65 CheckSpecularTexture(materialMapperContext);
66 CheckMetallicValue(materialMapperContext);
67 CheckMetallicGlossMapTexture(materialMapperContext);
68 }
69
70 BuildMaterial(materialMapperContext);
71 BuildHDRPMask(materialMapperContext);
72 }
73
74 private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
75 {
76 var diffuseTexturePropertyName =
77 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseTexture);
78 var texture = LoadTexture(materialMapperContext, TextureType.Diffuse,
79 materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName));
80 ApplyDiffuseMapTexture(materialMapperContext, TextureType.Diffuse, texture);
81 }
82
83 private void ApplyDiffuseMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
84 Texture texture)
85 {
86 materialMapperContext.VirtualMaterial.SetProperty("_BaseColorMap", texture);
87 }
88
89 private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
90 {
91 var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(
92 GenericMaterialProperty.Glossiness,
93 materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Glossiness));
94 materialMapperContext.VirtualMaterial.SetProperty("_Smoothness", value);
95 }
96
97 private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
98 {
99 var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(
100 GenericMaterialProperty.Metallic,
101 materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Metallic));
102 materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
103 }
104
105 private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
106 {
107 var emissionTexturePropertyName =
108 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionTexture);
109 var texture = LoadTexture(materialMapperContext, TextureType.Emission,
110 materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName));
111 ApplyEmissionMapTexture(materialMapperContext, TextureType.Emission, texture);
112 }
113
114 private void ApplyEmissionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
115 Texture texture)
116 {
117 materialMapperContext.VirtualMaterial.SetProperty("_EmissiveColorMap", texture);
118 if (texture)
119 {
120 materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSIVE_COLOR_MAP");
121 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
122 MaterialGlobalIlluminationFlags.RealtimeEmissive;
123 materialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity",
124 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
125 GenericMaterialProperty.EmissionColor, 1f));
126 }
127 else
128 {
129 materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSIVE_COLOR_MAP");
130 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
131 MaterialGlobalIlluminationFlags.EmissiveIsBlack;
132 }
133 }
134
135 private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
136 {
137 var normalMapTexturePropertyName =
138 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalTexture);
139 var texture = LoadTexture(materialMapperContext, TextureType.NormalMap,
140 materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName));
141 ApplyNormalMapTexture(materialMapperContext, TextureType.NormalMap, texture);
142 }
143
144 private void ApplyNormalMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
145 Texture texture)
146 {
147 materialMapperContext.VirtualMaterial.SetProperty("_NormalMap", texture);
148 if (texture != null)
149 {
150 materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
151 materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
152 materialMapperContext.VirtualMaterial.SetProperty("_NormalScale",
153 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
154 GenericMaterialProperty.NormalTexture, 1f));
155 }
156 else
157 {
158 materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
159 materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP_TANGENT_SPACE");
160 }
161 }
162
163 private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
164 {
165 var specularTexturePropertyName =
166 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularTexture);
167 var texture = LoadTexture(materialMapperContext, TextureType.Specular,
168 materialMapperContext.Material.GetTextureValue(specularTexturePropertyName));
169 ApplySpecGlossMapTexture(materialMapperContext, TextureType.Specular, texture);
170 }
171
172 private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
173 Texture texture)
174 {
175 ((HDRPVirtualMaterial) materialMapperContext.VirtualMaterial).SmoothnessTexture = texture;
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 ((HDRPVirtualMaterial) materialMapperContext.VirtualMaterial).OcclusionTexture = texture;
191 }
192
193 private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
194 {
195 var metallicGlossMapTextureName =
196 materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicGlossMap);
197 var texture = LoadTexture(materialMapperContext, TextureType.Metalness,
198 materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName));
199 ApplyMetallicGlossMapTexture(materialMapperContext, TextureType.Metalness, texture);
200 }
201
202 private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
203 Texture texture)
204 {
205 ((HDRPVirtualMaterial) materialMapperContext.VirtualMaterial).MetallicTexture = texture;
206 }
207
208 private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
209 {
210 var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor);
211 materialMapperContext.VirtualMaterial.SetProperty("_EmissiveColor", value);
212 if (value != Color.black)
213 {
214 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
215 MaterialGlobalIlluminationFlags.RealtimeEmissive;
216 materialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity",
217 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
218 GenericMaterialProperty.EmissionColor, 1f));
219 }
220 else
221 {
222 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
223 MaterialGlobalIlluminationFlags.EmissiveIsBlack;
224 }
225 }
226
227 private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
228 {
229 var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.DiffuseColor) *
230 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
231 GenericMaterialProperty.DiffuseColor, 1f);
232 value.a *= materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.AlphaValue);
233 if (!materialMapperContext.VirtualMaterial.HasAlpha && value.a < 1f)
234 materialMapperContext.VirtualMaterial.HasAlpha = true;
235 materialMapperContext.VirtualMaterial.SetProperty("_BaseColor", value);
236 }
237
238 private void BuildHDRPMask(MaterialMapperContext materialMapperContext)
239 {
240 if (materialMapperContext.UnityMaterial == null) return;
241 var hdrpVirtualMaterial = (HDRPVirtualMaterial) materialMapperContext.VirtualMaterial;
242 var maskBaseTexture = hdrpVirtualMaterial.MetallicTexture ?? hdrpVirtualMaterial.OcclusionTexture ??
243 hdrpVirtualMaterial.DetailMaskTexture ?? hdrpVirtualMaterial.SmoothnessTexture;
244 if (maskBaseTexture == null)
245 {
246 materialMapperContext.UnityMaterial.DisableKeyword("_MASKMAP");
247 return;
248 }
249
250 var material = new Material(Shader.Find("Hidden/TriLib/BuildHDRPMask"));
251 material.SetTexture("_MetallicTex", hdrpVirtualMaterial.MetallicTexture);
252 material.SetTexture("_OcclusionTex", hdrpVirtualMaterial.OcclusionTexture);
253 material.SetTexture("_DetailMaskTex", hdrpVirtualMaterial.DetailMaskTexture);
254 material.SetTexture("_SmoothnessTex", hdrpVirtualMaterial.SmoothnessTexture);
255 var graphicsFormat = materialMapperContext.Context.Options.Enforce16BitsTextures
256 ? GraphicsFormat.R16G16B16A16_SFloat
257 : GraphicsFormat.R8G8B8A8_SRGB;
258 var renderTexture =
259 RenderTexture.GetTemporary(maskBaseTexture.width, maskBaseTexture.height, 0, graphicsFormat);
260 renderTexture.useMipMap = true;
261 renderTexture.autoGenerateMips = false;
262 Graphics.Blit(null, renderTexture, material);
263 var texture2D = new Texture2D(
264 maskBaseTexture.width,
265 maskBaseTexture.height,
266 graphicsFormat,
267 materialMapperContext.Context.Options.GenerateMipmaps
268 ? TextureCreationFlags.MipChain
269 : TextureCreationFlags.None)
270 {
271 name = "Mask"
272 };
273 materialMapperContext.Context.Allocations.Add(texture2D);
274 Graphics.CopyTexture(renderTexture, texture2D);
275 materialMapperContext.UnityMaterial.EnableKeyword("_MASKMAP");
276 materialMapperContext.UnityMaterial.SetTexture("_MaskMap", texture2D);
277 Graphics.SetRenderTarget(null);
278 renderTexture.Release();
279 if (Application.isPlaying)
280 Destroy(material);
281 else
282 DestroyImmediate(material);
283 }
284 }
285}
UnityEngine.Color Color
Definition: TestScript.cs:32
Represents a container to hold HDRP Material properties temporarily.
Represents a Material Mapper that converts TriLib Materials into Unity HDRP Materials.
override void Map(MaterialMapperContext materialMapperContext)
override Material LoadingMaterial
inheritdoc />
override bool IsCompatible(MaterialMapperContext materialMapperContext)
inheritdoc />
Represents the TriLib project settings provider. You can override this behavior to store the settings...
static bool GetBool(string key)