66
77public class WorldMapTextureView : MonoBehaviour , IWorldMapView
88{
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 ;
2014
21- [ SerializeField ] private FilterMode filterMode = FilterMode . Point ;
15+ [ SerializeField ] private bool scaleRenderer = false ;
2216
2317 [ SerializeField ] private bool gradiate = false ;
2418
@@ -27,63 +21,12 @@ public class WorldMapTextureView : MonoBehaviour, IWorldMapView
2721 [ SerializeField ] private bool drawSpawnPoints = false ;
2822 [ SerializeField ] private bool drawPoissonPoints = false ;
2923
24+ [ SerializeField , Range ( 0f , 1f ) ] private float borderAlpha = 0.5f ;
25+ [ SerializeField , Range ( 0 , 1f ) ] private float regionFillAlpha = 0.2f ;
3026 [ SerializeField ] private Color [ ] regionColors = new Color [ 0 ] ;
27+ [ SerializeField ] private FilterMode filterMode = FilterMode . Point ;
3128
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 )
8730 {
8831 var heightMapLayer = worldMapData . GetLayer < HeightMapLayerData > ( ) ;
8932 var regionMapLayer = worldMapData . GetLayer < RegionMapLayerData > ( ) ;
@@ -98,7 +41,7 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
9841 var terrainMap = _terrainTable . GetTerrainMap ( heightMap ) ;
9942 var colorMap = TerrainTable . GetColorMap ( heightMap , terrainMap , gradiate ) ;
10043
101- if ( fillRegions )
44+ if ( fillRegions && regionColors . Length > 0 )
10245 {
10346 for ( int i = 0 ; i < colorMap . Length ; i ++ )
10447 {
@@ -107,13 +50,13 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
10750 {
10851 continue ;
10952 }
110- var regionColor = regionColors [ regionIndex - 1 ] ;
53+ var regionColor = regionColors [ ( regionIndex - 1 ) % regionColors . Length ] ;
11154 var alpha = Mathf . Clamp01 ( regionColor . a * regionFillAlpha ) ;
11255 colorMap [ i ] = regionColor * alpha + ( 1 - alpha ) * colorMap [ i ] ;
11356 }
11457 }
11558
116- if ( drawBorders )
59+ if ( drawBorders && regionColors . Length > 0 )
11760 {
11861 foreach ( var region in regions )
11962 {
@@ -153,157 +96,25 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
15396 }
15497
15598 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-
22099 texture . filterMode = filterMode ;
221100 SetTexture ( texture ) ;
222-
101+
223102 if ( scaleRenderer )
224103 {
225104 _renderer . transform . localScale = new Vector3 ( width , height , 1 ) ;
226105 }
227106 }
228107
229- [ SerializeField ] private bool URP = false ;
230- private static readonly int BaseMap = Shader . PropertyToID ( "_BaseMap" ) ;
231-
232108 private void SetTexture ( Texture2D texture )
233109 {
234- if ( URP )
110+ if ( ! mainTexture )
235111 {
236- //_renderer.sharedMaterial.SetTexture("BaseMap",texture);
237- _renderer . sharedMaterial . SetTexture ( BaseMap , texture ) ;
112+ _renderer . sharedMaterial . SetTexture ( texturePropertyName , texture ) ;
238113 }
239114 else
240115 {
241116 _renderer . sharedMaterial . mainTexture = texture ;
242117 }
243118 }
244119
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-
309120}
0 commit comments