private static void SetupTerrainColors() { TerrainColors.Add(MapTerrain.Empty, ColorPresets.Gray); TerrainColors.Add(MapTerrain.Wall, ColorPresets.DarkSlateGray); TerrainColors.Add(MapTerrain.NonWalkableFeature, ColorPresets.White); TerrainColors.Add(MapTerrain.NonWalkableFeatureLightBlocking, ColorPresets.White); TerrainColors.Add(MapTerrain.HellWall, ColorPresets.Crimson); TerrainColors.Add(MapTerrain.Corridor, ColorPresets.Gray); TerrainColors.Add(MapTerrain.SkeletonWall, ColorPresets.BlanchedAlmond); TerrainColors.Add(MapTerrain.SkeletonWallWhite, ColorPresets.GhostWhite); TerrainColors.Add(MapTerrain.ClosedDoor, ColorPresets.DarkGoldenrod); TerrainColors.Add(MapTerrain.OpenDoor, ColorPresets.DarkGoldenrod); TerrainColors.Add(MapTerrain.ClosedLock, ColorPresets.DarkGoldenrod); TerrainColors.Add(MapTerrain.OpenLock, ColorPresets.DarkGoldenrod); TerrainColors.Add(MapTerrain.Flooded, ColorPresets.Blue); TerrainColors.Add(MapTerrain.Grass, ColorPresets.Green); TerrainColors.Add(MapTerrain.River, ColorPresets.Blue); TerrainColors.Add(MapTerrain.Trees, ColorPresets.DarkGreen); TerrainColors.Add(MapTerrain.Road, ColorPresets.DarkGoldenrod); TerrainColors.Add(MapTerrain.Rubble, ColorPresets.Gray); TerrainColors.Add(MapTerrain.Mountains, ColorPresets.Gray); TerrainColors.Add(MapTerrain.Volcano, ColorPresets.Red); TerrainColors.Add(MapTerrain.Forest, ColorPresets.DarkSeaGreen); TerrainColors.Add(MapTerrain.Gravestone, ColorPresets.Gray); TerrainColors.Add(MapTerrain.BarDoor, ColorPresets.Gray); TerrainColors.Add(MapTerrain.DockWall, ColorPresets.DarkSlateGray); TerrainColors.Add(MapTerrain.Void, ColorPresets.Gray); var brickWallColor = ColorPresets.DarkSlateBlue; var panelWallColor = ColorPresets.Teal; var irisWallColor = ColorPresets.SlateGray; var lineWallColor = ColorPresets.MediumSeaGreen; var securityWallColor = ColorPresets.SteelBlue; var bioWallColor = ColorPresets.Firebrick; var DipWallColor = ColorPresets.DarkOliveGreen; var CutWallColor = ColorPresets.Sienna; TerrainColors.Add(MapTerrain.BrickWall1, brickWallColor); TerrainColors.Add(MapTerrain.BrickWall2, brickWallColor); TerrainColors.Add(MapTerrain.BrickWall3, brickWallColor); TerrainColors.Add(MapTerrain.BrickWall4, brickWallColor); TerrainColors.Add(MapTerrain.BrickWall5, brickWallColor); TerrainColors.Add(MapTerrain.PanelWall1, panelWallColor); TerrainColors.Add(MapTerrain.PanelWall2, panelWallColor); TerrainColors.Add(MapTerrain.PanelWall3, panelWallColor); TerrainColors.Add(MapTerrain.PanelWall4, panelWallColor); TerrainColors.Add(MapTerrain.PanelWall5, panelWallColor); TerrainColors.Add(MapTerrain.IrisWall1, irisWallColor); TerrainColors.Add(MapTerrain.IrisWall2, irisWallColor); TerrainColors.Add(MapTerrain.IrisWall3, irisWallColor); TerrainColors.Add(MapTerrain.IrisWall4, irisWallColor); TerrainColors.Add(MapTerrain.IrisWall5, irisWallColor); TerrainColors.Add(MapTerrain.LineWall1, lineWallColor); TerrainColors.Add(MapTerrain.LineWall2, lineWallColor); TerrainColors.Add(MapTerrain.LineWall3, lineWallColor); TerrainColors.Add(MapTerrain.LineWall4, lineWallColor); TerrainColors.Add(MapTerrain.LineWall5, lineWallColor); TerrainColors.Add(MapTerrain.SecurityWall1, securityWallColor); TerrainColors.Add(MapTerrain.SecurityWall2, securityWallColor); TerrainColors.Add(MapTerrain.SecurityWall3, securityWallColor); TerrainColors.Add(MapTerrain.SecurityWall4, securityWallColor); TerrainColors.Add(MapTerrain.SecurityWall5, securityWallColor); TerrainColors.Add(MapTerrain.BioWall1, bioWallColor); TerrainColors.Add(MapTerrain.BioWall2, bioWallColor); TerrainColors.Add(MapTerrain.BioWall3, bioWallColor); TerrainColors.Add(MapTerrain.BioWall4, bioWallColor); TerrainColors.Add(MapTerrain.BioWall5, bioWallColor); TerrainColors.Add(MapTerrain.DipWall1, DipWallColor); TerrainColors.Add(MapTerrain.DipWall2, DipWallColor); TerrainColors.Add(MapTerrain.DipWall3, DipWallColor); TerrainColors.Add(MapTerrain.DipWall4, DipWallColor); TerrainColors.Add(MapTerrain.DipWall5, DipWallColor); TerrainColors.Add(MapTerrain.CutWall1, CutWallColor); TerrainColors.Add(MapTerrain.CutWall2, CutWallColor); TerrainColors.Add(MapTerrain.CutWall3, CutWallColor); TerrainColors.Add(MapTerrain.CutWall4, CutWallColor); TerrainColors.Add(MapTerrain.CutWall5, CutWallColor); }
public TerrainColors[,] GetTerrainColorsMap(Size sizTile, TemplateDoc tmpd) { TerrainColors[,] atclrMap = new TerrainColors[Bounds.Height * 2, Bounds.Width * 2]; foreach (IMapItem mi in m_alsmi) { Tile tile = mi as Tile; if (tile == null) continue; int x = (int)mi.tx * sizTile.Width; int y = (int)mi.ty * sizTile.Height; Rectangle rc = tile.GetBoundingRectAt(x, y, sizTile, m_tmpd); rc.Width /= sizTile.Width; rc.Height /= sizTile.Height; rc.X /= sizTile.Width; rc.Y /= sizTile.Height; rc.Intersect(Bounds); for (int ty = rc.Y - (int)tile.ty; ty < rc.Bottom - (int)tile.ty; ty++) { for (int tx = rc.X - (int)tile.tx; tx < rc.Right - (int)tile.tx; tx++) { if (tile.Visibility == null || tile.Visibility[ty, tx]) { Template tmpl = tile.GetTemplate(m_tmpd); // Maybe this template doesn't exist in document's current tile collection if (tmpl == null) continue; // TerrainColors are a 2x2 subgrid inside each tile for (int tyT = 0; tyT < 2; tyT++) { for (int txT = 0; txT < 2; txT++) { TerrainColors tclr = TerrainColors.Grass; if (tmpl.TerrainColors != null) tclr = tmpl.TerrainColors[ty * 2 + tyT, tx * 2 + txT]; atclrMap[(ty + (int)tile.ty - Bounds.Y) * 2 + tyT, (tx + (int)tile.tx - Bounds.X) * 2 + txT] = tclr; } } } } } } return atclrMap; }
private void GenerateMap() { string mapName = string.Format("map_{0}_{1}.png", World.Seed, World.Size); if (File.Exists(mapName)) { return; } Debug.LogWarning("Starting map generate"); /*int[] mapSize = { * (int)(TerrainMeta.Size.x / density), * (int)(TerrainMeta.Size.z / density) * };*/ int[] mapSize = { (int)(World.Size / density), (int)(World.Size / density) }; float[] mapStart = { TerrainMeta.Position.x, TerrainMeta.Position.z }; TerrainColors colors = TerrainMeta.Colors; Texture.anisotropicFiltering = AnisotropicFiltering.ForceEnable; Texture2D texture2D = new Texture2D(mapSize[0], mapSize[1]); texture2D.anisoLevel = 16; texture2D.mipMapBias = 0.1f; Debug.LogWarningFormat("{0} {1}", mapSize[0], mapSize[1]); Debug.LogWarningFormat("{0} {1}", mapStart[0], mapStart[1]); Color[,] colorMap = new Color[Mathf.CeilToInt(mapSize[0]), Mathf.CeilToInt(mapSize[1])]; float[,] heightMap = new float[Mathf.CeilToInt(mapSize[0]), Mathf.CeilToInt(mapSize[1])]; float[,] depthMap = new float[Mathf.CeilToInt(mapSize[0]), Mathf.CeilToInt(mapSize[1])]; float[,] shadowMap = new float[Mathf.CeilToInt(mapSize[0]), Mathf.CeilToInt(mapSize[1])]; Vector3[,] normalMap = new Vector3[Mathf.CeilToInt(mapSize[0]), Mathf.CeilToInt(mapSize[1])]; Debug.LogWarning("RUSTMAP: Create base color map"); ulong pointsCount = Convert.ToUInt64(mapSize[0]) * Convert.ToUInt64(mapSize[1]) / Convert.ToUInt64(density) / Convert.ToUInt64(density); ulong processedPointsCount = 0; for (float x = 0f; x < mapSize[0] * density; x += density) { for (float z = 0f; z < mapSize[1] * density; z += density) { int xarr = (int)(x / density); int yarr = (int)(z / density); RaycastHit hit; Vector3 pointOnMap = new Vector3(mapStart[0] + x, 0f, mapStart[1] + z); Color pointColor = colors.GetColor(pointOnMap, -1); bool collided = false; if (Physics.BoxCast(pointOnMap + Vector3.up * 1000f, Vector3.one * density, Vector3.down, out hit, default(Quaternion), 1100f, mask, QueryTriggerInteraction.Ignore)) { collided = true; if (hit.collider != null) { if (materialsColors.ContainsKey(hit.collider.material.name)) { pointColor = materialsColors[hit.collider.material.name]; } } } colorMap[xarr, yarr] = pointColor; heightMap[xarr, yarr] = collided ? hit.point.y : TerrainMeta.HeightMap.GetHeight(pointOnMap); depthMap[xarr, yarr] = TerrainMeta.WaterMap.GetDepth(pointOnMap); if (processedPointsCount % (pointsCount / 100) == 0) { Debug.LogWarningFormat("{0}% done", processedPointsCount / (pointsCount / 100)); } processedPointsCount++; } } Debug.LogWarning("RUSTMAP: Normal map generation"); for (int x = 0; x < mapSize[0]; x++) { for (int y = 0; y < mapSize[1]; y++) { Vector3 result; if (x == 0 || y == 0 || x == mapSize[0] - 1 || y == mapSize[1] - 1) { result = Vector3.up; } else { result = new Vector3( heightMap[x + 1, y] - heightMap[x - 1, y], 1f, heightMap[x, y + 1] - heightMap[x, y - 1] ); } normalMap[x, y] = result.normalized; } } Debug.LogWarning("RUSTMAP: Shadow map generating"); for (int x = 0; x < mapSize[0]; x++) { for (int y = 0; y < mapSize[1]; y++) { Vector3 groundVec = normalMap[x, y]; float angle = Vector3.Angle(groundVec, sunrayVector); shadowMap[x, y] = Mathf.Lerp(0f, maxShading, Mathf.Clamp((angle > 180f) ? (180f - angle) : angle, 0f, 90f) / 90f ); } } Debug.LogWarning("RUSTMAP: Map image compiling"); for (int x = 0; x < mapSize[0]; x++) { for (int y = 0; y < mapSize[1]; y++) { Color color = colorMap[x, y]; float depth = depthMap[x, y]; float height = heightMap[x, y]; float shade = shadowMap[x, y]; if (depth > 0f || height < 0) { color = GetWaterColor(color, depth); color *= 1f - maxShading; color.a = 1f; } else { color *= 1f - shade; color.a = 1f; } colorMap[x, y] = color; } } Debug.LogWarning("RUSTMAP: Bluring color map"); for (int x = 1; x < mapSize[0] - 1; x++) { for (int y = 1; y < mapSize[1] - 1; y++) { Color[] pointsNear = { /*colorMap[x - 1, y - 1],*/ colorMap[x - 1, y], /*colorMap[x - 1, y + 1],*/ colorMap[x, y - 1],colorMap[x, y], colorMap[x, y + 1], /*colorMap[x + 1, y - 1],*/ colorMap[x + 1, y], /*colorMap[x + 1, y + 1] */ }; float r = 0, g = 0, b = 0; foreach (Color c in pointsNear) { r += c.r; g += c.g; b += c.b; } r /= pointsNear.Length; g /= pointsNear.Length; b /= pointsNear.Length; colorMap[x, y] = new Color(r, g, b); } } Debug.LogWarning("RUSTMAP: Save map"); for (int x = 0; x < mapSize[0]; x++) { for (int y = 0; y < mapSize[1]; y++) { texture2D.SetPixel(x, y, colorMap[x, y]); } } File.WriteAllBytes(mapName, texture2D.EncodeToPNG()); UnityEngine.Object.Destroy(texture2D); /*Texture2D heightTex = new Texture2D(mapSize[0], mapSize[1]); * heightTex.anisoLevel = 16; * heightTex.mipMapBias = 0.1f; * for (int x = 0; x < mapSize[0]; x++) * for (int y = 0; y < mapSize[0]; y++) * { * Color c = Color.white; * float height = heightMap[x, y]; * if (height < 0f) * { * float clampedHeight = Mathf.Clamp(Mathf.Abs(height), 0f, 20f); * float col = 0.5f + Mathf.Lerp(0f, 0.5f, clampedHeight / 20f); * c.r -= col; c.g -= col; c.b -= col; * } * heightTex.SetPixel(x, y, c); * } * * File.WriteAllBytes("heightMap.png", heightTex.EncodeToPNG()); * UnityEngine.Object.Destroy(heightTex);*/ }