Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created November 5, 2021 12:32
Show Gist options
  • Save marcosci/5cba144a8bd782d4d8fdec619a56e444 to your computer and use it in GitHub Desktop.
Save marcosci/5cba144a8bd782d4d8fdec619a56e444 to your computer and use it in GitHub Desktop.
Hexagonal Rayshading
library(LaCroixColoR)
library(sf)
library(fasterize)
library(rayshader)
library(raster)
library(exactextractr)
library(rayrender)
# load raster into R
elevation <- raster("Olympus_Mons_ortho-image.tif")
# make hexagonal grid
hexbin_vec <- st_make_grid(st_as_sfc(st_bbox(elevation)), cellsize = 25, square = FALSE)
# get mean raster values for each hexagon
values <- exact_extract(elevation, hexbin_vec, "mean")
# turn it into a simple feature and give it values
hexbin_vec <- st_sf(geom=hexbin_vec)
hexbin_vec$elevation <- values
# turn hexbin into raster
hexbins <- fasterize(hexbin_vec,
elevation,
field = "elevation",
fun="sum")
# and the raster into a rayrender!
elevation_matrix <- raster_to_matrix(hexbins)
elevation_matrix %>%
height_shade(texture = lacroix_palette("Pamplemousse", 256)) %>%
add_shadow(ray_shade(elevation_matrix, zscale=2, sunaltitude=25, sunangle=315), 0.3) %>%
plot_3d(elevation_matrix, zscale = 0.3, shadow = FALSE, fov = 0, theta = -45, baseshape = "hex")
render_highquality(filename = "hexagons_highres.png",
samples=500,
lightdirection = c(60,120, 240),
lightaltitude=c(90,25, 12),
lightintensity=c(100, 500, 450),
lightcolor = c("white", "#FF9956", "#FF79E7"),
progress=TRUE,
parallel=TRUE)
@marcosci
Copy link
Author

marcosci commented Nov 5, 2021

@sugatoray
Copy link

🚀 Output as shared on tweet.

output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment