smoothConservative static method

void smoothConservative(
  1. Float32List g,
  2. TerrainOptions options, [
  3. double? multiplier
])

Smooth the terrain by clamping each point within its neighbors' extremes.

Float32List g The geometry's z-positions to modify with heightmap data. 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}(). double multiplier = null By default, this filter clamps each point within the highest and lowest value of its neighbors. This parameter is a multiplier for the range outside of which the point will be clamped. Higher values mean that the point can be farther outside the range of its neighbors.

Implementation

static void smoothConservative(Float32List g, TerrainOptions options, [double? multiplier]) {
  final heightmap = Float32List(g.length);
  for (int i = 0, xl = options.xSegments + 1, yl = options.ySegments + 1; i < xl; i++) {
    for (int j = 0; j < yl; j++) {
      double max = -double.infinity,
          min = double.infinity;
      for (int n = -1; n <= 1; n++) {
        for (int m = -1; m <= 1; m++) {
          final key = (j+n)*xl + i + m;
          if (n != 0 && m != 0 && i+m >= 0 && j+n >= 0 && i+m < xl && j+n < yl) {//typeof g[key] != 'undefined' &&
            if (g[key] < min) min = g[key];
            if (g[key] > max) max = g[key];
          }
        }
      }
      final kk = j*xl + i;
      if (multiplier != null) {
        final halfdiff = (max - min)*0.5,
            middle = min + halfdiff;
        max = middle + halfdiff*multiplier;
        min = middle - halfdiff*multiplier;
      }
      heightmap[kk] = g[kk] > max ? max : (g[kk] < min ? min : g[kk]);
    }
  }
  for (int k = 0, l = g.length; k < l; k++) {
    g[k] = heightmap[k];
  }
}