public void Init(Terrain _terrain, MapDataGround.Tile[,] tiles, float[,] height, byte[,] zones) { InitGlobal(); InitWorld(); terrain = _terrain; InitTerrain(tiles, height, zones); }
public IEnumerator Init() { //инициализация глобальных компонентов InitGlobal(); InitWorld(); InitUI(); terrain = scene.AddComponent <MapTerrain>(); lighting = scene.AddComponent <MapLighting>(); objects = scene.AddComponent <MapObjects>(); sound = scene.AddComponent <MapSound>(); test = scene.AddComponent <MapTest>(); //компоненты загрузки данных MapDataObjects DataObjects = new MapDataObjects(global.map); MapDataZones DataZones = new MapDataZones(global.map); MapDataGround DataGround = new MapDataGround(global.map); MapDataHeight DataHeight = new MapDataHeight(global.map); DataObjects.EventErrorFile += NotFoundDataFiles; DataZones.EventErrorFile += NotFoundDataFiles; DataGround.EventErrorFile += NotFoundDataFiles; DataHeight.EventErrorFile += NotFoundDataFiles; //инициализация ландшафта terrain.Init(Terrain.activeTerrain, DataGround.data, DataHeight.data, DataZones.data); //инициализация объектов objects.Init(DataObjects.data); //инициализация маппинга lighting.Init(global.map); //инициализация маппинга sound.Init(global.map); //передача данных в мир if (EventDataZones != null) { EventDataZones(DataZones.data); } if (EventDataTiles != null) { EventDataTiles(DataGround.data); } //чистим данные DataObjects = null; DataZones = null; DataGround = null; DataHeight = null; System.GC.Collect(); yield break; }
public IEnumerator Init() { //инициализация глобальных компонентов InitGlobal(); InitWorld(); InitUI(); terrain = scene.AddComponent<MapTerrain>(); lighting = scene.AddComponent<MapLighting>(); objects = scene.AddComponent<MapObjects>(); sound = scene.AddComponent<MapSound>(); test = scene.AddComponent<MapTest>(); //компоненты загрузки данных MapDataObjects DataObjects = new MapDataObjects (global.map); MapDataZones DataZones = new MapDataZones (global.map); MapDataGround DataGround = new MapDataGround (global.map); MapDataHeight DataHeight = new MapDataHeight (global.map); DataObjects.EventErrorFile += NotFoundDataFiles; DataZones.EventErrorFile += NotFoundDataFiles; DataGround.EventErrorFile += NotFoundDataFiles; DataHeight.EventErrorFile += NotFoundDataFiles; //инициализация ландшафта terrain. Init(Terrain.activeTerrain, DataGround.data, DataHeight.data, DataZones.data); //инициализация объектов objects. Init(DataObjects.data); //инициализация маппинга lighting. Init(global.map); //инициализация маппинга sound. Init(global.map); //передача данных в мир if (EventDataZones!=null) EventDataZones ( DataZones.data ); if (EventDataTiles!=null) EventDataTiles ( DataGround.data ); //чистим данные DataObjects = null; DataZones = null; DataGround = null; DataHeight = null; System.GC.Collect(); yield break; }
//применяются ямы void ApplyHole(ref MapDataGround.Tile[,] tiles, ref float[,] height, byte[,] zones, byte count) { for (int y = 0; y < Config.MapLength; ++y) { for (int x = 0; x < Config.MapLength; ++x) { if (Util.Map.isHoleZone(zones[y,x])) { //устанавливаем цвет ямы по дефолту заливкой tiles[x,y].id1 = (byte)(count-1); tiles[x,y].id2 = 255; tiles[x,y].alpha = 1; //если левве или ниже не яма, тогда пропускаем ребро if ((y>0 && !Util.Map.isHoleZone(zones[y-1,x])) || (x>0 && !Util.Map.isHoleZone(zones[y,x-1]))) { continue; } height[x,y] = 0; } } } }
void InitTerrain(MapDataGround.Tile[,] tiles, float[,] height, byte[,] zones) { int size = Config.TileSize; //получаем активный террейн terrain.transform.parent = this.transform; TerrainData data = terrain.terrainData; //устанавливаем видимость terrain.basemapDistance = terrain.detailObjectDistance = cam.farClipPlane; terrain.heightmapPixelError = Config.HeightmapPixelError; //устанавливаем абсолютную ширину террейна data.heightmapResolution = 257; //устанавливаем физическую ширину data.size = new Vector3(Config.MapLength*size, Config.TileHeight+Config.HoleHeight*Config.TileHeight, Config.MapLength*size); //получаем текстуры SplatPrototype[] prototypes = GetPrototypes(); //загружаем текстуры data.splatPrototypes = prototypes; //устанавливаем ямы ApplyHole(ref tiles, ref height, zones, (byte)prototypes.Length); //устанавливаем высоты data.SetHeights(0, 0, height); if (prototypes.Length>0) { //формируем alphamaps float[,,] alphamaps = GetAlphamaps(tiles, prototypes.Length); //загружаем alphamaps data.SetAlphamaps(0, 0, alphamaps); } //получаем текстуры деталей DetailPrototype[] details = GetDetails(); data.detailPrototypes = details; //устанавливаем разрешение травы data.SetDetailResolution(Config.MapLength, Config.GrassPerPatch); for (int i = 0; i<details.Length; ++i) { //загружаем слои трав data.SetDetailLayer(0, 0, i, GetDetailsMap(tiles, i)); } //переопределенице цвета травы data.wavingGrassTint = Color.white; //устанавливаем разрешение для удаленных объектов data.baseMapResolution = 64; //устанавливаем запеченный свет terrain.lightmapIndex = 0; //применяем все параметры terrain.Flush(); //очищаем переменные zones = null; height = null; prototypes = null; System.GC.Collect(); }
//устсанавливает на координаты траву int[,] GetDetailsMap(MapDataGround.Tile[,] tiles, int layer) { int[,] maps = new int[Config.MapLength,Config.MapLength]; if (!global.settings.RenderGrass) return maps; for (int x = 0; x<Config.MapLength; ++x) { for (int y =0; y<Config.MapLength; ++y) { MapDataGround.Tile tile = tiles[x, y]; float alpha = tile.id1==layer ? tile.alpha : 1f-tile.alpha; if ((tile.id1==layer || tile.id2==layer) && alpha>=0.5f) { maps[x,y] = Mathf.RoundToInt(alpha*Config.GrassPerPatch); } } } return maps; }
//ставит текстуры в координаты float[,,] GetAlphamaps(MapDataGround.Tile[,] tiles, int count) { float[,,] alphamaps = new float[Config.MapLength,Config.MapLength,count]; for (int x = 0; x<Config.MapLength; ++x) { for (int y =0; y<Config.MapLength; ++y) { MapDataGround.Tile tile = tiles[x, y]; bool f = false; if (tile.id1 < count && tile.alpha!=0.0f) { alphamaps[x, y, tile.id1] = tile.alpha; f = true; } if (tile.id2 < count && 1-tile.alpha!=0.0f) { alphamaps[x, y, tile.id2] = (f) ? 1 : 1-tile.alpha; } } } return alphamaps; }