コード例 #1
0
        public void AssignToMaterial(Material target, out List <Material> materials, out List <Texture2D> masks, ref Texture2D normal)
        {
            materials = null;
            masks     = null;

            // Find textureset & get masks
            TextureSet textureSet = FindTextureSet(target.name);

            if (textureSet == null)
            {
                return;
            }

            masks = new List <Texture2D>();
            foreach (string maskPath in textureSet.m_PackedMasks)
            {
                masks.Add(AssetDatabase.LoadAssetAtPath <Texture2D>(maskPath));
            }


            // Find textureset shader
            Shader shader = FindShader(textureSet.m_Shader);

            materials = new List <Material>(shader.m_Materials.Count);
            for (int i = 0; i < shader.m_Materials.Count; i++)
            {
                materials.Add(null);
            }

            foreach (Shader.Material material in shader.m_Materials)
            {
                ProceduralMaterial loadedMaterial = material.RetrieveProceduralMaterial();
                if (loadedMaterial == null)
                {
                    Utils.LogError("Could not find referenced .sbsar file: " + material.m_ResourcePath + ". Make sure the file is in your Unity project and load json file again.");
                }
                MaterialConfigurationData data = new MaterialConfigurationData();
                data.m_Material = loadedMaterial;
                ApplyMaterial(target, data, material.m_Name);

                int matIndex;
                if (int.TryParse(material.m_Name.Replace("Material", ""), out matIndex))
                {
                    matIndex--;
                    materials[matIndex] = loadedMaterial;
                }
            }

            normal = AssetDatabase.LoadAssetAtPath <Texture2D>(m_MeshNormalsPath);
        }
コード例 #2
0
    void SaveUserData()
    {
        JSONClass userData = new JSONClass();

        for (int i = 0; i < m_MaterialConfigurations.Count; i++)
        {
            MaterialConfigurationData data = m_MaterialConfigurations[i];
            if (data.m_Material != null)
            {
                userData["mat" + (i + 1).ToString()] = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(data.m_Material));
            }
        }

        AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(target));

        ai.userData = userData.ToString();
    }
コード例 #3
0
    static void ApplyMaterial(Material material, MaterialConfigurationData materialData, string matName)
    {
        ProceduralMaterial proceduralMaterial = materialData.m_Material as ProceduralMaterial;

        if (proceduralMaterial != null)
        {
            Utils.Log("Setting procmat " + proceduralMaterial + " on property " + matName);
            material.SetTexture("_" + matName + "_Color", proceduralMaterial.GetGeneratedTexture(proceduralMaterial.name + "_basecolor"));
            material.SetTexture("_" + matName + "_RM", proceduralMaterial.GetGeneratedTexture(proceduralMaterial.name + "_metallic"));
            material.SetTexture("_" + matName + "_Normal", proceduralMaterial.GetGeneratedTexture(proceduralMaterial.name + "_normal"));
        }
        else if (materialData.m_Material != null)
        {
            material.SetTexture("_" + matName + "_Color", materialData.m_Material.GetTexture("_MainTex"));
            material.SetTexture("_" + matName + "_RM", materialData.m_Material.GetTexture("_MetallicGlossMap"));
            material.SetTexture("_" + matName + "_Normal", materialData.m_Material.GetTexture("_BumpMap"));
        }
    }
コード例 #4
0
    public override void OnInspectorGUI()
    {
        if (!m_ProgressBarCleared)
        {
            m_ProgressBarCleared = true;
            EditorUtility.ClearProgressBar();
        }

        EditorGUI.BeginChangeCheck();

        // Warn if colorspace is not linear
        if (PlayerSettings.colorSpace != ColorSpace.Linear)
        {
            GUILayout.Label("Color space mismatch", TitlesStyle);
            GUILayout.BeginVertical("box");
            EditorGUILayout.HelpBox("You need to set the project color space to *linear* for correct rendering. (Go to edit -> project settings -> player)", MessageType.Error);
            GUILayout.EndVertical();
        }

        // Warn if colorspace is not linear
        if (PlayerSettings.renderingPath != RenderingPath.DeferredShading)
        {
            GUILayout.Label("Rendering path", TitlesStyle);
            GUILayout.BeginVertical("box");
            EditorGUILayout.HelpBox("Depending on your hardware, the shader might not work. Try to set rendering path to *Deferred*. (Go to edit -> project settings -> player)", MessageType.Warning);
            GUILayout.EndVertical();
        }

        if (Utils.ErrorMessages.Count > 0)
        {
            GUILayout.Label("Errors", TitlesStyle);
            GUILayout.BeginVertical("box");
            foreach (string error in Utils.ErrorMessages)
            {
                EditorGUILayout.HelpBox(error, MessageType.Error);
            }
            GUILayout.EndVertical();
        }

        //LOAD LAYERED MATERIAL
        if (GUILayout.Button("Load .json file", GUILayout.Height(50)))
        {
            Utils.ErrorMessages.Clear();
            LoadJson(Target);
        }

        //Materials Slots
        GUILayout.Label("Materials", TitlesStyle);
        GUILayout.BeginVertical("box");

        for (int i = 0; i < m_MaterialConfigurations.Count; i++)
        {
            OnInspectorMaterial(ref m_MaterialConfigurations[i].m_Material, i + 1);
        }

        GUILayout.EndVertical();

        //Mask Slots
        GUILayout.Label("Masks", TitlesStyle);
        GUILayout.BeginVertical("box");
        m_MaskTexture = (Texture2D)EditorGUILayout.ObjectField("Composite Mask", Target.GetTexture("_Mask"), typeof(Texture2D), false);
        Target.SetTexture("_Mask", m_MaskTexture);
        GUILayout.EndVertical();

        //Normal Slots
        GUILayout.Label("Mesh", TitlesStyle);
        GUILayout.BeginVertical("box");
        m_NormalTexture = (Texture2D)EditorGUILayout.ObjectField("Mesh normal", Target.GetTexture("BaseNormal"), typeof(Texture2D), false);
        Target.SetTexture("BaseNormal", m_NormalTexture);
        GUILayout.EndVertical();

        //Shader Parameters
        GUILayout.Label("Parameters", TitlesStyle);
        GUILayout.BeginVertical("box");


        bool tmp = EditorGUILayout.Toggle("Generate normals from masks", m_UseNormalFromMask);

        if (tmp != m_UseNormalFromMask)
        {
            m_UseNormalFromMask = tmp;
            SetupUseNormalFromMask();
        }


        Object[] materialList = new Object[1];
        materialList[0] = Target;
        for (int i = 0; i < m_MaterialConfigurations.Count; i++)
        {
            OnInspectorMaterialParameters(materialList, i + 1);
        }

        GUILayout.EndVertical();

        if (EditorGUI.EndChangeCheck())
        {
            PropertiesChanged();
            SaveUserData();

            for (int i = 0; i < m_MaterialConfigurations.Count; i++)
            {
                MaterialConfigurationData data = m_MaterialConfigurations[i];
                if (data.m_Material != null)
                {
                    ApplyMaterial(Target, data, "Material" + (i + 1).ToString());
                }
            }
        }
    }