Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
tylermorganwall / jwst_ao.R
Created February 7, 2022 22:53
JWST rendered with ambient occlusion in R with rayrender
#JWST OBJ file located at www.tylermw.com/webb.zip
#Parse OBJ MTL file to read and write red color for highlighted component
mtl_file = readLines("jwebb.mtl")
kd_entries = (grepl("Kd",mtl_file,fixed=T))
kd_ind = which(grepl("Kd",mtl_file,fixed=T))
#Set all to white
mtl_file[kd_entries] = "Kd 1.000000 1.000000 1.000000"
@tylermorganwall
tylermorganwall / cloud_animate.R
Created February 1, 2022 04:34
Cloud animate
library(rayshader)
montereybay2 = montereybay
montereybay2[montereybay2<0] = 0
for(i in 1:720) {
montereybay2 %>%
sphere_shade() %>%
add_water(detect_water(montereybay2), color="lightblue") |>
add_shadow(cloud_shade(montereybay2,zscale=50, sun_altitude = 90, seed=10, attenuation_coef = 1, time=i,
@tylermorganwall
tylermorganwall / christmas_csg_realistic_camera.R
Created December 24, 2021 19:43
CSG Christmas Tree in R
library(rayrender)
#Generate CSG branches
branches0 = csg_cone(end=c(0,0.5,0)) %>%
csg_combine(csg_cone(start=c(0,0.25,0),end=c(0,0.75,0), radius=0.75), operation="blend", radius = 0.7)%>%
csg_scale(1.25)
branches1 = csg_cone(end=c(0,0.5,0)) %>%
csg_combine(csg_cone(start=c(0,0.25,0),end=c(0,0.75,0), radius=0.75), operation="blend", radius = 0.7)
@tylermorganwall
tylermorganwall / submarine_cable_map.R
Last active January 15, 2025 14:29
Submarine Cable Map Dataviz
library(geojsonsf)
library(sf)
library(rayrender)
#Data source: https://github.com/telegeography/www.submarinecablemap.com
cables = geojson_sf("cable-geo.json")
cablescene = list()
counter = 1
for(i in 1:length(cables$geometry)) {
@tylermorganwall
tylermorganwall / humanity_globe.R
Created August 17, 2021 14:37
3D Humanity Globe
library(rayshader)
library(rayrender)
popdata = raster::raster("gpw_v4_population_density_rev11_2020_15_min.tif")
population_mat = rayshader:::flipud(raster_to_matrix(popdata))
above1 = population_mat > 1
above5 = population_mat > 5
above10 = population_mat > 10
@tylermorganwall
tylermorganwall / star_wars_rayvertex.R
Created June 12, 2021 16:11
Star Wars Intro with Rayvertex
library(rayvertex)
text3d_mesh("A long time ago in a rayverse far,",color=c(75, 213, 238)/255) %>%
add_shape(text3d_mesh("far, away...",color=c(75, 213, 238)/255,position=c(-5,-1,0))) %>%
rasterize_scene(fov=120,lookat=c(0,-0.5,0),lookfrom=c(0,-0.5,5),
filename="start")
for(i in 1:(24*6)) {
png::readPNG(glue::glue("start.png")) %>%
png::writePNG(glue::glue("starwarsvertex{i}.png"))
@tylermorganwall
tylermorganwall / earth_halo.R
Created April 8, 2021 13:19
R: 3D Rayshader/Rayrender Dataviz 360° VR Video (Earth Halo)
#You can find the surface textures at the link below:
#http://www.shadedrelief.com/natural3/pages/extra.html
#The background is just starfield image—any will do.
#Note: this uses the latest version of rayrender (0.21.4) on github
library(rayrender)
for(i in 1:360) {
group_objects(cylinder(angle=c(90,90,0), material=diffuse(image_texture = "8081_earthmap10k.jpg",
bump_texture = "8081_earthbump10k.jpg", bump_intensity = 100),
radius=12, capped = FALSE, length = 6) %>%
@tylermorganwall
tylermorganwall / hadley_attractor.R
Last active April 9, 2021 19:06
3D Hadley Attractor
library(deSolve)
library(rayrender)
parameters = c(a = 0.2, b = 4, c = 8, d = 1)
state = c(X = 1, Y = 0, Z = 1)
Lorenz = function(t, state, parameters) {
with(as.list(c(state, parameters)), {
dX = -Y^2 - Z^2 - a*X + a*c
dY = X * Y - b * X * Z - Y + d
dZ = b * X * Y + X * Z - Z
@tylermorganwall
tylermorganwall / gist:1fd7adb32199e3d57d45768b6946f8fb
Last active November 10, 2020 22:34
ASCII rayshader + rayrender
library(rayshader)
library(crayon)
ivory <- make_style("ivory")
bgMaroon <- make_style(rgb(0.2,0.2,0.2,0), bg = TRUE)
fancy <- combine_styles(ivory, bgMaroon)
cat(fancy("This will have some fancy colors"), "\n")
@tylermorganwall
tylermorganwall / terminator_pig.R
Last active September 25, 2020 12:56
Terminator Pig, rendered in R
library(rayrender)
#CSG Pig Elements
leg_scene = list(csg_cylinder(start = c(1,-0.6,0.5), end = c(1,1.,0.5), radius=0.25+0.01),
csg_cylinder(start = c(-1,-0.6,0.5), end = c(-1,1.,0.5), radius=0.25+0.01),
csg_cylinder(start = c(1,-0.6,-0.5), end = c(1,1.,-0.5), radius=0.25+0.01),
csg_cylinder(start = c(-1,-0.6,-0.5), end = c(-1,1.,-0.5), radius=0.25+0.01))
eye_scene = list(csg_sphere(x = 2, y = 2.5, z = 0.3, radius=0.25+0.01),
csg_sphere(x = 2, y = 2.5, z = -0.3, radius=0.25+0.01+0.01),
csg_sphere(x = 2.2, y = 2.5, z = 0.3, radius=0.1+0.01),