Skip to content

Commit

Permalink
1.0.4-experimental - 2018/03/23
Browse files Browse the repository at this point in the history
@2018.1
  • Loading branch information
ErikMoczi committed Sep 3, 2018
1 parent 1e73afa commit 89da94f
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 13 deletions.
41 changes: 32 additions & 9 deletions package/Editor/SVGParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void Import()
throw new SVGFormatException("Document doesn't have 'svg' root");
svg();

PostProcess();
PostProcess(scene.root);
}

public Dictionary<SceneNode, float> NodeOpacities { get { return nodeOpacity; } }
Expand Down Expand Up @@ -735,23 +735,23 @@ void pattern()
transform = Matrix2D.identity
};

bool relativeToWorld;
bool relativeToWorld = false;
switch (node["patternUnits"])
{
case null:
case "userSpaceOnUse":
relativeToWorld = true;
break;

case "objectBoundingBox":
relativeToWorld = false;
break;

case "userSpaceOnUse":
relativeToWorld = true;
break;

default:
throw node.GetUnsupportedAttribValException("patternUnits");
}

bool contentRelativeToWorld;
bool contentRelativeToWorld = true;
switch (node["patternContentUnits"])
{
case null:
Expand Down Expand Up @@ -1402,12 +1402,12 @@ struct HierarchyUpdate
public SceneNode replaceNode;
}

void PostProcess()
void PostProcess(SceneNode root)
{
var hierarchyUpdates = new List<HierarchyUpdate>();

// Adjust fills on all objects
foreach (var nodeInfo in VectorUtils.WorldTransformedSceneNodes(scene.root, nodeOpacity))
foreach (var nodeInfo in VectorUtils.WorldTransformedSceneNodes(root, nodeOpacity))
{
if (nodeInfo.node.drawables == null)
continue;
Expand Down Expand Up @@ -1588,6 +1588,8 @@ SceneNode AdjustPatternFill(SceneNode node, Matrix2D worldTransform, Filled fill
};
}

PostProcess(patternNode); // This will take care of adjusting gradients/inner-patterns

// Duplicate the filling pattern
var grid = new SceneNode() {
transform = data.patternTransform,
Expand Down Expand Up @@ -1617,6 +1619,17 @@ SceneNode AdjustPatternFill(SceneNode node, Matrix2D worldTransform, Filled fill
};
bounds = VectorUtils.Bounds(boundVerts);

const int kMaxReps = 5000;
float xCount = bounds.xMax / patternRect.width;
float yCount = bounds.yMax / patternRect.height;
if (Mathf.Abs(patternRect.width) < VectorUtils.Epsilon ||
Mathf.Abs(patternRect.height) < VectorUtils.Epsilon ||
(xCount*yCount) > kMaxReps)
{
Debug.LogWarning("Ignoring pattern which would result in too many repetitions");
return null;
}

// Start the pattern filling process
var offset = patternRect.position;
float xStart = (int)(bounds.x / patternRect.width) * patternRect.width - patternRect.width;
Expand Down Expand Up @@ -2261,6 +2274,16 @@ public static object ParseRelativeRef(string iri, SVGDictionary dict)
fill = (new SVGAttribParser(paintParts[1], attribName, opacity, mode, dict, false)).fill;
else Debug.LogWarning("Referencing non-existent paint (" + reference + ")");
}
else
{
var gradientFill = fill as GradientFill;
if (gradientFill != null)
gradientFill.tint = new Color(1.0f, 1.0f, 1.0f, opacity);

var textureFill = fill as TextureFill;
if (textureFill != null)
textureFill.tint = new Color(1.0f, 1.0f, 1.0f, opacity);
}
return;
}
}
Expand Down
3 changes: 3 additions & 0 deletions package/Editor/SVGSpriteData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public void Load(SerializedObject so)
{
var importer = so.targetObject as SVGImporter;
var sprite = SVGImporter.GetImportedSprite(importer.assetPath);
if (sprite == null)
return;

spriteRect.name = sprite.name;

int targetWidth;
Expand Down
15 changes: 14 additions & 1 deletion package/Runtime/VectorRasterization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ public struct PackRectItem
static List<PackRectItem> PackRects(IList<KeyValuePair<IFill, Vector2>> fillSizes, out Vector2 atlasDims)
{
var pack = new List<PackRectItem>(fillSizes.Count);
var fillSetting = new Dictionary<IFill, int>();
atlasDims = new Vector2(1024, 1024);
var maxPos = Vector2.zero;
var curPos = Vector2.zero;
float curColThickness = 0.0f;
int currentSetting = 1;

foreach (var fillSize in fillSizes)
{
var fill = fillSize.Key;
Expand All @@ -142,8 +144,19 @@ static List<PackRectItem> PackRects(IList<KeyValuePair<IFill, Vector2>> fillSize
curPos.y = 0;
curColThickness = size.x;
}

curColThickness = Mathf.Max(curColThickness, size.x);
int setting = fill == null ? 0 : currentSetting++;

int setting = 0;
if (fill != null)
{
if (!fillSetting.TryGetValue(fill, out setting))
{
setting = currentSetting++;
fillSetting[fill] = setting;
}
}

pack.Add(new PackRectItem() { position = curPos, size = size, fill = fill, settingIndex = setting });
maxPos = Vector2.Max(maxPos, curPos + size);
curPos.y += size.y;
Expand Down
5 changes: 5 additions & 0 deletions package/Runtime/VectorRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ private static void TessellateRectangleSquareCorners(Rectangle rect, List<Geomet
var solidFill = rect.fill as SolidFill;
var color = solidFill != null ? solidFill.color : Color.white;

if (rect.fill is GradientFill)
color *= (rect.fill as GradientFill).tint;
if (rect.fill is TextureFill)
color *= (rect.fill as TextureFill).tint;

geoms.Add(new Geometry() { vertices = vertices, indices = indices, color = color, fill = rect.fill, fillTransform = rect.fillTransform });
}

Expand Down
8 changes: 8 additions & 0 deletions package/Runtime/VectorScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public class GradientFill : IFill
/// <summary>The filling method (non-zero or even-odd) of the fill.</summary>
public FillMode mode { get; set; }

/// <summary>The tint of the filling</summary>
public Color tint { get { return m_Tint; } set { m_Tint = value; } }
private Color m_Tint = Color.white;

/// <summary>The adressing mode (wrap, clamp or mirror) of this fill.</summary>
public AddressMode addressing { get; set; }

Expand All @@ -173,6 +177,10 @@ public class TextureFill : IFill
/// <summary>The filling method (non-zero or even-odd) of the fill.</summary>
public FillMode mode { get; set; }

/// <summary>The tint of the filling</summary>
public Color tint { get { return m_Tint; } set { m_Tint = value; } }
private Color m_Tint = Color.white;

/// <summary>The adressing mode (wrap, clamp or mirror) of this fill.</summary>
public AddressMode addressing { get; set; }
}
Expand Down
21 changes: 19 additions & 2 deletions package/Runtime/VectorSceneTesselation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ internal static void TessellateShape(Shape vectorShape, List<Geometry> geoms, Te
if (vectorShape.fill is SolidFill)
shapeColor = ((SolidFill)vectorShape.fill).color;

if (vectorShape.fill is GradientFill)
shapeColor *= (vectorShape.fill as GradientFill).tint;
if (vectorShape.fill is TextureFill)
shapeColor *= (vectorShape.fill as TextureFill).tint;

var tess = new Tess();

var angle = 45.0f * Mathf.Deg2Rad;
Expand Down Expand Up @@ -353,7 +358,10 @@ public static TextureAtlas GenerateAtlas(IEnumerable<Geometry> geoms, uint raste
atlasSize.y += 1;

// Need enough space on first row for texture settings
int minWidth = (texturedGeomCount+1) * 3;
int maxSettingIndex = 0;
foreach (var item in pack)
maxSettingIndex = Math.Max(maxSettingIndex, item.settingIndex);
int minWidth = (maxSettingIndex+1) * 3;
atlasSize.x = Math.Max(minWidth, (int)atlasSize.x);

int atlasWidth = (int)atlasSize.x;
Expand Down Expand Up @@ -381,14 +389,23 @@ public static TextureAtlas GenerateAtlas(IEnumerable<Geometry> geoms, uint raste
WriteRawInt2Packed(rawAtlasTex, (int)whiteTexelsScreenPos.x, (int)whiteTexelsScreenPos.y, 0, 0);
WriteRawInt2Packed(rawAtlasTex, (int)whiteTexelsScreenPos.x, (int)whiteTexelsScreenPos.y, 1, 0);

var writtenSettings = new HashSet<int>();
writtenSettings.Add(0);

foreach (var g in geoms)
{
AtlasEntry entry;
int vertsCount = g.vertices.Length;
if ((g.fill != null) && fills.TryGetValue(g.fill, out entry))
{
int setting = entry.atlasLocation.settingIndex;
if (writtenSettings.Contains(setting))
continue;

writtenSettings.Add(setting);

// There are 3 consecutive pixels to store the settings
int destX = entry.atlasLocation.settingIndex * 3;
int destX = setting * 3;

var gradientFill = g.fill as GradientFill;
if (gradientFill != null)
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.unity.vectorgraphics",
"version": "1.0.3-experimental",
"version": "1.0.4-experimental",
"unity": "2018.1",
"description": "Vector graphics importers and related utilities.",
"keywords": ["vector", "graphics", "svg", "importer"],
Expand Down
1 change: 1 addition & 0 deletions versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
1.0.1-experimental
1.0.2-experimental
1.0.3-experimental
1.0.4-experimental

0 comments on commit 89da94f

Please sign in to comment.