Created
April 13, 2020 19:06
-
-
Save ryantimpe/4a4d0b98c511d2694c0f35b80356dc44 to your computer and use it in GitHub Desktop.
flametree & brickr
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
#Adapted from Jacqueline Nolis' project | |
# https://gist.github.com/jnolis/045403c4aa5e9b28e82d1c83b3dfe9ef | |
# remotes::install_github("djnavarro/flametree") | |
library(flametree) | |
library(brickr) | |
library(ggplot2) | |
library(png) | |
library(dplyr) | |
library(rayshader) | |
library(rgl) | |
# CREATE TREE ------------------------------ | |
temp_file <- tempfile(fileext=".png") | |
dat <- flametree_grow(seed = 4, time = 5, angle = c(-20,10,30), split = 3) # data structure | |
flametree_img <- flametree_plot(tree = dat, | |
background = "#FFFFFF", | |
palette = "scico::berlin") # ggplot object | |
ggsave(temp_file, plot = flametree_img, width = 4, height = 4, dpi = 64) | |
# TURN IT INTO LEGO ------------------------ | |
readPNG(temp_file) %>% | |
image_to_mosaic(60) -> | |
tree_mosaic_object | |
#Okay, but now let's hack into brickr to make it more fun | |
tree_mosaic_object$Img_lego %>% | |
#We want Z to be the height, not Y | |
mutate(z = y) %>% | |
#Remove the white background | |
filter(Lego_color != "#F4F4F4") %>% | |
#Let's instead relate the Y values to the brightness | |
# This is a common formula for luminocity | |
# (0.21 × R) + (0.72 × G) + (0.07 × B) | |
mutate(light = 0.21*R + 0.72*G + 0.07*B) %>% | |
#Let's have 12 Y values %>% | |
mutate(y = as.numeric(cut(light, seq(0, 1, length.out = 12)))+3) %>% | |
select(x, y, z, color = Lego_name) %>% | |
mutate(piece_type = "b") -> | |
tree_hacked | |
#Make it 3D | |
tree_hacked %>% | |
#Give it some texture | |
mutate(piece_type = "w1") %>% | |
#Add some depth | |
bind_rows( | |
list( | |
tree_hacked %>% mutate(y = y-1), | |
tree_hacked %>% mutate(y = y-2)) | |
) %>% | |
bricks_from_coords() %>% | |
build_bricks(background_color = "#fcedcc") | |
# ANIMATE IT ----------------------\ | |
# Just like in rayshader | |
par3d(windowRect = c(20, 30, 800, 600), zoom=0.8) | |
U <- par3d("userMatrix") | |
thetavec = seq(0, 2*pi, length.out = 180) | |
for(ii in seq_along(thetavec)){ | |
par3d(userMatrix = rotate3d(U, thetavec[ii], 0, 0 ,1)) # Rotate aboutviewer's z axis | |
snapshot3d(paste0("rrgl/tree", stringr::str_pad(ii, width=3, pad="0"), ".png")) | |
} | |
av::av_encode_video(paste0("rrgl/", list.files("rrgl/")), | |
"flametree.mp4", framerate = 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment