Today, it´s time to render images using the Rayshader library, “…an open source package for producing 2D and 3D data visualizations in R…”. I wanted a somewhat dramatic landscape, so Etna seemed like a nice pick.
The interactive model and the final, rendered image may differ a bit, so remember to look at the final result. The high resolution render is CPU only, no GPU. My fairly decent laptop CPU (14 cores, 20 threads) takes several minutes, at almost 100%, to render the final image. GPU support would be nice here.
After a lot of experimenation, the final result looks nice. The R script:
# Set directory
setwd("C:/Eget/R")
# Libraries
library(rayshader)
library(rayrender)
library(raster)
library(elevatr)
cat("Rayshader version:", as.character(packageVersion("rayshader")), "\n\n")
# 1. Define area for Etna
etna_bbox <- data.frame(
x = c(14.8, 15.2),
y = c(37.6, 37.9)
)
# 2. Get elevation data
cat("Fetching elevation data...\n")
etna_elevation <- get_elev_raster(
locations = etna_bbox,
z = 10,
prj = "+proj=longlat +datum=WGS84"
)
# 3. To matrix
etna_matrix <- raster_to_matrix(etna_elevation)
# 4. Textures and shadows
etna_render <- etna_matrix %>%
sphere_shade(texture = "desert") %>%
add_shadow(ray_shade(etna_matrix, zscale = 20), 0.5) %>%
add_shadow(ambient_shade(etna_matrix), 0.3) %>%
add_water(detect_water(etna_matrix), color = "lightblue")
# 5. Render interactively
etna_render %>%
plot_3d(etna_matrix, zscale = 20,
theta = 45, phi = 30, zoom = 0.6,
windowsize = c(1200, 800),
water = TRUE,
waterdepth = 0,
wateralpha = 0.6,
watercolor = "lightblue")
# Move window
rgl::par3d(windowRect = c(100, 100, 1300, 900))
# 6. Add water
render_water(etna_matrix,
zscale = 20,
waterdepth = 0,
watercolor = "#00B4D8",
wateralpha = 0.5)
# 7. Add clouds
render_clouds(etna_matrix,
zscale = 20,
start_altitude = 2000,
end_altitude = 2500,
fractal_levels = 32,
cloud_cover=0.3,
clear_clouds = TRUE)
# 8. Render highres image
render_highquality(
filename = "etna_highres.png",
samples = 200,
width = 2000,
height = 1500,
lightdirection = 315,
lightaltitude = 45,
lightintensity = 1000,
backgroundhigh = "#87CEEB",
backgroundlow = "#E0F6FF",
ambient_light = TRUE,
clear = FALSE
)
From here, it´s easy to render different locations, mostly just by changing the coordinates. Here is Volcán Alcedo, Galapagos:

Like all 3D rendering, the options are endless, such as lighting, clouds, materials etc. Some of them are very CPU-intensive, so use them wisely.