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; }
/// <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); }
public void ImportSVG_CanReadRadialGradientStopDefinedLater() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""20""> <defs> <radialGradient id=""grad"" xlink:href=""#stops"" /> <radialGradient id=""stops""> <stop offset=""0%"" stop-color=""blue"" /> <stop offset=""100%"" stop-color=""red"" /> </radialGradient> </defs> <rect x=""5"" y=""10"" width=""100"" height=""20"" fill=""url(#grad)"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.Scene.Root.Children[0].Shapes[0]; var fill = shape.Fill as GradientFill; Assert.AreEqual(GradientFillType.Radial, fill.Type); Assert.AreEqual(2, fill.Stops.Length); Assert.AreEqual(0.0f, fill.Stops[0].StopPercentage); Assert.AreEqual(Color.blue, fill.Stops[0].Color); Assert.AreEqual(1.0f, fill.Stops[1].StopPercentage); Assert.AreEqual(Color.red, fill.Stops[1].Color); }
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; }
void Start() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""68"" height=""68"" viewBox=""0 0 68 68""> <defs> <clipPath id=""clip-Die""> <rect width=""68"" height=""68""/> </clipPath> </defs> <g id=""Die"" clip-path=""url(#clip-Die)""> <rect width=""68"" height=""68"" fill=""#fff"" fill-opacity=""0""/> <g id=""single_die_simple"" data-name=""single die simple"" transform=""translate(-0.05 -70)""> <path id=""border"" d=""M23.067,70H56.191Q68.05,70,68.05,81.85v44.3Q68.05,138,56.191,138H11.909Q.05,138,.05,125.2V81.85Q.05,70,11.909,70H23.067"" fill=""#305ae1""/> <path id=""shadow"" d=""M21.614,72H11.907Q2.05,72,2.05,81.85v44.3q0,9.85,9.857,9.85H56.19q9.857,0,9.857-9.85V81.85q0-9.85-9.857-9.85H21.614"" transform=""translate(0.001)"" fill=""#eee""/> <path id=""face"" d=""M22.114,73.25Q3.95,73.85,3.35,92v23.95Q4,134.7,23.415,134.7H44.63q20.165,0,20.165-20.15V93.4q0-19.5-18.864-20.15H22.114"" transform=""translate(0.002)"" fill=""#fff""/> <path id=""circle-7"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(18.05 18)"" fill=""#656fb2""/> <path id=""circle-6"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(18.05)"" fill=""#656fb2""/> <path id=""circle-5"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(18.05 -18)"" fill=""#656fb2""/> <path id=""circle-4"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(0.05)"" fill=""#656fb2""/> <path id=""circle-3"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(-17.95 18)"" fill=""#656fb2""/> <path id=""circle-2"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(-17.95)"" fill=""#656fb2""/> <path id=""circle-1"" d=""M40,104a6.01,6.01,0,0,0-10.25-4.25A5.786,5.786,0,0,0,28,104l.05.5.05.4a5.986,5.986,0,0,0,10.15,3.35,6.263,6.263,0,0,0,1.7-3.2l.05-.55V104"" transform=""translate(-17.95 -18)"" fill=""#656fb2""/> </g> </g> </svg>"; // Dynamically import the SVG data, and tessellate the resulting vector scene. sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); GenerateAssets(); }
/// <summary> /// Create a Sprite icon of a SVG image. /// </summary> /// <param name="svgContent">String containing svg content.</param> /// <remarks> /// Standard header and footer will be included if not found in svgContent /// </remarks> public static Sprite GetSprite(string svgContent) { string svg; if (svgContent.StartsWith("<?xml version = \"1.0\" encoding=\"UTF - 8\"?>")) { svg = svgContent; } else { svg = svgIconHeader + svgContent; } if (!svg.EndsWith(svgIconFooter)) { svg = svg + svgIconFooter; } // 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!"); } List <VectorUtils.Geometry> iconGeometry = VectorUtils.TessellateScene(sceneInfo.Scene, tessellationOptions); Sprite sprite = VectorUtils.BuildSprite(iconGeometry, 100f, VectorUtils.Alignment.Center, Vector2.zero, 128, true); return(sprite); }
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; }
/// <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); }
/// <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); } }
public void ImportSVG_RemovesElementsWithDisplayNone() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""20""> <rect width=""100"" height=""20"" display=""none"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); Assert.AreEqual(0, sceneInfo.Scene.Root.Children.Count); }
public void ImportSVG_ImageDoesNotSupportFileURLScheme() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""200"" height=""200""> <image id=""Image0"" xlink:href=""file://192.168.0.1/image.png"" width=""200"" height=""200"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); SceneNode node; Assert.IsFalse(sceneInfo.NodeIDs.TryGetValue("Image0", out node)); }
public void ImportSVG_CreatesScene() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""20""> <rect width=""100"" height=""20"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); Assert.IsNotNull(sceneInfo.Scene); Assert.IsNotNull(sceneInfo.Scene.Root); }
public static Rect CalcBounds(string svg) { using (var reader = new StringReader(svg)) { var sceneInfo = SVGParser.ImportSVG(reader, ViewportOptions.DontPreserve); var tessOptions = SvgToPng.TessellationOptions; var geometry = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions, sceneInfo.NodeOpacity); var vertices = geometry.SelectMany(geom => geom.Vertices.Select(x => (geom.WorldTransform * x))).ToArray(); var bounds = VectorUtils.Bounds(vertices); return(bounds); } }
public void ImportSVG_NoneStrokeIsNull() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""100""> <rect x=""0"" y=""0"" width=""100"" height=""100"" stroke=""none"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.Scene.Root.Children[0].Shapes[0]; Assert.IsNull(shape.PathProps.Stroke); }
public void ImportSVG_SupportsFillOpacities() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""20""> <rect x=""5"" y=""10"" width=""100"" height=""20"" fill=""red"" fill-opacity=""0.5"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.Scene.Root.Children[0].Shapes[0]; var fill = shape.Fill as SolidFill; Assert.AreEqual(0.5f, fill.Color.a); }
/// <summary> /// Parse SVG into VectorShape list. /// </summary> public static List <VectorShape> ReadSVG(System.IO.TextReader svg) { List <VectorShape> shapes = new List <VectorShape>(); SVGParser.SceneInfo sceneInfo = SVGParser.ImportSVG(svg); //Debug.Log(sceneInfo.SceneViewport); Matrix2D rootTransform = Matrix2D.Scale(new Vector2(0.01f, -0.01f)) * sceneInfo.Scene.Root.Transform; RecurseSVGNodes(sceneInfo.Scene.Root, rootTransform, shapes); return(shapes); }
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 bool ImportSVG(TextReader textReader, ViewportOptions viewportOptions, float dpi = 0, float pixelsPerUnit = 1, int windowWidth = 0, int windowHeight = 0) { bool ret = false; try { _scene = SVGParser.ImportSVG(textReader, viewportOptions, dpi, pixelsPerUnit, windowWidth, windowHeight); ret = true; } catch (System.Exception e) { Debug.LogError(e); } return(ret); }
public void ImportSVG_DefaultFillIsBlack() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""100""> <rect x=""0"" y=""0"" width=""100"" height=""100"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.Scene.Root.Children[0].Shapes[0]; Assert.IsNotNull(shape.Fill); var fill = shape.Fill as SolidFill; Assert.AreEqual(Color.black, fill.Color); }
public void ImportSVG_PolygonSkipsDuplicatedPoints() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" viewBox=""0 0 216 216""> <g> <polygon id=""Poly1"" points=""143.781 45.703 72.401 45.703 36.711 107.52 72.401 169.337 143.781 169.337 179.471 107.52 143.781 45.703 143.781 45.703""/> </g> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.NodeIDs["Poly1"].Shapes[0]; Assert.AreEqual(1, shape.Contours.Length); Assert.AreEqual(7, shape.Contours[0].Segments.Length); }
public void ImportSVG_UseCannotOverrideFillOnSymbolWitNoneFill() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""100""> <defs> <rect id=""Rect"" width=""10"" height=""10"" fill=""none"" /> </defs> <use id=""Rect"" xlink:href=""#Rect"" fill=""red"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.NodeIDs["Rect"].Shapes[0]; Assert.IsNull(shape.Fill); }
public void ImportSVG_UseCannotOverrideSymbolStrokeWhenDefined() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""100""> <defs> <rect id=""Rect"" width=""10"" height=""10"" fill=""red"" stroke=""black"" /> </defs> <use id=""Rect"" xlink:href=""#Rect"" stroke=""red"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.NodeIDs["Rect"].Shapes[0]; Assert.IsNotNull(shape.PathProps.Stroke); Assert.AreEqual(Color.black, shape.PathProps.Stroke.Color); }
public void ImportSVG_UseCanOverrideSymbolWithEmptyFillWithColor() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""100""> <defs> <rect id=""Rect"" width=""10"" height=""10"" /> </defs> <use id=""Rect"" xlink:href=""#Rect"" fill=""black"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.NodeIDs["Rect"].Shapes[0]; var solidFill = shape.Fill as SolidFill; Assert.AreEqual(Color.black, solidFill.Color); }
void ApplySVGPath() { string svgPath = "Assets/RouteData/drawsvg.svg"; SVGParser.SceneInfo sceneInfo = SVGParser.ImportSVG(new StreamReader(svgPath)); Shape path = sceneInfo.NodeIDs["e1_polyline"].Shapes[0]; Debug.Log(path); BezierContour[] cs = path.Contours; BezierContour c = cs[0]; //Debug.Log(c); BezierPathSegment[] ss = c.Segments; Debug.Log($"SVGRoute segments count: {ss.Length}"); for (int i = 0; i < ss.Length; i++) { BezierPathSegment s = ss[i]; Debug.Log($"SVGRoute Segment points: {s.P0} -> {s.P1} -> {s.P2}"); var debug1 = GameObject.Find($"SVGTarget{(i * 3) + 1}"); var debug2 = GameObject.Find($"SVGTarget{(i * 3) + 2}"); var debug3 = GameObject.Find($"SVGTarget{(i * 3) + 3}"); debug1.transform.localPosition = s.P0; debug2.transform.localPosition = s.P1; debug3.transform.localPosition = s.P2; Debug.Log(debug3); } // debug1.transform.position = new Vector3(s.P0.x, 0.1f, s.P0.y); // //(s.P0.x / 10) - 10f, 0.1f, (s.P0.y / 10) + 4.3f); // debug2.transform.position = new Vector3(s.P1.x, 0.1f, s.P1.y); // debug3.transform.position = new Vector3(s.P2.x, 0.1f, s.P2.y); var debug0 = GameObject.Find("SVGTarget0"); debug0.transform.localPosition = Vector3.zero; //path. //var fill = shape.Fill as SolidFill; //fill.Color = Color.red; // ... //var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions); //var sprite = VectorUtils.BuildSprite(geoms, 100.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true); }
public void ImportSVG_CanReferenceClipPathDefinedLater() { string svg = @"<svg width=""758"" height=""844"" viewBox=""0 0 758 844"" fill=""none"" xmlns=""http://www.w3.org/2000/svg""> <rect clip-path=""url(#clip0)"" id=""Rect"" x=""378"" y=""180"" width=""300"" height=""300"" fill=""#95BFD0""/> <defs> <clipPath id=""clip0""> <rect width=""758"" height=""844"" fill=""white""/> </clipPath> </defs> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var rectNode = sceneInfo.NodeIDs["Rect"]; Assert.IsNotNull(rectNode.Clipper); }
public void StyleResolver_MatchesIDOverClassOrElementName() { string svg = string.Format( @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""20""> <defs> <style>{0}</style> </defs> <rect id=""myid"" class=""blueish"" width=""100"" height=""20"" /> </svg>", k_GlobalStyle); var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var node = sceneInfo.Scene.Root.Children[0]; var solidFill = node.Shapes[0].Fill as SolidFill; Assert.AreEqual(Color.magenta, solidFill.Color); }
public void StyleResolver_MatchesStar() { string svg = string.Format( @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""20""> <defs> <style>{0}</style> </defs> <circle cx=""50"" cy=""50"" r=""50"" /> </svg>", k_GlobalStyle); var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var node = sceneInfo.Scene.Root.Children[0]; var solidFill = node.Shapes[0].Fill as SolidFill; Assert.AreEqual(Color.green, solidFill.Color); }
public void ImportSVG_UseCanOverrideFillWithTransparentFill() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""100""> <defs> <rect id=""Rect"" width=""10"" height=""10"" /> </defs> <use id=""Rect"" xlink:href=""#Rect"" fill-opacity=""0"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.NodeIDs["Rect"].Shapes[0]; var solidFill = shape.Fill as SolidFill; Assert.IsNotNull(solidFill); Assert.AreEqual(new Color(0, 0, 0, 0), solidFill.Color); }
public void ImportSVG_SupportsGroups() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" width=""100"" height=""20""> <g> <rect x=""5"" y=""10"" width=""100"" height=""20"" /> <rect x=""5"" y=""50"" width=""100"" height=""20"" /> </g> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); Assert.AreEqual(1, sceneInfo.Scene.Root.Children.Count); var group = sceneInfo.Scene.Root.Children[0]; Assert.AreEqual(2, group.Children.Count); }
public void ImportSVG_UseHaveFillAndStrokeFromSymbol() { string svg = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""100""> <defs> <rect id=""Rect"" width=""10"" height=""10"" fill=""#FF0000"" stroke=""#00FF00"" /> </defs> <use id=""Rect0"" xlink:href=""#Rect"" /> </svg>"; var sceneInfo = SVGParser.ImportSVG(new StringReader(svg)); var shape = sceneInfo.NodeIDs["Rect0"].Shapes[0]; var fill = shape.Fill as SolidFill; var stroke = shape.PathProps.Stroke; Assert.AreEqual(Color.red, fill.Color); Assert.AreEqual(Color.green, stroke.Color); }