Search code examples
rrenderrendererplot3drayshader

render_highquality from rayshader R package only rendering black .png image


I'm following this tutorial: https://www.youtube.com/watch?v=zgFXVhmKNbU&lc=Ugxj9Lfa6m7FfLaJlzN4AaABAg.9kn2A9lbmnP9kn2SD5s2vs In the video render_highquality function exports 3d plot successfully as .png image. My code runs fine, everything up until render_highquality is producing the expected output but render_highquality is not. I'm getting the image exported but .png image is exporting as completely black image and I don't know what is happening nor how to fix it. Other considerations are that render_snapshot and save_obj functions are working fine as well.

Here is my code:

library(rayshader)
library(MetBrewer)
library(colorspace)
library(sf)
library(raster)
library(ggplot2)
library(stars)

data <- st_read("data/kontur_population_MX_20220630.gpkg")
ags <- getData("GADM", country = "Mexico", level = 1) %>% 
  st_as_sf() %>%
  filter(NAME_1 == "Aguascalientes") |> 
  st_transform(crs= st_crs(data))

st_ags <- st_intersection(data, ags)

bb <- st_bbox(st_ags)

bottom_left <- st_point(c(bb[["xmin"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

bottom_right <- st_point(c(bb[["xmax"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

width <- st_distance(bottom_left, bottom_right)

top_left <- st_point(c(bb[["xmin"]], bb[["ymax"]])) |> 
  st_sfc(crs = st_crs(data))

height <- st_distance(bottom_left, top_left)

if (width > height) {
  w_ratio <- 1
  h_ratio <- height / width
} else {
  h_ration <- 1
  w_ratio <- width / height
}

size <- 1000

ags_rast <- st_rasterize(st_ags, 
                     nx = floor(size * w_ratio),
                     ny = floor(size * h_ratio))

mat <- matrix(ags_rast$population, 
          nrow = floor(size * w_ratio),
          ncol = floor(size * h_ratio))


c1 <- met.brewer("OKeeffe2")
swatchplot(c1)

texture <- grDevices::colorRampPalette(c1, bias = 2)(256)
swatchplot(texture)

mat |> 
  height_shade(texture = texture) |> 
  plot_3d(heightmap = mat,
          zscale = 100,
          solid = FALSE,
          shadowdepth = 0)

png_outfile <- 'imgs/ags_model.png'

render_camera(theta = -20, phi = 45, zoom = .8)

render_highquality(
  filename = png_outfile
)

The only thing I tried differently to fix the problem is removing and reinstalling rayshader package from GitHub with the following command:

devtools::install_github("tylermorganwall/rayshader") I'm working on a M1 Mac mini with Mac OS Ventura 13.0.1


Solution

  • Reinstalling rayshader and rayrender packages through remotes did the trick.

    install.packages("remotes")
    remotes::install_github("tylermorganwall/rayshader")
    remotes::install_github("tylermorganwall/rayrender")