terrainr advertises itself as a package for landscape visualizationin R andUnity. This vignette focuseson the Unity half of the equation – specifically, on how tomostly-automatically import tiles into Unity. If you’re interested inthe R half of the package, refer to (the overviewvignette)[overview.html]. Note that this vignette will assume youalready have Unity installed on your computer.
In order to import terrain tiles into Unity, we will first need tohave some data worth importing! For the purposes of this vignette, we’llbe using data from the USGS National Map downloaded usingget_tiles
, but note that you can useany rasterdata for this process.
First things first, I’m going to use thegeocode_OSM
function fromtmaptools
to get the latitude and longitudeof Zion National Park, out in Utah. We’ll use this area for ourvisualization today:
zion<-tmaptools::geocode_OSM("Zion National Park")$coords
Ourzion
object now contains the x and y coordinates fora spot near the middle of Zion National Park. Let’s go ahead and turnthat into ansf
object, then useset_bbox_side_length
to add a buffer around the pointcoordinates – we’ll download data for the entire 8 kilometer squarearound the central point:
library(terrainr)library(sf)library(magrittr)zion<-data.frame(x=zion[["x"]], y=zion[["y"]])%>%st_as_sf(coords=c("x","y"), crs=4326)%>%set_bbox_side_length(8000)
And now we can go ahead and download data for our area of interest,then merge the downloaded tiles into individual rasters. For more onthis process or what these functions do, check out (the overviewvignette)[overview.html].
Fair warning – downloading this amount of data can take a bit oftime! You can optionally add a progress bar to theget_tiles
download by callinglibrary(progressr)
and thenhandlers(global = TRUE)
before running this code.
We’ve now got our data downloaded! All that’s left is to import thesetiles into Unity.
As of terrainr 0.7.0, the way to do this is via the functionmake_unity
. Assuming you have Unity installed on yourcomputer, we can go ahead and import our terrain into a brand newproject, namedzion
, through the following:
make_unity( project="zion", heightmap=merged_tiles$elevation, overlay=merged_tiles$ortho)
This will create a new folder, namedzion
, containingour Unity project. Open that project in Unity, and then open the scene(either usingCtrl+O
orFile -> Open Scene
)namedScenes/terrainr_scene.unity
. Double click on one ofthe terrain tiles and you’ll zoom out to see the entire terrain:
You can now move around your surface by right clicking on the imageand moving around with the W-A-S-D keys on your keyboard. Note that yourmovement speed starts off very slow, and then accelerates over time(especially if you press and hold the “shift” key).
And ta-da, you have a surface in Unity! You can go ahead andcustomize the scene further (I’ll usually then click on “DirectionalLight” and change “Render Mode” to “Not Important” and “Shadow Type” to“No Shadows”), fly around and across the scene, or do whatever else youwant!