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