-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathobjects.R
4254 lines (4166 loc) · 160 KB
/
objects.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Sphere Object
#'
#' @param x Default `0`. x-coordinate of the center of the sphere.
#' @param y Default `0`. y-coordinate of the center of the sphere.
#' @param z Default `0`. z-coordinate of the center of the sphere.
#' @param radius Default `1`. Radius of the sphere.
#' @param material Default \code{\link{diffuse}}. The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#' @importFrom grDevices col2rgb
#'
#' @return Single row of a tibble describing the sphere in the scene.
#' @export
#'
#' @examples
#' #Generate a sphere in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(sphere(x = 555/2, y = 555/2, z = 555/2, radius = 100)) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, clamp_value = 5)
#' }
#'
#' #Generate a gold sphere in the cornell box
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(sphere(x = 555/2, y = 100, z = 555/2, radius = 100,
#' material = microfacet(color = "gold"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, clamp_value = 5)
#' }
sphere = function(
x = 0,
y = 0,
z = 0,
radius = 1,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "sphere",
material = material,
shape_info = ray_shape_info(
shape_properties = list(radius = radius),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Cube Object
#'
#' @param x Default `0`. x-coordinate of the center of the cube
#' @param y Default `0`. y-coordinate of the center of the cube
#' @param z Default `0`. z-coordinate of the center of the cube
#' @param width Default `1`. Cube width.
#' @param xwidth Default `1`. x-width of the cube. Overrides `width` argument for x-axis.
#' @param ywidth Default `1`. y-width of the cube. Overrides `width` argument for y-axis.
#' @param zwidth Default `1`. z-width of the cube. Overrides `width` argument for z-axis.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#' @importFrom grDevices col2rgb
#'
#' @return Single row of a tibble describing the cube in the scene.
#' @export
#'
#' @examples
#' #Generate a cube in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(cube(x = 555/2, y = 100, z = 555/2,
#' xwidth = 200, ywidth = 200, zwidth = 200, angle = c(0, 30, 0))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#' #Generate a gold cube in the cornell box
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(cube(x = 555/2, y = 100, z = 555/2,
#' xwidth = 200, ywidth = 200, zwidth = 200, angle = c(0, 30, 0),
#' material = metal(color = "gold", fuzz = 0.2))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#'
#' #Generate a rotated dielectric box in the cornell box
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(cube(x = 555/2, y = 200, z = 555/2,
#' xwidth = 200, ywidth = 100, zwidth = 200, angle = c(-30, 30, -30),
#' material = dielectric())) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
cube = function(
x = 0,
y = 0,
z = 0,
width = 1,
xwidth = 1,
ywidth = 1,
zwidth = 1,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
xwidth = ifelse(missing(xwidth), width, xwidth)
ywidth = ifelse(missing(ywidth), width, ywidth)
zwidth = ifelse(missing(zwidth), width, zwidth)
boxinfo = c(xwidth, ywidth, zwidth)
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "box",
material = material,
shape_info = ray_shape_info(
shape_properties = list(boxinfo = boxinfo),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Rectangular XY Plane Object
#'
#' @param x Default `0`. x-coordinate of the center of the rectangle.
#' @param y Default `0`. x-coordinate of the center of the rectangle.
#' @param z Default `0`. z-coordinate of the center of the rectangle.
#' @param xwidth Default `1`. x-width of the rectangle.
#' @param ywidth Default `1`. y-width of the rectangle.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @return Single row of a tibble describing the XY plane in the scene.
#' @export
#'
#' @examples
#' #Generate a purple rectangle in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(xy_rect(x = 555/2, y = 100, z = 555/2, xwidth = 200, ywidth = 200,
#' material = diffuse(color = "purple"))) %>%
#' render_scene(lookfrom = c(278, 278, -800), lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#'
#' #Generate a gold plane in the cornell box
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(xy_rect(x = 555/2, y = 100, z = 555/2,
#' xwidth = 200, ywidth = 200, angle = c(0, 30, 0),
#' material = metal(color = "gold"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
xy_rect = function(
x = 0,
y = 0,
z = 0,
xwidth = 1,
ywidth = 1,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
rectinfo = c(xwidth, ywidth)
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "xy_rect",
material = material,
shape_info = ray_shape_info(
shape_properties = list(rectinfo = rectinfo),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Rectangular YZ Plane Object
#'
#' @param x Default `0`. x-coordinate of the center of the rectangle.
#' @param y Default `0`. y-coordinate of the center of the rectangle.
#' @param z Default `0`. z-coordinate of the center of the rectangle.
#' @param ywidth Default `1`. y-width of the rectangle.
#' @param zwidth Default `1`. z-width of the rectangle.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @return Single row of a tibble describing the YZ plane in the scene.
#' @export
#'
#' @examples
#' #Generate a purple rectangle in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(yz_rect(x = 100, y = 100, z = 555/2, ywidth = 200, zwidth = 200,
#' material = diffuse(color = "purple"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#' #Generate a gold plane in the cornell box
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(yz_rect(x = 100, y = 100, z = 555/2,
#' ywidth = 200, zwidth = 200, angle = c(0, 30, 0),
#' material = metal(color = "gold"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
yz_rect = function(
x = 0,
y = 0,
z = 0,
ywidth = 1,
zwidth = 1,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
rectinfo = c(ywidth, zwidth)
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "yz_rect",
material = material,
shape_info = ray_shape_info(
shape_properties = list(rectinfo = rectinfo),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Rectangular XZ Plane Object
#'
#' @param x Default `0`. x-coordinate of the center of the rectangle.
#' @param y Default `0`. y-coordinate of the center of the rectangle.
#' @param z Default `0`. z-coordinate of the center of the rectangle.
#' @param xwidth Default `1`. x-width of the rectangle.
#' @param zwidth Default `1`. z-width of the rectangle.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @return Single row of a tibble describing the XZ plane in the scene.
#' @export
#'
#' @examples
#' #Generate a purple rectangle in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(xz_rect(x = 555/2, y = 100, z = 555/2, xwidth = 200, zwidth = 200,
#' material = diffuse(color = "purple"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#'
#' #Generate a gold plane in the cornell box
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(xz_rect(x = 555/2, y = 100, z = 555/2,
#' xwidth = 200, zwidth = 200, angle = c(0, 30, 0),
#' material = metal(color = "gold"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
xz_rect = function(
x = 0,
xwidth = 1,
z = 0,
zwidth = 1,
y = 0,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
rectinfo = c(xwidth, zwidth)
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "xz_rect",
material = material,
shape_info = ray_shape_info(
shape_properties = list(rectinfo = rectinfo),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Triangle Object
#'
#' @param v1 Default `c(1, 0, 0)`. Length-3 vector indicating the x, y, and z coordinate of the first triangle vertex.
#' @param v2 Default `c(0, 1, 0)`. Length-3 vector indicating the x, y, and z coordinate of the second triangle vertex.
#' @param v3 Default `c(-1, 0, 0)`. Length-3 vector indicating the x, y, and z coordinate of the third triangle vertex.
#' @param n1 Default `NA`. Length-3 vector indicating the normal vector associated with the first triangle vertex.
#' @param n2 Default `NA`. Length-3 vector indicating the normal vector associated with the second triangle vertex.
#' @param n3 Default `NA`. Length-3 vector indicating the normal vector associated with the third triangle vertex.
#' @param color1 Default `NA`. Length-3 vector or string indicating the color associated with the first triangle vertex.
#' If NA but other vertices specified, color inherits from material.
#' @param color2 Default `NA`. Length-3 vector or string indicating the color associated with the second triangle vertex.
#' If NA but other vertices specified, color inherits from material.
#' @param color3 Default `NA`. Length-3 vector or string indicating the color associated with the third triangle vertex.
#' If NA but other vertices specified, color inherits from material.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param reversed Default `FALSE`. Similar to the `flipped` argument, but this reverses the handedness of the
#' triangle so it will be oriented in the opposite direction.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @return Single row of a tibble describing the XZ plane in the scene.
#' @export
#'
#' @examples
#' #Generate a triangle in the Cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(triangle(v1 = c(100, 100, 100), v2 = c(555/2, 455, 455), v3 = c(455, 100, 100),
#' material = diffuse(color = "purple"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#' #Pass individual colors to each vertex:
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(triangle(v1 = c(100, 100, 100), v2 = c(555/2, 455, 455), v3 = c(455, 100, 100),
#' color1 = "green", color2 = "yellow", color3 = "red")) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
triangle = function(
v1 = c(1, 0, 0),
v2 = c(0, 1, 0),
v3 = c(-1, 0, 0),
n1 = rep(NA, 3),
n2 = rep(NA, 3),
n3 = rep(NA, 3),
color1 = rep(NA, 3),
color2 = rep(NA, 3),
color3 = rep(NA, 3),
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
reversed = FALSE,
scale = c(1, 1, 1)
) {
if (!reversed) {
vertex_vec = c(v1, v2, v3)
normal_vec = c(n1, n2, n3)
} else {
vertex_vec = c(v3, v2, v1)
normal_vec = c(n3, n2, n1)
}
vb = matrix(vertex_vec, nrow = 3, ncol = 3)
it = matrix(c(1, 2, 3), nrow = 3, ncol = 1)
if (all(!is.na(normal_vec))) {
normals = matrix(normal_vec, nrow = 3, ncol = 3)
} else {
normals = matrix(0, nrow = 3, ncol = 0)
}
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
vertex_colors = FALSE
if (all(!is.na(color1))) {
vertex_colors = TRUE
color1 = convert_color(color1)
}
if (all(!is.na(color2))) {
vertex_colors = TRUE
color2 = convert_color(color2)
}
if (all(!is.na(color3))) {
vertex_colors = TRUE
color3 = convert_color(color3)
}
if (any(is.na(color1)) && any(!is.na(c(color2, color3)))) {
color1 = unlist(material[[1]]$properties)[1:3]
}
if (any(is.na(color2)) && any(!is.na(c(color1, color3)))) {
color2 = unlist(material[[1]]$properties)[1:3]
}
if (any(is.na(color3)) && any(!is.na(c(color1, color2)))) {
color3 = unlist(material[[1]]$properties)[1:3]
}
color_matrix = matrix(
c(color1, color2, color3),
nrow = 3,
ncol = 3,
byrow = TRUE
)
mesh_obj = list(vb = vb, it = it, normals = normals)
if (vertex_colors) {
mesh_obj$material$color = color_matrix
} else {
mesh_obj$material$color = matrix(nrow = 0, ncol = 0)
}
class(mesh_obj) = "mesh3d"
mesh_obj$meshColor = ifelse(vertex_colors, "default", "vertex")
mesh3d_model(
mesh_obj,
material = material,
angle = angle,
order_rotation = order_rotation,
scale = scale,
flipped = flipped
)
}
#' Disk Object
#'
#' @param x Default `0`. x-coordinate of the center of the disk
#' @param y Default `0`. y-coordinate of the center of the disk
#' @param z Default `0`. z-coordinate of the center of the disk
#' @param radius Default `1`. Radius of the disk.
#' @param inner_radius Default `0`. Inner radius of the disk.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @importFrom grDevices col2rgb
#'
#' @return Single row of a tibble describing the disk in the scene.
#' @export
#'
#' @examples
#' #Generate a disk in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(disk(x = 555/2, y = 50, z = 555/2, radius = 150,
#' material = diffuse(color = "orange"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#' #Rotate the disk.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(disk(x = 555/2, y = 555/2, z = 555/2, radius = 150, angle = c(-45, 0, 0),
#' material = diffuse(color = "orange"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) , lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#' #Pass a value for the inner radius.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(disk(x = 555/2, y = 555/2, z = 555/2,
#' radius = 150, inner_radius = 75, angle = c(-45, 0, 0),
#' material = diffuse(color = "orange"))) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
disk = function(
x = 0,
y = 0,
z = 0,
radius = 1,
inner_radius = 0,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
info = c(inner_radius)
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "disk",
material = material,
shape_info = ray_shape_info(
shape_properties = list(radius = radius, inner_radius = inner_radius),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' `obj` File Object
#'
#' Load an obj file via a filepath. Currently only supports the diffuse texture with the `texture` argument.
#' Note: light importance sampling currently not supported for this shape.
#'
#' @param filename Filename and path to the `obj` file. Can also be a `txt` file, if it's in the correct `obj` internally.
#' @param x Default `0`. x-coordinate to offset the model.
#' @param y Default `0`. y-coordinate to offset the model.
#' @param z Default `0`. z-coordinate to offset the model.
#' @param scale_obj Default `1`. Amount to scale the model. Use this to scale the object up or down on all axes, as it is
#' more robust to numerical precision errors than the generic scale option.
#' @param load_material Default `TRUE`. Whether to load the obj file material (MTL file). If material for faces
#' aren't specified, the default material will be used (specified by the user in `material`).
#' @param load_textures Default `TRUE`. If `load_material = TRUE`, whether to load textures in the MTL file (versus
#' just using the colors specified for each material).
#' @param load_normals Default `TRUE`. Whether to load the vertex normals if they exist in the OBJ file.
#' @param calculate_consistent_normals Default `TRUE`. Whether to calculate consistent vertex normals to prevent energy
#' loss at edges.
#' @param vertex_colors Default `FALSE`. Set to `TRUE` if the OBJ file has vertex colors to apply them
#' to the model.
#' @param importance_sample_lights Default `TRUE`. Whether to importance sample lights specified in the OBJ material
#' (objects with a non-zero Ke MTL material).
#' @param subdivision_levels Default `1`. Number of Loop subdivisions to be applied to the mesh.
#' @param displacement_texture Default `""`. File path to the displacement texture.
#' This texture is used to displace the vertices of the mesh based on the texture's pixel values.
#' @param displacement_intensity Default `1`. Intensity of the displacement effect.
#' Higher values result in greater displacement.
#' @param displacement_vector Default `FALSE`. Whether to use vector displacement.
#' If `TRUE`, the displacement texture is interpreted as providing a 3D displacement vector.
#' Otherwise, the texture is interpreted as providing a scalar displacement.
#' @param recalculate_normals Default `FALSE`. Whether to recalculate vertex normals based on the
#' connecting face orientations. This can be used to compute normals for meshes lacking them or
#' to calculate new normals after a displacement map has been applied to the mesh.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @return Single row of a tibble describing the obj model in the scene.
#' @export
#'
#' @examples
#' #Load the included example R object file, by calling the r_obj() function. This
#' #returns the local file path to the `r.txt` obj file. The file extension is "txt"
#' #due to package constraints, but the file contents are identical and it does not
#' #affect the function.
#'
#' if(run_documentation()) {
#' #Load the basic 3D R logo with the included materials
#' generate_ground(material = diffuse(checkercolor = "grey50")) %>%
#' add_object(obj_model(y = 0.2, filename = rayrender::r_obj(),
#' scale_obj=3)) %>%
#' add_object(sphere(z = 20, x = 20, y = 20, radius = 10,
#' material = light(intensity = 10))) %>%
#' render_scene(parallel = TRUE, samples = 16, aperture = 0.05,
#' sample_method="sobol_blue",
#' fov = 20, lookfrom = c(0, 2, 10))
#' }
#'
#' if(run_documentation()) {
#' # Smooth a mesh by setting the number of subdivision levels
#' generate_ground(material = diffuse(checkercolor = "grey50")) %>%
#' add_object(obj_model(y = 0.2, filename = rayrender::r_obj(),
#' scale_obj=3, subdivision_levels = 3)) %>%
#' add_object(sphere(z = 20, x = 20, y = 20, radius = 10,
#' material = light(intensity = 10))) %>%
#' render_scene(parallel = TRUE, samples = 16, aperture = 0.05,
#' sample_method="sobol_blue",
#' fov = 20, lookfrom = c(0, 2, 10))
#' }
#'
#' if(run_documentation()) {
#' #Override the materials for each object
#' generate_ground(material = diffuse(checkercolor = "grey50")) %>%
#' add_object(obj_model(y = 1.4, filename = rayrender::r_obj(), load_material = FALSE,
#' scale_obj = 1.8, angle=c(10,0,0),
#' material = microfacet(color = "gold", roughness = 0.05))) %>%
#' add_object(obj_model(x = 0.9, y = 0, filename = rayrender::r_obj(), load_material = FALSE,
#' scale_obj = 1.8, angle=c(0,-20,0),
#' material = diffuse(color = "dodgerblue"))) %>%
#' add_object(obj_model(x = -0.9, y = 0, filename = rayrender::r_obj() , load_material = FALSE,
#' scale_obj = 1.8, angle=c(0,20,0),
#' material = dielectric(attenuation = c(1,0.3,1), priority = 1,
#' attenuation_intensity = 20))) %>%
#' add_object(sphere(z = 20, x = 20, y = 20, radius = 10,
#' material = light(intensity = 10))) %>%
#' render_scene(parallel = TRUE, samples = 16, aperture = 0.05,
#' sample_method="sobol_blue", lookat=c(0,0.5,0),
#' fov = 22, lookfrom = c(0, 2, 10))
#'
#' }
obj_model = function(
filename,
x = 0,
y = 0,
z = 0,
scale_obj = 1,
load_material = TRUE,
load_textures = TRUE,
load_normals = TRUE,
vertex_colors = FALSE,
calculate_consistent_normals = TRUE,
subdivision_levels = 1,
displacement_texture = "",
displacement_intensity = 1,
displacement_vector = FALSE,
recalculate_normals = FALSE,
importance_sample_lights = TRUE,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1)
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
if (!load_material) {
load_textures = FALSE
}
filename = path.expand(filename)
base_dir = function(x) {
dirname_processed = dirname(x)
if (dirname_processed == ".") {
return("")
} else {
return(dirname_processed)
}
}
displacement_texture = check_image_texture(displacement_texture)
if (subdivision_levels > getOption("rayrender_subdivision_max", 5)) {
stop(
"Default maximum subdivision level set to 5. Did you really mean to set a subdivision level ",
"of ",
subdivision_levels,
"? If so, set `options(rayrender_subdivision_max = ",
subdivision_levels,
")`."
)
}
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "obj",
material = material,
shape_info = ray_shape_info(
shape_properties = list(
scale_obj = scale_obj,
load_textures = load_textures,
load_material = load_material,
vertex_colors = vertex_colors,
importance_sample_lights = importance_sample_lights,
load_normals = load_normals,
calculate_consistent_normals = calculate_consistent_normals,
subdivision_levels = subdivision_levels,
basename = base_dir(filename),
displacement_texture = displacement_texture,
displacement_intensity = displacement_intensity,
displacement_vector = displacement_vector,
recalculate_normals = recalculate_normals
),
tricolorinfo = list(NA),
fileinfo = filename,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Cylinder Object
#'
#' @param x Default `0`. x-coordinate of the center of the cylinder
#' @param y Default `0`. y-coordinate of the center of the cylinder
#' @param z Default `0`. z-coordinate of the center of the cylinder
#' @param radius Default `1`. Radius of the cylinder.
#' @param length Default `1`. Length of the cylinder.
#' @param phi_min Default `0`. Minimum angle around the segment.
#' @param phi_max Default `360`. Maximum angle around the segment.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly.
#' @param capped Default `TRUE`. Whether to add caps to the segment. Turned off when using the `light()` material.
#' Note: emissive objects may not currently function correctly when scaled.
#'
#' @importFrom grDevices col2rgb
#'
#' @return Single row of a tibble describing the cylinder in the scene.
#' @export
#'
#' @examples
#' #Generate a cylinder in the cornell box. Add a cap to both ends.
#'
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(cylinder(x = 555/2, y = 250, z = 555/2,
#' length = 300, radius = 100, material = metal())) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#' #Rotate the cylinder
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(cylinder(x = 555/2, y = 250, z = 555/2,
#' length = 300, radius = 100, angle = c(0, 0, 45),
#' material = diffuse())) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#'
#' # Only render a subtended arc of the cylinder, flipping the normals.
#' if(run_documentation()) {
#' generate_cornell(lightintensity=3) %>%
#' add_object(cylinder(x = 555/2, y = 250, z = 555/2, capped = FALSE,
#' length = 300, radius = 100, angle = c(45, 0, 0), phi_min = 0, phi_max = 180,
#' material = diffuse(), flipped = TRUE)) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
cylinder = function(
x = 0,
y = 0,
z = 0,
radius = 1,
length = 1,
phi_min = 0,
phi_max = 360,
material = diffuse(),
angle = c(0, 0, 0),
order_rotation = c(1, 2, 3),
flipped = FALSE,
scale = c(1, 1, 1),
capped = TRUE
) {
if (length(scale) == 1) {
scale = c(scale, scale, scale)
}
stopifnot(phi_max > phi_min)
new_tibble_row(list(
x = x,
y = y,
z = z,
shape = "cylinder",
material = material,
shape_info = ray_shape_info(
shape_properties = list(
radius = radius,
length = length,
phi_min = phi_min * pi / 180,
phi_max = phi_max * pi / 180,
has_cap = capped
),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = flipped
),
transforms = ray_transform(
angle = list(angle),
order_rotation = list(order_rotation),
scale = list(scale),
group_transform = list(matrix(NA_real_))
),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0,
end_time = 1
)
))
}
#' Segment Object
#'
#' Similar to the cylinder object, but specified by start and end points.
#'
#' @param start Default `c(0, -1, 0)`. Start point of the cylinder segment, specifing `x`, `y`, `z`.
#' @param end Default `c(0, 1, 0)`. End point of the cylinder segment, specifing `x`, `y`, `z`.
#' @param radius Default `1`. Radius of the segment.
#' @param phi_min Default `0`. Minimum angle around the segment.
#' @param phi_max Default `360`. Maximum angle around the segment.
#' @param direction Default `NA`. Alternative to `start` and `end`, specify the direction (via
#' a length-3 vector) of the segment. Segment will be centered at `start`, and the length will be
#' determined by the magnitude of the direction vector.
#' @param from_center Default `TRUE`. If orientation specified via `direction`, setting this argument
#' to `FALSE` will make `start` specify the bottom of the segment, instead of the middle.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param flipped Default `FALSE`. Whether to flip the normals.
#' @param scale Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value,
#' number, the object will be scaled uniformly. Notes: this will change the stated start/end position of the segment.
#' Emissive objects may not currently function correctly when scaled.
#' @param capped Default `TRUE`. Whether to add caps to the segment. Turned off when using the `light()` material.
#'
#' @importFrom grDevices col2rgb
#'
#' @return Single row of a tibble describing the segment in the scene.
#' @export
#'
#' @examples
#' #Generate a segment in the cornell box.
#' if(run_documentation()) {
#' generate_cornell() %>%
#' add_object(segment(start = c(100, 100, 100), end = c(455, 455, 455), radius = 50)) %>%
#' render_scene(lookfrom = c(278, 278, -800) ,lookat = c(278, 278, 0), fov = 40,
#' ambient_light = FALSE, samples = 16, parallel = TRUE, clamp_value = 5)
#' }
#'
#' # Draw a line graph representing a normal distribution, but with metal:
#' xvals = seq(-3, 3, length.out = 30)
#' yvals = dnorm(xvals)
#'
#' scene_list = list()
#' for(i in 1:(length(xvals) - 1)) {
#' scene_list[[i]] = segment(start = c(555/2 + xvals[i] * 80, yvals[i] * 800, 555/2),
#' end = c(555/2 + xvals[i + 1] * 80, yvals[i + 1] * 800, 555/2),
#' radius = 10,
#' material = metal())
#' }
#' scene_segments = do.call(rbind,scene_list)