in unity/Assets/UnityFBXExporter/FBXUnityMaterialGetter.cs [50:189]
public static void GetAllMaterialsToString(GameObject gameObj, string newPath, bool copyMaterials, bool copyTextures, out Material[] materials, out string matObjects, out string connections)
{
StringBuilder tempObjectSb = new StringBuilder();
StringBuilder tempConnectionsSb = new StringBuilder();
// Need to get all unique materials for the submesh here and then write them in
//@cartzhang modify.As meshrender and skinnedrender is same level in inherit relation shape.
// if not check,skinned render ,may lost some materials.
Renderer[] meshRenders = gameObj.GetComponentsInChildren<Renderer>();
List<Material> uniqueMaterials = new List<Material>();
// Gets all the unique materials within this GameObject Hierarchy
for(int i = 0; i < meshRenders.Length; i++)
{
for(int n = 0; n < meshRenders[i].sharedMaterials.Length; n++)
{
Material mat = meshRenders[i].sharedMaterials[n];
if(uniqueMaterials.Contains(mat) == false && mat != null)
{
uniqueMaterials.Add(mat);
}
}
}
for (int i = 0; i < uniqueMaterials.Count; i++)
{
Material mat = uniqueMaterials[i];
// We rename the material if it is being copied
string materialName = mat.name;
if(copyMaterials)
materialName = gameObj.name + "_" + mat.name;
int referenceId = Mathf.Abs(mat.GetInstanceID());
tempObjectSb.AppendLine();
tempObjectSb.AppendLine("\tMaterial: " + referenceId + ", \"Material::" + materialName + "\", \"\" {");
tempObjectSb.AppendLine("\t\tVersion: 102");
tempObjectSb.AppendLine("\t\tShadingModel: \"phong\"");
tempObjectSb.AppendLine("\t\tMultiLayer: 0");
tempObjectSb.AppendLine("\t\tProperties70: {");
tempObjectSb.AppendFormat("\t\t\tP: \"Diffuse\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}", mat.color.r, mat.color.g, mat.color.b);
tempObjectSb.AppendLine();
tempObjectSb.AppendFormat("\t\t\tP: \"DiffuseColor\", \"Color\", \"\", \"A\",{0},{1},{2}", mat.color.r, mat.color.g, mat.color.b);
tempObjectSb.AppendLine();
// TODO: Figure out if this property can be written to the FBX file
// if(mat.HasProperty("_MetallicGlossMap"))
// {
// Debug.Log("has metallic gloss map");
// Color color = mat.GetColor("_Color");
// tempObjectSb.AppendFormat("\t\t\tP: \"Specular\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}", color.r, color.g, color.r);
// tempObjectSb.AppendLine();
// tempObjectSb.AppendFormat("\t\t\tP: \"SpecularColor\", \"ColorRGB\", \"Color\", \" \",{0},{1},{2}", color.r, color.g, color.b);
// tempObjectSb.AppendLine();
// }
if(mat.HasProperty("_SpecColor"))
{
Color color = mat.GetColor("_SpecColor");
tempObjectSb.AppendFormat("\t\t\tP: \"Specular\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}", color.r, color.g, color.r);
tempObjectSb.AppendLine();
tempObjectSb.AppendFormat("\t\t\tP: \"SpecularColor\", \"ColorRGB\", \"Color\", \" \",{0},{1},{2}", color.r, color.g, color.b);
tempObjectSb.AppendLine();
}
if(mat.HasProperty("_Mode"))
{
Color color = Color.white;
switch((int)mat.GetFloat("_Mode"))
{
case 0: // Map is opaque
break;
case 1: // Map is a cutout
// TODO: Add option if it is a cutout
break;
case 2: // Map is a fade
color = mat.GetColor("_Color");
tempObjectSb.AppendFormat("\t\t\tP: \"TransparentColor\", \"Color\", \"\", \"A\",{0},{1},{2}", color.r, color.g, color.b);
tempObjectSb.AppendLine();
tempObjectSb.AppendFormat("\t\t\tP: \"Opacity\", \"double\", \"Number\", \"\",{0}", color.a);
tempObjectSb.AppendLine();
break;
case 3: // Map is transparent
color = mat.GetColor("_Color");
tempObjectSb.AppendFormat("\t\t\tP: \"TransparentColor\", \"Color\", \"\", \"A\",{0},{1},{2}", color.r, color.g, color.b);
tempObjectSb.AppendLine();
tempObjectSb.AppendFormat("\t\t\tP: \"Opacity\", \"double\", \"Number\", \"\",{0}", color.a);
tempObjectSb.AppendLine();
break;
}
}
// NOTE: Unity doesn't currently import this information (I think) from an FBX file.
if(mat.HasProperty("_EmissionColor"))
{
Color color = mat.GetColor("_EmissionColor");
tempObjectSb.AppendFormat("\t\t\tP: \"Emissive\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}", color.r, color.g, color.b);
tempObjectSb.AppendLine();
float averageColor = (color.r + color.g + color.b) / 3f;
tempObjectSb.AppendFormat("\t\t\tP: \"EmissiveFactor\", \"Number\", \"\", \"A\",{0}", averageColor);
tempObjectSb.AppendLine();
}
// TODO: Add these to the file based on their relation to the PBR files
// tempObjectSb.AppendLine("\t\t\tP: \"AmbientColor\", \"Color\", \"\", \"A\",0,0,0");
// tempObjectSb.AppendLine("\t\t\tP: \"ShininessExponent\", \"Number\", \"\", \"A\",6.31179285049438");
// tempObjectSb.AppendLine("\t\t\tP: \"Ambient\", \"Vector3D\", \"Vector\", \"\",0,0,0");
// tempObjectSb.AppendLine("\t\t\tP: \"Shininess\", \"double\", \"Number\", \"\",6.31179285049438");
// tempObjectSb.AppendLine("\t\t\tP: \"Reflectivity\", \"double\", \"Number\", \"\",0");
tempObjectSb.AppendLine("\t\t}");
tempObjectSb.AppendLine("\t}");
string textureObjects;
string textureConnections;
SerializedTextures(gameObj, newPath, mat, materialName, copyTextures, out textureObjects, out textureConnections);
tempObjectSb.Append(textureObjects);
tempConnectionsSb.Append(textureConnections);
}
materials = uniqueMaterials.ToArray<Material>();
matObjects = tempObjectSb.ToString();
connections = tempConnectionsSb.ToString();
}