private void GetData(ImplicitModuleBase module, ref MapData mapData) { mapData = new MapData (Width, Height); // x,y - for (var x = 0; x < Width; x++) { for (var y = 0; y < Height; y++) { // float x1 = 0, x2 = 1; float y1 = 0, y2 = 1; float dx = x2 - x1; float dy = y2 - y1; // float s = x / (float)Width; float t = y / (float)Height; // float nx = x1 + Mathf.Cos (s * 2 * Mathf.PI) * dx / (2 * Mathf.PI); float ny = x1 + Mathf.Sin (s * 2 * Mathf.PI) * dx / (2 * Mathf.PI); float nz = t; float heightValue = (float)HeightMap.Get (nx, ny, nz); // if (heightValue > mapData.Max) mapData.Max = heightValue; if (heightValue < mapData.Min) mapData.Min = heightValue; mapData.Data [x, y] = heightValue; } } }
private void GetData(ImplicitModuleBase module, ref MapData mapData) { mapData = new MapData (Width, Height); // x,y - for (var x = 0; x < Width; x++) { for (var y = 0; y < Height; y++) { // float x1 = 0, x2 = 2; float y1 = 0, y2 = 2; float dx = x2 - x1; float dy = y2 - y1; // float s = x / (float)Width; float t = y / (float)Height; // float nx = x1 + Mathf.Cos (s*2*Mathf.PI) * dx/(2*Mathf.PI); float ny = y1 + Mathf.Cos (t*2*Mathf.PI) * dy/(2*Mathf.PI); float nz = x1 + Mathf.Sin (s*2*Mathf.PI) * dx/(2*Mathf.PI); float nw = y1 + Mathf.Sin (t*2*Mathf.PI) * dy/(2*Mathf.PI); float heightValue = (float)HeightMap.Get (nx, ny, nz, nw); // if (heightValue > mapData.Max) mapData.Max = heightValue; if (heightValue < mapData.Min) mapData.Min = heightValue; mapData.Data[x,y] = heightValue; } } }
public Tile Left; public Tile Right; public Tile Top; public Tile Bottom;
private Tile GetTop(Tile t) { return Tiles [tX, MathHelper.Mod (tY - 1, Height)]; } private Tile GetBottom(Tile t) { return Tiles [tX, MathHelper.Mod (tY + 1, Height)]; } private Tile GetLeft(Tile t) { return Tiles [MathHelper.Mod(tX - 1, Width), tY]; } private Tile GetRight(Tile t) { return Tiles [MathHelper.Mod (tX + 1, Width), tY]; }
private void UpdateNeighbors() { for (var x = 0; x < Width; x++) { for (var y = 0; y < Height; y++) { Tile t = Tiles[x,y]; t.Top = GetTop(t); t.Bottom = GetBottom (t); t.Left = GetLeft (t); t.Right = GetRight (t); } } }
public void UpdateBitmask() { int count = 0; if (Top.HeightType == HeightType) count += 1; if (Right.HeightType == HeightType) count += 2; if (Bottom.HeightType == HeightType) count += 4; if (Left.HeightType == HeightType) count += 8; Bitmask = count; }
private void UpdateBitmasks() { for (var x = 0; x < Width; x++) { for (var y = 0; y < Height; y++) { Tiles [x, y].UpdateBitmask (); } } }
// if (tiles[x,y].Bitmask != 15) pixels[x + y * width] = Color.Lerp(pixels[x + y * width], Color.black, 0.4f);
using UnityEngine; using System.Collections.Generic; public enum TileGroupType { Water, Land } public class TileGroup { public TileGroupType Type; public List<Tile> Tiles; public TileGroup() { Tiles = new List<Tile> (); } }
public bool Collidable; public bool FloodFilled;
List<TileGroup> Waters = new List<TileGroup> (); List<TileGroup> Lands = new List<TileGroup> ();
private void FloodFill() { // Stack<Tile> stack = new Stack<Tile>(); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { Tile t = Tiles[x,y]; // , if (t.FloodFilled) continue; // if (t.Collidable) { TileGroup group = new TileGroup(); group.Type = TileGroupType.Land; stack.Push(t); while(stack.Count > 0) { FloodFill(stack.Pop(), ref group, ref stack); } if (group.Tiles.Count > 0) Lands.Add (group); } // else { TileGroup group = new TileGroup(); group.Type = TileGroupType.Water; stack.Push(t); while(stack.Count > 0) { FloodFill(stack.Pop(), ref group, ref stack); } if (group.Tiles.Count > 0) Waters.Add (group); } } } } private void FloodFill(Tile tile, ref TileGroup tiles, ref Stack<Tile> stack) { // if (tile.FloodFilled) return; if (tiles.Type == TileGroupType.Land && !tile.Collidable) return; if (tiles.Type == TileGroupType.Water && tile.Collidable) return; // TileGroup tiles.Tiles.Add (tile); tile.FloodFilled = true; // Tile t = GetTop (tile); if (!t.FloodFilled && tile.Collidable == t.Collidable) stack.Push (t); t = GetBottom (tile); if (!t.FloodFilled && tile.Collidable == t.Collidable) stack.Push (t); t = GetLeft (tile); if (!t.FloodFilled && tile.Collidable == t.Collidable) stack.Push (t); t = GetRight (tile); if (!t.FloodFilled && tile.Collidable == t.Collidable) stack.Push (t); }
Source: https://habr.com/ru/post/276281/
All Articles