Ejemplo n.º 1
0
    private Texture2D DrawSVG(string svgXml,
                              uint texWidth, uint texHeight,
                              Color clearColor,
                              bool fastUpload)
    {
        SVGDocument document;
        SVGSurface  surface;
        Texture2D   texture = null;

        // create a drawing surface, with the same texture dimensions
        surface = SVGAssets.CreateSurface(texWidth, texHeight);
        if (surface == null)
        {
            return(null);
        }
        // create the SVG document
        document = SVGAssets.CreateDocument(svgXml);
        if (document == null)
        {
            surface.Dispose();
            return(null);
        }
        // draw the SVG document onto the surface
        if (surface.Draw(document, new SVGColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a), SVGRenderingQuality.Better))
        {
            // create a 2D texture compatible with the drawing surface
            texture = surface.CreateCompatibleTexture(true, false);
            if (texture != null)
            {
                texture.hideFlags = HideFlags.DontSave;
                // copy the surface content into the texture
                if (fastUpload && Application.isPlaying)
                {
                    surface.CopyAndDestroy(texture);
                }
                else
                {
                    if (surface.Copy(texture))
                    {
                        // call Apply() so it's actually uploaded to the GPU
                        texture.Apply(false, true);
                    }
                }
            }
        }

        // destroy SVG surface and document
        surface.Dispose();
        document.Dispose();
        // return the created texture
        return(texture);
    }
Ejemplo n.º 2
0
    private Texture2D DrawAtlas(string svgXml1, string svgXml2, string svgXml3, string svgXml4,
                                uint texWidth, uint texHeight,
                                Color clearColor,
                                bool fastUpload)
    {
        SVGDocument document1;
        SVGDocument document2;
        SVGDocument document3;
        SVGDocument document4;
        SVGSurface  surface;
        Texture2D   texture = null;

        // create a drawing surface, with the same texture dimensions
        surface = SVGAssets.CreateSurface(texWidth, texHeight);
        if (surface == null)
        {
            return(null);
        }
        // create the SVG document
        document1 = SVGAssets.CreateDocument(svgXml1);
        if (document1 == null)
        {
            surface.Dispose();
            return(null);
        }
        document2 = SVGAssets.CreateDocument(svgXml2);
        if (document2 == null)
        {
            surface.Dispose();
            document1.Dispose();
            return(null);
        }
        document3 = SVGAssets.CreateDocument(svgXml3);
        if (document3 == null)
        {
            surface.Dispose();
            document1.Dispose();
            document2.Dispose();
            return(null);
        }
        document4 = SVGAssets.CreateDocument(svgXml4);
        if (document4 == null)
        {
            surface.Dispose();
            document1.Dispose();
            document2.Dispose();
            document3.Dispose();
            return(null);
        }

        // draw the SVG document1 onto the surface
        surface.Viewport = new SVGViewport(0.0f, 0.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document1, new SVGColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a), SVGRenderingQuality.Better);
        // draw the SVG document2 onto the surface
        surface.Viewport = new SVGViewport(texWidth / 2.0f, 0.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document2, null, SVGRenderingQuality.Better);
        // draw the SVG document3 onto the surface
        surface.Viewport = new SVGViewport(0.0f, texHeight / 2.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document3, null, SVGRenderingQuality.Better);
        // draw the SVG document4 onto the surface
        surface.Viewport = new SVGViewport(texWidth / 2.0f, texHeight / 2.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document4, null, SVGRenderingQuality.Better);

        // create a 2D texture compatible with the drawing surface
        texture = surface.CreateCompatibleTexture(true, false);
        if (texture != null)
        {
            texture.hideFlags = HideFlags.DontSave;
            // copy the surface content into the texture
            if (fastUpload && Application.isPlaying)
            {
                surface.CopyAndDestroy(texture);
            }
            else
            {
                if (surface.Copy(texture))
                {
                    // call Apply() so it's actually uploaded to the GPU
                    texture.Apply(false, true);
                }
            }
        }
        // destroy SVG surface and document
        surface.Dispose();
        document1.Dispose();
        document2.Dispose();
        document3.Dispose();
        document4.Dispose();
        // return the created texture
        return(texture);
    }
    private static bool GenerateSpritesFromBins(// input
        SVGPackedBin[] bins,
        Dictionary <uint, PackedSvgDocRef> loadedDocuments,
        float generationScale,
        Color clearColor,
        bool fastUpload,
        SVGSpritesDictionary previousSprites,
        // output
        List <Texture2D> textures, List <KeyValuePair <SVGSpriteRef, SVGSpriteData> > sprites,
        SVGSpritesListDictionary spritesListDict)
    {
        if (bins == null || loadedDocuments == null || textures == null || sprites == null)
        {
            return(false);
        }

        // sprite reference/key used to get pivot
        SVGSpriteRef tmpRef = new SVGSpriteRef(null, 0);

        for (int i = 0; i < bins.Length; ++i)
        {
            // extract the bin
            SVGPackedBin bin = bins[i];
            // create drawing surface
            SVGSurface surface = SVGAssets.CreateSurface(bin.Width, bin.Height);

            if (surface != null)
            {
                // draw packed rectangles of the current bin
                if (surface.Draw(bin, new SVGColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a), SVGRenderingQuality.Better))
                {
                    // bin rectangles
                    SVGPackedRectangle[] rectangles = bin.Rectangles;
                    // create a 2D texture compatible with the drawing surface
                    Texture2D texture = surface.CreateCompatibleTexture(true, false);

                    if (texture != null)
                    {
                        // push the created texture
                        textures.Add(texture);
                        for (int j = 0; j < rectangles.Length; ++j)
                        {
                            PackedSvgDocRef    svgDocRef;
                            SVGPackedRectangle rect = rectangles[j];
                            // get access to the referenced SVG document
                            if (loadedDocuments.TryGetValue(rect.DocHandle, out svgDocRef))
                            {
                                SVGSpriteAssetFile spriteAsset;
                                Vector2            pivot;
                                Vector4            border;
                                bool inCurrentInstancesGroup;
                                // try to see if this sprite was previously generated, and if so get its pivot
                                tmpRef.TxtAsset = svgDocRef.TxtAsset;
                                tmpRef.ElemIdx  = (int)rect.ElemIdx;
                                // get the previous pivot if present, else start with a default centered pivot
                                if (previousSprites.TryGetValue(tmpRef, out spriteAsset))
                                {
                                    float deltaScaleRatio = generationScale / spriteAsset.SpriteData.GenerationScale;
                                    pivot  = spriteAsset.SpriteData.Pivot;
                                    border = spriteAsset.SpriteData.Border * deltaScaleRatio;
                                    inCurrentInstancesGroup = spriteAsset.SpriteData.InCurrentInstancesGroup;
                                }
                                else
                                {
                                    pivot  = new Vector2(0.5f, 0.5f);
                                    border = Vector4.zero;
                                    inCurrentInstancesGroup = false;
                                }
                                // create a new sprite
                                Sprite sprite = Sprite.Create(texture, new Rect((float)rect.X, (float)((int)bin.Height - rect.Y - rect.Height), (float)rect.Width, (float)rect.Height), pivot, SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, 0, SpriteMeshType.FullRect, border);
                                sprite.name = svgDocRef.Name + "_" + rect.Name;
                                // push the sprite reference
                                SVGSpriteRef  key   = new SVGSpriteRef(svgDocRef.TxtAsset, (int)rect.ElemIdx);
                                SVGSpriteData value = new SVGSpriteData(sprite, pivot, border, rect.ZOrder, rect.OriginalX, rect.OriginalY, generationScale, inCurrentInstancesGroup);
                                sprites.Add(new KeyValuePair <SVGSpriteRef, SVGSpriteData>(key, value));
                                // check if we are interested in getting, for each SVG document, the list of its generated sprites
                                if (spritesListDict != null)
                                {
                                    SVGSpritesList spritesList;
                                    if (!spritesListDict.TryGetValue(svgDocRef.TxtAsset.GetInstanceID(), out spritesList))
                                    {
                                        // create the list of sprites location relative to the SVG text asset
                                        spritesList = new SVGSpritesList();
                                        spritesListDict.Add(svgDocRef.TxtAsset.GetInstanceID(), spritesList);
                                    }
                                    // add the new sprite the its list
                                    spritesList.Sprites.Add(key);
                                }

                                // decrement document references
                                if (svgDocRef.Dec(1) == 0)
                                {
                                    // we can free AmanithSVG native SVG document
                                    svgDocRef.Document.Dispose();
                                }
                            }
                        }
                        // copy the surface content into the texture
                        if (fastUpload && (Application.isPlaying))
                        {
                            surface.CopyAndDestroy(texture);
                        }
                        else
                        {
                            if (surface.Copy(texture))
                            {
                                // call Apply() so it's actually uploaded to the GPU
                                texture.Apply(false, false);
                            }
                        }
                    }
                }
                // destroy the AmanithSVG rendering surface
                surface.Dispose();
            }
        }
        return(true);
    }