Adds 3D polygons to the current scene, using latitude/longitude or coordinates in the reference system defined by the extent object.

Cache fallback messages are disabled by default. Set options(rayshader.verbose_scene_cache = TRUE) to print when cached metadata is reused.

render_polygons(
  polygon,
  color = "red",
  top = 1,
  bottom = NA,
  data_column_top = NULL,
  data_column_bottom = NULL,
  scale_data = 1,
  parallel = FALSE,
  holes = 0,
  alpha = 1,
  lit = TRUE,
  light_altitude = c(45, 30),
  light_direction = c(315, 135),
  light_intensity = 0.3,
  light_relative = FALSE,
  clear_previous = FALSE,
  crs = NULL,
  filter_to_extent = TRUE,
  extent = NULL,
  panel = NULL,
  zscale = 1,
  vertical_exaggeration = 1,
  heightmap = NULL,
  ...
)

Arguments

polygon

sf object, "SpatialPolygon" sp object, or xy coordinates of polygon represented in a way that can be processed by xy.coords(). If xy-coordinate based polygons are open, they will be closed by adding an edge from the last point to the first.

color

Default black. Color of the polygon. Use "height" to color polygons by the cached plot_gg() height aesthetic palette using data_column_top, data_column_bottom, or top.

top

Default 1. Extruded top distance. If this equals bottom, the polygon will not be extruded and just the one side will be rendered.

bottom

Default NA. Extruded bottom distance. With NA, places the bottom halfway between the scene's base and lowest surface. If this equals top, the polygon will not be extruded and just the one side will be rendered.

data_column_top

Default NULL. A string indicating the column in the sf object to use to specify the top of the extruded polygon. Values are coerced to numeric, and rows with missing or non-finite values after coercion are omitted.

data_column_bottom

Default NULL. A string indicating the column in the sf object to use to specify the bottom of the extruded polygon. Values are coerced to numeric, and rows with missing or non-finite values after coercion are omitted.

scale_data

Default 1. If specifying data_column_top or data_column_bottom, how much to scale that value when rendering. When omitted in a mapped plot_gg() scene, data-column values use the cached ggplot height scale. Supplying scale_data explicitly uses raw values multiplied by this factor instead. Explicit top and bottom remain in scene height units. If used with vertical_exaggeration, both are applied.

parallel

Default FALSE. If TRUE, polygons will be extruded in parallel, which may be faster (depending on how many geometries are in polygon).

holes

Default 0. If passing in a polygon directly, this specifies which index represents the holes in the polygon. See the earcut function in the decido package for more information.

alpha

Default 1. Transparency of the polygons.

lit

Default TRUE. Whether to light the polygons.

light_altitude

Default c(45, 60). Degree(s) from the horizon from which to light the polygons.

light_direction

Default c(45, 60). Degree(s) from north from which to light the polygons.

light_intensity

Default 0.3. Intensity of the specular highlight on the polygons.

light_relative

Default FALSE. Whether the light direction should be taken relative to the camera, or absolute.

clear_previous

Default FALSE. If TRUE, clears all existing polygons. A clear-only call returns without rendering a replacement.

crs

Default NULL. CRS of the input numeric x/y coordinates, or CRS to assign to CRS-less spatial data before transforming it into the active scene CRS. If spatial data already carries a CRS, that CRS is used automatically.

filter_to_extent

Default TRUE. If TRUE, polygon data outside the scene extent is omitted. Spatial polygon inputs are cropped to the extent. For scenes created with plot_gg(), filtering uses the ggplot panel extent rather than the full rendered 3D ggplot extent.

extent

Either an object representing the spatial extent of the 3D scene (either from the raster, terra, sf, or sp packages), a length-4 numeric vector specifying c("xmin", "xmax", "ymin", "ymax"), or the spatial object (from the previously aforementioned packages) which will be automatically converted to an extent object. If omitted, rayshader will use extent metadata cached by plot_3d() or plot_gg().

panel

Default NULL. Facet panel identifier for scenes created with plot_gg(). Required to disambiguate faceted ggplot scenes when panel-specific cached metadata is needed. Ignored for non-ggplot scenes.

zscale

Default 1. The ratio between the x and y spacing (which are assumed to be equal) and the z axis in the original heightmap.

vertical_exaggeration

Default 1. Multiplier applied to the effective visual relief. If omitted, rayshader uses the cached scene value from plot_3d() or plot_gg() when available; pass explicitly to override for this call.

heightmap

Default NULL. Height matrix for the current scene. If omitted, this is taken from the cached scene set by plot_3d() or plot_gg(). Pass explicitly to override the cached value. of matrix extent isn't working. A two-dimensional matrix, where each entry in the matrix is the elevation at that point. All points are assumed to be evenly spaced.

...

Additional arguments passed to rgl::triangles3d().

Examples

#Render the county borders as polygons in Monterey Bay
montereybay_spatial |>
  sphere_shade(texture = "desert", vertical_exaggeration = 20) |>
  add_shadow(ray_shade(vertical_exaggeration = 4)) |>
  plot_3d(water = TRUE, vertical_exaggeration = 4, window_size = 800, water_color = "dodgerblue")
render_camera(theta = 140,  phi = 55, zoom = 0.85, fov = 30)

#We will apply a negative buffer to create space between adjacent polygons. You may
#have to call `sf::sf_use_s2(FALSE)` before running this code to get it to run.
sf::sf_use_s2(FALSE)
mont_county_buff = sf::st_simplify(sf::st_buffer(monterey_counties_sf,-0.003), dTolerance=0.001)
#> Warning: st_buffer does not correctly buffer longitude/latitude data
#> dist is assumed to be in decimal degrees (arc_degrees).
#> Warning: st_simplify does not correctly simplify longitude/latitude data, dTolerance needs to be in decimal degrees

render_polygons(mont_county_buff, top = 2000,
                parallel = FALSE)
render_snapshot()

#We can specify the bottom of the polygons as well. Here I float the polygons above the surface
#by specifying the bottom argument. We clear the previous polygons with `clear_previous = TRUE`.
render_camera(theta=-60,  phi=20, zoom = 0.85, fov=0)
render_polygons(mont_county_buff, bottom = 24000, top=25000,
                parallel=FALSE,clear_previous=TRUE)
render_snapshot()

#We can set the height of the data to a column in the sf object: we'll use the land area.
#We'll have to scale this value because its max value is 2.6 billion:
render_camera(theta=-60,  phi=40, zoom = 0.85, fov=30)
render_polygons(mont_county_buff, data_column_top = "ALAND",
                scale_data = 300/(5E7), color = "chartreuse4",
                clear_previous = TRUE)
render_zaxis(data = "polygon", location = "topright",
             title_location = "top")
render_snapshot()

#This function also works with `render_highquality()`
render_highquality(samples = 16, min_variance = 0)