Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
Last active November 27, 2023 05:39
Show Gist options
  • Save tylermorganwall/b692495a0c6e4093bbbdd1660736f6ca to your computer and use it in GitHub Desktop.
Save tylermorganwall/b692495a0c6e4093bbbdd1660736f6ca to your computer and use it in GitHub Desktop.
3D Interactive R Tutorial
library(rayshader)
library(rayrender)
library(ggplot2)
ggdiamonds = ggplot(diamonds) +
stat_density_2d(aes(x = x, y = depth, fill = stat(nlevel)),
geom = "polygon", n = 200, bins = 50,contour = TRUE) +
facet_wrap(clarity~.) +
scale_fill_viridis_c(option = "A")
plot_gg(ggdiamonds, width = 5, height = 5, fov=30,phi=20,
raytrace = FALSE, triangulate=T, windowsize=c(1160,50,800,800))
#You need to download an environment light (see polyhaven.com for free HDRs) or set `light = TRUE`
render_highquality(light=FALSE,
environment_light="kiara_1_dawn_2k.hdr",
obj_material=rayrender::diffuse(sigma=90,color="grey20"),
samples = 256,
sample_method="sobol_blue",ground_size = 10000)
# Rayshader Animation Example
# First, set up the scene with rayshader
montereybay %>%
sphere_shade(zscale = 10, texture = "imhof1") %>%
plot_3d(montereybay, zscale = 50, fov = 70, theta = 270, phi = 30,
windowsize = c(50,50,800, 800), zoom = 0.6,
water = TRUE, waterdepth = 0, wateralpha = 0.5,
watercolor = "#233aa1",
waterlinecolor = "white", waterlinealpha = 0.5)
render_compass()
render_scalebar(limits=c(0, 80), label_unit = "km",position="N")
render_camera(theta=-45,phi=45,zoom=1)
#You need to download an environment light (see polyhaven.com for free HDRs) or set `light = TRUE`
render_highquality(clamp_value = 10, cache_filename = "mb1",
width=800, height=800,
samples = 256, camera_lookat= c(0,-50,0),
sample_method="sobol_blue",
environment_light="quarry_03_4k.hdr",
text_size = 60, text_angle = 0,
ground_material = diffuse(color="grey50",
checkercolor = "grey20",
checkerperiod = 100))
scene =
render_highquality(cache_filename = "mb1",
ground_material = diffuse(color="grey50",
checkercolor = "grey20",
checkerperiod = 100),
return_scene = T)
motion = generate_camera_motion(positions = get_saved_keyframes(),
frames=60, type = "cubic")
render_animation(scene=scene,
camera_motion = motion,
environment_light="quarry_03_4k.hdr",
samples=1,width = 800,height=800)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment