Hacker News

Rayshader 1.0: Exit the Matrix

Eight Years of Rayshader

Be sure to read to the end for what I can guarantee is the most exciting and cinematric R package bibliography you've ever seen.

Everything you see in the below blog post was rendered in R with the rayverse.

Eight years ago, rayshader started as a small experiment in making beautiful maps in R. The first version was based on a simple question: what if we combined raytracing and cartography? I had always been interested in learning both about mapping and pathtracing, and what better way than to combine the two ideas into a single R package?

The big "aha!" moment came when I applied that method with interactive 3D terrain for the first time and took it for a spin. The surface was no longer just a shaded map: it felt tangible and real, a 3D rectangle twirling like a Little Debbie cosmic brownie on a microwave turntable.

The combination of cartographic hillshading and 3D raytraced terrain looked spectacular in a way that, at the time, mostly lived in bespoke Blender scenes or other advanced 3D modeling tools. And unlike Blender, you didn't need to learn a whole new ecosystem to visualize your data in 3D: it lived in the same place your data did (R!), which allowed for a gentler learning curve.

But as time went on and the package and userbase expanded, I had a nagging feeling: rayshader was still too hard. Not always, of course: plenty of people were using it, quite effectively. Simple use cases were fine: pass in a matrix, add a few layers, call plot_3d(), and suddenly you had a cool-looking 3D landscape. If that's all you needed: great! But if the story you were trying to tell was about data within the landscape-well, that's where the friction began to burn.

Beauty Is Table Stakes

I built rayshader for people who wanted to make data beautiful without leaving R. Beauty has a real purpose in the world: it draws people's eyes. It doesn't matter how accurate or well-constructed your map is if the reader swipes on by: in the market for human attention, beauty and form are table stakes.

Journalists understood that quickly, and were some of rayshader's earliest adopters. The Wall Street Journal's The Future of Arctic Shipping used detailed shipping, bathymetric, and ice coverage data to explain the complexities of the Northern Sea Route. The piece was recognized by the Information is Beautiful Awards, which described it as a deeply researched story built from shipping, bathymetric, and ice coverage data. It appeared only a few months after I released rayshader: talk about an introduction!

Bloomberg's 2024 Gaza rebuilding story, Gaza Reduced to 42 Million Tonnes of Rubble. What Will It Take to Rebuild?, used R, rayshader, and rayrender as part of the visual system for communicating the scale of destruction and debris in Gaza.

And in June 2022, a rayshader 3D flood visualization appeared on the front page of The Sydney Morning Herald, inset into coverage of the Lismore flooding. That last one stuck with me (and not only because a rayshader map was featured "above the fold" in a major newspaper!).

Rayshader had started as a way to make beautiful data visualizations in R, but here it was being used in deadline journalism to help explain a disaster to a general audience. The package had left the domain of "fun engaging social media posts" and entered the visual language of public-interest reporting.

But getting a reader to stop scrolling is only half the problem. Working with journalists taught me how much friction stood between having the data and making the final product. A newsroom deadline is a very good detector of bad API design. If adding buildings takes half a day of coordinate fiddling, that feature might as well not exist. If a 3D path requires a hand-rolled coordinate transform pipeline, nobody is doing that 30 minutes before a deadline. If a pathtraced render looks great but takes too long to converge, it is not going to make it to print. Especially if the editor needs a slight tweak: "Oh, sure-just give me 8 hours to re-render the animation." Ha. Nope.

Working with journalists taught me how much friction stood between having the data and making the final product. The problem wasn't so much with rayshader itself: it was how much spatial bookkeeping and manual fiddling you had to do to get data into rayshader before you could do all the fun stuff. And at the root of all that bookkeeping was the humble matrix, rayshader's original choice for representing terrain.

The Problem With Matrices

Rayshader used matrices to represent terrain because it simplified a lot of the work that went into turning a bunch of elevation values in a 3D object. But most real data does not arrive nicely packaged as a plain rectangular numeric array. It arrives as georeferenced rasters, polygons, lines, and points. It arrives cloaked in an unknown coordinate reference system. It arrives in meters, feet, yards, degrees, or German legal metres.

Choosing the matrix as the base input made it easy for me, the developer, by putting the spatial problem (mostly) on the user. Is there an... illegal german metre?

  • Convert the raster to a matrix.
  • Save the extent.
  • Transform the vector data.
  • Save the CRS.
  • Crop the linework.
  • Pass the heightmap again.
  • Pass the extent again.
  • Wait, is the extent format lat/long/lat/long or lat/lat/long/long? I never remember-whatever, try both. The first choice is (somehow!) always wrong.
  • Pass zscale again.
  • Resize the overlay.
  • Pass the CRS again.
  • Check whether the map is oriented correctly. No? Surprise! Errant minus sign. Yuck.

This was not just annoying for beginners. It annoyed me, and I wrote the damn thing: now that's a usability red flag! The new version of rayshader includes a series of changes that gets rid of all of this overhead and allows you to focus on presentation instead of the data processing.

Rayshader Has Gained Object Permanence

The big change in rayshader 1.0 is this: rayshader no longer has short-term amnesia. Rayshader now keeps track of all the spatial details from your underlying map so you don't have to worry about it any more.

This changes the user experience from:

Transform and pass in your data into an aligned matrix coordinate system, and rayshader will render it in beautiful 3D.

To:

Transform and Pass in your data into an aligned matrix coordinate system, and rayshader will render it in beautiful 3D.

The black diamond learning curve is now a gentle bunny slope. Terrain can stay a spatial raster. Road can stay a spatial line and will automatically be converted to the correct coordinate system when plotting. Buildings can be passed in as spatial polygons and will automatically be cropped to the underlying map's extent.

A rayshader scene now extracts and caches the important metadata from the input data: heightmap, extent, CRS, zscale, vertical exaggeration, and rendered-object metadata. That cache lets downstream render_*() calls do the thing users always wanted them to do: simply drop data into the scene. Most of the functions can now add to the scene simply by passing the data, plus an optional argument that specifies altitude or height, either directly or through a column in an sf object. The rest of the arguments then focus on how you want to style the data.

For example, let's load in some road and elevation data and see what the new API looks like. The code below defines the helpers and loads the data into R. There's no rayshader in it, so you have absolutely no obligation to read it :).

Transform to a UTM CRS (so everything is in meters) Pass in our lat/long bounding box Pretty straightforward... just kidding, loading geospatial data is always a pain. Nothing I can do about that! But the real question is: how complex is visualizating all this data once it's loaded?

# Run once in the R console to install rayshader from my R-universe.
if (interactive()) {
  install.packages("rayshader", repos = c("https://tylermorganwall.r-universe.dev", "https://cloud.r-project.org"))
}
library(rayrender)
library(rayshader)
# Plot in 3D
dem_crop |>
  height_shade() |>
  plot_3d(window_size = c(1500, 500))
# Data
render_roads(roads)
render_water(water)
render_buildings(buildings)
# Camera angle
render_camera(fov = 80, phi = 40, theta = 135, zoom = 0.25)
render_highquality(datetime = as.POSIXct("2025-12-21 09:00:00", tz = "America/Los_Angeles"), iso = 5)
rgl::close3d()

Not complex at all! You call the function named after the type of data... and it renders the data in the scene. Any additional arguments are just for styling the data to your liking.

And the payoff is not just less typing: it lets us get to the fun part of 3D data visualization faster. The coolest part of rayshader has always been building strikingly beautiful 3D landscapes from your data. But the third dimension raises the cognitive cost of entry: before you can play director and cinematographer, you have to build the set. When every point, path, and polygon needs the user to track the CRS and extent info themselves and transform the data appropriately, many users never get to the stage where they can experiment with lighting and composition, styling the data (costume design!), or focus on editing the overall narrative.

But also, less typing! The third dimension raises the cognitive cost of entry: before you can play director and cinematographer, you have to build the set. This release automates almost all of the behind-the-scenes work. The boring parts-CRS juggling, extent passing, heightmap repetition, matrix conversion, and other bookkeeping-stay in the package internals. The user is then free to jump to the enjoyable creative roles: the executive producer of the analysis, the production designer of the world, or the sound designer of a multi-sensory data experience.

Spatial Data: Free Yourself From the Matrix!

Rayshader now accepts many more spatial inputs for the heightmap. The plot_3d() function can now accept a heightmap as a matrix, a terra::SpatRaster, a raster object, or a raster filename. When the input is spatial, rayshader extracts and caches the extent and CRS. If zscale is not supplied, rayshader derives it from raster cell resolution. If the raster is geographic longitude/latitude, rayshader estimates the meter-per-cell scale at the raster center. The data was always there; it just had to be piped around to the right place! Now I do the plumbing for you.

Let's look at an example. The same DEM and main-road data feed both versions below. The difference is who's now doing the spatial bookkeeping (hint: it's not you).

  1. Pull the raster extent out into a separate object.
  2. Pull the raster CRS out into a separate object.
  3. Convert the spatial raster into the matrix rayshader expects.
  4. Manually specify the zscale
  5. Color the elevation matrix before constructing the terrain.
  6. Re-specify all the inputs to plot the scene in 3D.
  7. Manually transform the vector layer at the point where it enters the scene.
  8. Repeat the scene metadata again when adding a path.
  9. Pathtrace the scene with rayrender.

new workflow:

dem |>
  height_shade() |>
  plot_3d()
render_path(roads)
render_highquality()
  1. Keep the DEM as a spatial raster.
  2. Create a hillshade.
  3. Pass the hillshade into plot_3d(). Let the cached heightmap, CRS, extent, and derived zscale carry forward into the scene.
  4. Pass the lines; rayshader transforms and filters it against the cached scene.
  5. Pathtrace the scene with rayrender.

Internally, rayshader still converts the raster to something it can render. Externally, the scene remembers enough to let the render_*() functions just work.

There is now also a cleaner division between physical scale and visual exaggeration. zscale now describes the relationship between horizontal and vertical units, while vertical_exaggeration describes the stylistic exaggeration. For spatial rasters, rayshader can derive zscale from the raster itself, placing the data at its physical scale. You can then adjust vertical_exaggeration to emphasize the terrain without changing the underlying unit conversion.

And this spatial data directionality can now be reversed. spatialize_image() turns a rayshader image, matrix, RGB array, or RGBA array back into a georeferenced terra::SpatRaster. It can inherit the cached extent and CRS from the active scene, convert rayshader's linear color values to sRGB, and optionally append the source heightmap as an additional raster layer. So rayshader's output can now feed back into other GIS software, rather than just assume rayshader is the final destination itself.

library(rayshader)
# Color cells below sea level before exporting the RGB raster.
rgb_raster = montereybay_spatial |>
  sphere_shade(vertical_exaggeration = 15) |>
  add_water(montereybay_spatial
spatialize_image(include_alpha = TRUE, include_height = TRUE)
terra::writeRaster(rgb_raster, "../images/2026/rayshader-100/monterey-georeferenced.tif", overwrite = TRUE)
terra::plotRGB(rgb_raster, r = 1, g = 2, b = 3)

This release lets rayshader fit into a real spatial workflow: spatial input, then rayshader styling, followed by spatial output. So you can generate beautiful hillshades and terrains in R, and then bring them into other spatial software like QGIS or ArcGIS. Work in the GIS interface you already know, render the terrain in R, and drop the result right back into your map.

A Watershed Moment for Rayshader Water

Rayshader's water was always a bit of an afterthought: it was good for rendering an ocean with bathymetry data, but it didn't really work if the water was localized to one portion of the map. A lake halfway up a mountain shouldn't require flooding everything below it to see the water, but rayshader's old water meshing method assumed one water level for the whole map. Which was perfect if all you needed was that one large body of water: set a level and fill the scene to that height. Separate lakes, reservoirs at different elevations, and imperfect shorelines called for something more flexible.

Now water can arrive as a raster or a polygon, high in the mountains or below sea level. render_water() uses the terrain and coordinates already st

Read on Hacker News ↗ ← Back to News

Comments

No comments yet. Start the discussion.