-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerate_full_scene.R
More file actions
190 lines (184 loc) · 6.02 KB
/
Copy pathgenerate_full_scene.R
File metadata and controls
190 lines (184 loc) · 6.02 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
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
#' Build Scene (bonds + atoms)
#'
#' Builds a combined atom and bond scene from a PDB or SDF model.
#'
#' @param model Model extracted from a PDB or SDF file.
#' @param x Default `0`. X offset, applied after centering.
#' @param y Default `0`. Y offset, applied after centering.
#' @param z Default `0`. Z offset, applied after centering.
#' @param scale Default `1`. Amount to scale the interatom spacing.
#' @param center Default `TRUE`. Centers the bounding box of the model.
#' @param force_single_bonds Default `FALSE`. Whether to force all bonds to show as a single connection.
#' @param material Default `rayrender::glossy`. Optional rayrender material
#' used to initialize the mesh material when `material_vertex` is not
#' supplied. Must be either `glossy`, `diffuse`, or `dielectric`.
#' @param material_args Default `list()`. Named list of additional arguments
#' passed to `material`. Arguments supplied by raymolecule for colors and
#' textures override entries with the same names. For example, use
#' `list(gloss = 0.35, reflectance = 0.12)` with `rayrender::glossy`, or
#' `list(sigma = 0.4)` with `rayrender::diffuse`.
#' @param material_vertex Default `rayvertex::material_list()`. Mesh material.
#' `diffuse`/`ambient` colors and `ambient_intensity` are determined automatically, but all other material
#' properties can be changed.
#'
#'
#' @return Raymesh scene
#' @importFrom rayrender glossy
#' @importFrom rayvertex material_list sphere_mesh segment_mesh scene_from_list
#' @export
#'
#' @examplesIf interactive() || identical(Sys.getenv("IN_PKGDOWN"), "true")
#' molecule_model = read_sdf(get_example_molecule("caffeine"))
#'
#' # Start with a centered raster scene that combines atom spheres and bond
#' # geometry using the default atom colors.
#' molecule_model |>
#' generate_full_scene(force_single_bonds = TRUE) |>
#' render_model(
#' pathtrace = FALSE,
#' width = 800,
#' height = 800,
#' background = "grey12"
#' )
#'
#' # This version changes the scale, keeps the model centered, and uses a
#' # diffuse mesh material for both atoms and bonds before pathtracing.
#' molecule_model |>
#' generate_full_scene(
#' x = 0,
#' y = 0,
#' z = 0,
#' scale = 0.75,
#' center = TRUE,
#' force_single_bonds = TRUE,
#' material = rayrender::diffuse,
#' material_args = list(sigma = 0.3)
#' ) |>
#' render_model(pathtrace = TRUE, width = 800, height = 800, samples = 32)
#'
#' # A toon material changes the raster shader and pass FSAA to render_model()
#' # so rayvertex adds anti-aliasing.
#' shiny_toon_material = rayvertex::material_list(
#' type = "toon_phong",
#' toon_levels = 3,
#' toon_outline_width = 10,
#' toon_outline_color = "white"
#' )
#' morphine_model = read_sdf(get_example_molecule("morphine"))
#' morphine_model |>
#' generate_full_scene(
#' material_vertex = shiny_toon_material
#' ) |>
#' render_model(
#' fsaa = 2,
#' pathtrace = FALSE,
#' width = 800,
#' height = 800,
#' background = "grey50"
#' )
generate_full_scene = function(
model,
x = 0,
y = 0,
z = 0,
scale = 1,
center = TRUE,
force_single_bonds = FALSE,
material = rayrender::glossy,
material_args = list(),
material_vertex = material_list(type = "phong")
) {
material_vertex = resolve_mesh_material(
material = material,
material_vertex = material_vertex,
use_material = !missing(material) && missing(material_vertex),
material_args = material_args,
package_args = list(color = "white", image_texture = "")
)
atoms = model$atoms
atoms$x = atoms$x * scale
atoms$y = atoms$y * scale
atoms$z = atoms$z * scale
if (center) {
atoms$x = atoms$x - mean(range(atoms$x))
atoms$y = atoms$y - mean(range(atoms$y))
atoms$z = atoms$z - mean(range(atoms$z))
}
atoms$x = atoms$x + x
atoms$y = atoms$y + y
atoms$z = atoms$z + z
scene = list()
cntr = 1
for (i in seq_len(nrow(atoms))) {
if (atoms$type[i] != "C") {
atomcol = PeriodicTable::atomColor(atoms$type[i])
} else {
atomcol = "grey5"
}
material_atom = material_vertex
material_atom$diffuse = convert_color(atomcol)
material_atom$ambient = convert_color(atomcol)
material_atom$ambient_intensity = 0.3
material_atom = update_rayrender_material_package_args(
material_atom,
package_args = list(color = atomcol, image_texture = "")
)
atomsize = (PeriodicTable::mass(atoms$type[i]) / 14)^(1 / 3)
scene[[cntr]] = sphere_mesh(
position = c(atoms$x[i], atoms$y[i], atoms$z[i]),
radius = atomsize / 2,
low_poly = FALSE,
material = material_atom
)
cntr = cntr + 1
}
get_segment_offsets = function(start, end, bond_order) {
zero_offset = c(0, 0, 0)
if (bond_order == 1 || force_single_bonds) {
return(list(zero_offset))
}
dir = end - start
onb = onb_from_w(dir)
if (bond_order == 2) {
offset = onb[3, ] / 8
return(list(offset, -offset))
}
if (bond_order == 3) {
offset = onb[3, ] / 4
return(list(offset, -offset, zero_offset))
}
return(NULL)
}
material_bond = material_vertex
material_bond$diffuse = convert_color("grey33")
material_bond$ambient = convert_color("grey33")
material_bond$ambient_intensity = 0.3
material_bond = update_rayrender_material_package_args(
material_bond,
package_args = list(color = "grey33", image_texture = "")
)
bonds = model$bonds
for (i in seq_len(nrow(bonds))) {
bond1 = atoms$index == bonds[i, 1]
bond2 = atoms$index == bonds[i, 2]
if (!any(bond1) || !any(bond2)) {
next
}
start = as.numeric(atoms[bond1, c("x", "y", "z")])
end = as.numeric(atoms[bond2, c("x", "y", "z")])
segment_offsets = get_segment_offsets(start, end, bonds[i, 3])
if (is.null(segment_offsets)) {
next
}
for (offset in segment_offsets) {
scene[[cntr]] = segment_mesh(
start = start + offset,
end = end + offset,
radius = 1 / 10,
material = material_bond
)
cntr = cntr + 1
}
}
return(rayvertex::scene_from_list(scene))
}