123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- using System;
- using TriLibCore.General;
- using TriLibCore.Utils;
- using UnityEngine;
- using UnityEngine.Rendering;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- namespace TriLibCore.Mappers
- {
- /// <summary>Represents a Material Mapper that converts TriLib Materials into Unity Standard Materials.</summary>
- [Serializable]
- [CreateAssetMenu(menuName = "TriLib/Mappers/Material/Standard Material Mapper", fileName = "StandardMaterialMapper")]
- #if UNITY_EDITOR
- [InitializeOnLoad]
- #endif
- public class StandardMaterialMapper : MaterialMapper
- {
- static StandardMaterialMapper()
- {
- AddToRegisteredMappers();
- }
- [RuntimeInitializeOnLoadMethod]
- private static void AddToRegisteredMappers()
- {
- if (RegisteredMappers.Contains("StandardMaterialMapper"))
- {
- return;
- }
- RegisteredMappers.Add("StandardMaterialMapper");
- }
- public override Material MaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandard");
- public override Material AlphaMaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandardAlphaCutout");
- public override Material AlphaMaterialPreset2 => Resources.Load<Material>("Materials/Standard/TriLibStandardAlpha");
- public override Material SpecularMaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandardSpecular");
- public override Material SpecularAlphaMaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandardSpecularAlphaCutout");
- public override Material SpecularAlphaMaterialPreset2 => Resources.Load<Material>("Materials/Standard/TriLibStandardSpecularAlpha");
- public override Material LoadingMaterial => Resources.Load<Material>("Materials/Standard/TriLibStandardLoading");
- ///<inheritdoc />
- public override bool IsCompatible(MaterialMapperContext materialMapperContext)
- {
- return GraphicsSettingsUtils.IsUsingStandardPipeline;
- }
- ///<inheritdoc />
- public override void Map(MaterialMapperContext materialMapperContext)
- {
- materialMapperContext.VirtualMaterial = new VirtualMaterial();
- CheckDiffuseMapTexture(materialMapperContext);
- }
- private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
- {
- var diffuseTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseTexture);
- if (materialMapperContext.Material.HasProperty(diffuseTexturePropertyName))
- {
- LoadTexture(materialMapperContext, TextureType.Diffuse, materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName), ApplyDiffuseMapTexture);
- }
- else
- {
- ApplyDiffuseMapTexture(materialMapperContext, TextureType.Diffuse, null);
- }
- }
- private void ApplyDiffuseMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_MainTex", texture);
- CheckGlossinessValue(materialMapperContext);
- }
- private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
- {
- var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.Glossiness, materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Glossiness));
- materialMapperContext.VirtualMaterial.SetProperty("_Glossiness", value);
- CheckMetallicValue(materialMapperContext);
- }
- private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
- {
- var value = materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Metallic);
- materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
- CheckEmissionMapTexture(materialMapperContext);
- }
- private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
- {
- var emissionTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionTexture);
- if (materialMapperContext.Material.HasProperty(emissionTexturePropertyName))
- {
- LoadTexture(materialMapperContext, TextureType.Emission, materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName), ApplyEmissionMapTexture);
- }
- else
- {
- ApplyEmissionMapTexture(materialMapperContext, TextureType.Emission, null);
- }
- }
- private void ApplyEmissionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_EmissionMap", texture);
- if (texture)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
- materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
- materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
- }
- CheckNormalMapTexture(materialMapperContext);
- }
- private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
- {
- var normalMapTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalTexture);
- if (materialMapperContext.Material.HasProperty(normalMapTexturePropertyName))
- {
- LoadTexture(materialMapperContext, TextureType.NormalMap, materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName), ApplyNormalMapTexture);
- }
- else
- {
- ApplyNormalMapTexture(materialMapperContext, TextureType.NormalMap, null);
- }
- }
- private void ApplyNormalMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_BumpMap", texture);
- if (texture != null)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
- }
- CheckSpecularTexture(materialMapperContext);
- }
- private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
- {
- var specularTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularTexture);
- if (materialMapperContext.Material.HasProperty(specularTexturePropertyName))
- {
- LoadTexture(materialMapperContext, TextureType.Specular, materialMapperContext.Material.GetTextureValue(specularTexturePropertyName), ApplySpecGlossMapTexture);
- }
- else
- {
- ApplySpecGlossMapTexture(materialMapperContext, TextureType.Specular, null);
- }
- }
- private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_SpecGlossMap", texture);
- if (texture != null)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_SPECGLOSSMAP");
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_SPECGLOSSMAP");
- }
- CheckOcclusionMapTexture(materialMapperContext);
- }
- private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
- {
- var occlusionMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionTexture);
- if (materialMapperContext.Material.HasProperty(occlusionMapTextureName))
- {
- LoadTexture(materialMapperContext, TextureType.Occlusion, materialMapperContext.Material.GetTextureValue(occlusionMapTextureName), ApplyOcclusionMapTexture);
- }
- else
- {
- ApplyOcclusionMapTexture(materialMapperContext, TextureType.Occlusion, null);
- }
- }
- private void ApplyOcclusionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_OcclusionMap", texture);
- if (texture != null)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_OCCLUSIONMAP");
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_OCCLUSIONMAP");
- }
- CheckParallaxMapTexture(materialMapperContext);
- }
- private void CheckParallaxMapTexture(MaterialMapperContext materialMapperContext)
- {
- var parallaxMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.ParallaxMap);
- if (materialMapperContext.Material.HasProperty(parallaxMapTextureName))
- {
- LoadTexture(materialMapperContext, TextureType.Parallax, materialMapperContext.Material.GetTextureValue(parallaxMapTextureName), ApplyParallaxMapTexture);
- }
- else
- {
- ApplyParallaxMapTexture(materialMapperContext, TextureType.Parallax, null);
- }
- }
- private void ApplyParallaxMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_ParallaxMap", texture);
- if (texture)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_PARALLAXMAP");
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_PARALLAXMAP");
- }
- CheckMetallicGlossMapTexture(materialMapperContext);
- }
- private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
- {
- var metallicGlossMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicGlossMap);
- if (materialMapperContext.Material.HasProperty(metallicGlossMapTextureName))
- {
- LoadTexture(materialMapperContext, TextureType.Metalness, materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName), ApplyMetallicGlossMapTexture);
- }
- else
- {
- ApplyMetallicGlossMapTexture(materialMapperContext, TextureType.Metalness, null);
- }
- }
- private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
- {
- materialMapperContext.VirtualMaterial.SetProperty("_MetallicGlossMap", texture);
- if (texture != null)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_METALLICGLOSSMAP");
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_METALLICGLOSSMAP");
- }
- CheckEmissionColor(materialMapperContext);
- }
- private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
- {
- var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.EmissionColor, 1f);
- materialMapperContext.VirtualMaterial.SetProperty("_EmissionColor", value);
- if (value != Color.black)
- {
- materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
- materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
- }
- else
- {
- materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
- materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
- }
- CheckDiffuseColor(materialMapperContext);
- }
- private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
- {
- var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.DiffuseColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.DiffuseColor, 1f);
- value.a *= materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.AlphaValue);
- if (!materialMapperContext.VirtualMaterial.HasAlpha && value.a < 1f)
- {
- materialMapperContext.VirtualMaterial.HasAlpha = true;
- }
- materialMapperContext.VirtualMaterial.SetProperty("_Color", value);
- BuildMaterial(materialMapperContext);
- }
- }
- }
|