public static Material FromUnity(Scene scene, UnityEngine.Material unity_material)
{
if (shadersProperties == null)
{
TextAsset properties = Resources.Load <TextAsset>(Constants.shaderDatabase);
if (properties != null && properties.bytes.Length > 0)
{
shadersProperties = (Dictionary <int, string[]>)Module.Import.Binary.serializer.Deserialize(properties.bytes);
}
}
if (shadersProperties != null)
{
// Create new material
Material material = new Material();
// Save material options
material.name = unity_material.name;
material.shader = unity_material.shader.name;
material.renderQueue = unity_material.renderQueue;
material.hideFlags = unity_material.hideFlags;
material.globalIlluminationFlags = unity_material.globalIlluminationFlags;
material.keywords = unity_material.shaderKeywords;
material.passes = new bool[unity_material.passCount];
// Save material enabled passes
for (int i = 0; i < material.passes.Length; i++)
{
#if UNITY_5_6_OR_NEWER
material.passes[i] = unity_material.GetShaderPassEnabled(unity_material.GetPassName(i));
#else
material.passes[i] = true;
#endif
}
// Save material properties
// Range properties in Unity are f****d-up. It can be either int or float values, and their is no way to know wich one it is.
// However, if we restore them using the wrong type, we get no errors but they are (obviously) ignored...
// Therefore, we will save range as both int and float to be conservative.
FromUnityMaterialProperties(unity_material, Shaders.PropertyType.RANGE, ref material.ints, (m, p) => m.GetInt(p));
FromUnityMaterialProperties(unity_material, Shaders.PropertyType.RANGE, ref material.floats, (m, p) => m.GetFloat(p));
FromUnityMaterialProperties(unity_material, Shaders.PropertyType.FLOAT, ref material.floats, (m, p) => m.GetFloat(p));
FromUnityMaterialProperties(unity_material, Shaders.PropertyType.VECTOR, ref material.vectors, (m, p) => m.GetVector(p));
FromUnityMaterialProperties(unity_material, Shaders.PropertyType.COLOR, ref material.colors, (m, p) => m.GetColor(p));
FromUnityMaterialProperties(unity_material, Shaders.PropertyType.TEXTURE, ref material.textures, (m, p) =>
{
UnityEngine.Texture unity_tex = m.GetTexture(p);
int index = (unity_tex != null && unity_tex is Texture2D ? scene.GetUnityTextureIndex((Texture2D)unity_tex) : -1);
return(index >= 0 ? new TextureParams((uint)index, m.GetTextureOffset(p), m.GetTextureScale(p)) : null);
});
return(material);
}
return(null);
}