14 [CreateAssetMenu(menuName =
"TriLib/Mappers/Material/HDRP Material Mapper", fileName =
"HDRPMaterialMapper")]
20 public override Material
MaterialPreset => Resources.Load<Material>(
"Materials/HDRP/TriLibHDRP");
23 Resources.Load<Material>(
"Materials/HDRP/TriLibHDRPAlphaCutout");
25 public override Material
AlphaMaterialPreset2 => Resources.Load<Material>(
"Materials/HDRP/TriLibHDRPAlpha");
30 Resources.Load<Material>(
"Materials/HDRP/TriLibHDRPAlphaCutout");
33 Resources.Load<Material>(
"Materials/HDRP/TriLibHDRPAlpha");
35 public override Material
LoadingMaterial => Resources.Load<Material>(
"Materials/HDRP/TriLibHDRPLoading");
38 public override bool IsCompatible(MaterialMapperContext materialMapperContext)
44 public override void Map(MaterialMapperContext materialMapperContext)
48 CheckDiffuseColor(materialMapperContext);
49 CheckDiffuseMapTexture(materialMapperContext);
50 CheckNormalMapTexture(materialMapperContext);
51 CheckEmissionColor(materialMapperContext);
52 CheckEmissionMapTexture(materialMapperContext);
53 CheckOcclusionMapTexture(materialMapperContext);
55 if (materialMapperContext.Material.MaterialShadingSetup == MaterialShadingSetup.Specular)
57 CheckMetallicValue(materialMapperContext);
58 CheckMetallicGlossMapTexture(materialMapperContext);
59 CheckGlossinessValue(materialMapperContext);
60 CheckSpecularTexture(materialMapperContext);
64 CheckGlossinessValue(materialMapperContext);
65 CheckSpecularTexture(materialMapperContext);
66 CheckMetallicValue(materialMapperContext);
67 CheckMetallicGlossMapTexture(materialMapperContext);
70 BuildMaterial(materialMapperContext);
71 BuildHDRPMask(materialMapperContext);
74 private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
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);
83 private void ApplyDiffuseMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
86 materialMapperContext.VirtualMaterial.SetProperty(
"_BaseColorMap", texture);
89 private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
91 var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(
92 GenericMaterialProperty.Glossiness,
93 materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Glossiness));
94 materialMapperContext.VirtualMaterial.SetProperty(
"_Smoothness", value);
97 private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
99 var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(
100 GenericMaterialProperty.Metallic,
101 materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Metallic));
102 materialMapperContext.VirtualMaterial.SetProperty(
"_Metallic", value);
105 private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
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);
114 private void ApplyEmissionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
117 materialMapperContext.VirtualMaterial.SetProperty(
"_EmissiveColorMap", texture);
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));
129 materialMapperContext.VirtualMaterial.DisableKeyword(
"_EMISSIVE_COLOR_MAP");
130 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
131 MaterialGlobalIlluminationFlags.EmissiveIsBlack;
135 private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
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);
144 private void ApplyNormalMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
147 materialMapperContext.VirtualMaterial.SetProperty(
"_NormalMap", texture);
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));
158 materialMapperContext.VirtualMaterial.DisableKeyword(
"_NORMALMAP");
159 materialMapperContext.VirtualMaterial.DisableKeyword(
"_NORMALMAP_TANGENT_SPACE");
163 private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
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);
172 private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
175 ((HDRPVirtualMaterial) materialMapperContext.VirtualMaterial).SmoothnessTexture = texture;
178 private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
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);
187 private void ApplyOcclusionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
190 ((HDRPVirtualMaterial) materialMapperContext.VirtualMaterial).OcclusionTexture = texture;
193 private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
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);
202 private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType,
205 ((HDRPVirtualMaterial) materialMapperContext.VirtualMaterial).MetallicTexture = texture;
208 private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
210 var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor);
211 materialMapperContext.VirtualMaterial.SetProperty(
"_EmissiveColor", value);
212 if (value !=
Color.black)
214 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
215 MaterialGlobalIlluminationFlags.RealtimeEmissive;
216 materialMapperContext.VirtualMaterial.SetProperty(
"_EmissiveIntensity",
217 materialMapperContext.Material.GetGenericPropertyValueMultiplied(
218 GenericMaterialProperty.EmissionColor, 1f));
222 materialMapperContext.VirtualMaterial.GlobalIlluminationFlags =
223 MaterialGlobalIlluminationFlags.EmissiveIsBlack;
227 private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
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);
238 private void BuildHDRPMask(MaterialMapperContext materialMapperContext)
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)
246 materialMapperContext.UnityMaterial.DisableKeyword(
"_MASKMAP");
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;
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,
267 materialMapperContext.Context.Options.GenerateMipmaps
268 ? TextureCreationFlags.MipChain
269 : TextureCreationFlags.None)
273 materialMapperContext.Context.Allocations.Add(texture2D);
274 Graphics.CopyTexture(renderTexture, texture2D);
275 materialMapperContext.UnityMaterial.EnableKeyword(
"_MASKMAP");
276 materialMapperContext.UnityMaterial.SetTexture(
"_MaskMap", texture2D);
278 renderTexture.Release();
279 if (Application.isPlaying)
282 DestroyImmediate(material);
Represents a container to hold HDRP Material properties temporarily.
Represents a Material Mapper that converts TriLib Materials into Unity HDRP Materials.
override Material AlphaMaterialPreset2
override void Map(MaterialMapperContext materialMapperContext)
override Material SpecularAlphaMaterialPreset
override Material LoadingMaterial
inheritdoc />
override Material SpecularMaterialPreset
override Material MaterialPreset
override bool IsCompatible(MaterialMapperContext materialMapperContext)
inheritdoc />
override Material SpecularAlphaMaterialPreset2
override Material AlphaMaterialPreset
Represents the TriLib project settings provider. You can override this behavior to store the settings...
static bool GetBool(string key)