rayshader
From https://github.com/tylermorganwall/rayshader:
rayshader is an open source package for producing 2D and 3D data visualizations in R. rayshader uses elevation data in a base R matrix and a combination of raytracing, spherical texture mapping, overlays, and ambient occlusion to generate beautiful topographic 2D and 3D maps. In addition to maps, rayshader also allows the user to translate ggplot2 objects into beautiful 3D data visualizations.
The models can be rotated and examined interactively or the camera movement can be scripted to create animations. Scenes can also be rendered using a high-quality pathtracer, rayrender.The user can also create a cinematic depth of field post-processing effect to direct the user’s focus to important regions in the figure. The 3D models can also be exported to a 3D-printable format with a built-in STL export function.
Installation
if (!require(rayshader)) {
# To install the latest version from Github:
# install.packages("devtools")
devtools::install_github("tylermorganwall/rayshader", update = "never")
}
library(rayshader)
if (file.exists("data/terrain-medium.png")) {
small_input <- "data/terrain-medium.png"
} else {
small_input <- file.choose()
}
image_test <- data.frame(png::readPNG(small_input))
# import desert heightmap demo
if (file.exists("data/dem_01.tif")) {
localtif <- raster::raster("data/dem_01.tif")
} else {
localtif <- file.choose()
}
#And convert it to a matrix:
desertmap <- raster_to_matrix(localtif)
mapHeight <- function(image_map, image_width, image_height) {
# make a 1.0 x 1.0 plane starting at (0, 0, 0)... z is up
heights <- matrix(1, nrow = image_height, ncol = image_width)
for (y in 1:image_height) {
for (x in 1:image_width) {
heights[y, x] <- round(image_map[y, x], 6)
}
}
heights
}
heightmap <- mapHeight(image_test, length(image_test[1,]), length(image_test[, 1]))
print(nrow(heightmap)) # width of the heightmap
[1] 512
print(ncol(heightmap)) # height of the heightmap
[1] 512
# print without tabs so R markdown will add it to the DOM tree of the resulting page
cat(paste('<div id="data_in_html"><script type="application/json">', jsonlite::toJSON(heightmap), '\n</script>\n</div>', sep = ""))