scatterHelper static method

Float32List Function() scatterHelper(
  1. void method(
    1. Float32List,
    2. TerrainOptions
    ),
  2. TerrainOptions options, [
  3. int? skip,
  4. double? threshold,
])

Generate a function that returns a heightmap to pass to ScatterMeshes.

Specifically, this function generates a heightmap and then uses that heightmap as a map of probabilities of where meshes will be placed.

void Function(Float32List,TerrainOptions) method A random terrain generation function (i.e. a valid value for the options.heightmap parameter of the THREE.Terrain function). TerrainOptions options A map of settings that control how the resulting noise should be generated (with the same parameters as the options parameter to the THREE.Terrain function). options.minHeight must equal 0 and options.maxHeight must equal 1 if they are specified. int skip The number of sequential faces to skip between faces that are candidates for placing a mesh. This avoid clumping meshes too closely together. Defaults to 1. double threshold The probability that, if a mesh can be placed on a non-skipped face due to the shape of the heightmap, a mesh actually will be placed there. Helps thin out placement and make it less regular. Defaults to 0.25.

return Float32List Function() Returns a function that can be passed as the value of the options.randomness parameter to the {@link THREE.Terrain.ScatterMeshes} function.

Implementation

static Float32List Function() scatterHelper(void Function(Float32List,TerrainOptions) method, TerrainOptions options, [int? skip, double? threshold]) {
  skip ??= 1;
  threshold ??= 0.25;
  options.frequency = options.frequency;

  final clonedOptions = TerrainOptions();
  for (final opt in options.keys) {
    clonedOptions[opt] = options[opt];
  }

  clonedOptions.xSegments *= 2;
  clonedOptions.stretch = true;
  clonedOptions.maxHeight = 1;
  clonedOptions.minHeight = 0;
  final heightmap = heightmapArray(method, clonedOptions);

  for (int i = 0, l = heightmap.length; i < l; i++) {
    if (i % skip != 0 || math.Random().nextDouble() > threshold) {
      heightmap[i] = 1; // 0 = place, 1 = don't place
    }
  }
  return () {
    return heightmap;
  };
}