I currently have a Unity3D tool that allows users to import different heightmap files into the Unity3d Game Engine, in order to produce Unity Terrain objects. The tool currently has limited support for GeoTIFF files, as I am manually reading them and that only works for some projection types. I would like to expand support to (hopefully) all GeoTIFF files by using the GDAL Nuget package from Tamas Szekeres.
I basically need to take a GeoTIFF file, and produce a heightmap from it, which is a float[,] array of normalized values between 0 and 1. Unity uses this information to create the Terrain object.
What is the best way to do this? I first attempted to use the DataSet.ReadRaster method, but that is crashing Unity with an error saying I am trying to read an invalid memory address. Even if I could get that method working, I am not sure if it is the correct route to take.
Here is what that looked like:
double rasterData = new double[outputWidth * outputLength]; ds.ReadRaster(0, 0, 65, 65, rasterData, 65, 65, ds.RasterCount, null, 0, 0, 0);
Most suggestions I see say to use the individual Raster Bands, which I did test and it does not cause an error (I did not actually try to use this data), however how do I handle different GeoTIFF files (using different projections and data formats) with this method? For example, I have many files with a single Raster Band and which store individual height data as floats, which seems easy enough to read, but then I have other files where the height data is in RGB format.
Is there a single method I can call to translate the data, no matter what format it is, to a normalized value or a value that I can manually normalize? Or do I need to handle each projection type independently?
I am using this in a 64 bit Unity Editor.
I have been using LibTiff.Net to manually read the GeoTIFF files. I thought using GDAL I would be able to gain access to some sort of auto translate function to convert almost any GeoTIFF file to either an array of raw height values or even an array of normalized values. But the more I look into it it seems I will have to read the DataType of each file and manually convert each data point as I am already doing, so I am not sure if there is any advantage in switching to GDAL.
Is that correct?