-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathbuild-bricks-rgl.R
526 lines (425 loc) · 19.3 KB
/
build-bricks-rgl.R
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#' Build 3D brick model with 'rgl'
#'
#' Render the output of any of the \code{bricks_from_*} functions as a 3D model. Opens an 'rgl' window.
#'
#' @param brick_list List output from a \code{bricks_from_*} function. Contains an element \code{Img_lego}.
#' @param background_color Default 'white'. Color of the background.
#' @param rgl_lit Default 'TRUE'. Include RGL lighting features in rendering.
#' @param outline_bricks Default 'FALSE'. Include black outlines around brick edges.
#' Set to 'TRUE' and rgl_lit='FALSE' for cartoon-looking bricks.
#' @param trans_alpha Default 0.5. Alpha level for transparent bricks.
#' @param view_levels Numeric array of Levels/z values to display. Leave as 'NULL' to include all.
#' @examples
#' #This is a brick
#'brick <- data.frame(
#' Level="A",
#' X1 = rep(3,4), #The number 3 is the brickrID for 'bright red'
#' X2 = rep(3,4)
#')
#'
#'#Convert the dataframe to a list object that can be rendered
#'brick_object <- brick %>%
#' bricks_from_table()
#'
#'#Render it
#'brick_object %>%
#' build_bricks()
#'
#' rgl::clear3d()
#'
#'#Combine the option rgl_lit=FALSE & outline_bricks=TRUE
#'# This makes the rendering look like a drawing
#' brick_object %>%
#' build_bricks(outline_bricks = TRUE, rgl_lit = FALSE,
#' background_color = "#99e7ff")
#'rgl::clear3d()
#'
#' @return 3D brick model rendered in the 'rgl' package.
#' @family 3D Models
#' @export
#'
build_bricks <- function(brick_list,
background_color = "white", rgl_lit = TRUE,
outline_bricks = FALSE,
trans_alpha = 0.5,
view_levels = NULL){
#Get previous data
in_list <- brick_list
img_lego <- in_list$Img_lego %>%
tidyr::drop_na() %>%
dplyr::select(-dplyr::contains("lum")) %>%
dplyr::left_join(lego_colors %>%
dplyr::select(Lego_name = Color, lum),
by = c("Lego_name"))
img_bricks <- in_list$Img_bricks %>%
tidyr::drop_na()%>%
dplyr::left_join(lego_colors %>%
dplyr::select(Lego_name = Color, Trans_lego, lum),
by = c("Lego_name"))
if(is.null(view_levels)){
view_levels <- unique(img_lego$Level)
}
#SET PARAMETERS ----
# For use inside brick drawing functions below
nudge = 0.01 #Space between bricks
scale = 1 #Reduce to unit size
height_scale = 9.6/7.8
color_outline = "black"
color_outline_trans = "white"
contrast_knobs = TRUE
contrast_lum = 0.2
knob_diameter = 5/8
brick_diameter = 96/100
outline_bricks = outline_bricks
suppress_knobs = TRUE #this won't draw 'hidden' knobs
pieces_knobbed = c("B", "P")
pieces_knobbed = c(pieces_knobbed, tolower(pieces_knobbed))
#For now, use the current collect_bricks output.
#This was designed for rayshader, and I don't want to drop rayshader just yet.
#Bricks & pieces without knobs ----
rgl_bricks_base <- list(
# x & y are the CENTERS of bricks. rgl scales shapes from center
x = img_bricks$xmin + 0.5 + (img_bricks$xmax - img_bricks$xmin)/2 ,
y = img_bricks$ymin + 0.5 + (img_bricks$ymax - img_bricks$ymin)/2 ,
z = img_bricks$Level + (img_bricks$mid_level/3),
color = img_bricks$Lego_color,
trans = img_bricks$Trans_lego,
lum = img_bricks$lum,
#Grab brick size from brick type id
width = as.numeric(img_bricks$brick_width),
length = as.numeric(img_bricks$brick_height),
piece = tolower(img_bricks$piece_type)
) %>%
purrr::transpose()
rgl_bricks_base_list <- rgl_bricks_base %>%
purrr::map(function(this_brick){
if(!(this_brick$piece %in% pieces_knobbed)){return(NULL)}
this_height = switch(this_brick$piece,
b = 1,
p = 1/3
)
z_drop = switch(this_brick$piece,
b = 0,
p = -1/3
)
#Solid brick ----
brk_fill <- rgl::cube3d(col = this_brick$color,
alpha = if(this_brick$trans){trans_alpha}else{1})
brk_fill$vb[4,] <- brk_fill$vb[4,]/scale*2 + nudge
brk_fill2 <- brk_fill %>%
rgl::scale3d(this_brick$width, this_brick$length, height_scale*this_height) %>% #Increase height
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale + z_drop*height_scale)
if(outline_bricks){
# Brick Outline ----
brk_out <- rgl::cube3d(col = if(this_brick$trans){colorspace::lighten(this_brick$color)}
else{color_outline})
brk_out$vb[4,] <- brk_out$vb[4,]/scale*2 + nudge
brk_out$material$lwd <- 1
brk_out$material$front <- 'line'
brk_out$material$back <- 'line'
brk_out2 <- brk_out %>%
rgl::scale3d(this_brick$width, this_brick$length, height_scale*this_height) %>% #Increase height
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale + z_drop*height_scale)
out_list <- list(brk_fill2, brk_out2)
} else {
brk_out2 <- NULL
out_list <- list(brk_fill2, brk_out2)
}
#Save ----
return(out_list)
}) %>%
purrr::discard(is.null) %>%
purrr::transpose()
rgl_bricks_wedge_list <- rgl_bricks_base %>%
purrr::map(function(this_brick){
if(!(this_brick$piece %in% paste0("w", 1:4))){return(NULL)}
#Solid brick ----
brk_fill <- rgl::cube3d(col = this_brick$color,
alpha = if(this_brick$trans){trans_alpha}else{1})
#Turn it into a wedge
w_lhs <- switch(this_brick$piece,
w1 = c(7, 8),
w2 = c(6, 8),
w3 = c(5, 6),
w4 = c(5, 7))
w_rhs <- switch(this_brick$piece,
w1 = c(3, 4),
w2 = c(2, 4),
w3 = c(1, 2),
w4 = c(1, 3))
w_ratio = 1.2/4
brk_fill$vb[, w_lhs] <- brk_fill$vb[, w_rhs] * (1-w_ratio) + brk_fill$vb[, w_lhs] * w_ratio
brk_fill$vb[4,] <- brk_fill$vb[4,]/scale*2 + nudge
brk_fill2 <- brk_fill %>%
rgl::scale3d(this_brick$width, this_brick$length, height_scale * 2/3) %>% #Increase height
rgl::translate3d(this_brick$x, this_brick$y,
this_brick$z * height_scale - height_scale*(1-2/3)/2)
if(outline_bricks){
# Brick Outline ----
brk_out <- rgl::cube3d(col = if(this_brick$trans){colorspace::lighten(this_brick$color)}
else{color_outline})
#Turn it into a wedge
brk_out$vb[, w_lhs] <- brk_out$vb[, w_rhs] * (1-w_ratio) + brk_out$vb[, w_lhs] * w_ratio
brk_out$vb[4,] <- brk_out$vb[4,]/scale*2 + nudge
brk_out$material$lwd <- 1
brk_out$material$front <- 'line'
brk_out$material$back <- 'line'
brk_out2 <- brk_out %>%
rgl::scale3d(this_brick$width, this_brick$length, height_scale * 2/3) %>% #Increase height
rgl::translate3d(this_brick$x, this_brick$y,
this_brick$z * height_scale - height_scale*(1-2/3)/2)
out_list <- list(brk_fill2, brk_out2)
} else {
brk_out2 <- NULL
out_list <- list(brk_fill2, brk_out2)
}
#Save ----
return(out_list)
}) %>%
purrr::discard(is.null) %>%
purrr::transpose()
rgl_bricks_cyln_list <- rgl_bricks_base %>%
purrr::map(function(this_brick){
if(!(this_brick$piece %in% c("c", paste0("c", 1:2)))){return(NULL)}
this_piece = tolower(this_brick$piece)
bottom_diameter = 12/16
bottom_gap = height_scale/6
cyl_scale = 2
#Solid brick ----
# Base
# Between c/c1 (cylinder) and c2 (cone), only base is different
cyl_base_diameter = switch(
this_piece,
c = brick_diameter,
c1 = brick_diameter,
c2 = c(brick_diameter, (brick_diameter+knob_diameter)/2, knob_diameter)
)
cyl_base <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/cyl_scale, ncol=2, byrow = TRUE),
sides = 32,
radius = cyl_base_diameter,
closed = -2)
cyl_base$material$color <- this_brick$color
cyl_base$material$alpha <- if(this_brick$trans){trans_alpha}else{1}
cyl_base$vb[4,] <- cyl_base$vb[4,]/scale*2 + nudge
cyl_base2 <- cyl_base %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, height_scale - bottom_gap) %>%
rgl::translate3d(0.25, -0.25, -height_scale + bottom_gap*1.5) %>%
rgl::translate3d(this_brick$x, this_brick$y,
this_brick$z * height_scale)
# Knob
cyl_knob <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/cyl_scale, ncol=2, byrow = TRUE),
sides = 32,
radius = knob_diameter,
closed = -2)
cyl_knob$material$color <- this_brick$color
cyl_knob$material$alpha <- if(this_brick$trans){trans_alpha}else{1}
cyl_knob$vb[4,] <- cyl_knob$vb[4,]/scale*2 + nudge
cyl_knob2 <- cyl_knob %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, height_scale) %>%
rgl::translate3d(0.25, -0.25, -height_scale + bottom_gap - 0.02 + (1.7/9.6)/2 - 0.02) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale)
# Bottom
cyl_bttm <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/cyl_scale, ncol=2, byrow = TRUE),
sides = 32,
radius = bottom_diameter,
closed = -2)
cyl_bttm$material$color <- this_brick$color
cyl_bttm$material$alpha <- if(this_brick$trans){trans_alpha}else{1}
cyl_bttm$vb[4,] <- cyl_bttm$vb[4,]/scale*2 + nudge
cyl_bttm2 <- cyl_bttm %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, bottom_gap) %>%
rgl::translate3d(0.25, -0.25, -bottom_gap*3.5) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale)
#Outlines ----
if(outline_bricks){
cyl_ot_diameter = switch(
this_piece,
c = brick_diameter,
c1 = brick_diameter,
c2 = knob_diameter
)
# These are 2-dimensional cylinders
#Base, top ----
cyl_base_ot_prep <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/2, ncol=2, byrow = TRUE),
sides = 32,
radius = cyl_ot_diameter*1.015) #Conditional on cone or cylinder
cyl_base_ot_prep$vb[4,] <- cyl_base_ot_prep$vb[4,]/scale*2 + nudge
cyl_base_ot_prep$material$color <- if(this_brick$trans){colorspace::lighten(this_brick$color)}
else{color_outline}
cyl_base_ot <- cyl_base_ot_prep %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, 0.01) %>% #Make the height super short
rgl::translate3d(0.25, -0.25, height_scale/2 - 0.02) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale)
#Base, bottom ---
cyl_bttm_ot_prep <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/2, ncol=2, byrow = TRUE),
sides = 32,
radius = brick_diameter*1.015)
cyl_bttm_ot_prep$vb[4,] <- cyl_bttm_ot_prep$vb[4,]/scale*2 + nudge
cyl_bttm_ot_prep$material$color <- if(this_brick$trans){colorspace::lighten(this_brick$color)}
else{color_outline}
cyl_bttm_ot <- cyl_bttm_ot_prep %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, 0.01) %>% #Make the height super short
rgl::translate3d(0.25, -0.25, height_scale/2) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale) %>%
rgl::translate3d(0, 0, -1*(height_scale - bottom_gap))
#Knob ----
cyl_knob_ot_prep <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/2, ncol=2, byrow = TRUE),
sides = 32,
radius = knob_diameter*1.015)
cyl_knob_ot_prep$vb[4,] <- cyl_knob_ot_prep$vb[4,]/scale*2 + nudge
cyl_knob_ot_prep$material$color <- if(this_brick$trans){colorspace::lighten(this_brick$color)}
else{color_outline}
cyl_knob_ot <- cyl_knob_ot_prep %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, 0.01) %>% #Make the height super short
rgl::translate3d(0.25, -0.25, height_scale/2) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale) %>%
rgl::translate3d(0, 0, 0.22)
} else {
cyl_base_ot <- NULL
cyl_bttm_ot <- NULL
cyl_knob_ot <- NULL
}
out_list <- list(cyl_base2, cyl_bttm2, cyl_knob2,
cyl_base_ot, cyl_bttm_ot, cyl_knob_ot)
#Save ----
return(out_list)
}) %>%
purrr::discard(is.null) %>%
purrr::transpose()
#Bricks knobs ----
if(suppress_knobs){
img_lego <- img_lego %>%
dplyr::mutate(temp_level = Level*3 + mid_level,
piece_type = tolower(piece_type)) %>%
dplyr::group_by(x, y) %>%
dplyr::filter(
#Bricks: Keep knobs when next level is not right above it, 3 1-height units
((dplyr::lead(temp_level, order_by = temp_level) > (temp_level + 3)) & piece_type == "b") |
#Plates: Keep knobs when next level is not right above it, 1 1-height unit
((dplyr::lead(temp_level, order_by = temp_level) > (temp_level + 1)) & piece_type %in% c("p", "s")) |
#Or next level is na
is.na(dplyr::lead(temp_level, order_by = temp_level)) |
# Or this or next level is transparent
dplyr::lead(Trans_lego, order_by = temp_level) | Trans_lego
) %>%
dplyr::ungroup()
}
rgl_bricks_knobs <- list(
x = img_lego$x,
y = img_lego$y,
z = img_lego$Level + (img_lego$mid_level/3),
color = img_lego$Lego_color,
trans = img_lego$Trans_lego,
lum = img_lego$lum,
piece = tolower(img_lego$piece_type)
) %>%
purrr::transpose()
rgl_bricks_knobs_list <- rgl_bricks_knobs %>%
purrr::map(function(this_brick){
if(!(this_brick$piece %in% pieces_knobbed)){return(NULL)}
adj_height = switch(this_brick$piece,
b = 1,
p = 1/3
)
z_drop = switch(this_brick$piece,
b = 0,
p = 1/3
)
cap_drop = switch(this_brick$piece,
b = 0,
p = -2/3
)
# Brick knob ----
brk_knob <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/2, ncol=2, byrow = TRUE),
sides = 32,
radius = knob_diameter,
closed = -2)
brk_knob$vb[4,] <- brk_knob$vb[4,]/scale*2 + nudge
#Knob side color
if(contrast_knobs & !this_brick$trans){
if(this_brick$lum <= contrast_lum){
brk_knob$material$color <- colorspace::lighten(this_brick$color)
} else {
brk_knob$material$color <- colorspace::darken(this_brick$color)
}
} else{
brk_knob$material$color <- this_brick$color
}
brk_knob$material$alpha <- if(this_brick$trans){trans_alpha}else{1}
this_brick$x <- this_brick$x + 0.5
this_brick$y <- this_brick$y + 0.5
brk_knob2 <- brk_knob %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, (height_scale*adj_height) + 1.7/9.6) %>%
rgl::translate3d(0.25, -0.25, -height_scale-0.02) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale + z_drop*height_scale)
# Brick knob outlines ----
# These are 2-dimensional cylinders
if(outline_bricks){
brk_knob_ot_prep <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/2, ncol=2, byrow = TRUE),
sides = 32,
radius = knob_diameter*1.015)
brk_knob_ot_prep$vb[4,] <- brk_knob_ot_prep$vb[4,]/scale*2 + nudge
brk_knob_ot_prep$material$color <- if(this_brick$trans){colorspace::lighten(this_brick$color)}
else{color_outline}
#Base of the knob
brk_knob_ot <- brk_knob_ot_prep %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, 0.01) %>% #Make the height super short
rgl::translate3d(0.25, -0.25, height_scale/2) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale + cap_drop*height_scale)
#Top of the knob
brk_knob_ot2 <- brk_knob_ot %>%
rgl::translate3d(0, 0, 0.22)
out_list <- list(brk_knob2, brk_knob_ot, brk_knob_ot2)
} else {
brk_knob_ot <- NULL
brk_knob_ot2 <- NULL
out_list <- list(brk_knob2, brk_knob_ot, brk_knob_ot2)
}
#Brick knob cap ----
# This uses the bricks color if the knob has contrasting sides
# Only use if the knob side is contrasted
if(contrast_knobs){
brk_knob_top <- rgl::cylinder3d(matrix(c(rep(1, 3), rep(1, 3))/2, ncol=2, byrow = TRUE),
sides = 32,
radius = knob_diameter*.99,
closed = -2)
brk_knob_top$vb[4,] <- brk_knob_top$vb[4,]/scale*2 + nudge
brk_knob_top$material$color <- this_brick$color
brk_knob_top$material$alpha <- if(this_brick$trans){trans_alpha}else{1}
#A 2-dimensional filled circle on the top the knob
brk_knob_top2 <- brk_knob_top %>%
rgl::rotate3d(pi/2, 0, 1, 0) %>%
rgl::scale3d(1, 1, 0.01) %>%
rgl::translate3d(0.25, -0.25, height_scale/2+0.22+0.01) %>%
rgl::translate3d(this_brick$x, this_brick$y, this_brick$z * height_scale + cap_drop*height_scale)
out_list[[4]] <- brk_knob_top2
} else{
brk_knob_top2 <- NULL
out_list[[4]] <- brk_knob_top2
}
#Save ----
return(out_list)
}) %>%
purrr::discard(is.null) %>%
purrr::transpose()
#Draw
shapelist <- c( purrr::flatten(rgl_bricks_base_list)
, purrr::flatten(rgl_bricks_wedge_list)
, purrr::flatten(rgl_bricks_cyln_list)
, purrr::flatten(rgl_bricks_knobs_list)
)
shapelist[sapply(shapelist, is.null)] <- NULL
shapelist %>%
rgl::shapelist3d(lit=rgl_lit, shininess = 100, specular = "black")
rgl::bg3d(color = background_color)
rgl::rgl.viewpoint(userMatrix = rgl::rotate3d(rgl::par3d("userMatrix"), 0, 0, 0 ,1) ,
fov=0) #All bricks, regardless of Z, are perceived as same size
}