tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.06k stars 214 forks source link

How to make animate the sun angle being rotated? #39

Closed xiaodaigh closed 5 years ago

xiaodaigh commented 5 years ago

I see on the front page where the sun rotates around. However do I make the same animation where the sun rotates around the square pole I have set up below??

elmat1 = matrix(0, 500, 500)
elmat1[200:400, 200:400] = 971.0

plotlah <- function(elmat1, sunangle1) {
  elmat1 %>%
    sphere_shade %>%
    add_shadow(ray_shade(elmat1,zscale=3,maxsearch = 300, sunangle = sunangle1),0.5) %>%
    plot_3d(elmat1,zscale=10,fov=0,theta=0,zoom=0.75, phi=25, windowsize = c(1000,800))
  render_snapshot()
}

# I want to animate sunangle = 1:360 each for 0.1s
for(sunangle in 1:360) {
  plotlah(elmat1, 315)
  addplot_to_gif() # how to code this function?
}

In the above I envision a addplot_to_gif() function to build up a gif where each frame is a plot with a different sunangle.

tylermorganwall commented 5 years ago

You don't need a function for that, you can simply put it into the for loop.

library(rayshader)
library(av)
elmat1 = matrix(0, 500, 500)
elmat1[200:400, 200:400] = 971.0

for(sunangle in 1:360) {
  elmat1 %>%
    sphere_shade %>%
    add_shadow(ray_shade(elmat1,zscale=3,maxsearch = 300, sunangle = sunangle),0.5) %>%
    plot_3d(elmat1,zscale=10,fov=0,theta=0,zoom=0.75, phi=25, windowsize = c(1000,800))
  render_snapshot(filename=paste0("frame",sunangle))
  rgl::rgl.clear()
}

png_files <- sprintf("frame%d.png", 1: 360)
av::av_encode_video(png_files, 'output.mp4', framerate = 10)
utils::browseURL('output.mp4')
tylermorganwall commented 5 years ago

Feel free to ask any more questions here if you have them