#https://www.tylermw.com/posts/data_visualization/adding-open-street-map-data-to-rayshader-maps-in-r.html

library(rayshader)
library(raster)
library(sf)
library(osmdata)
library(dplyr)
library(ggplot2)
library(magick)

crater = raster('~/desktop/working/dwv26/Crater_Lake.tif')
crater_mat = raster_to_matrix(crater)

crater_small = resize_matrix(crater_mat,0.25)

#The desert texture, made once and used in each map below.
#ray_read_image() gives it the transparency channel that current versions
#of rayshader need before add_overlay() can blend it in.
desert = sphere_shade(crater_small, texture = "desert", 
                      zscale=4, colorintensity = 5) %>%
  rayimage::ray_read_image(convert_to_array = TRUE)

crater_small %>% 
  height_shade() %>% 
  plot_map()

crater_small %>% 
  height_shade() %>% 
  add_overlay(desert, alphalayer=0.5) %>%
  plot_map()

crater_small %>% 
  height_shade() %>% 
  add_overlay(desert, alphalayer=0.5) %>%
  add_shadow(lamb_shade(crater_small,zscale = 6),0) %>%
  plot_map()


crater_small %>% 
  height_shade() %>% 
  add_overlay(desert, alphalayer=0.5) %>%
  add_shadow(lamb_shade(crater_small,zscale=6), 0) %>%
  add_shadow(texture_shade(crater_small,detail=8/10,contrast=9,brightness = 11), 0.1) %>%
  plot_map()


crater_small %>% 
  height_shade() %>% 
  add_overlay(desert, alphalayer=0.5) %>%
  add_shadow(lamb_shade(crater_small,zscale=6), 0) %>%
  add_shadow(ambient_shade(crater_small), 0) %>%
  add_shadow(texture_shade(crater_small,detail=8/10,contrast=9,brightness = 11), 0.1) %>%
  plot_map()