/// <summary>Sets and initialises the Splat/AlphaMaps of all layers from MapInfo. Called when first loading/creating a map.</summary> private static IEnumerator SetSplatMaps(MapInfo mapInfo, int progressID) { SplatMapRes = mapInfo.splatRes; SetSplatMap(mapInfo.splatMap, LayerType.Ground); SetSplatMap(mapInfo.biomeMap, LayerType.Biome); SetAlphaMap(mapInfo.alphaMap); yield return(null); Progress.Report(progressID, .8f, "Loaded: Splats."); TopologyData.Set(mapInfo.topology); Parallel.For(0, TerrainTopology.COUNT, i => { if (CurrentLayerType != LayerType.Topology || TopologyLayer != i) { SetSplatMap(TopologyData.GetTopologyLayer(TerrainTopology.IndexToType(i)), LayerType.Topology, i); } }); SetSplatMap(TopologyData.GetTopologyLayer(TerrainTopology.IndexToType(TopologyLayer)), LayerType.Topology, TopologyLayer); Progress.Report(progressID, .9f, "Loaded: Topologies."); }
/// <summary>Converts Unity terrains to WorldSerialization.</summary> public static WorldSerialization TerrainToWorld(Terrain land, Terrain water) { WorldSerialization world = new WorldSerialization(); world.world.size = (uint)land.terrainData.size.x; var textureResolution = Mathf.Clamp(Mathf.NextPowerOfTwo((int)(world.world.size * 0.50f)), 16, 2048); byte[] splatBytes = new byte[textureResolution * textureResolution * 8]; var splatMap = new TerrainMap <byte>(splatBytes, 8); var splatTask = Task.Run(() => { Parallel.For(0, 8, i => { for (int j = 0; j < textureResolution; j++) { for (int k = 0; k < textureResolution; k++) { splatMap[i, j, k] = BitUtility.Float2Byte(GroundArray[j, k, i]); } } }); splatBytes = splatMap.ToByteArray(); }); byte[] biomeBytes = new byte[textureResolution * textureResolution * 4]; var biomeMap = new TerrainMap <byte>(biomeBytes, 4); var biomeTask = Task.Run(() => { Parallel.For(0, 4, i => { for (int j = 0; j < textureResolution; j++) { for (int k = 0; k < textureResolution; k++) { biomeMap[i, j, k] = BitUtility.Float2Byte(BiomeArray[j, k, i]); } } }); biomeBytes = biomeMap.ToByteArray(); }); byte[] alphaBytes = new byte[textureResolution * textureResolution * 1]; var alphaMap = new TerrainMap <byte>(alphaBytes, 1); bool[,] terrainHoles = GetAlphaMap(); var alphaTask = Task.Run(() => { Parallel.For(0, textureResolution, i => { for (int j = 0; j < textureResolution; j++) { alphaMap[0, i, j] = BitUtility.Bool2Byte(terrainHoles[i, j]); } }); alphaBytes = alphaMap.ToByteArray(); }); var topologyTask = Task.Run(() => TopologyData.SaveTopologyLayers()); PrefabDataHolder[] prefabs = GameObject.FindGameObjectWithTag("Prefabs").GetComponentsInChildren <PrefabDataHolder>(false); foreach (PrefabDataHolder p in prefabs) { if (p.prefabData != null) { p.UpdatePrefabData(); // Updates the prefabdata before saving. world.world.prefabs.Insert(0, p.prefabData); } } PathDataHolder[] paths = GameObject.FindObjectsOfType <PathDataHolder>(); foreach (PathDataHolder p in paths) { if (p.pathData != null) { p.pathData.nodes = new VectorData[p.transform.childCount]; for (int i = 0; i < p.transform.childCount; i++) { Transform g = p.transform.GetChild(i); p.pathData.nodes[i] = g.position - (0.5f * land.terrainData.size); } world.world.paths.Insert(0, p.pathData); } } byte[] landHeightBytes = FloatArrayToByteArray(land.terrainData.GetHeights(0, 0, land.terrainData.heightmapResolution, land.terrainData.heightmapResolution)); byte[] waterHeightBytes = FloatArrayToByteArray(water.terrainData.GetHeights(0, 0, water.terrainData.heightmapResolution, water.terrainData.heightmapResolution)); Task.WaitAll(splatTask, biomeTask, alphaTask, topologyTask); world.AddMap("terrain", landHeightBytes); world.AddMap("height", landHeightBytes); world.AddMap("water", waterHeightBytes); world.AddMap("splat", splatBytes); world.AddMap("biome", biomeBytes); world.AddMap("alpha", alphaBytes); world.AddMap("topology", TopologyData.GetTerrainMap().ToByteArray()); return(world); }
public bool topology(string streamsLayer) { MapWinGIS.Shapefile streamShp = new MapWinGIS.Shapefile(); streamShp.Open(streamsLayer); ArrayList array = mwSWATTopology.readTree(hruGlobals, streamShp); hruData.computeDrainAreas(array, hruGlobals); TopologyData cd = new TopologyData(); // add internal outlets (monitoring points) int monNum = 1; for (int i = 0; i < hruGlobals.monitoringPointBasins.Count; i++) { int basin = hruGlobals.monitoringPointBasins[i]; // guard against basins upstream from inlets if (hruGlobals.basinToSWATBasin.ContainsKey(basin)) { int SWATBasin = hruGlobals.basinToSWATBasin[basin]; cd.addSavePoint(basin, SWATBasin, monNum); monNum = monNum + 1; } } // add reservoirs int resNum = 1; for (int i = 0; i < hruGlobals.reservoirBasins.Count; i++) { int basin = hruGlobals.reservoirBasins[i]; // guard against basins upstream from inlets if (hruGlobals.basinToSWATBasin.ContainsKey(basin)) { int SWATBasin = hruGlobals.basinToSWATBasin[basin]; cd.addReservoir(basin, SWATBasin, resNum); resNum = resNum + 1; } } // add inlets int fileNum = 1; for (int i = 0; i < hruGlobals.inletBasins.Count; i++) { int basin = hruGlobals.inletBasins[i]; // guard against inlets upstream from inlets (!) if (hruGlobals.basinToSWATBasin.ContainsKey(basin)) { int SWATBasin = hruGlobals.basinToSWATBasin[basin]; cd.addInlet(basin, SWATBasin, fileNum); fileNum = fileNum + 1; } } // add point sources (still incrementing file numbers) for (int i = 0; i < hruGlobals.srcBasins.Count; i++) { int basin = hruGlobals.srcBasins[i]; // guard against basins upstream from inlets if (hruGlobals.basinToSWATBasin.ContainsKey(basin)) { int SWATBasin = hruGlobals.basinToSWATBasin[basin]; cd.addPointSource(basin, SWATBasin, fileNum); fileNum = fileNum + 1; } } if (hruGlobals.figFileWanted) { // Write fig.fig file string configFilePath = hruGlobals.TxtInOutDir + "\\fig.fig"; return(mwSWATTopology.makeConfigFile(array, cd, configFilePath, hruGlobals)); } return(true); }