Skip to content

Instantly share code, notes, and snippets.

@xiaodaigh
Created March 12, 2019 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaodaigh/e4f64d287030c6f666aaf3cb9e325598 to your computer and use it in GitHub Desktop.
Save xiaodaigh/e4f64d287030c6f666aaf3cb9e325598 to your computer and use it in GitHub Desktop.
Example rotating sunangle
library(rayshader)
library(av)
library(future)
plan(multiprocess)
# set up an elevation matrix with a wall around the outside and a gap on each of
# the walls.
elmat1 = matrix(0, 500, 500)
elmat1[1:100, 1:200] = 3000
elmat1[1:100, 300:500] = 3000
elmat1[400:500, 1:200] = 3000
elmat1[400:500, 300:500] = 3000
elmat1[1:200, 400:500] = 3000
elmat1[300:500, 400:500] = 3000
elmat1[1:200, 1:100] = 3000
elmat1[300:500, 1:100] = 3000
# rotate the sun angle around from 0 360 and generate a snapshot for each angle
furrr::future_map(seq(0,360,5), ~{
sunangle = .x
rgl::rgl.clear()
elmat1 %>%
sphere_shade %>%
add_shadow(ray_shade(elmat1,zscale=3,maxsearch = 1000, sunangle = sunangle),0.5) %>%
plot_3d(elmat1,zscale=10,fov=0,theta=65,zoom=0.75, phi=65, windowsize = c(1000,800))
render_snapshot(filename=paste0("frame",sunangle))
print(glue::glue("{sunangle} {Sys.time()}"))
.x
})
# all the png files created
png_files = sprintf("frame%d.png", seq(0,360,5))
# make mp4 from individual snapshots
av::av_encode_video(png_files, 'output2.mp4', framerate = 5)
# view the video
utils::browseURL('output2.mp4')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment