Generates a semi-transparent constant-color overlay whose opacity follows an exponential atmospheric column model.

haze_shade(
  heightmap,
  color = "white",
  optical_depth = 0.5,
  scale_height = NULL,
  scale_height_relative = FALSE,
  reference_height = NULL,
  alpha = 1,
  blur = 0
)

Arguments

heightmap

A two-dimensional matrix, where each entry in the matrix is the elevation at that point. If omitted, the function attempts to use the cached heightmap from the current rayshader context.

color

Default "white". Color of the haze overlay.

optical_depth

Default 0.5. Optical depth of the haze layer at reference_height. Larger values make haze more opaque at all elevations.

scale_height

Default NULL. Vertical distance over which haze optical depth decreases by a factor of exp(-1). If scale_height_relative = FALSE, this is in the same elevation units as heightmap. If scale_height_relative = TRUE, this is a proportion of the heightmap elevation range and must be in (0, 1]. If NULL, defaults to 0.2 of the elevation range, or 1 for a flat heightmap.

scale_height_relative

Default FALSE. If TRUE, interpret scale_height as a fraction of the heightmap elevation range rather than as an absolute elevation distance.

reference_height

Default NULL. Elevation where haze optical depth is equal to optical_depth. Elevations above this height have less haze; elevations below this height have more haze. If NULL, uses the minimum finite elevation in heightmap.

alpha

Default 1. Multiplier applied to the final haze alpha. Must be in [0, 1].

blur

Default 0. Gaussian standard deviation in output-image pixels. 0 disables convolution. Positive values use a normalized kernel spanning approximately plus or minus three standard deviations.

Value

A 4-layer RGBA array representing the haze overlay.

Details

haze_shade() creates a haze overlay whose alpha channel is largest near reference_height and decreases exponentially with elevation above that height.

The haze opacity is computed as:

height_rel = heightmap - reference_height
log_tau = log(optical_depth) - height_rel / scale_height
haze_alpha = -expm1(-exp(log_tau))

optical_depth controls the haze strength at reference_height. scale_height controls how quickly haze decreases with elevation. After increasing elevation by one scale_height, optical depth is multiplied by exp(-1).

When scale_height = NULL, the automatic scale height depends on the finite elevation range: it is 20 percent of that range for non-flat terrain and one elevation unit for flat terrain. Consequently, subsetting a heightmap or adding an extreme outlier can change the default haze profile.

The final alpha channel is multiplied by alpha and clamped to [0, 1].

Examples

# Prepare a mountain heightmap.
mb_mountains = raster_to_matrix(montereybay_spatial)
mb_mountains[mb_mountains < 0] = 0
mb_mountains = mb_mountains[271:540, 271:540]

# Render terrain without haze.
no_haze = mb_mountains |>
  sphere_shade(
    texture = "imhof2",
    zscale = 200,
    vertical_exaggeration = 4
  ) |>
  add_shadow(
    lamb_shade(
      mb_mountains,
      vertical_exaggeration = 4
    ),
    0
  ) |>
  add_water(
    detect_water(mb_mountains, cutoff = 0.9999999),
    "dodgerblue4"
  )

plot_map(no_haze)


# Add default white haze.
no_haze |>
  add_overlay(
    haze_shade(mb_mountains)
  ) |>
  plot_map()


# Increase optical depth to make the haze denser everywhere.
no_haze |>
  add_overlay(
    haze_shade(
      mb_mountains,
      optical_depth = 1
    )
  ) |>
  plot_map()


# Use a smaller scale height (150 meters) to keep haze concentrated in valleys.
no_haze |>
  add_overlay(
    haze_shade(
      mb_mountains,
      optical_depth = 0.7,
      scale_height = 150
    )
  ) |>
  plot_map()


# Express scale height as a fraction of the elevation range.
no_haze |>
  add_overlay(
    haze_shade(
      mb_mountains,
      optical_depth = 0.7,
      scale_height = 0.02,
      scale_height_relative = TRUE
    )
  ) |>
  plot_map()


# Raise the haze layer by setting the reference height.
no_haze |>
  add_overlay(
    haze_shade(
      mb_mountains,
      optical_depth = 0.6,
      reference_height = as.numeric(quantile(mb_mountains, 0.25))
    )
  ) |>
  plot_map()


# Add bluish atmospheric haze.
no_haze |>
  add_overlay(
    haze_shade(
      mb_mountains,
      color = "#abbbca",
      optical_depth = 1
    )
  ) |>
  plot_map()


# Blur the haze alpha map to soften transitions.
no_haze |>
  add_overlay(
    haze_shade(
      mb_mountains,
      optical_depth = 1,
      blur = 2
    )
  ) |>
  plot_map()