Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
tylermorganwall / shanghai_ring_hdr.R
Last active December 4, 2019 13:46
A simple animation with a scene lit by an HDR environment image (from hrdihaven.com)
library(rayrender)
vecx = 10 * sinpi(1:360/180)
vecz = 10 * cospi(1:360/180)
anglevec = -10 * cospi(1:360/90)
for(i in seq(1,360,by=1)) {
generate_ground(material = dielectric()) %>%
add_object(sphere(material = metal())) %>%
add_object(disk(radius=3, inner_radius = 2, angle = c(anglevec[i],-i,0))) %>%
@tylermorganwall
tylermorganwall / pennsylvania_rayshader_example.R
Last active March 26, 2024 13:54
30 Day Map Challenge, Day 30: Home (Pennsylvania)
library(rayshader)
library(raster)
library(stringr)
library(sp)
library(maptools)
library(dplyr)
library(rgdal)
library(sf)
readShapePoly("eastern-states")
@tylermorganwall
tylermorganwall / ripple.R
Created November 22, 2019 12:35
Pathtraced ripple effect in R with rayshader/rayrender
library(rayshader)
wave = matrix(0,200,200)
for(t in seq(1,360,1)) {
for(i in 1:200) {
for(j in 1:200) {
wave[i,j] = 50*sinpi(sqrt((i-100)^2 + (j-100)^2)/30-t/180) * exp(-(sqrt((i-100)^2 + (j-100)^2))/100)
}
}
wave %>% height_shade() %>% plot_3d(wave, soliddepth = -50, shadowdepth = -70, theta=315,zoom=1.1)
@tylermorganwall
tylermorganwall / masterclass_precheck.R
Last active November 17, 2019 13:54
Run this script to make sure your system is ready to run rayshader
options(rgl.useNULL = FALSE)
install.packages(c("ggplot2","raster", "spatstat", "spatstat.utils","suncalc", "remotes",
"here", "sp","lubridate","rgdal", "magick", "av","xml2","rayrender"))
install.packages("whitebox", repos="http://R-Forge.R-project.org")
library(whitebox)
whitebox::wbt_init()
#macOS: xcrun issue? type "xcode-select --install" into terminal
#macOS imager.so or libx11 errors? Install X11: https://www.xquartz.org
@tylermorganwall
tylermorganwall / ortho_example.R
Created October 7, 2019 12:18
rayrender orthographic projection example
library(rayrender)
#Create base scene
basescene = xz_rect(x=555/2,y=0.1,z=555/2,555,555,
material = lambertian(color="#bababa", checkercolor = "grey10", checkerperiod = 100)) %>%
add_object(xz_rect(x=555/2,y=1000,z=555/2,343,332,
material = lambertian(color="white", lightintensity=40,implicit_sample = TRUE),
flipped=TRUE))
#Function for sphere bouncing
@tylermorganwall
tylermorganwall / gilded_weedle.R
Created September 26, 2019 13:27
A 3D render of a gilded Weedle
library(rayrender)
#Weedle 3D model: https://free3d.com/3d-model/weedle-52681.html
#Background image: https://www.tylermw.com/wp-content/uploads/2019/09/free-panorama-wallpaper-1.jpg
for(i in 1:360) {
system.time(generate_ground(material = lambertian(checkercolor = "grey50")) %>%
add_object(group_objects(obj_model("weedle.obj",y=-0.7,x=0.5,z=0.3,angle=c(0,45,0),
material = metal(color="gold",fuzz=0.1)) %>%
add_object(disk(y=-0.8,z=-0.3,material = metal(color="gold",fuzz=0.1))) %>%
@tylermorganwall
tylermorganwall / cholera_snow.R
Last active November 5, 2020 23:34
John Snow's cholera clusters, visualized in 3D with rayshader and ggplot2
#theme and ggplot derived from David Kretch, his code: https://github.com/davidkretch/london_cholera_map/blob/master/london_cholera_map.R
library(HistData)
library(ggplot2)
library(ggpointdensity)
library(rayshader)
deaths = Snow.deaths
streets = Snow.streets
@tylermorganwall
tylermorganwall / raytraced_fractal_spheres.R
Last active September 17, 2019 12:07
Raytracing fractal spheres, and showing off floating point error
library(rayrender)
indexval = 1
disklist = list()
drawCircle = function(x=0, y=0, z=0, radius = 1, depth = 4, frac = 2,
ystep = 1, layercolors = rainbow(depth)) {
materialval = lambertian(color = layercolors[depth])
disklist[[indexval]] <<- sphere(x=x, y=y, z=z, radius = radius,
material = materialval)
@tylermorganwall
tylermorganwall / distracted_bf.R
Created September 10, 2019 13:31
Create the distracted boyfriend meme with depth of field in R
library(rayfocus)
library(doParallel)
library(foreach)
#Download images
tempfile1 = tempfile()
tempfile2 = tempfile()
download.file("https://www.tylermw.com/wp-content/uploads/2019/09/memewords.png", tempfile1)
download.file("https://www.tylermw.com/wp-content/uploads/2019/09/memedepthwords.png", tempfile2)
@tylermorganwall
tylermorganwall / jumping_water.R
Created August 27, 2019 13:31
Creating the River Derwent, jumping out of the map
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) {