Created
August 27, 2019 13:31
-
-
Save tylermorganwall/ce755fdf6bafd00fe6d68ca445780b52 to your computer and use it in GitHub Desktop.
Creating the River Derwent, jumping out of the map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(rayshader) | |
loadzip = tempfile() | |
download.file("https://www.tylermw.com/data/dem_01.tif.zip", loadzip) | |
localtif = raster::raster(unzip(loadzip, "dem_01.tif")) | |
unlink(loadzip) | |
xposfunction = function(x0,v0,a0, loss=1/4, low,starttime,cutoff) { | |
xpos = rep(x0,360) | |
for(i in starttime:360) { | |
x0 = x0 + v0 | |
if(x0 > low) { | |
v0 = v0 - a0 | |
} else { | |
x0 = -x0 | |
v0 = -v0*loss | |
} | |
xpos[i] = x0 | |
} | |
xpos[(cutoff+starttime):360] = low | |
xpos | |
} | |
heightvals = c(xposfunction(0,5.5,0.08,1/2,0,1,330)[66:360], | |
xposfunction(0,5.5,0.08,1/2,0,1,330)[1:65]) | |
elmat_all_na = matrix(NA,nrow(elmat), ncol(elmat)) | |
elmat_all_na[1,1] = 400 | |
elmat_no_water = elmat | |
elmat_no_water[rayshader:::fliplr(detect_water(elmat)) == 1] = NA | |
elmat_no_water %>% | |
sphere_shade() %>% | |
plot_3d(elmat_no_water, zscale=10,windowsize = c(1000,1000),soliddepth=-40,shadowdepth = -300, | |
background = "#ffd9e0", shadowcolor = "#8a4854", solidcolor = "#241605", solidlinecolor = NULL) | |
keepids = rgl::rgl.ids() | |
elmat_water = elmat | |
elmat_water[rayshader:::fliplr(detect_water(elmat)) == 0] = NA | |
thetaval = 45 + 30 * sinpi(1:360/180) | |
for(i in 1:360) { | |
elmat_water %>% | |
sphere_shade() %>% | |
add_water(detect_water(elmat), color = "#6fbdf2") %>% | |
plot_3d(elmat_water+heightvals[i]*10, zscale=10,windowsize = c(1000,1000),soliddepth=heightvals[i]-40, | |
shadow=FALSE, | |
background = "#ffd9e0", solidcolor = "#4b86ad", solidlinecolor = NULL) | |
elmat_all_na %>% | |
sphere_shade() %>% | |
plot_3d(elmat_all_na, zscale=10,windowsize = c(1000,1000),solid=TRUE, soliddepth = 399, | |
shadow=FALSE,background = "#ffd9e0",) | |
removeids = rgl::rgl.ids() %>% | |
filter(!(id %in% keepids$id)) | |
render_camera(theta=thetaval[i],zoom=0.8,fov=0) | |
rgl::par3d(userMatrix = rgl::par3d()$userMatrix %*% t(rgl::translationMatrix(0,100,0))) | |
render_snapshot(title_text = "River Derwent, Tasmania", title_size = 40, | |
filename=glue::glue("waterslicename{i}")) | |
rgl::rgl.pop(id = removeids$id) | |
} | |
system("ffmpeg -framerate 60 -i waterslicename%d.png -pix_fmt yuv420p waterslicenameweb.mp4") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Tyler, thanks for sharing the gist (I came across it on twitter). I'm having trouble running it, the object elmat seems to be used before it is defined, so I set elmat to be localtif and that worked to a point, but there might be another issue. Regards, Martin