Fixed unity terrain generation bug.

This commit is contained in:
2015-03-19 12:34:58 +02:00
parent 8f9f935796
commit 68140b11a7
40 changed files with 326 additions and 245 deletions

View File

@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TransportGame.MapViewer.Model;
using TransportGame.Model;
namespace TransportGame.MapViewer
@ -23,8 +18,8 @@ namespace TransportGame.MapViewer
/// <summary>
/// Gets or sets the scale
/// </summary>
private int _scale;
public int Scale
private float _scale;
public float Scale
{
get
{
@ -45,7 +40,7 @@ namespace TransportGame.MapViewer
public MapRenderer()
{
Scale = 4;
Scale = 1;
}
///<summary>
@ -54,72 +49,46 @@ namespace TransportGame.MapViewer
///<param name="file"></param>
///<param name="map"></param>
///<param name="layers"></param>
public Color[,] Render(Map map, Layers layers = Layers.All)
public Bitmap24 Render(Map map, Layers layers = Layers.All)
{
lock (this)
{
// Create texture on which to draw
Color[,] pixels = new Color[map.Width * Scale, map.Height * Scale];
Bitmap24 bitmap = new Bitmap24(Convert.ToInt32(map.Width * Scale), Convert.ToInt32(map.Height * Scale));
// First layer - cells
DrawCells(pixels, map, (layers & Layers.Elevation) > 0);
DrawCells(bitmap, map, (layers & Layers.Elevation) > 0);
// Done
return pixels;
return bitmap;
}
}
private void DrawCells(Color[,] pixels, Map map, bool elevation)
private void DrawCells(Bitmap24 bitmap, Map map, bool elevation)
{
for (int x = 0; x < pixels.GetLength(0); x++)
for (int y = 0; y < pixels.GetLength(1); y++)
for (int x = 0; x < bitmap.Width; x++)
for (int y = 0; y < bitmap.Height; y++)
{
int mapX = x / Scale;
int mapY = y / Scale;
int mapX = Convert.ToInt32(x / Scale);
int mapY = Convert.ToInt32(y / Scale);
// Draw water
if (map.IsWater(mapX, mapY))
pixels[x, y] = WaterColor;
bitmap[x, y] = WaterColor;
// Draw elevation
else if (elevation)
{
float alpha = (map[mapX, mapY] - map.Biome.HeightRange.Minimum) / (map.Biome.HeightRange.Maximum - map.Biome.HeightRange.Minimum);
pixels[x, y] = Color.Multiply(ElevationTerrainColor, alpha);
float alpha = map[mapX, mapY]; // map height range is [0,1]
bitmap[x, y] = Color.Multiply(ElevationTerrainColor, alpha);
}
// Draw terrain
else
{
pixels[x, y] = TerrainColor;
bitmap[x, y] = TerrainColor;
}
}
}
public async Task<BitmapSource> RenderToImageSource(Map map, Layers layers = Layers.All)
{
var colors = await Task.Run(() => Render(map, layers));
colors[0, 0] = Colors.Red;
colors[colors.GetLength(0) - 1, 0] = Colors.Red;
// Convert to raw pixels
byte[] raw = new byte[colors.GetLength(0) * colors.GetLength(1) * 3];
for (int x = 0; x < colors.GetLength(0); x++)
for (int y = 0; y < colors.GetLength(1); y++)
{
int pixelIndex = x + y * colors.GetLength(0);
raw[3 * pixelIndex] = colors[x, y].R;
raw[3 * pixelIndex + 1] = colors[x, y].G;
raw[3 * pixelIndex + 2] = colors[x, y].B;
}
// Create bitmap source
BitmapSource bmp = BitmapSource.Create(colors.GetLength(0), colors.GetLength(1),
96, 96, PixelFormats.Rgb24, null, raw, colors.GetLength(0) * 3);
return bmp;
}
}
}