Skip to content

Commit

Permalink
changed how additional tiles are generated and loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed Sep 20, 2023
1 parent 75d184c commit 9575d54
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 65 deletions.
13 changes: 12 additions & 1 deletion Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,22 @@ public Sprite GetAdditionalSprite(LDtkProjectImporter project, TilesetDefinition
return null;
}

Sprite sprite = artifacts.GetAdditionalSpriteForRectSlow(id, def.PxHei);
Sprite sprite = null;

Profiler.BeginSample("GetAdditionalSpriteForRectByNameCheck");
sprite = artifacts.GetAdditionalSpriteForRectByName(id, def.PxHei);
Profiler.EndSample();
if (sprite)
{
return sprite;
}
/*Profiler.BeginSample("GetAdditionalSpriteForRect");
sprite = artifacts.GetAdditionalSpriteForRect(id, def);
Profiler.EndSample();
if (sprite)
{
return sprite;
}*/

Logger.LogError($"Failed to load an additional sprite at id \"{id}\" from \"{def.Identifier}\"");
return null;
Expand Down
97 changes: 40 additions & 57 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal sealed partial class LDtkTilesetImporter : LDtkJsonImporter<LDtkTileset
/// It's separate because we don't want to draw them in the sprite editor window, or otherwise make them configurable.
/// Also because they won't have tilemap assets generated for them anyways, as their size wouldn't fit in the tilemap.
/// </summary>
[SerializeField] internal List<LDtkSpriteRect> _additionalTiles = new List<LDtkSpriteRect>();
private List<LDtkSpriteRect> _additionalTiles = new List<LDtkSpriteRect>();
[SerializeField] internal SecondarySpriteTexture[] _secondaryTextures;

private Texture2D _cachedTex;
Expand Down Expand Up @@ -128,21 +128,17 @@ protected override void Import()
var rects = ReadSourceRectsFromJsonDefinition(_definition.Def);
Profiler.EndSample();

bool changedMetaData = false;

Profiler.BeginSample("ReformatRectMetaData");
changedMetaData |= ReformatRectMetaData(rects);
if (ReformatRectMetaData(rects))
{
EditorUtility.SetDirty(this);
}
Profiler.EndSample();

Profiler.BeginSample("ReformatAdditionalTiles");
changedMetaData |= ReformatAdditionalTiles();
ReformatAdditionalTiles();
Profiler.EndSample();

if (changedMetaData)
{
EditorUtility.SetDirty(this);
}

Profiler.BeginSample("PrepareGenerate");
TextureGenerationOutput output = PrepareGenerate(platformSettings);
Profiler.EndSample();
Expand Down Expand Up @@ -276,63 +272,50 @@ public static bool IsShapeSetForGrid(List<Vector2> shape)
shape.Any(p => p == GridCheck4);
}

private bool ReformatAdditionalTiles()
private void ReformatAdditionalTiles()
{
var srcRects = _definition.Rects;
bool changed = false;
Debug.Assert(_definition != null);
//Debug.Assert();

//if no tiles were populated (can be null)
if (srcRects.IsNullOrEmpty())
var additionalRects = _definition.Rects;
if (additionalRects.IsNullOrEmpty())
{
changed = _additionalTiles.Any();
_additionalTiles.Clear();
return changed;
return;
}

if (_additionalTiles.Count > srcRects.Count)
_additionalTiles.Clear();
for (int i = _additionalTiles.Count; i < additionalRects.Count; i++)
{
_additionalTiles.RemoveRange(srcRects.Count, _additionalTiles.Count - srcRects.Count);
changed = true;
}

if (_additionalTiles.Count < srcRects.Count)
{
for (int i = _additionalTiles.Count; i < srcRects.Count; i++)
var rect = _definition.Rects[i].ToRect();
rect = LDtkCoordConverter.ImageSlice(rect, _definition.Def.PxHei);
LDtkSpriteRect newRect = new LDtkSpriteRect
{
var rect = _definition.Rects[i].ToRect();
rect = LDtkCoordConverter.ImageSlice(rect, _definition.Def.PxHei);
LDtkSpriteRect newRect = new LDtkSpriteRect
{
border = Vector4.zero,
pivot = new Vector2(0.5f, 0.5f),
alignment = SpriteAlignment.Center,
rect = rect,
spriteID = GUID.Generate(),
name = MakeAssetName()
};
_additionalTiles.Add(newRect);

string MakeAssetName()
{
StringBuilder sb = new StringBuilder();
sb.Append(_definition.Def.Identifier);
sb.Append('_');
sb.Append(rect.x);
sb.Append('_');
sb.Append(rect.y);
sb.Append('_');
sb.Append(rect.width);
sb.Append('_');
sb.Append(rect.height);
return sb.ToString();
}
border = Vector4.zero,
pivot = new Vector2(0.5f, 0.5f),
alignment = SpriteAlignment.Center,
rect = rect,
spriteID = GUID.Generate(),
name = MakeAssetName()
};
_additionalTiles.Add(newRect);

string MakeAssetName()
{
StringBuilder sb = new StringBuilder();
sb.Append(_definition.Def.Identifier);
sb.Append('_');
sb.Append(rect.x);
sb.Append('_');
sb.Append(rect.y);
sb.Append('_');
sb.Append(rect.width);
sb.Append('_');
sb.Append(rect.height);
return sb.ToString();
}
changed = true;
}

Debug.Assert(_additionalTiles.Count == srcRects.Count);

return changed;
Debug.Assert(_additionalTiles.Count == additionalRects.Count);
}

private static void RefreshSceneTilemapColliders()
Expand Down
37 changes: 37 additions & 0 deletions Assets/LDtkUnity/Runtime/Tools/LDtkCoordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ public static Vector2Int IntGridValueCsvCoord(int csvIndex, Vector2Int cellSize)
LDtkDebug.LogError("Failed to get CSV coord");
return Vector2Int.zero;
}

//this is slow. find a way to speed it up?
public static int TilesetSliceIndex(Rect rect, TilesetDefinition def)
{
int gridSize = def.TileGridSize;

int rectX = (int)rect.x;
int rectY = (int)rect.y;
int rectW = (int)rect.width;
int rectH = (int)rect.height;

if (rectW != rectH)
{
return -1;
}

//dont need to check height because of above
if (rectW != gridSize)
{
return -1;
}

int i = 0;
for (int y = 0; y < def.PxWid; y += def.TileGridSize)
{
for (int x = 0; x < def.PxHei; x += def.TileGridSize)
{
if (rectX == x && rectY == y)
{
return i;
}
i++;
}
}

return -1;
}

public static Vector2Int ConvertCell(Vector2Int cellPos, int verticalCellCount)
{
Expand Down
8 changes: 7 additions & 1 deletion Assets/LDtkUnity/Runtime/Tools/LDtkExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Tilemaps;
Expand Down Expand Up @@ -77,6 +78,11 @@ internal static GameObject CreateChildGameObject(this GameObject parent, string
return child;
}

internal static bool IsInteger(this float value)
{
return Math.Abs(value - Mathf.Floor(value)) < 0.0001f;
}

internal static Rect ToRect(this RectInt rect)
{
return new Rect(rect.x, rect.y, rect.width, rect.height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,23 @@ internal Dictionary<Rect, Sprite> AdditionalSpritesToDict()
return _additionalSprites.ToDictionary(sprite => sprite.rect);
}

internal Sprite GetAdditionalSpriteForRectSlow(Rect rect, int pxHei)
internal Sprite GetAdditionalSpriteForRect(Rect rect, TilesetDefinition def)
{
return _additionalSprites.FirstOrDefault(p => p.rect == LDtkCoordConverter.ImageSlice(rect, pxHei));
Debug.Log($"");
Debug.Log($"trying slice index {rect}");
int i = LDtkCoordConverter.TilesetSliceIndex(rect, def);
if (i == -1)
{
return null;
}

Debug.Log($"Getting a perfect sprite at {i}!");
return _sprites[i];
}

internal Sprite GetAdditionalSpriteForRectByName(Rect rect, int textureHeight)
{
return _additionalSprites.FirstOrDefault(p => p.rect == LDtkCoordConverter.ImageSlice(rect, textureHeight));
}
}
}
Loading

0 comments on commit 9575d54

Please sign in to comment.