Day 20: rayrender

Welcome back for the 20th day of the #packagecalendar, today we will be taking a look at the rayrender package by Tyler Morgan-Wall. The rayrender package provides a raytracer written in R.

The package is not yet available from CRAN but can be downloaded with

remotes::install_github("tylermorganwall/rayrender")

The package website provides many cool examples you should look at.

For this post, I’m gonna do something simple. I’ll try to generate a scene with a single Christmas tree.

The data object used in this example is downloaded from here.

We start by generating a ground using the generate_ground(), this will create a specification and not render directly yet.

library(rayrender)
generate_ground()
## # A tibble: 1 x 32
##       x     y     z radius type  shape properties velocity checkercolor
##   <dbl> <dbl> <dbl>  <dbl> <chr> <chr> <list>     <list>   <list>      
## 1     0 -1001     0   1000 diff… sphe… <dbl [3]>  <dbl [3… <dbl [2]>   
## # … with 23 more variables: gradient_color <list>, gradient_transpose <lgl>,
## #   noise <dbl>, noisephase <dbl>, noiseintensity <dbl>, noisecolor <list>,
## #   angle <list>, image <list>, lightintensity <lgl>, flipped <lgl>, fog <lgl>,
## #   fogdensity <dbl>, implicit_sample <lgl>, sigma <dbl>,
## #   order_rotation <list>, pivot_point <list>, group_translate <list>,
## #   group_angle <list>, group_order_rotation <list>, tricolorinfo <list>,
## #   fileinfo <lgl>, scale_factor <list>, group_scale <list>

Next, we add the Christmas tree. This is done using the add_object() function along with the obj_model() function inport the .obj file. For this, I decide to have the tree be made of green metal. Notice again how we just get a data.frame back with a specification.

generate_ground() %>%
  add_object(obj_model(filename = "~/Downloads/55-ct/Christmas Tree.obj", 
                       scale_obj = 0.5,
                       material = metal(color = "green")))
## # A tibble: 2 x 32
##       x     y     z radius type  shape properties velocity checkercolor
##   <dbl> <dbl> <dbl>  <dbl> <chr> <chr> <list>     <list>   <list>      
## 1     0 -1001     0   1000 diff… sphe… <dbl [3]>  <dbl [3… <dbl [2]>   
## 2     0     0     0     NA metal obj   <dbl [5]>  <dbl [3… <lgl [1]>   
## # … with 23 more variables: gradient_color <list>, gradient_transpose <lgl>,
## #   noise <dbl>, noisephase <dbl>, noiseintensity <dbl>, noisecolor <list>,
## #   angle <list>, image <list>, lightintensity <lgl>, flipped <lgl>, fog <lgl>,
## #   fogdensity <dbl>, implicit_sample <lgl>, sigma <dbl>,
## #   order_rotation <list>, pivot_point <list>, group_translate <list>,
## #   group_angle <list>, group_order_rotation <list>, tricolorinfo <list>,
## #   fileinfo <chr>, scale_factor <list>, group_scale <list>

Lastly, we pass all of that into render_scene() where I have changed the lookfrom to zoom out a little bit.

generate_ground() %>%
  add_object(obj_model(filename = "~/Downloads/55-ct/Christmas Tree.obj", 
                       scale_obj = 0.5,
                       material = metal(color = "green"))) %>%
  render_scene(lookfrom = c(0,10,100))

And there we have it! A beautiful Christmas tree!