6
6
7
7
public class WorldMapTextureView : MonoBehaviour , IWorldMapView
8
8
{
9
- [ SerializeField ]
10
- private Renderer _renderer = null ;
11
-
12
- [ SerializeField ]
13
- private DrawMode _drawMode = DrawMode . Color ;
14
-
15
- [ SerializeField ]
16
- private TerrainTable _terrainTable = null ;
17
-
18
- [ SerializeField ]
19
- private bool scaleRenderer = false ;
9
+ [ SerializeField ] private Renderer _renderer = null ;
10
+ [ SerializeField ] private bool mainTexture = false ;
11
+ [ SerializeField ] private string texturePropertyName = "_BaseMap" ;
12
+
13
+ [ SerializeField ] private TerrainTable _terrainTable = null ;
20
14
21
- [ SerializeField ] private FilterMode filterMode = FilterMode . Point ;
15
+ [ SerializeField ] private bool scaleRenderer = false ;
22
16
23
17
[ SerializeField ] private bool gradiate = false ;
24
18
@@ -27,63 +21,12 @@ public class WorldMapTextureView : MonoBehaviour, IWorldMapView
27
21
[ SerializeField ] private bool drawSpawnPoints = false ;
28
22
[ SerializeField ] private bool drawPoissonPoints = false ;
29
23
24
+ [ SerializeField , Range ( 0f , 1f ) ] private float borderAlpha = 0.5f ;
25
+ [ SerializeField , Range ( 0 , 1f ) ] private float regionFillAlpha = 0.2f ;
30
26
[ SerializeField ] private Color [ ] regionColors = new Color [ 0 ] ;
27
+ [ SerializeField ] private FilterMode filterMode = FilterMode . Point ;
31
28
32
- public enum DrawMode
33
- {
34
- Greyscale ,
35
- Color
36
- }
37
-
38
- public bool applyRegions = false ;
39
- public int regions = 50 ;
40
- public int seed = 100 ;
41
- public int minimumRegionSize = 50 ;
42
- public bool remapSmallRegions = false ;
43
-
44
- [ Range ( 0f , 1f ) ]
45
- public float borderAlpha = 0.5f ;
46
-
47
- [ Range ( 0 , 1f ) ]
48
- public float regionFillAlpha = 0.2f ;
49
-
50
- public void DisplayMap ( WorldMapData mapData )
51
- {
52
- if ( applyRegions )
53
- {
54
- DisplayMapWithRegions ( mapData ) ;
55
- return ;
56
- }
57
-
58
- var heightMap = mapData . GetLayer < HeightMapLayerData > ( ) . heightMap ;
59
-
60
- if ( _renderer == null )
61
- {
62
- return ;
63
- }
64
-
65
- Texture2D texture = null ;
66
- if ( _drawMode == DrawMode . Greyscale || _terrainTable == null )
67
- {
68
- texture = TextureUtility . GetHeightMap ( heightMap , mapData . width , mapData . height ) ;
69
- }
70
- else
71
- {
72
- var terrainMap = _terrainTable . GetTerrainMap ( heightMap ) ;
73
- var colorMap = TerrainTable . GetColorMap ( heightMap , terrainMap , gradiate ) ;
74
- texture = TextureUtility . GetColorMap ( colorMap , mapData . width , mapData . height ) ;
75
- }
76
-
77
- texture . filterMode = filterMode ;
78
- SetTexture ( texture ) ;
79
-
80
- if ( scaleRenderer )
81
- {
82
- _renderer . transform . localScale = new Vector3 ( mapData . width , mapData . height , 1 ) ;
83
- }
84
- }
85
-
86
- public void DisplayMapWithRegions ( WorldMapData worldMapData )
29
+ public void DisplayMap ( WorldMapData worldMapData )
87
30
{
88
31
var heightMapLayer = worldMapData . GetLayer < HeightMapLayerData > ( ) ;
89
32
var regionMapLayer = worldMapData . GetLayer < RegionMapLayerData > ( ) ;
@@ -98,7 +41,7 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
98
41
var terrainMap = _terrainTable . GetTerrainMap ( heightMap ) ;
99
42
var colorMap = TerrainTable . GetColorMap ( heightMap , terrainMap , gradiate ) ;
100
43
101
- if ( fillRegions )
44
+ if ( fillRegions && regionColors . Length > 0 )
102
45
{
103
46
for ( int i = 0 ; i < colorMap . Length ; i ++ )
104
47
{
@@ -107,13 +50,13 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
107
50
{
108
51
continue ;
109
52
}
110
- var regionColor = regionColors [ regionIndex - 1 ] ;
53
+ var regionColor = regionColors [ ( regionIndex - 1 ) % regionColors . Length ] ;
111
54
var alpha = Mathf . Clamp01 ( regionColor . a * regionFillAlpha ) ;
112
55
colorMap [ i ] = regionColor * alpha + ( 1 - alpha ) * colorMap [ i ] ;
113
56
}
114
57
}
115
58
116
- if ( drawBorders )
59
+ if ( drawBorders && regionColors . Length > 0 )
117
60
{
118
61
foreach ( var region in regions )
119
62
{
@@ -153,157 +96,25 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
153
96
}
154
97
155
98
var texture = TextureUtility . GetColorMap ( colorMap , width , height ) ;
156
- SetTexture ( texture ) ;
157
-
158
- if ( scaleRenderer )
159
- {
160
- _renderer . transform . localScale = new Vector3 ( width , height , 1 ) ;
161
- }
162
- }
163
-
164
- public void DisplayMapWithRegions ( float [ , ] noiseMap )
165
- {
166
- if ( _renderer == null )
167
- {
168
- return ;
169
- }
170
-
171
- var width = noiseMap . GetLength ( 0 ) ;
172
- var height = noiseMap . GetLength ( 1 ) ;
173
-
174
- //Generate Regions
175
- var voronoiData = Voronoi . Create ( width , height , regions , seed ) ;
176
- var regionColors = Voronoi . GenerateColors ( voronoiData . regionCount ) ;
177
-
178
- Texture2D texture = null ;
179
- if ( _drawMode == DrawMode . Greyscale || _terrainTable == null )
180
- {
181
- texture = TextureUtility . GetHeightMap ( noiseMap ) ;
182
- }
183
- else
184
- {
185
- var terrainMap = _terrainTable . GetSingleDimensionTerrainMap ( noiseMap ) ;
186
- var colorMap = TerrainTable . GetColorMap ( noiseMap , terrainMap , gradiate ) ;
187
-
188
- //We only want non-water regions
189
- var regionMask = new int [ terrainMap . Length ] ;
190
- for ( var i = 0 ; i < terrainMap . Length ; i ++ )
191
- {
192
- regionMask [ i ] = terrainMap [ i ] . Elevation > 0 ? 1 : 0 ;
193
- }
194
-
195
- if ( remapSmallRegions )
196
- {
197
- for ( int i = 0 ; i < 20 ; i ++ )
198
- {
199
- Debug . Log ( $ "Combine Step { i } ") ;
200
- if ( CombineSmallRegions ( regionMask , ref voronoiData . regionData , minimumRegionSize , width , height ) )
201
- {
202
- break ;
203
- }
204
- }
205
- }
206
-
207
- Debug . Log ( "Colorizing Map" ) ;
208
- for ( int i = 0 ; i < colorMap . Length ; i ++ )
209
- {
210
- if ( regionMask [ i ] != 0 )
211
- {
212
- var regionIndex = voronoiData . regionData [ i ] ;
213
- colorMap [ i ] *= regionColors [ regionIndex ] ;
214
- }
215
- }
216
-
217
- texture = TextureUtility . GetColorMap ( colorMap , width , height ) ;
218
- }
219
-
220
99
texture . filterMode = filterMode ;
221
100
SetTexture ( texture ) ;
222
-
101
+
223
102
if ( scaleRenderer )
224
103
{
225
104
_renderer . transform . localScale = new Vector3 ( width , height , 1 ) ;
226
105
}
227
106
}
228
107
229
- [ SerializeField ] private bool URP = false ;
230
- private static readonly int BaseMap = Shader . PropertyToID ( "_BaseMap" ) ;
231
-
232
108
private void SetTexture ( Texture2D texture )
233
109
{
234
- if ( URP )
110
+ if ( ! mainTexture )
235
111
{
236
- //_renderer.sharedMaterial.SetTexture("BaseMap",texture);
237
- _renderer . sharedMaterial . SetTexture ( BaseMap , texture ) ;
112
+ _renderer . sharedMaterial . SetTexture ( texturePropertyName , texture ) ;
238
113
}
239
114
else
240
115
{
241
116
_renderer . sharedMaterial . mainTexture = texture ;
242
117
}
243
118
}
244
119
245
- private static bool CombineSmallRegions ( int [ ] regionMask , ref int [ ] regionData , int minSize , int width , int height )
246
- {
247
- //Get Region Sizes
248
- var regionSizes = Voronoi . GetRegionSizes ( regionMask , regionData ) ;
249
- var adjacencies = Voronoi . GetAdjacentRegions ( regionMask , regionData , width , height ) ;
250
-
251
- /*foreach (var pair in regionSizes)
252
- {
253
- Debug.Log($"*{pair.Key} = {pair.Value} < {minSize}");
254
- }*/
255
-
256
- var smallList = new List < int > ( ) ;
257
- foreach ( var pair in regionSizes )
258
- {
259
- if ( pair . Value < minSize )
260
- {
261
- smallList . Add ( pair . Key ) ;
262
- }
263
- }
264
-
265
- if ( smallList . Count == 0 )
266
- {
267
- return true ;
268
- }
269
-
270
- var msg = $ "{ smallList . Count } small regions";
271
- foreach ( var idx in smallList )
272
- {
273
- msg += $ " { idx } ,";
274
- }
275
- Debug . Log ( msg ) ;
276
-
277
- int count = 0 ;
278
- //Remap Small Regions to larger adjacent ones
279
- foreach ( var smallRegionIndex in smallList )
280
- {
281
- if ( ! adjacencies . TryGetValue ( smallRegionIndex , out var adjacentList ) )
282
- {
283
- //Debug.Log($"{smallRegionIndex} has no adjacent regions");
284
- continue ;
285
- }
286
- //Pick a region to remap to
287
- var adjacentIndex = adjacentList [ 0 ] ;
288
- //Debug.Log($"Remapping {smallRegionIndex} to {adjacentButNotSmallIndex}");
289
- int replaceCout = 0 ;
290
- for ( int i = 0 ; i < regionData . Length ; i ++ )
291
- {
292
- if ( regionData [ i ] == smallRegionIndex )
293
- {
294
- regionData [ i ] = adjacentIndex ;
295
- replaceCout ++ ;
296
- }
297
- }
298
-
299
- //Debug.Log($"Replaced {smallRegionIndex} ({replaceCout} pixels) with {adjacentIndex}");
300
-
301
- count ++ ;
302
- }
303
- Debug . Log ( $ "Remapped { count } ") ;
304
-
305
- return false ;
306
- }
307
-
308
-
309
120
}
0 commit comments