1- using Unity . Jobs ;
1+ using System . Collections . Generic ;
2+ using Unity . Jobs ;
23using UnityEngine ;
34using UnityEngine . Tilemaps ;
45
@@ -7,7 +8,9 @@ namespace LDtkUnity.Editor
78 internal sealed class LDtkBuilderTileset : LDtkBuilderLayer
89 {
910 private TileInstance [ ] _tiles ;
10- private TilemapTilesBuilder _tilesetProvider ;
11+
12+ //in most realistic situations, tiles will not overlap, but we can overestimate anyways to avoid resizing
13+ private Dictionary < Vector3Int , int > _depth ;
1114
1215 public Tilemap Map ;
1316
@@ -87,7 +90,7 @@ public void BuildTileset(TileInstance[] tiles)
8790 LDtkProfiler . EndSample ( ) ;
8891
8992 LDtkProfiler . BeginSample ( "new ConstructNewTilemap" ) ;
90- _tilesetProvider = new TilemapTilesBuilder ( Map , tiles . Length ) ;
93+ _depth = new Dictionary < Vector3Int , int > ( 10 ) ;
9194 LDtkProfiler . EndSample ( ) ;
9295
9396 //if we are also an intgrid layer, then we already reduced our position in the intGridBuilder
@@ -102,15 +105,17 @@ public void BuildTileset(TileInstance[] tiles)
102105 Map . SetOpacity ( Layer ) ;
103106 LDtkProfiler . EndSample ( ) ;
104107
105- LDtkProfiler . BeginSample ( "CacheNeededTilesArtifacts" ) ;
106- TileBase [ ] tileAssets = new TileBase [ tilesLength ] ;
108+ LDtkProfiler . BeginSample ( "CacheNeededTilesArtifactsAndSetupColor" ) ;
109+ TileChangeData [ ] tileAssets = new TileChangeData [ tilesLength ] ;
110+ //Determine tile asset and setup color while the job is running
107111 LDtkTilesetTile [ ] artifactTiles = artifacts . _tiles ;
108112 int artifactCount = artifactTiles . Length ;
109113 for ( int i = 0 ; i < tilesLength ; i ++ )
110114 {
111- int ? t = _tiles [ i ] . T ;
115+ tileAssets [ i ] . color = new Color ( 1 , 1 , 1 , _tiles [ i ] . A ) ;
112116
113- //it's possible that a t value is null in the json, unfortunately
117+ int ? t = _tiles [ i ] . T ;
118+ //it's possible that a t value is null in the json, unfortunately for performance's sake
114119 if ( t == null )
115120 {
116121 continue ;
@@ -122,11 +127,12 @@ public void BuildTileset(TileInstance[] tiles)
122127 int tValue = t . Value ;
123128 if ( tValue < artifactCount )
124129 {
125- tileAssets [ i ] = artifactTiles [ tValue ] ;
130+ tileAssets [ i ] . tile = artifactTiles [ tValue ] ;
126131 }
127132 }
128133 LDtkProfiler . EndSample ( ) ;
129134
135+ //Job is done, we can use the output now!
130136 LDtkProfiler . BeginSample ( "handle.Complete" ) ;
131137 handle . Complete ( ) ;
132138 LDtkProfiler . EndSample ( ) ;
@@ -135,35 +141,24 @@ public void BuildTileset(TileInstance[] tiles)
135141 job . Input . Dispose ( ) ;
136142 LDtkProfiler . EndSample ( ) ;
137143
138- LDtkProfiler . BeginSample ( "RecalculateCellPositions" ) ;
139- Vector3Int [ ] cells = new Vector3Int [ tilesLength ] ;
144+ LDtkProfiler . BeginSample ( "RecalculateCellPositionsAndSetupMatrix" ) ;
140145 for ( int i = 0 ; i < tilesLength ; i ++ )
141146 {
142- cells [ i ] = job . Output [ i ] . Cell ;
143- cells [ i ] . z = _tilesetProvider . GetNextCellZ ( cells [ i ] ) ;
147+ //todo find a way to improve performance of this somehow where we dont need to make a vector3int copy
148+ Vector3Int cell = job . Output [ i ] . Cell ;
149+ cell . z = GetNextCellZ ( cell ) ;
150+ tileAssets [ i ] . position = cell ;
151+ tileAssets [ i ] . transform = job . Output [ i ] . Matrix ;
144152 }
145153 LDtkProfiler . EndSample ( ) ;
146154
147155 LDtkProfiler . BeginSample ( "Tilemap.SetTiles" ) ;
148- Map . SetTiles ( cells , tileAssets ) ;
149- LDtkProfiler . EndSample ( ) ;
150-
151- LDtkProfiler . BeginSample ( "SetColorAndMatrix" ) ;
152- for ( int i = 0 ; i < tilesLength ; i ++ )
153- {
154- Color color = new Color ( 1 , 1 , 1 , _tiles [ i ] . A ) ;
155- Matrix4x4 matrix = job . Output [ i ] . Matrix ;
156- _tilesetProvider . SetColorAndMatrix ( cells [ i ] , ref color , ref matrix ) ;
157- }
156+ TilemapTilesBuilder . SetTiles ( Map , tileAssets ) ;
158157 LDtkProfiler . EndSample ( ) ;
159158
160159 LDtkProfiler . BeginSample ( "Output.Dispose" ) ;
161160 job . Output . Dispose ( ) ;
162161 LDtkProfiler . EndSample ( ) ;
163-
164- LDtkProfiler . BeginSample ( "ApplyExtraData" ) ;
165- _tilesetProvider . ApplyExtraData ( ) ;
166- LDtkProfiler . EndSample ( ) ;
167162
168163 LDtkProfiler . BeginSample ( "CompressBounds" ) ;
169164 Map . CompressBounds ( ) ;
@@ -174,5 +169,18 @@ private TilesetDefinition EvaluateTilesetDefinition()
174169 {
175170 return Layer . OverrideTilesetUid != null ? Layer . OverrideTilesetDefinition : Layer . TilesetDefinition ;
176171 }
172+
173+ /// <param name="cell">Z is always 0</param>
174+ private int GetNextCellZ ( Vector3Int cell )
175+ {
176+ if ( ! _depth . ContainsKey ( cell ) )
177+ {
178+ _depth . Add ( cell , 0 ) ;
179+ return 0 ;
180+ }
181+
182+ _depth [ cell ] += 1 ;
183+ return _depth [ cell ] ;
184+ }
177185 }
178186}
0 commit comments