Пример #1
0
    private static List <VectorUtils.Geometry> BuildGeoms()
    {
        var rect = new Shape();

        VectorUtils.MakeRectangleShape(rect, new Rect(0, 0, 100, 50));
        rect.Fill = new SolidFill()
        {
            Color = Color.red
        };

        var node = new SceneNode()
        {
            Shapes = new List <Shape> {
                rect
            }
        };
        var scene = new Scene()
        {
            Root = node
        };

        var options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = float.MaxValue,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };

        var geoms = VectorUtils.TessellateScene(scene, options);

        return(geoms);
    }
Пример #2
0
    /// <summary>
    /// Create a Texture2D icon of a SVG image.
    /// </summary>
    /// <param name="svg">String containing svg content.</param>
    public static Texture2D GetIcon(string svg)
    {
        // Parse the SVG
        SVGParser.SceneInfo sceneInfo = SVGParser.ImportSVG(new System.IO.StringReader(svg));
        int width  = Mathf.CeilToInt(sceneInfo.SceneViewport.width);
        int height = Mathf.CeilToInt(sceneInfo.SceneViewport.height);

        if ((width > 64) || (height > 64))
        {
            Debug.LogWarning("SVG icon of unusual size!");
        }

        VectorUtils.TessellationOptions tessellationOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 0.05f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };
        List <VectorUtils.Geometry> iconGeometry = VectorUtils.TessellateScene(sceneInfo.Scene, tessellationOptions);

        Sprite    sprite      = VectorUtils.BuildSprite(iconGeometry, 1f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
        Texture2D iconTexture = VectorUtils.RenderSpriteToTexture2D(sprite, width, height, renderMaterial);

        return(iconTexture);
    }
Пример #3
0
    private void Awake()
    {
        LineWidth = m_LineWidth; //to kick in the PPU
        m_Scene   = new Scene()
        {
            Root = new SceneNode()
            {
            }
        };
        m_Scene.Root.Children = new List <SceneNode>();
        m_Scene.Root.Shapes   = new List <Shape>();

        m_Options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 1000.0f,
            MaxCordDeviation     = 0.05f,
            MaxTanAngleDeviation = 0.05f,
            SamplingStepSize     = 0.01f
        };

        m_mesh = new Mesh();

        var meshFilter = gameObject.AddComponent <MeshFilter>();

        meshFilter.mesh = m_mesh;

        var meshRenderer = gameObject.AddComponent <MeshRenderer>();

        Material material = Resources.Load(
            "Default2D", typeof(Material)) as Material;

        meshRenderer.materials = new Material[] { material };
    }
Пример #4
0
    static void VerifyCamera()
    {
        if (renderCamera == null)
        {
            GameObject cameraObject = GameObject.Find("Icon Render Camera");
            if (cameraObject == null)
            {
                cameraObject = new GameObject("Icon Render Camera");
            }
            renderCamera = cameraObject.GetComponent <Camera>();
            if (renderCamera == null)
            {
                renderCamera = cameraObject.AddComponent <Camera>();
            }

            renderCamera.orthographic     = true;
            renderCamera.orthographicSize = 1f;
            renderCamera.clearFlags       = CameraClearFlags.SolidColor;
            renderCamera.backgroundColor  = Color.clear;
            renderCamera.nearClipPlane    = 0.1f;
            renderCamera.farClipPlane     = 100.0f;
            renderCamera.depth            = Camera.main.depth + 1;
            renderCamera.enabled          = false;

            renderMaterial = new Material(Shader.Find("UI/Default"));

            tessellationOptions = new VectorUtils.TessellationOptions()
            {
                StepDistance         = 0.1f,
                MaxCordDeviation     = float.MaxValue,
                MaxTanAngleDeviation = Mathf.PI / 16.0f,
                SamplingStepSize     = 0.01f
            };
        }
    }
Пример #5
0
    void Start()
    {
        string svg =
            @"<svg width=""283.9"" height=""283.9"" xmlns=""http://www.w3.org/2000/svg"">
                <line x1=""170.3"" y1=""226.99"" x2=""177.38"" y2=""198.64"" fill=""none"" stroke=""#888"" stroke-width=""1""/>
                <line x1=""205.73"" y1=""198.64"" x2=""212.81"" y2=""226.99"" fill=""none"" stroke=""#888"" stroke-width=""1""/>
                <line x1=""212.81"" y1=""226.99"" x2=""219.9"" y2=""255.33"" fill=""none"" stroke=""#888"" stroke-width=""1""/>
                <line x1=""248.25"" y1=""255.33"" x2=""255.33"" y2=""226.99"" fill=""none"" stroke=""#888"" stroke-width=""1""/>
                <path d=""M170.08,226.77c7.09-28.34,35.43-28.34,42.52,0s35.43,28.35,42.52,0"" transform=""translate(0.22 0.22)"" fill=""none"" stroke=""red"" stroke-width=""1.2""/>
                <circle cx=""170.3"" cy=""226.99"" r=""1.2"" fill=""blue"" stroke-width=""0.6""/>
                <circle cx=""212.81"" cy=""226.99"" r=""1.2"" fill=""blue"" stroke-width=""0.6""/>
                <circle cx=""255.33"" cy=""226.99"" r=""1.2"" fill=""blue"" stroke-width=""0.6""/>
                <circle cx=""177.38"" cy=""198.64"" r=""1"" fill=""black"" />
                <circle cx=""205.73"" cy=""198.64"" r=""1"" fill=""black"" />
                <circle cx=""248.25"" cy=""255.33"" r=""1"" fill=""black"" />
                <circle cx=""219.9"" cy=""255.33"" r=""1"" fill=""black"" />
            </svg>";

        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100.0f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        // Dynamically import the SVG data, and tessellate the resulting vector scene.
        var sceneInfo = SVGParser.ImportSVG(new StringReader(svg));
        var geoms     = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);

        // Build a sprite with the tessellated geometry.
        var sprite = VectorUtils.BuildSprite(geoms, 10.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);

        GetComponent <SpriteRenderer>().sprite = sprite;
    }
Пример #6
0
    protected override void SetAppearance()
    {
        string sheepSvg =
            @"<svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 25.55 15.48"">
	            <g><path id=""Body"" d=""M19.59,9.45A4.27,4.27,0,0,0,19,7.12a2.3,2.3,0,0,0-2.07-1.07A2.47,2.47,0,0,0,15.34,7a7.41,7.41,0,0,0-.94,1.58,6.48,6.48,0,0,1-1.21-1,3.41,3.41,0,0,0-2.62-.83A2.59,2.59,0,0,0,8.48,8.36a.36.36,0,0,1-.11.2c-.08,0-.19,0-.28,0a7.84,7.84,0,0,0-2-.51,1.06,1.06,0,0,0-.47,0,1.15,1.15,0,0,0-.41.35,4.28,4.28,0,0,0-1,2,2,2,0,0,0,.84,2,2.15,2.15,0,0,0,1.34,4,2.36,2.36,0,0,0,.06,1.83,2.12,2.12,0,0,0,1.28.77,9.55,9.55,0,0,0,1.84.3,2.41,2.41,0,0,0,1.3-.21,1,1,0,0,0,.53-1.11,8.92,8.92,0,0,0,3.4,1.59,3.83,3.83,0,0,0,2.07,0,1.84,1.84,0,0,0,1.3-1.49l2.23.35a2.24,2.24,0,0,0,1,0c.76-.23,1.06-1.12,1.16-1.91a1.33,1.33,0,0,0-.35-1.27,3.22,3.22,0,0,0,2.43-1.82,1.3,1.3,0,0,0,.13-.86c-.15-.49-.71-.71-1.19-.87a.55.55,0,0,1-.32-.19.52.52,0,0,1-.05-.28c0-.44.21-.87.19-1.31a1.86,1.86,0,0,0-1.53-1.55,5.19,5.19,0,0,0-2.31.18"" transform=""translate(-3.38 -5.54)"" fill=""#fff"" stroke=""#000"" stroke-miterlimit=""10""/></g>
	            <g><path id=""Head"" d=""M23.53,10.81c.11,0,0,0,.16.06.75.21,1.53.25,2.3.39a4,4,0,0,1,2.1.93.44.44,0,0,1,.12.15.43.43,0,0,1,0,.25,1.92,1.92,0,0,1-.41,1.1,1.72,1.72,0,0,1-1,.41,10,10,0,0,1-1.81.13,4.2,4.2,0,0,0-1,0"" transform=""translate(-3.38 -5.54)"" fill=""#fff"" stroke=""#000"" stroke-miterlimit=""10""/></g>
	            <g><path id=""Mark"" d=""M13.41,12.23a2.84,2.84,0,0,0-.71-.11,3.35,3.35,0,0,0-1.86.81"" transform=""translate(-3.38 -5.54)"" fill=""none"" stroke=""#000"" stroke-miterlimit=""10""/></g>
            </svg>";

        var sceneInfo = SVGParser.ImportSVG(new StringReader(sheepSvg));
        var shape     = sceneInfo.NodeIDs["Body"].Shapes[0];

        shape.Fill = new SolidFill()
        {
            Color = Color.white
        };
        shape      = sceneInfo.NodeIDs["Head"].Shapes[0];
        shape.Fill = new SolidFill()
        {
            Color = Color.black
        };

        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        var geoms  = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
        var sprite = VectorUtils.BuildSprite(geoms, 26, VectorUtils.Alignment.Center, Vector2.zero, 128, true);

        GetComponent <SpriteRenderer>().sprite = sprite;
    }
Пример #7
0
    void Start()
    {
        float x     = -1.0f;
        float xStep = 0.48f;

        var options = new VectorUtils.TessellationOptions();

        options.MaxCordDeviation     = float.MaxValue;
        options.MaxTanAngleDeviation = Mathf.PI * 0.5f;
        options.SamplingStepSize     = 1.0f / 100.0f;

        // Fine tessellation
        options.StepDistance = 1.0f;
        BuildTestColumnWithProperties(x, 1.0f, PathEnding.Chop, PathCorner.Tipped, options);  x += xStep;
        BuildTestColumnWithProperties(x, 2.0f, PathEnding.Chop, PathCorner.Tipped, options);  x += xStep;
        BuildTestColumnWithProperties(x, 4.0f, PathEnding.Chop, PathCorner.Beveled, options); x += xStep;
        BuildTestColumnWithProperties(x, 6.0f, PathEnding.Round, PathCorner.Round, options);  x += xStep;

        // Coarse tessellation
        options.StepDistance = 100.0f;
        BuildTestColumnWithProperties(x, 1.0f, PathEnding.Chop, PathCorner.Tipped, options);  x += xStep;
        BuildTestColumnWithProperties(x, 2.0f, PathEnding.Chop, PathCorner.Tipped, options);  x += xStep;
        BuildTestColumnWithProperties(x, 4.0f, PathEnding.Chop, PathCorner.Beveled, options); x += xStep;
        BuildTestColumnWithProperties(x, 6.0f, PathEnding.Round, PathCorner.Round, options);  x += xStep;
    }
Пример #8
0
    void Hand(int hand)
    {
        string svg =
            @"<svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 30.11 27.39"">
                <g><circle id=""Hand"" cx=""4.66"" cy=""2.6"" r=""2.11"" fill=""#fff"" stroke=""#000"" stroke-miterlimit=""10"" stroke-width=""0.5""/></g>
            </svg>";

        var sceneInfo = SVGParser.ImportSVG(new StringReader(svg));
        var shape     = sceneInfo.NodeIDs["Hand"].Shapes[0];

        shape.Fill = new SolidFill()
        {
            Color = skinTone
        };

        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        var geoms  = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
        var sprite = VectorUtils.BuildSprite(geoms, 26, VectorUtils.Alignment.Center, Vector2.zero, 128, true);

        hands[hand].GetComponent <SpriteRenderer>().sprite = sprite;
    }
Пример #9
0
        /// <summary>Imports an SVG asset</summary>
        /// <param name="ctx">The asset import context of the scripted importer</param>
        public override void OnImportAsset(AssetImportContext ctx)
        {
            // We're using a hardcoded window size of 100x100. This way, using a pixels per point value of 100
            // results in a sprite of size 1 when the SVG file has a viewbox specified.
            SVGParser.SceneInfo sceneInfo;
            using (var stream = new StreamReader(ctx.assetPath))
                sceneInfo = SVGParser.ImportSVG(stream, 0, 1, 100, 100, PreserveViewport);

            if (sceneInfo.Scene == null || sceneInfo.Scene.Root == null)
            {
                throw new Exception("Wowzers!");
            }

            float stepDist         = StepDistance;
            float samplingStepDist = SamplingStepDistance;
            float maxCord          = MaxCordDeviationEnabled ? MaxCordDeviation : float.MaxValue;
            float maxTangent       = MaxTangentAngleEnabled ? MaxTangentAngle : Mathf.PI * 0.5f;

            if (!AdvancedMode)
            {
                // Automatically compute sensible tessellation options from the
                // vector scene's bouding box and target resolution
                ComputeTessellationOptions(sceneInfo, TargetResolution, ResolutionMultiplier, out stepDist, out maxCord, out maxTangent);
            }

            var tessOptions = new VectorUtils.TessellationOptions();

            tessOptions.MaxCordDeviation     = maxCord;
            tessOptions.MaxTanAngleDeviation = maxTangent;
            tessOptions.SamplingStepSize     = 1.0f / (float)samplingStepDist;
            tessOptions.StepDistance         = stepDist;

            var rect = Rect.zero;

            if (PreserveViewport)
            {
                rect = sceneInfo.SceneViewport;
            }

            var geometry = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions, sceneInfo.NodeOpacity);
            var sprite   = VectorUtils.BuildSprite(geometry, rect, SvgPixelsPerUnit, Alignment, CustomPivot, GradientResolution, true);

            var name = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath);

            if (SvgType == SVGType.VectorSprite)
            {
                GenerateSpriteAsset(ctx, sprite, name);
            }
            else if (SvgType == SVGType.TexturedSprite)
            {
                GenerateTexturedSpriteAsset(ctx, sprite, name);
            }
            else if (SvgType == SVGType.Texture2D)
            {
                GenerateTexture2DAsset(ctx, sprite, name);
            }
        }
Пример #10
0
    /// <summary>
    /// Create a Texture2D icon of a SVG image (editor only version).
    /// </summary>
    /// <param name="svg">String containing svg content.</param>
    /// <param name="renderUtil">PreviewRenderUtility to use for drawing</param>
    public static Texture2D GetIcon(string svg, PreviewRenderUtility renderUtil)
    {
        // Parse the SVG
        SVGParser.SceneInfo sceneInfo = SVGParser.ImportSVG(new System.IO.StringReader(svg));
        int width  = Mathf.CeilToInt(sceneInfo.SceneViewport.width);
        int height = Mathf.CeilToInt(sceneInfo.SceneViewport.height);

        if ((width > 64) || (height > 64))
        {
            Debug.LogWarning("SVG icon of unusual size!");
        }

        // Save the render state and get a temporary render texture
        RenderTexture activeTexture = RenderTexture.active;

        renderUtil.camera.targetTexture   = RenderTexture.GetTemporary(width, height, 8, RenderTextureFormat.ARGB32);
        renderUtil.camera.backgroundColor = Color.clear;

        // Generate the mesh
        Mesh iconMesh = new Mesh();

        VectorUtils.TessellationOptions tessellationOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 0.05f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };
        List <VectorUtils.Geometry> iconGeometry = VectorUtils.TessellateScene(sceneInfo.Scene, tessellationOptions);

        VectorUtils.FillMesh(iconMesh, iconGeometry, 1f);

        // Activate the render texture and draw the mesh into it
        RenderTexture.active = renderUtil.camera.targetTexture;
        float   cameraSize     = renderUtil.camera.orthographicSize;
        Vector3 cameraPosition = renderUtil.camera.transform.position;

        renderUtil.camera.orthographicSize   = sceneInfo.SceneViewport.height / 2;
        renderUtil.camera.transform.position = new Vector3(sceneInfo.SceneViewport.center.x, sceneInfo.SceneViewport.center.y, -1);
        // HACK until FillMesh() flpYAxis is fixed
        renderUtil.camera.transform.Rotate(0, 0, 180f);
        renderUtil.DrawMesh(iconMesh, Matrix4x4.identity, renderMaterial, 0);
        renderUtil.camera.Render();
        renderUtil.camera.transform.Rotate(0, 0, 180f);
        renderUtil.camera.orthographicSize   = cameraSize;
        renderUtil.camera.transform.position = cameraPosition;
        Texture2D iconTexture = new Texture2D(width, height);

        iconTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        iconTexture.Apply();

        // Restore the render state and release the temporary render texture
        RenderTexture.active = activeTexture;
        RenderTexture.ReleaseTemporary(renderUtil.camera.targetTexture);

        return(iconTexture);
    }
Пример #11
0
    static VectorUtils.TessellationOptions MakeTessOptions(float stepDistance)
    {
        var tessOptions = new VectorUtils.TessellationOptions();

        tessOptions.MaxCordDeviation     = float.MaxValue;
        tessOptions.MaxTanAngleDeviation = Mathf.PI * 0.5f;
        tessOptions.SamplingStepSize     = 1.0f / 100.0f;
        tessOptions.StepDistance         = stepDistance;
        return(tessOptions);
    }
Пример #12
0
 public ShowHanz()
 {
     wrpsvg      = new WrapHanzSvg();
     tessOptions = new VectorUtils.TessellationOptions()
     {
         StepDistance         = 100.0f,
         MaxCordDeviation     = 0.5f,
         MaxTanAngleDeviation = 0.1f,
         SamplingStepSize     = 0.01f
     };
 }
Пример #13
0
    void Start()
    {
        // Prepare the vector path, add it to the vector scene.

        /*m_Path = new Shape()
         * {
         *  Contours = new BezierContour[] { new BezierContour() { Segments = new BezierPathSegment[2] } },
         *  PathProps = new PathProperties()
         *  {
         *      Stroke = new Stroke() { Color = Color.white, HalfThickness = 0.1f }
         *  }
         * };*/
        m_Path = new Shape()
        {
            Contours = new BezierContour[] { new BezierContour()
                                             {
                                                 Segments = new BezierPathSegment[2]
                                             } }
        };
        //VectorUtils.MakeCircleShape(m_Path, Vector2.zero, 5.0f);
        m_Path.Fill = new SolidFill()
        {
            Color = Color.blue
        };
        m_Path.PathProps = new PathProperties()
        {
            Stroke = new Stroke()
            {
                Color = Color.red
            }
        };

        m_Scene = new Scene()
        {
            Root = new SceneNode()
            {
                Shapes = new List <Shape> {
                    m_Path
                }
            }
        };

        m_Options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 1.0f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };

        // Instantiate a new mesh, it will be filled with data in Update()
        m_Mesh = new Mesh();
        GetComponent <MeshFilter>().mesh = m_Mesh;
    }
Пример #14
0
    protected VectorUtils.TessellationOptions MakeLineOptions(float stepDistance = float.MaxValue)
    {
        var options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = stepDistance,
            MaxCordDeviation     = 0.05f,
            MaxTanAngleDeviation = 0.05f,
            SamplingStepSize     = 0.01f
        };

        return(options);
    }
Пример #15
0
    private void Awake()
    {
        tessellation = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100.0f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        render = GetComponent <SpriteRenderer>();
    }
    void Start()
    {
        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100.0f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        // Prepare the fill
        var fill = new GradientFill()
        {
            Type  = GradientFillType.Linear,
            Stops = new GradientStop[] {
                new GradientStop()
                {
                    Color = Color.blue, StopPercentage = 0.0f
                },
                new GradientStop()
                {
                    Color = Color.red, StopPercentage = 1.0f
                },
            }
        };

        // Build the scene
        var cornerRad = new Vector2(10, 10);
        var rect      = VectorUtils.BuildRectangleContour(new Rect(0, 0, 100, 100), cornerRad, cornerRad, cornerRad, cornerRad);
        var scene     = new Scene()
        {
            Root = new SceneNode()
            {
                Shapes = new List <Shape> {
                    new Shape()
                    {
                        Contours = new BezierContour[] { rect },
                        Fill     = fill
                    }
                }
            }
        };

        // Dynamically import the SVG data, and tessellate the resulting vector scene.
        var geoms = VectorUtils.TessellateScene(scene, tessOptions);

        // Build a sprite with the tessellated geometry.
        var sprite = VectorUtils.BuildSprite(geoms, 10.0f, VectorUtils.Alignment.Center, Vector2.zero, 16, true);

        GetComponent <SpriteRenderer>().sprite = sprite;
    }
Пример #17
0
    void OnEnable()
    {
        // Build the vector scene, which consist of a rectangle, clipped by a circle.
        m_Clipper = new SceneNode()
        {
            Transform = Matrix2D.identity,
            Drawables = new List <IDrawable> {
                VectorUtils.MakeCircle(Vector2.zero, 4.0f)
            }
        };

        m_Rectangle = new Rectangle()
        {
            Position = Vector2.zero,
            Size     = new Vector2(10.0f, 10.0f),
            Fill     = new SolidFill()
            {
                Color = Color.blue
            },
            PathProps = new PathProperties()
            {
                Stroke = new Stroke()
                {
                    Color = Color.red
                }
            },
            FillTransform = Matrix2D.identity
        };

        m_Scene = new Scene()
        {
            Root = new SceneNode()
            {
                Drawables = new List <IDrawable> {
                    m_Rectangle
                },
                Transform = Matrix2D.identity
            }
        };

        m_Options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 1.0f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };

        m_Mesh = new Mesh();
        GetComponent <MeshFilter>().mesh = m_Mesh;
    }
Пример #18
0
    static VectorShape()
    {
        tessellationScene = new Scene();

        tessellationOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 50f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 16.0f,
            SamplingStepSize     = 0.05f
        };

        lineBuilder = new VectorLineMeshBuilder();
    }
Пример #19
0
    void OnEnable()
    {
        // Build the vector scene, which consist of a rectangle, clipped by a circle.
        var circle = new Shape();

        VectorUtils.MakeCircleShape(circle, Vector2.zero, 4.0f);
        m_Clipper = new SceneNode()
        {
            Transform = Matrix2D.identity,
            Shapes    = new List <Shape> {
                circle
            }
        };

        m_Rectangle = new Shape();
        VectorUtils.MakeRectangleShape(m_Rectangle, new Rect(0, 0, 10, 10));
        m_Rectangle.Fill = new SolidFill()
        {
            Color = Color.blue
        };
        m_Rectangle.PathProps = new PathProperties()
        {
            Stroke = new Stroke()
            {
                Color = Color.red
            }
        };

        m_Scene = new Scene()
        {
            Root = new SceneNode()
            {
                Shapes = new List <Shape> {
                    m_Rectangle
                }
            }
        };

        m_Options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 1.0f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };

        m_Mesh = new Mesh();
        GetComponent <MeshFilter>().mesh = m_Mesh;
    }
Пример #20
0
    public void SetAppearance()
    {
        worldController = WorldController.GetWorldController;
        citizen         = GetComponent <Citizen>();

        skinTone = worldController.skinTones[Random.Range(0, worldController.skinTones.Length)];
        skinTone = ChangeColorBrightness(skinTone);

        string svg =
            @"<svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 30.11 27.39"">
                <g><path id=""Body"" d=""M30.81,17.36c0,7.43-6.63,13.45-14.81,13.45s-14.81-6-14.81-13.45a12.56,12.56,0,0,1,.36-3C3,8.4,8.94,3.92,16,3.92,24.18,3.92,30.81,9.94,30.81,17.36Z"" transform=""translate(-0.94 -3.67)"" fill=""#fff"" stroke=""#000"" stroke-miterlimit=""10"" stroke-width=""0.5""/></g>";

        Hand(0);
        Hand(1);

        int hairChance = Random.Range(0, citizen.gender ? 100 : 55);

        SVGParser.SceneInfo sceneInfo = default;
        if (hairChance < 70)
        {
            SetHair(ref svg, ref sceneInfo);
        }
        else
        {
            svg += "</svg>";

            sceneInfo = SVGParser.ImportSVG(new StringReader(svg));
        }

        var shape = sceneInfo.NodeIDs["Body"].Shapes[0];

        shape.Fill = new SolidFill()
        {
            Color = skinTone
        };

        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        var geoms  = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
        var sprite = VectorUtils.BuildSprite(geoms, 26, VectorUtils.Alignment.Center, Vector2.zero, 128, true);

        GetComponent <SpriteRenderer>().sprite = sprite;
    }
    public void FillMesh_WithFlipYAxis_FlipsYAxis()
    {
        // Build a square of size 1x1 located at (2,2)
        var rectShape = new Shape();

        VectorUtils.MakeRectangleShape(rectShape, new Rect(2, 2, 1, 1));
        rectShape.Fill = new SolidFill()
        {
            Color = Color.red
        };
        var scene = new Scene()
        {
            Root = new SceneNode()
            {
                Shapes = new List <Shape>()
                {
                    rectShape
                }
            }
        };

        var options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 1000.0f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = float.MaxValue,
            SamplingStepSize     = 0.01f
        };
        var geoms = VectorUtils.TessellateScene(scene, options);

        // Build a mesh without flipping
        var mesh = new Mesh();

        VectorUtils.FillMesh(mesh, geoms, 1.0f, false);

        // Build a mesh with flipping
        var flippedMesh = new Mesh();

        VectorUtils.FillMesh(flippedMesh, geoms, 1.0f, true);

        for (int i = 0; i < mesh.vertices.Length; ++i)
        {
            var   vert        = mesh.vertices[i];
            var   flippedVert = flippedMesh.vertices[i];
            float expectedY   = (1.0f - (vert.y - 2.0f)) + 2.0f; // Manual flip for the test!
            Assert.AreEqual(expectedY, flippedVert.y, 0.001f);
        }
    }
Пример #22
0
    /// <summary>
    /// Create a Texture2D icon out of a VectorShape.
    /// </summary>
    public static Texture2D GetIcon(VectorShape shape)
    {
        VectorUtils.TessellationOptions activeOptions = VectorShape.tessellationOptions;
        VectorShape.tessellationOptions = tessellationOptions;
        shape.Dirty = true;

        Rect shapeBounds = shape.ShapeBounds;
        int  width       = Mathf.CeilToInt(shapeBounds.width);
        int  height      = Mathf.CeilToInt(shapeBounds.height);

        renderMesh = shape.ShapeMesh;

        // Save the render state and get a temporary render texture
        VerifyCamera();
        RenderTexture activeTexture = RenderTexture.active;
        RenderTexture tempTexture   = RenderTexture.GetTemporary(width, height, 8, RenderTextureFormat.ARGB32);;

        // Activate the render texture and draw the mesh into it
        renderCamera.orthographicSize   = shapeBounds.height / 2;
        renderCamera.transform.position = new Vector3(shapeBounds.center.x, shapeBounds.center.y, -1);

        renderCamera.targetTexture = tempTexture;

        Camera.onPostRender += OnPostRender;

        renderCamera.Render();

        Camera.onPostRender -= OnPostRender;

        RenderTexture.active = renderCamera.targetTexture;

        Texture2D iconTexture = new Texture2D(width, height);

        iconTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        iconTexture.Apply();

        // Restore the render state and release the temporary objects
        RenderTexture.active       = activeTexture;
        renderCamera.targetTexture = activeTexture;
        RenderTexture.ReleaseTemporary(tempTexture);

        VectorShape.tessellationOptions = activeOptions;
        shape.Dirty = true;

        renderMesh = null;

        return(iconTexture);
    }
Пример #23
0
    protected List <VectorUtils.Geometry> BuildGeometry(Shape shape, VectorUtils.TessellationOptions options)
    {
        var node = new SceneNode()
        {
            Shapes = new List <Shape>  {
                shape
            }
        };
        var scene = new Scene()
        {
            Root = node
        };
        var geom = VectorUtils.TessellateScene(scene, options);

        return(geom);
    }
Пример #24
0
    static VectorShape()
    {
#if UNITY_EDITOR
        handleDrawTexture = new Texture2D(1, 2);
        handleDrawTexture.SetPixel(0, 0, Color.white);
        handleDrawTexture.SetPixel(0, 1, Color.clear);
#endif
        tessellationScene = new Scene();

        tessellationOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 0.1f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = Mathf.PI / 2.0f,
            SamplingStepSize     = 0.01f
        };
    }
Пример #25
0
        protected void GenerateBezierCurve(BezierContour[] bezierContours)
        {
            var vectorScene = new Scene
            {
                Root = new SceneNode
                {
                    Shapes = new List <Shape>
                    {
                        new Shape
                        {
                            Contours = bezierContours,
                            IsConvex = false,
                            Fill     = new SolidFill
                            {
                                Mode = FillMode.NonZero
                            }
                        }
                    }
                }
            };

            var tessellationOptions = new VectorUtils.TessellationOptions
            {
                MaxCordDeviation     = float.MaxValue,
                MaxTanAngleDeviation = Mathf.PI / 2.0f,
                SamplingStepSize     = 0.5f,
                StepDistance         = 0.05f
            };

            VectorUtils.TessellateScene(vectorScene, tessellationOptions);
            var geometry = VectorUtils.TessellateScene(vectorScene, tessellationOptions);

            if (spriteRender)
            {
                _sprite = VectorUtils.BuildSprite(geometry, 1, VectorUtils.Alignment.Center, Vector2.zero, 0);

                spriteRenderer.sprite = _sprite;

                transform.position = geometry[0].UnclippedBounds.center;
            }
            else
            {
                VectorUtils.FillMesh(meshFilter.mesh, geometry, 1f);
            }
        }
Пример #26
0
    /// <summary>
    /// Create a Texture2D icon out of a VectorShape (editor only version).
    /// </summary>
    public static Texture2D GetIcon(VectorShape shape, PreviewRenderUtility renderUtil)
    {
        VectorUtils.TessellationOptions activeOptions = VectorShape.tessellationOptions;
        VectorShape.tessellationOptions = tessellationOptions;
        shape.Dirty = true;

        Rect shapeBounds = shape.ShapeBounds;
        int  width       = Mathf.CeilToInt(shapeBounds.width);
        int  height      = Mathf.CeilToInt(shapeBounds.height);

        // Save the render state and get a temporary render texture
        RenderTexture activeTexture = RenderTexture.active;

        renderUtil.camera.targetTexture   = RenderTexture.GetTemporary(width * 2, height * 2, 8, RenderTextureFormat.ARGB32);
        renderUtil.camera.backgroundColor = Color.clear;

        // Activate the render texture and draw the shape into it
        RenderTexture.active = renderUtil.camera.targetTexture;
        float   cameraSize     = renderUtil.camera.orthographicSize;
        Vector3 cameraPosition = renderUtil.camera.transform.position;

        renderUtil.camera.orthographicSize   = shapeBounds.height / 2;
        renderUtil.camera.transform.position = new Vector3(shapeBounds.center.x, shapeBounds.center.y, -1);

        Matrix4x4 drawMatrix = Matrix4x4.identity;

        renderUtil.DrawMesh(shape.ShapeMesh, drawMatrix, renderMaterial, 0);

        renderUtil.camera.Render();
        renderUtil.camera.orthographicSize   = cameraSize;
        renderUtil.camera.transform.position = cameraPosition;
        Texture2D iconTexture = new Texture2D(width * 2, height * 2);

        iconTexture.ReadPixels(new Rect(0, 0, width * 2, height * 2), 0, 0);
        iconTexture.Apply();

        // Restore the render state and release the temporary render texture
        RenderTexture.active = activeTexture;
        RenderTexture.ReleaseTemporary(renderUtil.camera.targetTexture);

        VectorShape.tessellationOptions = activeOptions;
        shape.Dirty = true;

        return(iconTexture);
    }
Пример #27
0
    public void PreserveViewport_ClipsInViewportSpace()
    {
        string svg =
            @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""100"" viewBox=""600 600 100 100"">
                <rect x=""590"" y=""590"" width=""100"" height=""100"" />
            </svg>";

        var sceneInfo = SVGParser.ImportSVG(new StringReader(svg), 0.0f, 1.0f, 0, 0, true);
        var options   = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100.0f,
            MaxCordDeviation     = float.MaxValue,
            MaxTanAngleDeviation = float.MaxValue,
            SamplingStepSize     = 0.01f
        };
        var geom = VectorUtils.TessellateScene(sceneInfo.Scene, options);

        Assert.That(geom.Count == 1);
        Assert.That(geom[0].Vertices.Length > 0);
    }
Пример #28
0
    public bool Update(List <Geometry> geoms, VectorUtils.TessellationOptions tessOptions)
    {
        bool ret   = false;
        var  genos = VectorUtils.TessellateScene(nodeScene, tessOptions);

        if (genos.Count > 0)
        {
            ret = true;
            genos[0].WorldTransform.m11 = -1;
            if (-1 == _nodeIndex)
            {
                geoms.Add(genos[0]);
                _nodeIndex = geoms.Count - 1;
            }
            else
            {
                geoms[_nodeIndex] = genos[0];
            }
        }
        return(ret);
    }
Пример #29
0
    void Start()
    {
        // Prepare the vector path, add it to the vector scene.
        m_Path = new Path()
        {
            Contour = new BezierContour()
            {
                Segments = new BezierPathSegment[2]
            },
            PathProps = new PathProperties()
            {
                Stroke = new Stroke()
                {
                    Color = Color.white, HalfThickness = 0.1f
                }
            }
        };

        m_Scene = new Scene()
        {
            Root = new SceneNode()
            {
                Drawables = new List <IDrawable> {
                    m_Path
                }
            }
        };

        m_Options = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 1000.0f,
            MaxCordDeviation     = 0.05f,
            MaxTanAngleDeviation = 0.05f,
            SamplingStepSize     = 0.01f
        };

        // Instantiate a new mesh, it will be filled with data in Update()
        m_Mesh = new Mesh();
        GetComponent <MeshFilter>().mesh = m_Mesh;
    }
Пример #30
0
    private void GenerateSprite(int dieValue)
    {
        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100.0f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        ColorShape("face", faceColor);
        ColorShape("shadow", shadowColor);
        ColorShape("border", borderColor);

        ColorCircles(dieValue, circlesColor);

        var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);

        // Build a sprite with the tessellated geometry.
        var sprite = VectorUtils.BuildSprite(geoms, pixelsPerUnit, VectorUtils.Alignment.Center, Vector2.zero, gradientResolution, true);

        GetComponent <SpriteRenderer>().sprite = sprite;
        SaveSprite(sprite, dieValue);
    }