toHeightmap static method

Uint8List toHeightmap(
  1. Float32List g,
  2. TerrainOptions options
)

Convert a terrain plane into an image-based heightmap.

Parameters are the same as for {@link THREE.Terrain.fromHeightmap} except that if options.heightmap is a canvas element then the image will be painted onto that canvas; otherwise a new canvas will be created.

Float32List g The vertex position array for the geometry to paint to a heightmap. TerrainOptions options A map of settings that control how the terrain is constructed and displayed. Valid values are the same as those for the options parameter of {@link THREE.Terrain}().

@return {HTMLCanvasElement} A canvas with the relevant heightmap painted on it.

Implementation

static Uint8List toHeightmap(Float32List g, TerrainOptions options) {
    bool hasMax = options.maxHeight != null;
    bool hasMin = options.minHeight != null;

    double max = hasMax ? options.maxHeight! : -double.infinity;
    double min = hasMin ? options.minHeight! :  double.infinity;

    if (!hasMax || !hasMin) {
      double max2 = max,
        min2 = min;
      for (int k = 2, l = g.length; k < l; k += 3) {
        if (g[k] > max2) max2 = g[k];
        if (g[k] < min2) min2 = g[k];
      }
      if (!hasMax) max = max2;
      if (!hasMin) min = min2;
    }

    int rows = options.ySegments + 1;
    int cols = options.xSegments + 1;
    double spread = max - min;
    Uint8List data = Uint8List(rows*cols*4);

    for (int row = 0; row < rows; row++) {
      for (int col = 0; col < cols; col++) {
        int i = row*cols + col,
        idx = i*4;
        data[idx] = data[idx+1] = data[idx+2] = (((g[i*3 + 2] - min) / spread)*255).round();
        data[idx+3] = 255;
      }
    }
    return data;
}