Ejemplo n.º 1
0
        private static JSONNode GetSpDataLinkToSend(MeshInfo meshInfo, string substancePainterProjectPath)
        {
            // Prepare data to send to SP
            string workspacePath = Path.GetDirectoryName(Application.dataPath);
            string meshPath      = Path.GetFullPath(Path.Combine(workspacePath, meshInfo.AssetPath).ToString());
            string exportPath    = Path.Combine(Path.Combine("Assets", "SP_Textures"), Path.GetFileNameWithoutExtension(meshInfo.AssetPath)).ToString();
            string meshUri       = new System.Uri(meshPath).AbsoluteUri;

            JSONClass materialsLink = new JSONClass();

            foreach (Material material in meshInfo.Materials)
            {
                ShaderInfos shaderInfos           = ShadersInfos.GetShaderInfos(material.shader);
                JSONClass   propertiesAssociation = new JSONClass();
                foreach (string propertyName in shaderInfos.PropertiesAssociation.Keys)
                {
                    propertiesAssociation.Add(propertyName, new JSONData(shaderInfos.PropertiesAssociation[propertyName]));
                }

                JSONClass materialLink = new JSONClass();
                materialLink.Add("assetPath", AssetDatabase.GetAssetPath(material));
                materialLink.Add("exportPreset", shaderInfos.ExportPreset);
                materialLink.Add("resourceShader", shaderInfos.ResourceShader);
                materialLink.Add("spToLiveLinkProperties", propertiesAssociation);

                // HACK: Sanitize the name the same way SP internally do it
                string sanitizedName = MaterialsManipulation.SanitizeMaterialName(material.name);
                materialsLink.Add(sanitizedName, materialLink);
            }

            JSONClass project = new JSONClass();

            project.Add("meshUrl", new JSONData(meshUri));
            project.Add("normal", new JSONData("OpenGL"));
            project.Add("template", new JSONData(""));
            project.Add("url", new JSONData(new System.Uri(substancePainterProjectPath).AbsoluteUri));

            JSONClass jsonData = new JSONClass();

            jsonData.Add("applicationName", new JSONData("Unity"));
            jsonData.Add("exportPath", new JSONData(exportPath));
            jsonData.Add("workspacePath", new JSONData(workspacePath));
            jsonData.Add("materials", materialsLink);
            jsonData.Add("project", project);
            jsonData.Add("linkIdentifier", new JSONData(meshInfo.Identifier));

            return(jsonData);
        }
        public static bool SetMaterialParam(Material material, string property, JSONNode valueNode)
        {
            int propertyIndex;

            if (!CheckMaterialProperty(material, property, out propertyIndex))
            {
                return(false);
            }

            // Set the property value
            bool succeed = false;

            ShaderUtil.ShaderPropertyType type = ShaderUtil.GetPropertyType(material.shader, propertyIndex);
            switch (type)
            {
            case ShaderUtil.ShaderPropertyType.TexEnv: succeed = SetMaterialTexture(material, property, valueNode); break;

            default:
                Debug.LogWarning(string.Format("{0} property exchange not implemented", type.ToString()));
                break;
            }

            if (!succeed)
            {
                Debug.LogWarning(string.Format("Failed to load property '{0}' value of type {1} on material {2}: {3}", property, type.ToString(), AssetDatabase.GetAssetPath(material), valueNode.Value));
            }
            else
            {
                // Apply property changed post process
                ShaderInfos shaderInfos = ShadersInfos.GetShaderInfos(material.shader);
                if (shaderInfos.PostProcesses.ContainsKey(property))
                {
                    shaderInfos.PostProcesses[property](material, valueNode);
                }
            }
            return(succeed);
        }
        private static bool CheckMaterialProperty(Material material, string property, out int propertyIndex)
        {
            propertyIndex = -1;
            // Check parameter validity
            ShaderInfos shaderInfos = ShadersInfos.GetShaderInfos(material.shader);

            if (shaderInfos == null || !shaderInfos.PropertiesAssociation.ContainsValue(property))
            {
                Debug.LogWarning(string.Format("Unknown '{0}' parameter in shader {1}", property, material.shader.name));
                return(false);
            }
            int propertyCount = ShaderUtil.GetPropertyCount(material.shader);

            for (int i = 0; i < propertyCount; ++i)
            {
                if (ShaderUtil.GetPropertyName(material.shader, i).Equals(property))
                {
                    propertyIndex = i;
                    return(true);
                }
            }
            Debug.LogWarning(string.Format("Material '{0}' doesn't contain '{1}' property", AssetDatabase.GetAssetPath(material), property));
            return(false);
        }