Пример #1
0
        public CCGradient AddGradient(CCGradient gradient, bool renderAtlasTexture = true)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(null);
            }
            if (_atlasData == null)
            {
                return(null);
            }
            gradient = _atlasData.AddGradient(gradient);

            int  x = 0, y = 0;
            bool newTexture = GetCoords(out x, out y);

            gradient.index      = imageIndex++;
            gradient.atlasIndex = atlasIndex;

            if (renderAtlasTexture)
            {
                if (newTexture)
                {
                    CreateAtlasTexture(atlasIndex, atlasTextureWidth, atlasTextureHeight);
                }

                RenderGradient(atlasTextures [atlasIndex], gradient, x, y, gradientWidth, gradientHeight);
                atlasTextures [atlasIndex].Apply();
            }

            return(gradient);
        }
Пример #2
0
        public static void RenderGradient(Texture2D texture, CCGradient gradient, int x, int y, int gradientWidth, int gradientHeight)
        {
            //Debug.Log(string.Format("x: {0}, y: {1}, gradient: {2}", x, y, gradient));
            if (texture == null || gradient == null || !gradient.initialised)
            {
                return;
            }

            float tempWidth = gradientWidth - 1 - pixelOffset * 2;

            Color[] pixels = new Color[gradientWidth * gradientHeight];

            Color pixel;

            for (int i = 0; i < gradientWidth; i++)
            {
                pixel = gradient.Evaluate((float)(i - pixelOffset) / tempWidth);
                for (int j = 0; j < gradientHeight; j++)
                {
                    pixels [gradientWidth * j + i] = pixel;
                }
            }

            texture.SetPixels(x, y, gradientWidth, gradientHeight, pixels);
        }
Пример #3
0
        public CCGradient AddGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(null);
            }

            if (gradientCache == null || gradientCache.Count == 0)
            {
                RebuildGradientCache();
            }

            string gradientHash = gradient.hash;

            if (gradientCache.ContainsKey(gradientHash))
            {
                gradient = gradientCache [gradientHash];
                return(gradient);
            }

            gradient.index = gradients.Count;
            gradients.Add(gradient);
            gradientCache.Add(gradient.hash, gradient);

            return(gradient);
        }
    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        CCGradient gradient = (CCGradient)target;

        if (r.width <= 1f)
        {
            // events, no drawing
            return;
        }
        if (r.width == 40f)
        {
            // draw the thumbnail
            gradient.WriteToTexture(0f, 1f, thumbTexture);
            thumbTexture.Apply();
            CCEditorUtility.DrawTexture(r, thumbTexture, previewMode);
            return;
        }

        // draw the preview
        if ((int)r.width != previewTexture.width)
        {
            previewTexture.Resize((int)r.width, 1);
        }
        gradient.WriteToTexture(previewFrom, previewTo, previewTexture);
        previewTexture.Apply();
        CCEditorUtility.DrawTexture(r, previewTexture, previewMode);
    }
Пример #5
0
        public CCGradient AddGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(null);
            }

            if (_atlasData == null)
            {
                _atlasData = new SVGAtlasData();
                _atlasData.Init(atlasTextureWidth * atlasTextureHeight);
            }
            bool gradientExist;

            gradient = _atlasData.AddGradient(gradient, out gradientExist);
            if (gradientExist)
            {
                return(gradient);
            }

            int x = 0, y = 0;

            GetCoords(out x, out y, gradient.index);

//            Debug.Log("AddGradient: x: "+x+", y: "+y);
            gradient.atlasIndex = atlasIndex;
            _atlasHasChanged    = true;

            return(gradient);
        }
Пример #6
0
        public bool HasGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(false);
            }

            if (_atlasData == null)
            {
                return(false);
            }
            return(_atlasData.HasGradient(gradient));;
        }
Пример #7
0
        public CCGradient GetGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(null);
            }

            if (_atlasData == null)
            {
                return(null);
            }
            return(_atlasData.GetGradient(gradient));
        }
Пример #8
0
        public CCGradient GetGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised || gradientCache == null)
            {
                return(null);
            }

            if (gradientCache.ContainsKey(gradient.hash))
            {
                gradient = gradientCache [gradient.hash];
                gradient.references++;
                return(gradient);
            }
            else
            {
                return(null);
            }
        }
Пример #9
0
        public CCGradient GetGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised || gradientCache == null)
            {
                return(null);
            }

            string gradientHash = gradient.hash;

            if (gradientCache.ContainsKey(gradientHash))
            {
                return(gradientCache [gradientHash]);
            }
            else
            {
                return(null);
            }
        }
Пример #10
0
        public bool RemoveGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(false);
            }

            if (_atlasData == null)
            {
                return(false);
            }
            if (!_atlasData.RemoveGradient(gradient))
            {
                return(false);
            }

//            Debug.Log("RemoveGradient");
            return(true);
        }
Пример #11
0
        public bool HasGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised || gradientCache == null)
            {
                return(false);
            }

            string gradientHash = gradient.hash;

            if (gradientCache.ContainsKey(gradientHash))
            {
                gradient = gradientCache [gradientHash];
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #12
0
    void OnEnable()
    {
        gradient = Selection.activeObject as CCGradient;

        minimum         = EditorPrefs.GetFloat(minimumKey);
        maximum         = EditorPrefs.GetFloat(maximumKey, 1f);
        pixels          = EditorPrefs.GetInt(pixelsKey, 16);
        bilinearPreview = EditorPrefs.GetBool(bilinearPreviewKey, true);

        texture            = new Texture2D(pixels, 1, TextureFormat.ARGB32, false);
        texture.hideFlags  = HideFlags.HideAndDontSave;
        texture.wrapMode   = TextureWrapMode.Clamp;
        texture.filterMode = bilinearPreview ? FilterMode.Bilinear : FilterMode.Point;

        if (gradient != null)
        {
            gradient.WriteToTexture(minimum, maximum, texture);
            texture.Apply();
        }
    }
Пример #13
0
        public CCGradient AddGradient(CCGradient gradient, bool renderAtlasTexture = true)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(null);
            }

            if (gradientCache == null || gradientCache.Count == 0)
            {
                RebuildGradientCache();
            }

            if (gradientCache.ContainsKey(gradient.hash))
            {
                gradient = gradientCache [gradient.hash];
                return(gradient);
            }

            int  x = 0, y = 0;
            bool newTexture = GetCoords(out x, out y);

            gradient.index      = imageIndex++;
            gradient.atlasIndex = atlasIndex;
            gradients.Add(gradient);
            gradientCache.Add(gradient.hash, gradient);

            if (renderAtlasTexture)
            {
                if (newTexture)
                {
                    CreateAtlasTexture(atlasIndex, atlasTextureWidth, atlasTextureHeight);
                }

                RenderGradient(atlasTextures [atlasIndex], gradient, x, y, gradientWidth, gradientHeight);
                atlasTextures [atlasIndex].Apply();
            }

            return(gradient);
        }
Пример #14
0
        public bool RemoveGradient(CCGradient gradient)
        {
            if (gradient == null || !gradient.initialised)
            {
                return(false);
            }

            if (gradientCache == null || gradientCache.Count == 0)
            {
                return(false);
            }

            string gradientHash = gradient.hash;

            if (gradientCache.ContainsKey(gradientHash))
            {
                gradientCache.Remove(gradientHash);
                gradients[gradient.index] = null;
                return(true);
            }
            return(false);
        }
Пример #15
0
        public CCGradient AddGradient(CCGradient gradient, out bool gradientExist)
        {
            gradientExist = false;
            if (gradient == null || !gradient.initialised)
            {
                return(null);
            }

            if (gradientCache == null || gradientCache.Count == 0)
            {
                RebuildGradientCache();
            }

            string gradientHash = gradient.hash;

            if (gradientCache.ContainsKey(gradientHash))
            {
                gradient      = gradientCache [gradientHash];
                gradientExist = true;
            }
            else
            {
                int gradientsLength = gradients.Length;
                for (int i = 0; i < gradientsLength; i++)
                {
                    if (gradients[i] != null)
                    {
                        continue;
                    }
                    gradient.index = i;
                    gradients[i]   = gradient;
                    gradientCache.Add(gradientHash, gradient);
                    break;
                }
                gradientExist = false;
            }
            return(gradient);
        }
Пример #16
0
 public static bool IsDefaultGradient(CCGradient gradient)
 {
     return(DefaultGradient.GradientEquals(gradient));
 }
Пример #17
0
        public void StartProcess(SVGAsset asset)
        {
            if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            if (errors == null)
            {
                errors = new List <SVGError>();
            }
            else
            {
                errors.Clear();
            }
            _importingSVG = true;

            System.Reflection.FieldInfo _editor_runtimeMaterials = typeof(SVGAsset).GetField("_runtimeMaterials", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            _editor_runtimeMaterials.SetValue(asset, null);

            System.Reflection.FieldInfo _editor_runtimeMesh = typeof(SVGAsset).GetField("_runtimeMesh", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            _editor_runtimeMesh.SetValue(asset, null);

            UnityEditor.SerializedObject   svgAsset      = new UnityEditor.SerializedObject(asset);
            UnityEditor.SerializedProperty sharedMesh    = svgAsset.FindProperty("_sharedMesh");
            UnityEditor.SerializedProperty sharedShaders = svgAsset.FindProperty("_sharedShaders");

            Clear();
            SVGParser.Init();
            SVGGraphics.Init();
            atlasData = new SVGAtlasData();
            atlasData.Init(SVGAtlas.defaultAtlasTextureWidth * SVGAtlas.defaultAtlasTextureHeight);
            atlasData.AddGradient(SVGAtlasData.GetDefaultGradient());
            SVGElement _rootSVGElement = null;

#if IGNORE_EXCEPTIONS
            try {
#else
            Debug.LogWarning("Exceptions are turned on!");
#endif
                // Create new Asset
                CreateEmptySVGDocument();
                _rootSVGElement = this._svgDocument.rootElement;
#if IGNORE_EXCEPTIONS
            } catch (System.Exception exception) {
                _rootSVGElement = null;
                errors.Add(SVGError.Syntax);
                Debug.LogError("SVG Document Exception: " + exception.Message, asset);
            }
#endif

                if (_rootSVGElement == null)
                {
                    Debug.LogError("SVG Document is corrupted! " + UnityEditor.AssetDatabase.GetAssetPath(asset), asset);
                    _importingSVG = false;
                    return;
                }

#if IGNORE_EXCEPTIONS
                try {
#endif
                _rootSVGElement.Render();

                Rect viewport = _rootSVGElement.paintable.viewport;
                viewport.x    *= SVGAssetImport.meshScale;
                viewport.y    *= SVGAssetImport.meshScale;
                viewport.size *= SVGAssetImport.meshScale;

                Vector2 offset;
                SVGGraphics.CorrectSVGLayers(SVGGraphics.layers, viewport, asset, out offset);

                // Handle gradients
                bool hasGradients = false;

                // Create actual Mesh
                Shader[] outputShaders;
                Mesh mesh = new Mesh();
                SVGMesh.CombineMeshes(SVGGraphics.layers.ToArray(), mesh, out outputShaders, useGradients, format, compressDepth, asset.antialiasing);
                if (mesh == null)
                {
                    return;
                }

                if (useGradients == SVGUseGradients.Always)
                {
                    if (outputShaders != null)
                    {
                        for (int i = 0; i < outputShaders.Length; i++)
                        {
                            if (outputShaders[i] == null)
                            {
                                continue;
                            }
                            if (outputShaders[i].name == SVGShader.SolidColorOpaque.name)
                            {
                                outputShaders[i] = SVGShader.GradientColorOpaque;
                            }
                            else if (outputShaders[i].name == SVGShader.SolidColorAlphaBlended.name)
                            {
                                outputShaders[i] = SVGShader.GradientColorAlphaBlended;
                            }
                            else if (outputShaders[i].name == SVGShader.SolidColorAlphaBlendedAntialiased.name)
                            {
                                outputShaders[i] = SVGShader.GradientColorAlphaBlendedAntialiased;
                            }
                        }
                    }
                    hasGradients = true;
                }
                else
                {
                    if (outputShaders != null)
                    {
                        for (int i = 0; i < outputShaders.Length; i++)
                        {
                            if (outputShaders[i] == null)
                            {
                                continue;
                            }
                            if (outputShaders[i].name == SVGShader.GradientColorOpaque.name ||
                                outputShaders[i].name == SVGShader.GradientColorAlphaBlended.name ||
                                outputShaders[i].name == SVGShader.GradientColorAlphaBlendedAntialiased.name ||
                                outputShaders[i].name == SVGShader.GradientColorAlphaBlendedAntialiasedCompressed.name)
                            {
                                hasGradients = true;
                                break;
                            }
                        }
                    }
                }

                if (!asset.useLayers)
                {
                    sharedMesh.objectReferenceValue = AddObjectToAsset <Mesh>(mesh, asset, HideFlags.HideInHierarchy);
                }

//                Material sharedMaterial;
                if (outputShaders != null && outputShaders.Length > 0)
                {
                    sharedShaders.arraySize = outputShaders.Length;
                    if (hasGradients)
                    {
                        for (int i = 0; i < outputShaders.Length; i++)
                        {
                            sharedShaders.GetArrayElementAtIndex(i).stringValue = outputShaders[i].name;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < outputShaders.Length; i++)
                        {
                            if (outputShaders[i].name == SVGShader.GradientColorAlphaBlended.name)
                            {
                                outputShaders[i] = SVGShader.SolidColorAlphaBlended;
                            }
                            else if (outputShaders[i].name == SVGShader.GradientColorOpaque.name)
                            {
                                outputShaders[i] = SVGShader.SolidColorOpaque;
                            }
                            sharedShaders.GetArrayElementAtIndex(i).stringValue = outputShaders[i].name;
                        }
                    }
                }

                // Serialize the Asset
                svgAsset.ApplyModifiedProperties();

                // Handle Canvas Rectangle
                System.Reflection.MethodInfo _editor_SetCanvasRectangle = typeof(SVGAsset).GetMethod("_editor_SetCanvasRectangle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                _editor_SetCanvasRectangle.Invoke(asset, new object[] { new Rect(viewport.x, viewport.y, viewport.size.x, viewport.size.y) });

                if (asset.generateCollider)
                {
                    // Create polygon contour
                    if (SVGGraphics.paths != null && SVGGraphics.paths.Count > 0)
                    {
                        List <List <Vector2> > polygons = new List <List <Vector2> >();
                        for (int i = 0; i < SVGGraphics.paths.Count; i++)
                        {
                            Vector2[] points = SVGGraphics.paths[i].points;
                            for (int j = 0; j < points.Length; j++)
                            {
                                points[j].x = points[j].x * SVGAssetImport.meshScale - offset.x;
                                points[j].y = (points[j].y * SVGAssetImport.meshScale + offset.y) * -1f;
                            }

                            polygons.Add(new List <Vector2>(points));
                        }

                        polygons = SVGGeom.MergePolygon(polygons);

                        SVGPath[] paths = new SVGPath[polygons.Count];
                        for (int i = 0; i < polygons.Count; i++)
                        {
                            paths[i] = new SVGPath(polygons[i].ToArray());
                        }

                        System.Reflection.MethodInfo _editor_SetColliderShape = typeof(SVGAsset).GetMethod("_editor_SetColliderShape", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        if (paths != null && paths.Length > 0)
                        {
                            _editor_SetColliderShape.Invoke(asset, new object[] { paths });
                        }
                        else
                        {
                            _editor_SetColliderShape.Invoke(asset, new object[] { null });
                        }
                    }
                }
                else
                {
                    System.Reflection.MethodInfo _editor_SetColliderShape = typeof(SVGAsset).GetMethod("_editor_SetColliderShape", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    _editor_SetColliderShape.Invoke(asset, new object[] { null });
                }

                System.Reflection.MethodInfo _editor_SetGradients = typeof(SVGAsset).GetMethod("_editor_SetGradients", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                _editor_SetGradients.Invoke(asset, new object[] { null });
                if (hasGradients)
                {
                    if (atlasData.gradientCache != null && atlasData.gradientCache.Count > 0)
                    {
                        int          gradientsCount = SVGAssetImport.atlasData.gradientCache.Count;
                        CCGradient[] gradients      = new CCGradient[gradientsCount];
                        int          i = 0;
                        foreach (KeyValuePair <string, CCGradient> entry in SVGAssetImport.atlasData.gradientCache)
                        {
                            gradients[i++] = entry.Value;
                        }
                        _editor_SetGradients.Invoke(asset, new object[] { gradients });
                    }
                }

                System.Reflection.MethodInfo _editor_SetLayers = typeof(SVGAsset).GetMethod("_editor_SetLayers", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                _editor_SetLayers.Invoke(asset, new object[] { null });
                if (asset.useLayers)
                {
                    if (SVGGraphics.layers != null && SVGGraphics.layers.Count > 0)
                    {
                        _editor_SetLayers.Invoke(asset, new object[] { SVGGraphics.layers.ToArray() });
                    }
                }

#if IGNORE_EXCEPTIONS
            } catch (System.Exception exception) {
                Debug.LogWarning("Asset: " + UnityEditor.AssetDatabase.GetAssetPath(asset) + " Failed to import\n" + exception.Message, asset);
                errors.Add(SVGError.CorruptedFile);
            }
#endif
                if (_svgDocument != null)
                {
                    _svgDocument.Clear();
                    _svgDocument = null;
                }
                Clear();

                UnityEditor.EditorUtility.SetDirty(asset);
                _importingSVG = false;
            }
Пример #18
0
        public CCGradient AddGradient(CCGradient gradient)
        {
            bool gradientExist;

            return(AddGradient(gradient, out gradientExist));
        }
Пример #19
0
    void OnGUI()
    {
        CCGradient oldGradient = gradient;

        gradient = (CCGradient)EditorGUILayout.ObjectField("Gradient", gradient, typeof(CCGradient), false);
        bool updateTexture = gradient != oldGradient;

        if (gradient == null)
        {
            return;
        }

        float oldValue = minimum;

        minimum = EditorGUILayout.FloatField("Minimum", minimum);
        if (minimum != oldValue)
        {
            EditorPrefs.SetFloat(minimumKey, minimum);
            updateTexture = true;
        }
        oldValue = maximum;
        maximum  = EditorGUILayout.FloatField("Maximum", maximum);
        if (maximum != oldValue)
        {
            EditorPrefs.SetFloat(maximumKey, maximum);
            updateTexture = true;
        }

        int oldPixels = pixels;

        pixels = Mathf.Max(1, EditorGUILayout.IntField("Pixels", pixels));
        if (pixels != oldPixels)
        {
            texture.Resize(pixels, 1);
            EditorPrefs.SetInt(pixelsKey, pixels);
            updateTexture = true;
        }

        bool oldToggle = bilinearPreview;

        bilinearPreview = EditorGUILayout.Toggle("Bilinear Preview", bilinearPreview);
        if (bilinearPreview != oldToggle)
        {
            EditorPrefs.SetBool(bilinearPreviewKey, bilinearPreview);
            texture.filterMode = bilinearPreview ? FilterMode.Bilinear : FilterMode.Point;
        }

        if (updateTexture)
        {
            gradient.WriteToTexture(minimum, maximum, texture);
            texture.Apply();
        }

        if (GUILayout.Button("Export PNG file"))
        {
            string filePath = CCEditorUtility.SaveFilePanelInSameFolder("Save Color Gradient", gradient.name + " map", "png", gradient);
            if (filePath.Length > 0)
            {
                File.WriteAllBytes(filePath, texture.EncodeToPNG());
                AssetDatabase.Refresh();
                Close();
            }
        }
        CCEditorUtility.DrawTexture(new Rect(2f, 130f, position.width - 4f, position.height - 132f), texture);
    }