forked from tylermorganwall/rayshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
231 lines (182 loc) · 10.4 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
output:
github_document:
fig_width: 7.5
fig_height: 6
html_preview: false
editor_options:
chunk_output_type: console
---
rayshader<img src="man/figures/raylogosmall.png" align="right" />
=========================================================
<img src="man/figures/smallhobart.gif" ></img>
Overview
--------
**rayshader** is an open source R package for producing 2D and 3D hillshaded maps of elevation matrices using a combination of raytracing, spherical texture mapping, overlays, and ambient occlusion. It also includes the ability to export 3D models to a 3D print-ready format, and includes a post-processing depth of field effect for 3D visualizations.
Installation
------------
``` r
# To install the latest version from Github:
# install.packages("devtools")
devtools::install_github("tylermorganwall/rayshader")
```
Functions
---------
<img src="man/figures/ray_small.png"><img src="man/figures/sphere_small.png"><img src="man/figures/imhof_small.png"><img src="man/figures/amb_small.png"><img src="man/figures/lamb_small.png"><img src="man/figures/alltogether_small.png">
Rayshader has seven functions related to hillshading:
- `ray_shade` uses user specified light directions to calculate a global shadow map for an elevation matrix. By default, this also scales the light intensity at each point by the dot product of the mean ray direction and the surface normal (also implemented in function `lamb_shade`, this can be turned off by setting `lambert=FALSE`.
- `sphere_shade` maps an RGB texture to a hillshade by spherical mapping. A texture can be generated with the `create_texture` function, or loaded from an image. `sphere_shade` also includes 7 built-in palettes: "imhof1","imhof2","imhof3",imhof4","desert","bw","unicorn".
- `create_texture` programmatically creates texture maps given five colors: a highlight, a shadow, a left fill light, a right fill light, and a center color for flat areas. The user can also optionally specify the colors at the corners, but `create_texture` will interpolate those if they aren't given.
- `ambient_shade` creates an ambient occlusion shadow layer, darkening areas that have less scattered light from the atmosphere. This results in valleys being darker than flat areas and ridges.
- `lamb_shade` uses a single user specified light direction to calculate a local shadow map based on the dot product between the surface normal and the light direction for an elevation matrix.
- `add_shadow` takes two of the shadow maps above and combines them, scaling the second one (or, if the second is an RGB array, the matrix) as specified by the user.
- `add_overlay` takes a 3 or 4-layer RGB/RGBA array and overlays it on the current map. If the map includes transparency, this is taken into account when overlaying the image. Otherwise, the user can specify a single color that will be marked as completely transparent, or set the full overlay as partly transparent.
Rayshader also has three functions to detect and add water to maps:
- `detect_water` uses a flood-fill algorithm to detect bodies of water of a user-specified minimum area.
- `add_water` uses the output of `detect_water` to add a water color to the map. The user can input their own color, or pass the name of one of the pre-defined palettes from `sphere_shade` to get a matching hue.
- `render_water` adds a 3D tranparent water layer to 3D maps, after the rgl device has already been created. This can either add to a map that does not already have a water layer, or replace an existing water layer on the map.
Also included are two functions to add additional effects and information to your 3D visualizations:
- `render_depth` generates a depth of field effect for the 3D map. The user can specify the focal distance, focal length, and f-stop of the camera, as well as aperture shape and bokeh intensity. This either plots the image to the local device, or saves it to a file if given a filename.
- `render_label` adds a text label to the `x` and `y` coordinate of the map at a specified altitude `z` (in units of the matrix). The altitude can either be specified relative to the elevation at that point (the default), or absolutely.
And four functions to display and save your maps:
- `plot_map` Plots the current map. Accepts either a matrix or an array.
- `write_png` Writes the current map to disk with a user-specified filename.
- `plot_3d` Creates a 3D map, given a texture and an elevation matrix. You can customize the appearance of the map, as well as add a user-defined water level.
- `render_snapshot` Saves an image of the current 3D view to disk (if given a filename), or plots the 3D view to the current device (useful for including images in R Markdown files).
All of these functions are designed to be used with the magrittr pipe `%>%`.
Usage
-----
```{r setup, include=FALSE}
library(knitr)
library(rgl)
knit_hooks$set(rgl = hook_rgl)
knitr::opts_chunk$set(
fig.path = "man/figures/"
)
```
```{r basicmapping}
library(rayshader)
#Here, I load a map with the raster package.
loadzip = tempfile()
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)
#And convert it to a matrix:
elmat = matrix(raster::extract(localtif,raster::extent(localtif),buffer=1000),
nrow=ncol(localtif),ncol=nrow(localtif))
#We use another one of rayshader's built-in textures:
elmat %>%
sphere_shade(texture = "desert") %>%
plot_map()
#sphere_shade can shift the sun direction:
elmat %>%
sphere_shade(sunangle = 45, texture = "desert") %>%
plot_map()
#detect_water and add_water adds a water layer to the map:
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color="desert") %>%
plot_map()
raymat = ray_shade(elmat)
#And we can add a raytraced layer from that sun direction as well:
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color="desert") %>%
add_shadow(raymat) %>%
plot_map()
#And here we add an ambient occlusion shadow layer, which models
#lighting from atmospheric scattering:
ambmat = ambient_shade(elmat)
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color="desert") %>%
add_shadow(raymat) %>%
add_shadow(ambmat) %>%
plot_map()
```
Rayshader also supports 3D mapping by passing a texture map (either external or one produced by rayshader) into the `plot_3d` function.
```{r three-d}
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color="desert") %>%
add_shadow(ray_shade(elmat,zscale=3,maxsearch = 300),0.5) %>%
add_shadow(ambmat,0.5) %>%
plot_3d(elmat,zscale=10,fov=0,theta=135,zoom=0.75,phi=45, windowsize = c(1000,800))
render_snapshot()
```
```{r include=FALSE}
rgl::rgl.close()
system("rm dem_01.tif")
```
You can also easily add a water layer by setting `water = TRUE` in `plot_3d()` (and setting `waterdepth` if the water level is not 0), or by using the function `render_water()` after the 3D map has been rendered. You can customize the appearance and transparancy of the water layer via function arguments. Here's an example using bathymetric/topographic data of Monterey Bay, CA (included with rayshader):
```{r three-d-water}
montshadow = ray_shade(montereybay,zscale=50,lambert=FALSE)
montamb = ambient_shade(montereybay,zscale=50)
montereybay %>%
sphere_shade(zscale=10,texture = "imhof1") %>%
add_shadow(montshadow,0.5) %>%
add_shadow(montamb) %>%
plot_3d(montereybay,zscale=50,fov=0,theta=-45,phi=45,windowsize=c(1000,800),zoom=0.75,
water=TRUE, waterdepth = 0, wateralpha = 0.5,watercolor = "lightblue",
waterlinecolor = "white",waterlinealpha = 0.5)
render_snapshot()
```
```{r include=FALSE}
rgl::rgl.close()
```
Rayshader also has map shapes other than rectangular included `c("hex", "circle")`, and you can customize the map into any shape you want by setting the areas you do not want to display to `NA`.
```{r three-d-shapes, fig.width = 15, fig.height=6}
par(mfrow=c(1,2))
montereybay %>%
sphere_shade(zscale=10,texture = "imhof1") %>%
add_shadow(montshadow,0.5) %>%
add_shadow(montamb) %>%
plot_3d(montereybay,zscale=50,fov=0,theta=-45,phi=45,windowsize=c(1000,800),zoom=0.6,
water=TRUE, waterdepth = 0, wateralpha = 0.5,watercolor = "lightblue",
waterlinecolor = "white",waterlinealpha = 0.5,baseshape = "circle")
render_snapshot()
rgl::rgl.clear()
montereybay %>%
sphere_shade(zscale=10,texture = "imhof1") %>%
add_shadow(montshadow,0.5) %>%
add_shadow(montamb) %>%
plot_3d(montereybay,zscale=50,fov=0,theta=-45,phi=45,windowsize=c(1000,800),zoom=0.6,
water=TRUE, waterdepth = 0, wateralpha = 0.5,watercolor = "lightblue",
waterlinecolor = "white",waterlinealpha = 0.5,baseshape = "hex")
render_snapshot()
```
```{r include=FALSE}
rgl::rgl.close()
```
Adding text labels is done with the `render_label()` function, which also allows you to customize the line type, color, and size along with the font:
```{r three-d-labels}
montereybay %>%
sphere_shade(zscale=10,texture = "imhof1") %>%
add_shadow(montshadow,0.5) %>%
add_shadow(montamb) %>%
plot_3d(montereybay,zscale=50,fov=0,theta=-100,phi=30,windowsize=c(1000,800),zoom=0.6,
water=TRUE, waterdepth = 0, waterlinecolor = "white", waterlinealpha = 0.5,
wateralpha = 0.5,watercolor = "lightblue")
render_label(montereybay,x=350,y=240, z=4000,zscale=50,
text = "Moss Landing",textsize = 2,linewidth = 5)
render_label(montereybay,x=220,y=330, z=6000,zscale=50,
text = "Santa Cruz",color="darkred",textcolor = "darkred",textsize = 2,linewidth = 5)
render_label(montereybay,x=300,y=130, z=4000,zscale=50,
text = "Monterey",dashed = TRUE,textsize = 2,linewidth = 5)
render_label(montereybay,x=50,y=130, z=1000,zscale=50,
text = "Monterey Canyon",relativez=FALSE,textsize = 2,linewidth = 5)
render_snapshot()
```
```{r include=FALSE}
rgl::rgl.close()
```
You can also apply a post-processing effect to the 3D maps to render maps with depth of field with the `render_depth()` function:
```{r three-d-depth, fig.width = 10, fig.height = 8}
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color="desert") %>%
add_shadow(raymat,0.5) %>%
add_shadow(ambmat,0.5) %>%
plot_3d(elmat,zscale=10,fov=30,theta=-225,phi=25,windowsize=c(1000,800),zoom=0.3)
render_depth(focus=0.6,focallength = 200)
```