-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggplot_cube.R
More file actions
93 lines (72 loc) · 2.72 KB
/
ggplot_cube.R
File metadata and controls
93 lines (72 loc) · 2.72 KB
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
library(ggplot2)
# devtools::install_github("coolbutuseless/threed")
library(threed)
library(dplyr)
obj <- threed::mesh3dobj$cube
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Define camera 'look at' matrix i.e. camera-to-world transform
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
camera_to_world <- threed::look_at_matrix(eye = c(-1.5, 1.75, 4), at = c(0, 0, 0))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Transform the object into camera space and do perspective projection
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
obj <- obj %>%
transform_by(invert_matrix(camera_to_world)) %>%
perspective_projection()
# ##################
# create actual plots
ggplot(obj, aes(x, y)) +
geom_point() +
theme_void() +
theme(legend.position = 'none') +
coord_equal()
# just lines
ggplot(obj, aes(x, y, group = element_id)) +
geom_polygon(fill = NA, colour = 'black', size = 0.2) +
theme_void() +
theme(legend.position = 'none') +
coord_equal()
# dotted lines in the back
ggplot(obj, aes(x, y, group = element_id)) +
geom_polygon(fill = NA, colour='black', aes(linetype = hidden, size = hidden)) +
scale_linetype_manual(values = c('TRUE' = "dotted", 'FALSE' = 'solid')) +
scale_size_manual(values = c('TRUE' = 0.2, 'FALSE' = 0.5)) +
theme_void() +
theme(legend.position = 'none') +
coord_equal()
# colored sides
ggplot(obj, aes(x, y, group = zorder)) +
geom_polygon(aes(fill = fny + fnz), colour = 'black', size = 0.2) +
theme_void() +
theme(legend.position = 'none') +
coord_equal()
# ##################
# plotting a bunny
# ############
obj <- threed::mesh3dobj$bunny %>%
transform_by(invert_matrix(camera_to_world)) %>%
perspective_projection()
ggplot(obj, aes(x, y, group = element_id)) +
geom_polygon(aes(fill = fnx + fny, colour = fnx + fny, group = zorder)) +
theme_minimal() +
theme(
legend.position = 'none',
axis.text = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
) +
coord_equal()
ggplot(obj, aes(x, y, group = element_id)) +
geom_polygon(fill = NA, colour='black', aes(linetype = hidden, size = hidden)) +
scale_linetype_manual(values = c('TRUE' = "dotted", 'FALSE' = 'solid')) +
scale_size_manual(values = c('TRUE' = 0.2, 'FALSE' = 0.5)) +
theme_void() +
theme(legend.position = 'none') +
coord_equal()
ggplot(obj, aes(x, y, group = zorder)) +
geom_polygon(aes(fill = fny + fnz), colour = 'black', size = 0.2) +
theme_void() +
theme(legend.position = 'none') +
coord_equal()
# plot a cow, a teapot, a sphere, a icosahedron ...
# more here https://github.com/coolbutuseless/threed