Пример #1
0
 public void LoadPalette(CSPalette otherPalette)
 {
     this.otherPalette = otherPalette;
     palette.Load(otherPalette);
     UpdateCWMaterial();
     SetColorKeys();
 }
Пример #2
0
        void NewPalette()
        {
            CSPalette newPalette = ScriptableObject.CreateInstance <CSPalette>();

            LoadPalette(newPalette);
            otherPalette = null;
        }
Пример #3
0
        public static CSPalette CreateEmptyPalette()
        {
            CSPalette palette = CreateInstance <CSPalette>();

            palette.scheme      = ColorScheme.Custom;
            palette.colorsCount = 0;
            palette.hueCount    = 0;
            return(palette);
        }
Пример #4
0
 public void LoadPalette(CSPalette otherPalette)
 {
     this.otherPalette = otherPalette;
     if (palette == otherPalette)
     {
         // this should not happen, safety check
         palette = Instantiate(palette);
     }
     palette.Load(otherPalette);
     UpdateCWMaterial();
     SetColorKeys();
 }
Пример #5
0
        void UpdateSpriteTexture(CSPalette palette, SpriteRenderer r)
        {
            if (r.sprite == null || r.sprite.texture == null)
            {
                return;
            }
            Sprite    sprite     = r.sprite;
            Texture2D newTexture = palette.GetNearestTexture(sprite.texture, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
            Sprite    newSPrite  = Sprite.Create(newTexture, sprite.rect, new Vector2(sprite.pivot.x / sprite.rect.width, sprite.pivot.y / sprite.rect.height), sprite.pixelsPerUnit);

            newSPrite.name = sprite.name + CS_NAME_SUFFIX;
            r.sprite       = newSPrite;
        }
Пример #6
0
        bool DeletePalette(CSPalette otherPalette)
        {
            string path = AssetDatabase.GetAssetPath(otherPalette);

            if (!string.IsNullOrEmpty(path))
            {
                if (EditorUtility.DisplayDialog("Confirmation", "Delete palette?", "Yes", "No"))
                {
                    AssetDatabase.DeleteAsset(path);
                    return(true);
                }
            }
            return(false);
        }
Пример #7
0
 public void Load(CSPalette otherPalette)
 {
     order             = otherPalette.order;
     hueCount          = otherPalette.hueCount;
     shades            = otherPalette.shades;
     saturation        = otherPalette.saturation;
     minBrightness     = otherPalette.minBrightness;
     maxBrightness     = otherPalette.maxBrightness;
     splitAmount       = otherPalette.splitAmount;
     scheme            = otherPalette.scheme;
     kelvin            = otherPalette.kelvin;
     colorTempStrength = otherPalette.colorTempStrength;
     keyColors         = new KeyColor[otherPalette.keyColors.Length];
     for (int k = 0; k < keyColors.Length; k++)
     {
         keyColors[k] = otherPalette.keyColors[k];
     }
 }
Пример #8
0
        bool SaveAsNewPalette(CSPalette palette)
        {
            string basePath = GetExportsPath("Palettes");
            string path     = basePath + "/Palette.asset";
            int    counter  = 2;

            while (File.Exists(path))
            {
                path = basePath + "/Palette" + counter + ".asset";
                counter++;
            }
            otherPalette = Instantiate <CSPalette>(palette);
            AssetDatabase.DeleteAsset(path);
            AssetDatabase.CreateAsset(otherPalette, path);
            EditorGUIUtility.PingObject(otherPalette);
            UpdateCWMaterial();
            palette.UpdateMaterial();
            return(true);
        }
Пример #9
0
 void UpdateMeshColors(CSPalette palette)
 {
     if (isMeshFilter)
     {
         MeshFilter mf = GetComponent <MeshFilter>();
         if (mf == null || mf.sharedMesh == null)
         {
             return;
         }
         Color[] meshColors = mf.sharedMesh.colors;
         if (meshColors == null)
         {
             return;
         }
         Mesh mesh = Instantiate(mf.sharedMesh);
         mesh.name     = mesh.name + CS_NAME_SUFFIX;
         mesh.colors   = palette.GetNearestColors(meshColors, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
         mf.sharedMesh = mesh;
     }
     else if (isSkinnedMesh)
     {
         SkinnedMeshRenderer smr = GetComponent <SkinnedMeshRenderer>();
         if (smr == null || smr.sharedMesh == null)
         {
             return;
         }
         Color[] meshColors = smr.sharedMesh.colors;
         if (meshColors == null)
         {
             return;
         }
         Mesh mesh = Instantiate(smr.sharedMesh);
         mesh.name      = mesh.name + CS_NAME_SUFFIX;
         mesh.colors    = palette.GetNearestColors(meshColors, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
         smr.sharedMesh = mesh;
     }
 }
Пример #10
0
 private void OnEnable()
 {
     palette = (CSPalette)target;
     palette.UpdateMaterial();
 }
Пример #11
0
        public override void OnInspectorGUI()
        {
            Recolor rc = (Recolor)target;

            if (rc.GetComponent <Renderer>() == null)
            {
                EditorGUILayout.HelpBox("Recolor script requires an GameObject with a MeshRenderer or SpriteRenderer component.", MessageType.Warning);
                return;
            }

            serializedObject.Update();

            EditorGUILayout.PropertyField(this.palette);

            CSPalette palette = (CSPalette)this.palette.objectReferenceValue;

            if (palette != null)
            {
                if (palette.material == null || palette.material.GetColorArray("_Colors") == null)
                {
                    palette.UpdateMaterial();
                }

                EditorGUILayout.BeginVertical(GUI.skin.box);

                Rect space = EditorGUILayout.BeginVertical();
                GUILayout.Space(64);
                EditorGUILayout.EndVertical();

                palette.material.SetVector("_CursorPos", Vector3.left);
                EditorGUI.DrawPreviewTexture(space, Texture2D.whiteTexture, palette.material);

                if (GUILayout.Button("Open in Color Studio"))
                {
                    CSWindow cs = CSWindow.ShowWindow();
                    cs.LoadPalette(palette);
                }

                EditorGUILayout.EndVertical();

                EditorGUILayout.PropertyField(applyPalette);
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Open Color Studio"))
                {
                    CSWindow.ShowWindow();
                }
                if (GUILayout.Button("Help"))
                {
                    EditorUtility.DisplayDialog("Quick Help", "This Recolor script changes colors of the gameobject or sprite at runtime.\n\nIf you assign a palette created with Color Studio, Recolor will transform the colors of the original texture to the nearest colors of the palette.\n\nYou can also specify custom color operations, like preserving or replacing individual colors from the original texture.", "Ok");
                }
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.PropertyField(mode, new GUIContent("Recolor Mode"));
            EditorGUILayout.PropertyField(colorMatch);
            EditorGUILayout.PropertyField(threshold, new GUIContent("Color Threshold"));
            EditorGUILayout.PropertyField(materialIndex);

            if (mode.intValue != (int)RecolorMode.MainColorOnly)
            {
                if (originalTexture == null)
                {
                    originalTexture = rc.GetOriginalTexture();
                    if (originalTexture != null)
                    {
                        originalTexture            = Instantiate <Texture2D>(originalTexture);
                        originalTexture.filterMode = FilterMode.Point;
                    }
                    originalColors = rc.GetOriginalUniqueColors();
                }

                EditorGUILayout.PropertyField(showOriginalTexture);
                if (showOriginalTexture.boolValue)
                {
                    if (originalTexture != null)
                    {
                        EditorGUILayout.BeginVertical(GUI.skin.box);

                        Rect space = EditorGUILayout.BeginVertical();
                        GUILayout.Space(128);
                        EditorGUILayout.EndVertical();

                        EditorGUI.DrawPreviewTexture(space, originalTexture);
                        EditorGUILayout.EndVertical();
                    }
                }
            }

            EditorGUILayout.Separator();
            EditorGUILayout.PropertyField(colorOperations, new GUIContent("Per Color Operations"), true);

            if (originalColors != null && originalColors.Count < 64 && GUILayout.Button("Add All Texture Colors"))
            {
                colorOperations.isExpanded = true;
                List <ColorEntry> cc = new List <ColorEntry>();
                if (rc.colorOperations != null)
                {
                    for (int k = 0; k < rc.colorOperations.Length; k++)
                    {
                        int index = originalColors.IndexOf(rc.colorOperations[k].color);
                        if (index >= 0)
                        {
                            originalColors.RemoveAt(index);
                        }
                        cc.Add(rc.colorOperations[k]);
                    }
                }
                for (int k = 0; k < originalColors.Count; k++)
                {
                    ColorEntry ce = new ColorEntry {
                        color = originalColors[k], operation = ColorOperation.Preserve, replaceColor = originalColors[k]
                    };
                    cc.Add(ce);
                }
                rc.colorOperations = cc.ToArray();
                EditorUtility.SetDirty(rc);
                serializedObject.Update();
                requireRefresh = true;
            }

            if (mode.intValue != (int)RecolorMode.MainColorOnly)
            {
                if (!rc.isSprite && originalTexture != null && GUILayout.Button("Add Main Texture Colors"))
                {
                    colorOperations.isExpanded = true;
                    List <ColorEntry> cc         = new List <ColorEntry>();
                    List <Color>      mainColors = rc.GetOriginalTextureMainColors();
                    if (mainColors != null)
                    {
                        if (rc.colorOperations != null)
                        {
                            for (int k = 0; k < rc.colorOperations.Length; k++)
                            {
                                int index = mainColors.IndexOf(rc.colorOperations[k].color);
                                if (index >= 0)
                                {
                                    mainColors.RemoveAt(index);
                                }
                                cc.Add(rc.colorOperations[k]);
                            }
                        }

                        for (int k = 0; k < mainColors.Count; k++)
                        {
                            ColorEntry ce = new ColorEntry {
                                color = mainColors[k], operation = ColorOperation.Preserve, replaceColor = mainColors[k]
                            };
                            cc.Add(ce);
                        }
                        rc.colorOperations = cc.ToArray();
                        EditorUtility.SetDirty(rc);
                        serializedObject.Update();
                        requireRefresh = true;
                    }
                }
                if (!rc.isSprite && mode.intValue == (int)RecolorMode.VertexColors && GUILayout.Button("Add Vertex Colors"))
                {
                    colorOperations.isExpanded = true;
                    List <ColorEntry> cc         = new List <ColorEntry>();
                    List <Color>      mainColors = rc.GetOriginalVertexColors();
                    if (rc.colorOperations != null)
                    {
                        for (int k = 0; k < rc.colorOperations.Length; k++)
                        {
                            int index = mainColors.IndexOf(rc.colorOperations[k].color);
                            if (index >= 0)
                            {
                                mainColors.RemoveAt(index);
                            }
                            cc.Add(rc.colorOperations[k]);
                        }
                    }

                    for (int k = 0; k < mainColors.Count; k++)
                    {
                        ColorEntry ce = new ColorEntry {
                            color = mainColors[k], operation = ColorOperation.Preserve, replaceColor = mainColors[k]
                        };
                        cc.Add(ce);
                    }
                    rc.colorOperations = cc.ToArray();
                    EditorUtility.SetDirty(rc);
                    serializedObject.Update();
                    requireRefresh = true;
                }
            }

            // Color adjustments
            EditorGUILayout.Separator();
            EditorGUILayout.PropertyField(enableColorAdjustments, new GUIContent("Color Correction"), true);
            if (enableColorAdjustments.boolValue)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(colorAdjustments, true);
                EditorGUI.indentLevel--;
            }

            CheckLUTSettings((Texture2D)lutProp.objectReferenceValue);

            if (rc.enabled)
            {
                if (GUILayout.Button("Refresh"))
                {
                    requireRefresh = false;
                    rc.Refresh();
                }
            }

            if (serializedObject.ApplyModifiedProperties() || rc.dirty || requireRefresh)
            {
                requireRefresh = true;
                rc.dirty       = false;
                if (rc.enabled)
                {
                    if (GUIUtility.hotControl == 0)
                    {
                        requireRefresh = false;
                        rc.Refresh();
                    }
                }
            }
        }
Пример #12
0
 bool DuplicatePalette(CSPalette otherPalette)
 {
     Selection.activeObject = otherPalette;
     return(SaveAsNewPalette(otherPalette));
 }
Пример #13
0
        public void Refresh()
        {
            RestoreMaterialsBackup();
            MakeMaterialsBackup();

            Renderer renderer = GetComponent <Renderer>();

            if (renderer == null)
            {
                return;
            }
            Material[] mats = renderer.sharedMaterials;
            if (mats == null || materialIndex >= mats.Length)
            {
                return;
            }
            Material mat = Instantiate(mats[materialIndex]);

            mat.name = mats[materialIndex].name + CS_NAME_SUFFIX;

            CSPalette palette = (applyPalette && this.palette != null) ? this.palette : CSPalette.CreateEmptyPalette();

            switch (mode)
            {
            case RecolorMode.MainColorOnly:
                mat.color = palette.GetNearestColor(mat.color, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
                break;

            case RecolorMode.Texture:
                if (isSprite)
                {
                    SpriteRenderer spr = (SpriteRenderer)renderer;
                    UpdateSpriteTexture(palette, spr);
                    return;
                }
                else if (mat.mainTexture != null)
                {
                    mat.mainTexture = palette.GetNearestTexture(mat.mainTexture, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
                }
                break;

            case RecolorMode.MainColorAndTexture:
                mat.color = palette.GetNearestColor(mat.color, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
                if (isSprite)
                {
                    SpriteRenderer spr = (SpriteRenderer)renderer;
                    UpdateSpriteTexture(palette, spr);
                }
                else if (mat.mainTexture != null)
                {
                    mat.mainTexture = palette.GetNearestTexture(mat.mainTexture, colorMatch, threshold, colorOperations, enableColorAdjustments, colorAdjustments);
                }
                break;

            case RecolorMode.VertexColors:
                if (isMeshFilter || isSkinnedMesh)
                {
                    UpdateMeshColors(palette);
                }
                break;
            }

            mats[materialIndex]      = mat;
            renderer.sharedMaterials = mats;
        }
Пример #14
0
 private void OnEnable()
 {
     palette = (CSPalette)target;
 }