Exemple #1
0
        public string GetTitle(Material material)
        {
            if (material != null)
            {
                var shader = material.shader;

                if (shader != null)
                {
                    foreach (var texEnv in P3dHelper.GetTexEnvs(shader))
                    {
                        if (texEnv.Name == Name)
                        {
                            return(texEnv.Title);
                        }
                    }
                }
            }

            return(Name);
        }
Exemple #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var sObj      = property.serializedObject;
            var sIdx      = property.FindPropertyRelative("Index");
            var sNam      = property.FindPropertyRelative("Name");
            var rectA     = position; rectA.width = EditorGUIUtility.labelWidth;
            var rectB     = position; rectB.xMin += EditorGUIUtility.labelWidth; rectB.width = 20;
            var rectC     = position; rectC.xMin += EditorGUIUtility.labelWidth; rectC.xMin += 22; rectC.width -= 20;
            var rectD     = position; rectD.xMin = rectD.xMax - 18;
            var component = property.serializedObject.targetObject as Component;
            var paintable = component != null?component.GetComponentInParent <P3dPaintable>() : null;

            var missing = true;

            // Valid slot?
            if (paintable != null)
            {
                var material = P3dHelper.GetMaterial(paintable.CachedRenderer, sIdx.intValue);

                if (material != null && P3dHelper.TexEnvNameExists(material.shader, sNam.stringValue) == true)
                {
                    missing = false;
                }
            }

            P3dEditor.BeginError(missing);
            {
                EditorGUI.LabelField(rectA, label);

                sObj.Update();

                sIdx.intValue    = Mathf.Clamp(EditorGUI.IntField(rectB, sIdx.intValue), 0, 99);
                sNam.stringValue = EditorGUI.TextField(rectC, sNam.stringValue);

                sObj.ApplyModifiedProperties();

                // Draw menu
                if (GUI.Button(rectD, "", EditorStyles.popup) == true)
                {
                    var menu = new GenericMenu();

                    if (paintable != null)
                    {
                        var materials = paintable.CachedRenderer.sharedMaterials;

                        if (materials.Length > 0)
                        {
                            for (var i = 0; i < materials.Length; i++)
                            {
                                var material     = materials[i];
                                var materialName = i.ToString();
                                var matIndex     = i;

                                if (material != null)
                                {
                                    materialName += " (" + material.name + ")";

                                    var texEnvs = P3dHelper.GetTexEnvs(material.shader);

                                    if (texEnvs != null && texEnvs.Count > 0)
                                    {
                                        foreach (var texEnv in texEnvs)
                                        {
                                            var texName  = texEnv.Name;
                                            var texTitle = texEnv.Title;
                                            var tex      = material.GetTexture(texName);

                                            if (tex != null)
                                            {
                                                texTitle += " (" + tex.name + ")";
                                            }
                                            else
                                            {
                                                texTitle += " (empty)";
                                            }

                                            menu.AddItem(new GUIContent(materialName + "/" + texTitle), sIdx.intValue == matIndex && sNam.stringValue == texName, () => { sObj.Update(); sIdx.intValue = matIndex; sNam.stringValue = texName; sObj.ApplyModifiedProperties(); });
                                        }
                                    }
                                    else
                                    {
                                        menu.AddDisabledItem(new GUIContent(materialName + "/This Material's shader has no textures!"));
                                    }
                                }
                                else
                                {
                                    menu.AddDisabledItem(new GUIContent(materialName + "/This Material is null!"));
                                }
                            }
                        }
                        else
                        {
                            menu.AddDisabledItem(new GUIContent("This GameObject has no materials!"));
                        }
                    }

                    menu.DropDown(rectD);
                }
            }
            P3dEditor.EndError();
        }
        public static void Draw(SerializedProperty sSlots, Shader shader)
        {
            var removeIndex = -1;

            for (var i = 0; i < sSlots.arraySize; i++)
            {
                var sSlot  = sSlots.GetArrayElementAtIndex(i);
                var sName  = sSlot.FindPropertyRelative("Name");
                var sAlias = sSlot.FindPropertyRelative("Alias");

                EditorGUILayout.BeginVertical(EditorStyles.helpBox);

                P3dHelper.BeginColor(P3dHelper.TexEnvNameExists(shader, sName.stringValue) == false);
                {
                    var rect  = P3dHelper.Reserve();
                    var rectA = rect; rectA.width = EditorGUIUtility.labelWidth;
                    var rectB = rect; rectB.xMin += EditorGUIUtility.labelWidth; rectB.xMax -= 20;
                    var rectC = rect; rectC.xMin = rectC.xMax - 18;

                    EditorGUI.LabelField(rectA, new GUIContent(sName.name, sName.tooltip));
                    EditorGUI.PropertyField(rectB, sName, GUIContent.none);

                    // Draw menu
                    if (GUI.Button(rectC, "", EditorStyles.popup) == true)
                    {
                        var menu    = new GenericMenu();
                        var texEnvs = P3dHelper.GetTexEnvs(shader);

                        if (texEnvs != null && texEnvs.Count > 0)
                        {
                            for (var j = 0; j < texEnvs.Count; j++)
                            {
                                var texName = texEnvs[j].Name;

                                menu.AddItem(new GUIContent(texName), sName.stringValue == texName, () => { sName.stringValue = texName; sName.serializedObject.ApplyModifiedProperties(); });
                            }
                        }
                        else
                        {
                            menu.AddDisabledItem(new GUIContent("This shader has no textures!"));
                        }

                        menu.DropDown(rectC);
                    }
                }
                P3dHelper.EndColor();

                EditorGUILayout.PropertyField(sAlias);

                DrawElement(sSlot.FindPropertyRelative("WriteR"));
                DrawElement(sSlot.FindPropertyRelative("WriteG"));
                DrawElement(sSlot.FindPropertyRelative("WriteB"));
                DrawElement(sSlot.FindPropertyRelative("WriteA"));

                P3dHelper.BeginColor(Color.red);
                if (GUILayout.Button("Remove Slot") == true)
                {
                    removeIndex = i;
                }
                P3dHelper.EndColor();

                EditorGUILayout.EndVertical();
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Add Slot") == true)
            {
                sSlots.arraySize += 1;
            }

            if (removeIndex >= 0)
            {
                sSlots.DeleteArrayElementAtIndex(removeIndex);
            }
        }
Exemple #4
0
        private Material DrawMaterial(P3dWindowPaintable paintable, Material material, int materialIndex)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Material " + materialIndex, EditorStyles.boldLabel, GUILayout.Width(120.0f));
            material = (Material)EditorGUILayout.ObjectField("", material, typeof(Material), false);
            EditorGUILayout.EndHorizontal();

            if (material != null)
            {
                if (material.hideFlags != HideFlags.None)
                {
                    EditorGUILayout.HelpBox("This may be a shared material, so you should clone it before modification.", MessageType.Warning);
                }

                if (GUILayout.Button("Clone") == true)
                {
                    material = Instantiate(material);
                }

                if (MaterialIsShared(material) == true && GUILayout.Button("Shared Clone") == true)
                {
                    var oldMaterial = material;

                    material = Instantiate(material);

                    MaterialIsShared(oldMaterial, material);
                }

                if (P3dHelper.IsAsset(material) == false && GUILayout.Button("Save As Asset") == true)
                {
                    var path = P3dHelper.SaveDialog("Save Material As Asset", "Assets", material.name, "mat");

                    if (string.IsNullOrEmpty(path) == false)
                    {
                        var textures = P3dHelper.CopyTextures(material);

                        AssetDatabase.CreateAsset(material, path);

                        P3dHelper.PasteTextures(material, textures);
                    }
                }

                var texEnvs = P3dHelper.GetTexEnvs(material);

                if (texEnvs.Count > 0)
                {
                    for (var j = 0; j < texEnvs.Count; j++)
                    {
                        var texEnv           = texEnvs[j];
                        var rect             = P3dHelper.Reserve();
                        var paintableTexture = paintable.PaintableTextures.Find(t => t.MaterialIndex == materialIndex && t.SlotName == texEnv.Name);

                        EditorGUI.BeginDisabledGroup(paintableTexture != null && paintableTexture.Locked == true);
                        var texture = EditorGUI.ObjectField(rect, default(string), material.GetTexture(texEnv.Name), typeof(Texture), true) as Texture;
                        EditorGUI.EndDisabledGroup();

                        // Make sure this is done after the texture field so it can be edited
                        var expand = EditorGUI.Foldout(rect, paintableTexture != null, new GUIContent(texEnv.Name, texEnv.Desc), false);

                        if (expand == true)
                        {
                            if (paintableTexture == null)
                            {
                                paintableTexture = new P3dWindowPaintableTexture(paintable, materialIndex, texEnv.Name); paintable.PaintableTextures.Add(paintableTexture);
                            }

                            paintableTexture.Revert();

                            paintableTexture.OldTexture = material.GetTexture(texEnv.Name) as Texture2D;

                            EditorGUILayout.BeginVertical("box");
                            texture = DrawObject(paintableTexture, material, texture);
                            EditorGUILayout.EndVertical();
                        }
                        else
                        {
                            if (paintableTexture != null)
                            {
                                paintableTexture.Unlock();

                                paintable.PaintableTextures.Remove(paintableTexture);
                            }
                        }

                        material.SetTexture(texEnv.Name, texture);
                    }
                }
                else
                {
                    EditorGUILayout.HelpBox("This material's shader has no texture slots.", MessageType.Info);
                }
            }
            else
            {
                EditorGUILayout.HelpBox("There is no material in this material slot.", MessageType.Info);
            }

            return(material);
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var sObj      = property.serializedObject;
            var sIdx      = property.FindPropertyRelative("Index");
            var sNam      = property.FindPropertyRelative("Name");
            var right     = position; right.xMin += EditorGUIUtility.labelWidth;
            var rightA    = new Rect(right.xMin, right.y, 20.0f, right.height);
            var rightB    = new Rect(right.xMin + 25.0f, right.y, right.width - 50.0f, right.height);
            var rightC    = new Rect(right.xMax - 20.0f, right.y, 20.0f, right.height);
            var component = property.serializedObject.targetObject as Component;
            var exists    = false;

            // Invalid slot?
            if (component != null)
            {
                var material = P3dHelper.GetMaterial(component.gameObject, sIdx.intValue);

                if (P3dHelper.TexEnvNameExists(material, sNam.stringValue) == false)
                {
                    exists = true;
                }
            }

            P3dHelper.BeginColor(exists);
            {
                EditorGUI.LabelField(position, label);

                sIdx.intValue    = Mathf.Clamp(EditorGUI.IntField(rightA, sIdx.intValue), 0, 99);
                sNam.stringValue = EditorGUI.TextField(rightB, sNam.stringValue);

                sObj.ApplyModifiedProperties();

                // Draw menu
                if (GUI.Button(rightC, "", EditorStyles.popup) == true)
                {
                    if (component != null)
                    {
                        var menu     = new GenericMenu();
                        var renderer = component.GetComponent <Renderer>();

                        if (renderer != null)
                        {
                            var materials = renderer.sharedMaterials;

                            if (materials.Length > 0)
                            {
                                for (var i = 0; i < materials.Length; i++)
                                {
                                    var material     = materials[i];
                                    var materialName = i.ToString();
                                    var matIndex     = i;

                                    if (material != null)
                                    {
                                        materialName += " (" + material.name + ")";

                                        var texEnvs = P3dHelper.GetTexEnvs(material);

                                        if (texEnvs != null && texEnvs.Count > 0)
                                        {
                                            for (var j = 0; j < texEnvs.Count; j++)
                                            {
                                                var texName  = texEnvs[j].Name;
                                                var texTitle = texName;
                                                var tex      = material.GetTexture(texName);

                                                if (tex != null)
                                                {
                                                    texTitle += " (" + tex.name + ")";
                                                }
                                                else
                                                {
                                                    texTitle += " (empty)";
                                                }

                                                menu.AddItem(new GUIContent(materialName + "/" + texTitle), sIdx.intValue == matIndex && sNam.stringValue == texName, () => { sIdx.intValue = matIndex; sNam.stringValue = texName; sObj.ApplyModifiedProperties(); });
                                            }
                                        }
                                        else
                                        {
                                            menu.AddDisabledItem(new GUIContent(materialName + "/This Material's shader has no textures!"));
                                        }
                                    }
                                    else
                                    {
                                        menu.AddDisabledItem(new GUIContent(materialName + "/This Material is null!"));
                                    }
                                }
                            }
                            else
                            {
                                menu.AddDisabledItem(new GUIContent("This GameObject has no materials!"));
                            }
                        }
                        else
                        {
                            menu.AddDisabledItem(new GUIContent("This GameObject has no renderer!"));
                        }

                        menu.DropDown(rightC);
                    }
                }
            }
            P3dHelper.EndColor();
        }
        protected override void OnInspector()
        {
            var clashes = CachedInstances.Where(d => d.Index == Target.Index);

            BeginError(clashes.Count() > 1);
            Draw("index", "This allows you to set the ID of this group (e.g. 100).\n\nNOTE: This number should be unique, and not shared by any other <b>P3dGroupData</b>.");
            EndError();
            Draw("textureDatas", "This allows you to specify the way each channel of this group's pixels are mapped to textures. This is mainly used by the in-editor painting mateiral builder tool.");
            Draw("shaderData", "This allows you to specify which shaders and their proprties are associated with this group.");

            CheckForDuplicates();

            Separator();

            EditorGUILayout.LabelField("Current Groups", EditorStyles.boldLabel);

            var groupDatas = CachedInstances.OrderBy(d => d.Index);

            EditorGUI.BeginDisabledGroup(true);
            foreach (var groupData in groupDatas)
            {
                if (groupData != null)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(groupData.name);
                    EditorGUILayout.IntField(groupData.Index);
                    EditorGUILayout.EndHorizontal();
                }
            }
            EditorGUI.EndDisabledGroup();

            Separator();

            EditorGUILayout.LabelField("Shader Properties", EditorStyles.boldLabel);

            filter = EditorGUILayout.TextField("Filter", filter);
            clean  = EditorGUILayout.Toggle("Clean", clean);

            var text = "";

            if (string.IsNullOrEmpty(filter) == false)
            {
                var tokens = filter.Split(' ');

                if (entries.Count == 0)
                {
                    foreach (var shaderInfo in ShaderUtil.GetAllShaderInfo())
                    {
                        var entry = new Entry();

                        entry.Path = shaderInfo.name;
                        entry.TexEnvs.AddRange(P3dHelper.GetTexEnvs(Shader.Find(shaderInfo.name)));

                        entries.Add(entry);
                    }
                }

                foreach (var entry in entries)
                {
                    foreach (var texEnv in entry.TexEnvs)
                    {
                        foreach (var token in tokens)
                        {
                            if (string.IsNullOrEmpty(token) == false)
                            {
                                if (Contains(texEnv.Name, token) == true || Contains(texEnv.Desc, token) == true || Contains(entry.Path, token) == true)
                                {
                                    var line = "";

                                    if (clean == true)
                                    {
                                        line += texEnv.Name + "@" + entry.Path + "\n";
                                    }
                                    else
                                    {
                                        line += texEnv.Name + " - " + texEnv.Desc + " - " + entry.Path + "\n";
                                    }

                                    if (text.Contains(line) == false)
                                    {
                                        text += line;
                                    }

                                    continue;
                                }
                            }
                        }
                    }
                }

                EditorGUILayout.TextArea(text, GUILayout.ExpandHeight(true));
            }
        }