/
TilemapSystem3D.cs
687 lines (618 loc) · 44.2 KB
/
TilemapSystem3D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
using System.Collections.Generic;
/// <summary>
/// This script file contains only the runtime stuffs of the 'TilemapSystem3D' class.
/// </summary>
namespace UnityEngine.TilemapSystem3D
{
[AddComponentMenu("Tilemap/3D Tilemap System")]
[ExecuteInEditMode]
public partial class TilemapSystem3D : MonoBehaviour
{
/// <summary>
/// The shader used by the Standard Render Pipeline.
/// </summary>
public Shader legacyShader;
/// <summary>
/// The shader used by the Universal Render Pipeline.
/// </summary>
public Shader universalShader;
/// <summary>
/// The render pipeline used in this project.
/// </summary>
public TilemapSystemPipeline renderPipeline = TilemapSystemPipeline.Legacy;
/// <summary>
/// The resolution of the 3D Tileset texture.
/// </summary>
public int tilesetSize = 32;
/// <summary>
/// The size of the 3D Tilemap texture. It should be proportional to the terrain size or mesh uv.
/// </summary>
public Vector4 gridMapSize = new Vector4(32, 32, 1, 1);
/// <summary>
/// Stores the data of the current tilemap.
/// </summary>
public TilemapData tilemapData;
/// <summary>
/// The list of all layers in this tilemap.
/// </summary>
public List<TilemapSystemLayer> tilemapLayerList;
/// <summary>
/// Texture containing the tilemap data, it is used by the render material to determine which tile should be
/// rendered in each grid position.
/// </summary>
public Texture3D tilemapTexture;
/// <summary>
/// Pack texture containing all the tiles used by the painting system.
/// </summary>
public Texture3D tilesetTexture;
/// <summary>
/// Texture used to store the tiles count and alpha intensity of each layer.
/// In this situation is better to use a texture than a regular array because we can resize the texture every
/// time we want, what is not possible with a shader array.
/// Each pixel of this texture will store the data of the corespondent layer from the layer list.
/// Channel R: Stores the tiles count on that layer.
/// Channel G: Stores the alpha intensity of that layer.
/// </summary>
public Texture2D layerArrayTexture;
/// <summary>
/// The material used to render the tilemap system on the mesh.
/// </summary>
public Material renderMaterial;
/// <summary>
/// A block of material values to apply to the renderer component.
/// </summary>
public MaterialPropertyBlock materialPropertyBlock;
/// <summary>
/// Cached material property index.
/// </summary>
private static readonly int TilemapTexture = Shader.PropertyToID("_TilemapTexture");
private static readonly int TilesetTexture = Shader.PropertyToID("_TilesetTexture");
private static readonly int LayerArrayTexture = Shader.PropertyToID("_LayerArrayTexture");
private static readonly int LayersCount = Shader.PropertyToID("_LayersCount");
private static readonly int TilesCount = Shader.PropertyToID("_TilesCount");
private static readonly int GridMapSize = Shader.PropertyToID("_GridMapSize");
/// <summary>
/// Translate the result from the auto tile bitmasking computation to correct layout index.
/// </summary>
private readonly Dictionary<int, int> m_8BitMasking = new Dictionary<int, int>()
{
{0, 0}, {1, 1}, {4, 2}, {5, 3}, {7, 4}, {16, 5}, {17, 6}, {20, 7}, {21, 8}, {23, 9}, {28, 10}, {29, 11}, {31, 12}, {64, 13}, {65, 14}, {68, 15},
{69, 16}, {71, 17}, {80, 18}, {81, 19}, {84, 20}, {85, 21}, {87, 22}, {92, 23}, {93, 24}, {95, 25}, {112, 26}, {113, 27}, {116, 28}, {117, 29}, {119, 30}, {124, 31},
{125, 32}, {127, 33}, {193, 34}, {197, 35}, {199, 36}, {209, 37}, {213, 38}, {215, 39}, {221, 40}, {223, 41}, {241, 42}, {245, 43}, {247, 44}, {253, 45}, {255, 46}
};
/// <summary>
/// Called at start.
/// </summary>
private void Start()
{
if(tilemapData)
{
// Get the texture references stored in the TilemapData
tilemapTexture = tilemapData.tilemapTexture;
tilesetTexture = tilemapData.tilesetTexture;
layerArrayTexture = tilemapData.layerArrayTexture;
// Refresh the render material
UpdateRenderMaterial();
// Editor only
#if UNITY_EDITOR
UpdateLayerSettings();
#endif
}
}
/// <summary>
/// Called every frame.
/// </summary>
private void Update()
{
// For an unknown reason, the render material is reseted every time the scene is saved.
// So it is necessary to update the render material every frame only in the editor.
// This is not necessary in the build and is not recommended for performance reasons.
#if UNITY_EDITOR
if (tilemapData)
UpdateRenderMaterial();
#endif
}
/// <summary>
/// Update the render material.
/// </summary>
public void UpdateRenderMaterial()
{
// Update the material property block
if (materialPropertyBlock == null)
materialPropertyBlock = new MaterialPropertyBlock();
materialPropertyBlock.SetTexture(TilemapTexture, tilemapTexture);
materialPropertyBlock.SetTexture(TilesetTexture, tilesetTexture);
materialPropertyBlock.SetTexture(LayerArrayTexture, layerArrayTexture);
materialPropertyBlock.SetInt(LayersCount, tilemapTexture.depth);
materialPropertyBlock.SetInt(TilesCount, tilesetTexture.depth);
materialPropertyBlock.SetVector(GridMapSize, gridMapSize);
// Update the render material properties
renderMaterial.SetTexture(TilemapTexture, tilemapTexture);
renderMaterial.SetTexture(TilesetTexture, tilesetTexture);
renderMaterial.SetTexture(LayerArrayTexture, layerArrayTexture);
renderMaterial.SetInt(LayersCount, tilemapTexture.depth);
materialPropertyBlock.SetInt(TilesCount, tilesetTexture.depth);
materialPropertyBlock.SetVector(GridMapSize, gridMapSize);
// Set the material to the terrain or custom mesh
//#if UNITY_EDITOR
Terrain terrainComponent = gameObject.GetComponent<Terrain>();
if (terrainComponent)
{
terrainComponent.materialTemplate = renderMaterial;
terrainComponent.SetSplatMaterialPropertyBlock(materialPropertyBlock);
}
Renderer rendererComponent = gameObject.GetComponent<Renderer>();
if (rendererComponent)
{
rendererComponent.material = renderMaterial;
rendererComponent.SetPropertyBlock(materialPropertyBlock);
}
//#endif
}
/// <summary>
/// Sets the alpha intensity of a given layer.
/// </summary>
public void SetLayerIntensity(int layer, float intensity)
{
if (layerArrayTexture)
{
intensity = Mathf.Clamp01(intensity);
tilemapLayerList[layer].intensity = intensity;
Color32 color = layerArrayTexture.GetPixel(layer, 0);
color.g = (byte)(intensity * 255);
layerArrayTexture.SetPixel(layer, 0, color);
layerArrayTexture.Apply();
}
}
/// <summary>
/// Gets the alpha intensity of a given layer.
/// </summary>
public float GetLayerIntensity(int layer)
{
return tilemapLayerList[layer].intensity;
}
/// <summary>
/// Pickup the index of the clicked tile on the mesh.
/// </summary>
public int PickupTile(int x, int y, int layer)
{
return (int) (tilemapTexture.GetPixel(x, y, layer).r * 255);
}
/// <summary>
/// Paint a tile on the mesh.
/// </summary>
public void PaintTile(int x, int y, int index, int layer)
{
index = Mathf.Clamp(index, 0, tilemapLayerList[layer].tilePalette.tilesCount - 1);
if (index > 0)
tilemapLayerList[layer].autoTileMapArray[(y * tilemapTexture.width) + x] = true;
else
tilemapLayerList[layer].autoTileMapArray[(y * tilemapTexture.width) + x] = false;
tilemapTexture.SetPixel(x, y, layer, new Color32((byte) index, 0, 0, 0));
tilemapTexture.Apply();
}
// Based on "TextureFloodFill" from:
// https://wiki.unity3d.com/index.php/TextureFloodFill
/// <summary>
/// Fill a layer region with a tile.
/// </summary>
public void FillTile(int x, int y, int index, int layer)
{
index = Mathf.Clamp(index, 0, tilemapLayerList[layer].tilePalette.tilesCount - 1);
Texture2D tempTexture2D = GetTexture3DSlice(tilemapTexture, layer);
var width = tilemapTexture.width;
var height = tilemapTexture.height;
var colors = tempTexture2D.GetPixels32();
var refCol = colors[x + y * width];
var nodes = new Queue<Vector2Int>();
nodes.Enqueue(new Vector2Int(x, y));
while (nodes.Count > 0)
{
var currentPoint = nodes.Dequeue();
for (var i = currentPoint.x; i < width; i++)
{
int C = colors[i + currentPoint.y * width].r;
if (C != refCol.r || C == index)
break;
colors[i + currentPoint.y * width] = new Color32((byte)index, 0, 0, 0);
if (currentPoint.y + 1 < height)
{
C = colors[i + currentPoint.y * width + width].r;
if (C == refCol.r && C != index)
nodes.Enqueue(new Vector2Int(i, currentPoint.y + 1));
}
if (currentPoint.y - 1 < 0) continue;
C = colors[i + currentPoint.y * width - width].r;
if (C == refCol.r && C != index)
nodes.Enqueue(new Vector2Int(i, currentPoint.y - 1));
}
for (var i = currentPoint.x - 1; i >= 0; i--)
{
int C = colors[i + currentPoint.y * width].r;
if (C != refCol.r || C == index)
break;
colors[i + currentPoint.y * width] = new Color32((byte)index, 0, 0, 0);
if (currentPoint.y + 1 < height)
{
C = colors[i + currentPoint.y * width + width].r;
if (C == refCol.r && C != index)
nodes.Enqueue(new Vector2Int(i, currentPoint.y + 1));
}
if (currentPoint.y - 1 < 0) continue;
C = colors[i + currentPoint.y * width - width].r;
if (C == refCol.r && C != index)
nodes.Enqueue(new Vector2Int(i, currentPoint.y - 1));
}
}
tempTexture2D.SetPixels32(colors);
tempTexture2D.Apply();
SetTexture3DSlice(tempTexture2D, tilemapTexture, layer);
}
/// <summary>
/// Perform the auto tile painting.
/// </summary>
public void PaintAutoTile(int x, int y, int layoutIndex, int layer)
{
int autoTileMapX = x;
int autoTileMapY = y;
int north;
int northEast;
int east;
int southEast;
int south;
int southWest;
int west;
int northWest;
int autoTileIndex;
tilemapLayerList[layer].autoTileMapArray[y * tilemapTexture.width + x] = true;
// Compute the center tile
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
// if top = 0: topLeft = 0, topRight = 0
// if left = 0: topLeft = 0, bottomLeft = 0
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
// Compute the north tile
autoTileMapX = x;
autoTileMapY = y + 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the north-east tile
autoTileMapX = x + 1;
autoTileMapY = y + 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the east tile
autoTileMapX = x + 1;
autoTileMapY = y;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the east-south tile
autoTileMapX = x + 1;
autoTileMapY = y - 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the south tile
autoTileMapX = x;
autoTileMapY = y - 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the south-west tile
autoTileMapX = x - 1;
autoTileMapY = y - 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the west tile
autoTileMapX = x - 1;
autoTileMapY = y ;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the west-north tile
autoTileMapX = x - 1;
autoTileMapY = y + 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
tilemapTexture.Apply();
}
/// <summary>
/// Erase the tile from the auto tile array map.
/// </summary>
public void EraseAutoTile(int x, int y, int layoutIndex, int layer)
{
int autoTileMapX = x;
int autoTileMapY = y;
int north;
int northEast;
int east;
int southEast;
int south;
int southWest;
int west;
int northWest;
int autoTileIndex;
// Erase the clicked tile
tilemapLayerList[layer].autoTileMapArray[y * tilemapTexture.width + x] = false;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) 0, 0, 0, 0));
// Compute the north tile
autoTileMapX = x;
autoTileMapY = y + 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the north-east tile
autoTileMapX = x + 1;
autoTileMapY = y + 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the east tile
autoTileMapX = x + 1;
autoTileMapY = y;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the east-south tile
autoTileMapX = x + 1;
autoTileMapY = y - 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the south tile
autoTileMapX = x;
autoTileMapY = y - 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the south-west tile
autoTileMapX = x - 1;
autoTileMapY = y - 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the west tile
autoTileMapX = x - 1;
autoTileMapY = y ;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
// Compute the west-north tile
autoTileMapX = x - 1;
autoTileMapY = y + 1;
if (GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY))
{
north = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY + 1) ? 1 : 0;
east = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray,autoTileMapX + 1, autoTileMapY) ? 4 : 0;
south = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX, autoTileMapY - 1) ? 16 : 0;
west = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY) ? 64 : 0;
northEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY + 1) && north != 0 && east != 0 ? 2 : 0;
southEast = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX + 1, autoTileMapY - 1) && south != 0 && east != 0 ? 8 : 0;
southWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY - 1) && south != 0 && west != 0 ? 32 : 0;
northWest = GetArrayMapIndex(tilemapLayerList[layer].autoTileMapArray, autoTileMapX - 1, autoTileMapY + 1) && north != 0 && west != 0 ? 128 : 0;
autoTileIndex = north + northEast + east + southEast + south + southWest + west + northWest;
tilemapTexture.SetPixel(autoTileMapX, autoTileMapY, layer, new Color32((byte) tilemapLayerList[layer].autoTileList[layoutIndex].id[m_8BitMasking[autoTileIndex]], 0, 0, 0));
}
tilemapTexture.Apply();
}
/// <summary>
/// Copy a tilemap slice and save into a Texture2D.
/// </summary>
private Texture2D GetTexture3DSlice(Texture3D texture, int depth)
{
// Creates the temporary Texture2D
Texture2D tempTexture = new Texture2D(texture.width, texture.height, texture.format, false)
{
name = "Slice Texture",
filterMode = FilterMode.Point,
wrapMode = TextureWrapMode.Clamp,
anisoLevel = 0
};
// Copy the 3D texture slice color to the temporary Texture2D
for (int i = 0; i < texture.width; i++)
{
for (int j = 0; j < texture.height; j++)
{
Color32 tempColor = texture.GetPixel(i, j, depth);
tempTexture.SetPixel(i, j, tempColor);
}
}
// Apply the changes to the temporary Texture2D and return
tempTexture.Apply();
return tempTexture;
}
/// <summary>
/// Paste a Texture2D to a tilemap slice.
/// </summary>
private void SetTexture3DSlice(Texture2D sourceTex2D, Texture3D targetTex3D, int depth)
{
// Get the color of the source Texture2D
Color32[] newColor = sourceTex2D.GetPixels32();
// Used to access each index of the color array
int index = 0;
// Paste the Texture2D color into the tilemap slice pixel by pixel
for (int i = 0; i < targetTex3D.width; i++)
{
for (int j = 0; j < targetTex3D.height; j++)
{
targetTex3D.SetPixel(j, i, depth, newColor[index]);
index++;
}
}
// Apply the changes to the tilemap texture
targetTex3D.Apply();
}
/// <summary>
/// Get the value of an array using a 2D coordinate as a parameter.
/// </summary>
private bool GetArrayMapIndex(bool[] array, int x, int y)
{
if (x >= 0 && y >= 0 && x < tilemapTexture.width && y < tilemapTexture.height)
return array[y * tilemapTexture.width + x];
else
return false;
}
}
}