How does one convert a System.Drawing type Bitmap into a compatable terrain heightmap for unity
Off the bat i find it extremely annoying how unity is really picky about its height maps. In reality it doesn't really need to be gray scale as any rgb image can be used for a height map. Find the average of all colours to get the height.
I have managed to slice a large heightmap up based on the segmentation of my terrain generator. In my case im generating 5x5 128 unit grid of terrain. I figgured that i take a 5125 x 5125 pixel image and slice it up in the same way. So that gives me for each 128x128 terrain a nice high resolution 1025x1025 pixel image to pull height data from.
However i have tried a multitude of solutions arround the web and all of them seem to just be mimicking gray scale images by shifting the colours toward gray rather than changing the bit rate of the image.
I have scoured the forums and salvaged pieces of code from simmilar problems and this is the image generation code as of current:
public void GenerateChunkHeightmapTile(string outputPath, Vector2 position, TerrainData chunk)
{
//Find the bounds of the image based on the ammount of chunks and width of the image, ie cell 2,2 on a 5x5 with heightmap master being 5125x5125 would be 1025 -> 2025 for example
int xMin = (int)((position.x / terrainSpec.numChunks.x) * heightmapImage.Size.Width);
int xMax = xMin + (int)(heightmapImage.Size.Width / terrainSpec.numChunks.x);
int yMin = (int)((position.y / terrainSpec.numChunks.y) * heightmapImage.Size.Height);
int yMax = yMin + (heightmapImage.Size.Height / (int)terrainSpec.numChunks.y);
//Check if the output directory exists
if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); }
//The name of the file to be writen
string outputFileName = Path.Combine(outputPath, string.Format("Heightmap_{0}_{1}.raw", (int)position.x, (int)position.y));
//Define a rectangle that we will sample for our heightmap
Rectangle tileBounds = new Rectangle(xMin, yMin, heightmapImage.Size.Width / (int)terrainSpec.numChunks.x, heightmapImage.Size.Height / (int)terrainSpec.numChunks.y);
//size and format of the image, tried all pixel formats such as 16bitGrayscale and 1bit indexed, none works. It throws errors!
Bitmap target = new Bitmap(xMax - xMin, yMax - yMin, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(target))
{
graphics.DrawImage(
heightmapImage,
new Rectangle(0, 0, target.Width, target.Height),
tileBounds,
GraphicsUnit.Pixel);
}
target.Save(outputFileName);
}
I have managed to get all my images saved in raw format and are segmenting correctly. However the challenge is getting them saved as a file that can be used with the unity terrain. I do not want to have to manually convert these images myself as i intend to have terrains with hundreds of chunks on it. That would take hours. Here is the image code as is. I have tried changing the PixelFormat to 16bit grayscale 1bitindexed and all that and nothing seems to work. This is so annoying as i cant fathom why the heightmap has to be in raw format. Can anyone help me out?
Your answer
Follow this Question
Related Questions
World Machine to Unity 2018.4.10f1 Heightmap Import Issues 0 Answers
Heightmap from png 1 Answer
Jagged Heightmap 0 Answers
Change Terrain Heightmap Resolution without Resizing Terrain 1 Answer
unity terrain apply texture by altitude 0 Answers