Skip to content

Instantly share code, notes, and snippets.

@markgrujic
Last active May 11, 2020 05:02
Show Gist options
  • Save markgrujic/f924ad2b2f0f073723d2eb3b7ea89ac5 to your computer and use it in GitHub Desktop.
Save markgrujic/f924ad2b2f0f073723d2eb3b7ea89ac5 to your computer and use it in GitHub Desktop.
# devtools::install_github("tylermorganwall/rayshader")
library(rayshader)
library(raster)
# replace these with your rasters
r_shadow <- raster("to_shadow.tif")
r_elev <- raster("to_elev.tif")
# resample elevation raster to extent/res of shadow raster
r_elev <- resample(r_elev, r_shadow, method='bilinear')
# convert to matrix
mat_shadow <- raster_to_matrix(r_shadow)
mat_elev <- raster_to_matrix(r_elev)
# create shade object
obj <- mat_shadow %>% sphere_shade(texture = "imhof3")
# define rayshading and ambient shading
ray_s <- ray_shade(mat_shadow)
amb_s <- ambient_shade(mat_shadow)
# RGL 3D plot of elevation surface and rayshaded surface
obj %>%
add_shadow(ray_s, 0.5) %>%
add_shadow(amb_s, 0) %>%
plot_3d(mat_elev, zscale = 16,
fov = 50,
soliddepth = -50,
theta = 22.5,
zoom = 0.75,
phi = 45,
windowsize = c(2000, 1200))
# add label
render_label(clear_previous = TRUE)
render_label(mat_shadow,
x=600,
y=505,
z=8000,
zscale=16,
text = "Catchy Title!",
freetype = F)
render_snapshot()
save_3dprint("print_me.stl", maxwidth = 100, unit = "mm", clear=TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment