Ejemplo n.º 1
0
            static void ApplyMaterialProperties(Material mat, NeoFurConfig config)
            {
                Type shaderParameterType = typeof(ShaderParameters);

                FieldInfo[] shaderFields = shaderParameterType.GetFields();

                string unityMappedFieldName;

                foreach (FieldInfo field in shaderFields)
                {
                    unityMappedFieldName = field.Name;

                    // handle special cases here until things are standardized between Unity and Unreal versions
                    // check for uv scale
                    // check for color maps since we let the user pick two (one for tip and root each) instead of one for both tip and root

                    if (unityMappedFieldName == "HMPMap")
                    {
                        unityMappedFieldName = "HeightMapUND";
                    }
                    if (unityMappedFieldName == "GradientMapScatter")
                    {
                        unityMappedFieldName = "GradientScatterMapUND";
                    }

                    string propertyName = "_" + unityMappedFieldName;

                    if (!mat.HasProperty(propertyName) && !unityMappedFieldName.Contains("_UVScale") &&
                        propertyName != "_ColorMapUND" && propertyName != "_ColorMapOVR")
                    {
                        LogWarning("Trying to set property " + propertyName +
                                   " but material doesnt have that property");

                        continue;
                    }

                    // get the base type value from the config field
                    object configValue = field.GetValue(config.Shader);

                    if (configValue == null)
                    {
                        LogWarning("Null config value: " + unityMappedFieldName + " Type: " + field.FieldType);
                        continue;
                    }

                    // set texture properties
                    if (field.FieldType == typeof(string))
                    {
                        string fileName = (string)configValue;

                        if (fileName.StartsWith("./"))
                        {
                            fileName = fileName.Substring(fileName.IndexOf("/"));
                        }

                        string extension = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();

                        // check the file extension
                        if (extension == "tga" || extension == "png" || extension == "jpg")
                        {
                            string assetPath = m_currentFolder + fileName;

                            //get texture
                            Texture2D value = (Texture2D)AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D));

                            if (value == null)
                            {
                                Debug.LogErrorFormat("Missing Texture: {0}", assetPath);
                                continue;
                            }

                            // convert from Unreal single color map style to Unity double color map style
                            if (propertyName == "_ColorMapOVR" || propertyName == "_ColorMapUND")
                            {
                                string tipString  = propertyName.Replace("ColorMap", "ColorTipMap");
                                string rootString = propertyName.Replace("ColorMap", "ColorRootMap");

                                mat.SetTexture(tipString, value);
                                mat.SetTexture(rootString, value);
                            }
                            else
                            {
                                Log("Setting Texture: " + propertyName + " = " + value.name);
                                mat.SetTexture(propertyName, value);

                                if (propertyName == "_ColorTipMapUND" || propertyName == "_ColorTipMapOVR")
                                {
                                    propertyName = propertyName.Replace("Tip", "Root");
                                    Log("Setting Root Color Texture Manually: " + propertyName + " = " + value.name);
                                    mat.SetTexture(propertyName, value);
                                }
                            }
                        }
                    }

                    // set float properties
                    if (field.FieldType == typeof(float) ||
                        field.FieldType == typeof(int))
                    {
                        float value = (float)configValue;
                        Log("Setting Float: " + propertyName + " = " + value);
                        mat.SetFloat(propertyName, value);
                    }

                    // set color properties
                    if (field.FieldType == typeof(Color))
                    {
                        Color value = (Color)configValue;
                        Log("Setting Color: " + propertyName + " = " + value);
                        mat.SetColor(propertyName, value);
                    }

                    if (field.FieldType == typeof(Vector2))
                    {
                        Vector2 value = (Vector2)configValue;

                        if (propertyName.EndsWith("_UVScale"))
                        {
                            string subName = propertyName.Substring(0, propertyName.IndexOf("_UVScale"));

                            if (!mat.HasProperty(subName))
                            {
                                LogWarning("Material is missing Texture UV property: " + subName + ". Not setting texture UV scale.");
                                continue;
                            }

                            Log("Setting Texture Scale: " + subName + " = " + value);
                            mat.SetTextureScale(subName, value);

                            if (subName == "_ColorTipMapUND" || subName == "_ColorTipMapOVR")
                            {
                                subName = subName.Replace("Tip", "Root");
                                Log("Setting Texture Scale Manually: " + subName + " = " + value);
                                mat.SetTextureScale(subName, value);
                            }
                        }
                    }

                    // set vector properties
                    if (field.FieldType == typeof(Vector4))
                    {
                        Vector4 value = (Vector4)configValue;
                        Log("Setting Vector4: " + propertyName + " = " + value);
                        mat.SetVector(propertyName, value);
                    }
                }

                //@HACK @TODO: hardcoding roughness/smoothness
                mat.SetFloat("_RoughnessUND", .25f);
                mat.SetFloat("_RoughnessOVR", .25f);

                MaterialUtils.RebuildMaterialKeywords(mat);

                EditorUtility.SetDirty(mat);
            }