Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
tylermorganwall / screenshot_with_depth_rayshader.R
Last active February 20, 2019 15:01
Turn a screenshot into a 3D animation with depth of field
library(rayshader)
screenpng = png::readPNG("screenshot.png")
elmat = matrix(0,nrow = nrow(screenpng),ncol=ncol(screenpng))
plot_3d(screenpng,t(elmat),soliddepth = -100,windowsize=c(1000,1000),
background = "lightblue",shadowdepth = -200,
shadowcolor = "#185054",shadowwidth = 100)
@tylermorganwall
tylermorganwall / rayballer_mirror_animation
Created March 3, 2019 19:09
Rayballer sphere animation with two mirrors
library(doParallel)
numbercores = parallel::detectCores()
cl = parallel::makeCluster(numbercores)
doParallel::registerDoParallel(cl, cores = numbercores)
#generate position of spheres
zvec = seq(0+1/120,5,length.out = 120)
generate_y_pos = function(z) {
-1/1000*z^2
library(rayshader)
jpeg::readJPEG("meme_background.jpg") -> memebackground
par(mar = c(0,0,0,0))
plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
png(filename = "meme_text.png",width = dim(memebackground)[1],height=dim(memebackground)[2])
plot.new()
text(x = 0.5, y = 0.5, paste("WHEN YOU\nDISCOVER\n\n\nR CAN\nMAKE MEMES"),
cex = 11, col = "black",font=2)
@tylermorganwall
tylermorganwall / ramen.R
Last active June 17, 2019 08:47
3D ggplot ramen ratings (#tidytuesday)
library(dplyr)
library(tidyverse)
library(rayshader)
library(ggbeeswarm)
library(forcats)
library(jpeg)
library(grid)
ramen_ratings = readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-06-04/ramen_ratings.csv")
@tylermorganwall
tylermorganwall / HiRISE.R
Last active August 14, 2019 13:59
rayshader visualization of HiRISE data
library(rayshader)
library(raster)
localtif = raster("DTEPC_027860_2525_027768_2525_G01.IMG")
elmat = matrix(raster::extract(localtif, raster::extent(localtif), buffer = 1000),
nrow = ncol(localtif), ncol = nrow(localtif))
small_full_res = elmat[7500:12000, 7000:11500]
@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) {
@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 / 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 / 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 / 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))) %>%