Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [4.0.0] - 2023-03-07
### Added
- [RuleTile] Add RotatedMirror rule which checks neighbors using both the mirror and rotation Rule in addition to the standard rotation Rule

### Fixed
- [GameObjectBrush] Validate size of GameObjectBrush when it changes
  • Loading branch information
Unity Technologies committed Mar 7, 2023
1 parent 6d5ca46 commit 49dd0fb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [4.0.0] - 2023-03-07
### Added
- [RuleTile] Add RotatedMirror rule which checks neighbors using both the mirror and rotation Rule in addition to the standard rotation Rule

### Fixed
- [GameObjectBrush] Validate size of GameObjectBrush when it changes

## [4.0.0-pre.3] - 2022-11-23
### Fixed
- [TintBrush] Replace obsolete method calls.
Expand Down
13 changes: 12 additions & 1 deletion Editor/Brushes/GameObjectBrush/GameObjectBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ private void OnDisable()
DestroyImmediate(hiddenGrid);
}

private void OnValidate()
{
if (m_Size.x < 0)
m_Size.x = 0;
if (m_Size.y < 0)
m_Size.y = 0;
if (m_Size.z < 0)
m_Size.z = 0;
}

/// <summary>
/// Initializes the content of the GameObjectBrush.
/// </summary>
Expand Down Expand Up @@ -551,8 +561,9 @@ private bool ValidateCellPosition(Vector3Int position)

internal void SizeUpdated(bool keepContents = false)
{
OnValidate();
Array.Resize(ref m_Cells, sizeCount);
BoundsInt bounds = new BoundsInt(Vector3Int.zero, m_Size);
var bounds = new BoundsInt(Vector3Int.zero, m_Size);
foreach (Vector3Int pos in bounds.allPositionsWithin)
{
if (keepContents || m_Cells[GetCellIndex(pos)] == null)
Expand Down
7 changes: 6 additions & 1 deletion Editor/Tiles/RuleTile/RuleTileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class RuleTileEditor : Editor
private const string s_MirrorY = "iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAAG9JREFUOE+djckNACEMAykoLdAjHbPyw1IOJ0L7mAejjFlm9hspyd77Kk+kBAjPOXcakJIh6QaKyOE0EB5dSPJAiUmOiL8PMVGxugsP/0OOib8vsY8yYwy6gRyC8CB5QIWgCMKBLgRSkikEUr5h6wOPWfMoCYILdgAAAABJRU5ErkJggg==";
private const string s_MirrorXY = "iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABl0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC4yMfEgaZUAAAHkSURBVDhPrVJLSwJRFJ4cdXwjPlrVJly1kB62cpEguElXKgYKIpaC+EIEEfGxLqI/UES1KaJlEdGmRY9ltCsIWrUJatGm0eZO3xkHIsJdH3zce+ec75z5zr3cf2MMmLdYLA/BYFA2mUyPOPvwnR+GR4PXaDQLLpfrKpVKSb1eT6bV6XTeocAS4sIw7S804BzEZ4IgsGq1ykhcr9dlj8czwPdbxJdBMyX/As/zLiz74Ar2J9lsVulcKpUYut5DnEbsHFwEx8AhtFqtGViD6BOc1ul0B5lMRhGXy2Wm1+ufkBOE/2fsL1FsQpXCiCAcQiAlk0kJRZjf7+9TRxI3Gg0WCoW+IpGISHHERBS5UKUch8n2K5WK3O125VqtpqydTkdZie12W261WjIVo73b7RZVKccZDIZ1q9XaT6fTLB6PD9BFKhQKjITFYpGFw+FBNBpVOgcCARH516pUGZYZXk5R4B3efLBxDM9f1CkWi/WR3ICtGVh6Rd4NPE+p0iEgmkSRLRoMEjYhHpA4kUiIOO8iZRU8AmnadK2/QOOfhnjPZrO95fN5Zdq5XE5yOBwvuKoNxGfBkQ8FzXkPprnj9Xrfm82mDI8fsLON3x5H/Od+RwHdLfDds9vtn0aj8QoF6QH9JzjuG3acpxmu1RgPAAAAAElFTkSuQmCC";
private const string s_Rotated = "iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwQAADsEBuJFr7QAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAAHdJREFUOE+djssNwCAMQxmIFdgx+2S4Vj4YxWlQgcOT8nuG5u5C732Sd3lfLlmPMR4QhXgrTQaimUlA3EtD+CJlBuQ7aUAUMjEAv9gWCQNEPhHJUkYfZ1kEpcxDzioRzGIlr0Qwi0r+Q5rTgM+AAVcygHgt7+HtBZs/2QVWP8ahAAAAAElFTkSuQmCC";
private const string s_RotatedMirror = "iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAApklEQVQoFY2SMRaAIAxDwefknVg8uIt3ckVSKdYSnjBAi/lprcaUUphZ+3WGY3u1yJcJMBdNtqAyM3BAFRgohBNmUzDEzIDCVQgGK2rL1gAxhatY3vXh+U7hIs2uOqUZ7EGfN6O1RU/wEf5VX4zgAzpTSessIhL5VDrJkrepitJtFtRHvm0YtA6MMfRSUUGcbGC+A0AdOIJx7w1w1y1WWX/FYUV1uQFvVjvOTYh+rAAAAABJRU5ErkJggg==";
private const string s_Fixed = "iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMjHxIGmVAAAA50lEQVQ4T51Ruw6CQBCkwBYKWkIgQAs9gfgCvgb4BML/qWBM9Bdo9QPIuVOQ3JIzosVkc7Mzty9NCPE3lORaKMm1YA/LsnTXdbdhGJ6iKHoVRTEi+r4/OI6zN01Tl/XM7HneLsuyW13XU9u2ous6gYh3kiR327YPsp6ZgyDom6aZYFqiqqqJ8mdZz8xoca64BHjkZT0zY0aVcQbysp6Z4zj+Vvkp65mZttxjOSozdkEzD7KemekcxzRNHxDOHSDiQ/DIy3pmpjtuSJBThStGKMtyRKSOLnSm3DCMz3f+FUpyLZTkOgjtDSWORSDbpbmNAAAAAElFTkSuQmCC";

private static readonly string k_UndoName = L10n.Tr("Change RuleTile");
Expand Down Expand Up @@ -69,12 +70,13 @@ public static Texture2D[] autoTransforms
{
if (s_AutoTransforms == null)
{
s_AutoTransforms = new Texture2D[5];
s_AutoTransforms = new Texture2D[6];
s_AutoTransforms[0] = Base64ToTexture(s_Rotated);
s_AutoTransforms[1] = Base64ToTexture(s_MirrorX);
s_AutoTransforms[2] = Base64ToTexture(s_MirrorY);
s_AutoTransforms[3] = Base64ToTexture(s_Fixed);
s_AutoTransforms[4] = Base64ToTexture(s_MirrorXY);
s_AutoTransforms[5] = Base64ToTexture(s_RotatedMirror);
}
return s_AutoTransforms;
}
Expand Down Expand Up @@ -694,6 +696,9 @@ public virtual void RuleTransformOnGUI(Rect rect, RuleTile.TilingRuleOutput.Tran
case RuleTile.TilingRuleOutput.Transform.MirrorXY:
GUI.DrawTexture(rect, autoTransforms[4]);
break;
case RuleTile.TilingRuleOutput.Transform.RotatedMirror:
GUI.DrawTexture(rect, autoTransforms[5]);
break;
}
GUI.Label(rect, new GUIContent("", ruleTransform.ToString()));
}
Expand Down
53 changes: 46 additions & 7 deletions Runtime/Tiles/RuleTile/RuleTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ public enum Transform
/// <summary>
/// The Rule Tile will mirror in the X or Y axis and match its neighbors.
/// </summary>
MirrorXY
MirrorXY,
/// <summary>
/// The Rule Tile will rotate and mirror in the X and match its neighbors.
/// </summary>
RotatedMirror
}

/// <summary>
Expand Down Expand Up @@ -329,6 +333,15 @@ public void UpdateNeighborPositions()
{
positions.Add(GetMirroredPosition(position, false, true));
}
else if (rule.m_RuleTransform == TilingRuleOutput.Transform.RotatedMirror)
{
var mirroredPosition = GetMirroredPosition(position, true, false);
for (int angle = m_RotationAngle; angle < 360; angle += m_RotationAngle)
{
positions.Add(GetRotatedPosition(position, angle));
positions.Add(GetRotatedPosition(mirroredPosition, angle));
}
}
}
}
}
Expand Down Expand Up @@ -653,7 +666,23 @@ public virtual bool RuleMatches(TilingRule rule, Vector3Int position, ITilemap t
return true;
}
}

// Check rule against x-axis mirror with rotations of 0, 90, 180, 270
else if (rule.m_RuleTransform == TilingRuleOutput.Transform.RotatedMirror)
{
for (int angle = 0; angle < 360; angle += m_RotationAngle)
{
if (angle != 0 && RuleMatches(rule, position, tilemap, angle))
{
transform = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -angle), Vector3.one);
return true;
}
if (RuleMatches(rule, position, tilemap, angle, true))
{
transform = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -angle), new Vector3(-1f, 1f, 1f));
return true;
}
}
}
return false;
}

Expand All @@ -677,8 +706,15 @@ public virtual Matrix4x4 ApplyRandomTransform(TilingRuleOutput.Transform type, M
case TilingRuleOutput.Transform.MirrorY:
return original * Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1f, perlin < 0.5 ? 1f : -1f, 1f));
case TilingRuleOutput.Transform.Rotated:
int angle = Mathf.Clamp(Mathf.FloorToInt(perlin * m_RotationCount), 0, m_RotationCount - 1) * m_RotationAngle;
{
var angle = Mathf.Clamp(Mathf.FloorToInt(perlin * m_RotationCount), 0, m_RotationCount - 1) * m_RotationAngle;
return Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -angle), Vector3.one);
}
case TilingRuleOutput.Transform.RotatedMirror:
{
var angle = Mathf.Clamp(Mathf.FloorToInt(perlin * m_RotationCount), 0, m_RotationCount - 1) * m_RotationAngle;
return Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -angle), new Vector3(perlin < 0.5 ? 1f : -1f, 1f, 1f));
}
}
return original;
}
Expand Down Expand Up @@ -725,14 +761,17 @@ public virtual bool RuleMatch(int neighbor, TileBase other)
/// <param name="tilemap">Tilemap to match.</param>
/// <param name="angle">Rotation angle for matching.</param>
/// <returns>True if there is a match, False if not.</returns>
public bool RuleMatches(TilingRule rule, Vector3Int position, ITilemap tilemap, int angle)
public bool RuleMatches(TilingRule rule, Vector3Int position, ITilemap tilemap, int angle, bool mirrorX = false)
{
var minCount = Math.Min(rule.m_Neighbors.Count, rule.m_NeighborPositions.Count);
for (int i = 0; i < minCount ; i++)
{
int neighbor = rule.m_Neighbors[i];
Vector3Int positionOffset = GetRotatedPosition(rule.m_NeighborPositions[i], angle);
TileBase other = tilemap.GetTile(GetOffsetPosition(position, positionOffset));
var neighbor = rule.m_Neighbors[i];
var neighborPosition = rule.m_NeighborPositions[i];
if (mirrorX)
neighborPosition = GetMirroredPosition(neighborPosition, true, false);
var positionOffset = GetRotatedPosition(neighborPosition, angle);
var other = tilemap.GetTile(GetOffsetPosition(position, positionOffset));
if (!RuleMatch(neighbor, other))
{
return false;
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.unity.2d.tilemap.extras",
"displayName": "2D Tilemap Extras",
"version": "4.0.0-pre.3",
"version": "4.0.0",
"unity": "2023.1",
"description": "2D Tilemap Extras is a package that contains extra scripts for use with 2D Tilemap features in Unity. These include custom Tiles and Brushes for the Tilemap feature.\n\nThe following are included in the package:\nBrushes: GameObject Brush, Group Brush, Line Brush, Random Brush\nTiles: Animated Tile, Rule Tile, Rule Override Tile\nOther: Grid Information, Custom Rules for Rule Tile",
"keywords": [
Expand All @@ -15,7 +15,7 @@
"com.unity.modules.jsonserialize": "1.0.0"
},
"relatedPackages": {
"com.unity.2d.tilemap.extras.tests": "4.0.0-pre.3"
"com.unity.2d.tilemap.extras.tests": "4.0.0"
},
"samples": [
{
Expand All @@ -35,15 +35,15 @@
}
],
"_upm": {
"changelog": "### Fixed\n- [TintBrush] Replace obsolete method calls.\n- [TintBrushSmooth] Replace obsolete method calls.\n- [GameObjectBrush] Fix placement of GameObjects for Hexagon Layouts with Anchor\n- [GameObjectBrush] Align rotation and flip to 2D View in Editor\n- [RandomBrush] Use default color and transform when painting over with RandomBrush\n\n### Added\n- [AnimatedTileEditor] Add field to change TileAnimationFlags\n\n### Changed\n- [AnimatedTileEditor] Moved to Unity.2d.Tilemap.Extras.Editor"
"changelog": "### Added\n- [RuleTile] Add RotatedMirror rule which checks neighbors using both the mirror and rotation Rule in addition to the standard rotation Rule\n\n### Fixed\n- [GameObjectBrush] Validate size of GameObjectBrush when it changes"
},
"upmCi": {
"footprint": "95749e01960c09bfe2ac4ebf52e1c2a45ba9418a"
"footprint": "9700ea727395a7b9e99c30cc1b6d3ab7f7007532"
},
"documentationUrl": "https://docs.unity3d.com/Packages/[email protected]/manual/index.html",
"repository": {
"url": "https://github.com/Unity-Technologies/2d-extras.git",
"type": "git",
"revision": "0860ac36275ca081196ae54e56078366b73b9c6c"
"revision": "9961725b5671e3bdfad54e45abc175e446f2131e"
}
}

0 comments on commit 49dd0fb

Please sign in to comment.