Skip to content

Commit

Permalink
applying extra data, made jobs run upon schedule. made tileset evalua…
Browse files Browse the repository at this point in the history
…tion run above the scheduling to avoid unnessesary jobs runs
  • Loading branch information
Cammin committed May 20, 2024
1 parent 8ed1a10 commit e336a7c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
56 changes: 29 additions & 27 deletions Assets/LDtkUnity/Editor/Builders/LDtkBuilderTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ public void BuildTileset(TileInstance[] tiles)
{
_tiles = tiles;

LDtkProfiler.BeginSample("EvaluateTilesetDefinition");
TilesetDefinition tilesetDef = EvaluateTilesetDefinition();
LDtkProfiler.EndSample();
if (tilesetDef == null)
{
//It is possible that a layer has no tileset definition assigned. In this case, it's fine to not build any tiles.
return;
}

LDtkProfiler.BeginSample("LoadTilesetArtifacts");
LDtkArtifactAssetsTileset artifacts = Importer.LoadTilesetArtifacts(Project, tilesetDef);
LDtkProfiler.EndSample();

if (artifacts == null)
{
//failure to load should not spend time calculating tiles
return;
}

LDtkProfiler.BeginSample("construct TileBuildingJob");
TileBuildingJob job = new TileBuildingJob(_tiles, Layer, LayerScale);
LDtkProfiler.EndSample();
Expand All @@ -63,6 +82,7 @@ public void BuildTileset(TileInstance[] tiles)
int tilesLength = _tiles.Length;
int innerLoopBatchCount = Mathf.Max(1, (tilesLength / System.Environment.ProcessorCount) + 1);
JobHandle handle = job.ScheduleParallel(tilesLength, innerLoopBatchCount, default);
JobHandle.ScheduleBatchedJobs();
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("ConstructNewTilemap");
Expand All @@ -85,31 +105,6 @@ public void BuildTileset(TileInstance[] tiles)
Map.SetOpacity(Layer);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("EvaluateTilesetDefinition");
TilesetDefinition tilesetDef = EvaluateTilesetDefinition();
LDtkProfiler.EndSample();
if (tilesetDef == null)
{
//It is possible that a layer has no tileset definition assigned. In this case, it's fine to not build any tiles.
handle.Complete();
job.Input.Dispose();
job.Output.Dispose();
return;
}

LDtkProfiler.BeginSample("LoadTilesetArtifacts");
LDtkArtifactAssetsTileset artifacts = Importer.LoadTilesetArtifacts(Project, tilesetDef);
LDtkProfiler.EndSample();

if (artifacts == null)
{
//failure to load should not spend time calculating tiles
handle.Complete();
job.Input.Dispose();
job.Output.Dispose();
return;
}

LDtkProfiler.BeginSample("CacheNeededTilesArtifacts");
TileBase[] tileAssets = new TileBase[tilesLength];
for (int i = 0; i < tilesLength; i++)
Expand All @@ -127,7 +122,10 @@ public void BuildTileset(TileInstance[] tiles)
LDtkProfiler.EndSample();

handle.Complete();

LDtkProfiler.BeginSample("Input.Dispose");
job.Input.Dispose();
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("AddTiles");
Vector3Int[] cells = new Vector3Int[tilesLength];
Expand All @@ -151,7 +149,13 @@ public void BuildTileset(TileInstance[] tiles)
}
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("Output.Dispose");
job.Output.Dispose();
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("ApplyExtraData");
_tilesetProvider.ApplyExtraData();
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("CompressBounds");
Map.CompressBounds();
Expand All @@ -162,7 +166,5 @@ private TilesetDefinition EvaluateTilesetDefinition()
{
return Layer.OverrideTilesetUid != null ? Layer.OverrideTilesetDefinition : Layer.TilesetDefinition;
}


}
}
25 changes: 11 additions & 14 deletions Assets/LDtkUnity/Editor/Builders/TilemapTilesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ public void ApplyPendingTiles(bool isIntGrid)
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("ApplyExtraData");
foreach (KeyValuePair<Vector3Int,ExtraData> pair in _extraData)
{
pair.Value.ApplyExtraValues(Map, pair.Key);
}
ApplyExtraData();
LDtkProfiler.EndSample();

if (!isIntGrid)
{
return;
Expand All @@ -108,6 +105,14 @@ public void ApplyPendingTiles(bool isIntGrid)
LDtkProfiler.EndSample();
}

public void ApplyExtraData()
{
foreach (KeyValuePair<Vector3Int,ExtraData> pair in _extraData)
{
pair.Value.ApplyExtraValues(Map, pair.Key);
}
}

public void SetColor(Vector3Int cell, Color color)
{
if (!_extraData.ContainsKey(cell))
Expand All @@ -128,17 +133,9 @@ public void SetTransformMatrix(Vector3Int cell, Matrix4x4 matrix)

public void SetColorAndMatrix(Vector3Int cell, ref Color color, ref Matrix4x4 matrix)
{
if (!_extraData.ContainsKey(cell))
{
_extraData.Add(cell, new ExtraData());
}
_extraData.Add(cell, new ExtraData());
_extraData[cell].color = color;
_extraData[cell].matrix = matrix;
}

public void SetAllTiles(Vector3Int[] cells, TileBase[] tiles)
{

}
}
}

0 comments on commit e336a7c

Please sign in to comment.