Renders a 3D brain surface mesh using the rgl package. This function provides
flexible options for coloring the surface based on data values or predefined
colors, adjusting transparency, controlling lighting, setting viewpoints, and
overlaying spherical markers.
Usage
view_surface(
surfgeom,
vals = NA,
cmap = NULL,
vert_clrs = NULL,
bgcol = NULL,
alpha = 1,
add_normals = TRUE,
thresh = NULL,
irange = NULL,
specular = "black",
lit = NULL,
viewpoint = c("lateral", "medial", "ventral", "dorsal", "anterior", "posterior"),
new_window = TRUE,
offset = c(0, 0, 0),
zoom = 1,
spheres = NULL,
spheres_map_surface = NULL,
spheres_map_label = NULL,
spheres_as_vertices = FALSE,
vectors = NULL,
vector_vertices = NULL,
vector_scale = NULL,
vector_color = "red",
vector_alpha = 0.8,
vector_lwd = 1.5,
vals_vertices = NULL,
vals_smoothing = c("auto", "nearest"),
vals_smoothing_steps = 20,
label = NULL,
...
)Arguments
- surfgeom
A
SurfaceGeometryobject representing the 3D brain surface mesh to be displayed, or aSurfaceSetcontaining multiple variants.- vals
An optional numeric vector containing data values for each vertex on the surface. If provided and
vert_clrsis NULL, these values are mapped to colors usingcmapandirange.- cmap
A vector of colors (e.g., hex codes) defining the color map used when
valsis provided andvert_clrsis NULL. The default,NULL, choosessurface_palette: diverging whenirangespans zero, sequential otherwise.- vert_clrs
An optional character vector of hex color codes for each vertex. If provided, these colors directly override any coloring derived from
valsandcmap. The length should match the number of vertices insurfgeom.- bgcol
A single hex color code or a vector of hex color codes used as the base color for the surface. If
valsorvert_clrsare provided, this color is blended with the data/vertex colors. The default,NULL, shades the surface by its curvature (sulci dark, gyri light) withcurv_cols_smooth; useNAfor no base colour.- alpha
A numeric value between 0 (fully transparent) and 1 (fully opaque) controlling the overall transparency of the surface. Defaults to 1.
- add_normals
Logical. If TRUE (default), surface normals are calculated and added to the mesh, which improves the appearance of lighting effects.
- thresh
An optional numeric vector of length 2,
c(lower, upper). Values inside this band are not coloured, so the base colour shows through. With a base colour, opacity ramps up over a short band past each limit (a quarter of the larger limit's magnitude) so cluster edges fade smoothly. Defaults to NULL (no thresholding).- irange
An optional numeric vector of length 2,
c(min, max). Specifies the range ofvalsto map onto thecmap. Values outside this range will be clamped to the min/max colors. Defaults to the full range ofvals.- specular
The color of specular highlights on the surface, affecting its shininess. Can be a color name (e.g., "white") or hex code. Defaults to "black" for a matte look. Set to a brighter colour for a glossier appearance.
- lit
Logical. If
TRUE, enables lighting effects on the surface. IfFALSE, disables lighting for a flat appearance. IfNULL(default), automatically sets toTRUEfor interactive sessions andFALSEwhen knitting (whenrgl.useNULLisTRUE).- viewpoint
A character string specifying a predefined view (e.g., "lateral", "medial", "ventral", "dorsal", "anterior", "posterior"). The actual view depends on the hemisphere (
surfgeom@hemi, e.g., "left_lateral"). Alternatively, a 4x4 transformation matrix defining a custom view. Defaults to "lateral".- new_window
Logical. If TRUE (default), opens a new
rglwindow for the plot. If FALSE, attempts to plot in the currently activerglwindow (useful for updates or within Shiny apps).- offset
A numeric vector of length 3 specifying a translation offset
c(x, y, z)applied to the surface coordinates before rendering. Defaults toc(0, 0, 0).- zoom
Camera zoom passed to
rgl::view3d(). Defaults to 1; values below 1 zoom in (for example 0.7 fills more of the window), values above 1 zoom out.- spheres
An optional data frame to draw spheres at specific locations on or near the surface. Must contain columns
x,y,z(coordinates), andradius. Can optionally include acolorcolumn (hex codes or color names) for individual sphere colors (defaults to black). Alternatively, supply avertexcolumn (1-based vertex ids) and setspheres_as_vertices = TRUEto position foci by vertex.- spheres_map_surface
Optional
SurfaceGeometry,SurfaceSet, or file path used to map sphere coordinates to the nearest vertex on that surface before snapping tosurfgeom. Assumes both surfaces share the same vertex ordering (e.g., white -> inflated).- spheres_map_label
Optional surface label to use when
spheres_map_surfaceis aSurfaceSet.- spheres_as_vertices
Logical; if
TRUE, interpret thevertexcolumn ofspheresas 1-based vertex ids onsurfgeomrather than raw coordinates.- vectors
Optional matrix (n x 3) of XYZ vectors to draw as line glyphs.
- vector_vertices
Optional vertex ids matching rows of
vectorswhen they are defined on a subset of vertices.- vector_scale
Optional numeric scale factor for vectors. If
NULL, a heuristic scale based on mesh extent and vector magnitudes is used.- vector_color
Colour for the vectors (single value or vector).
- vector_alpha
Opacity for the vectors (0–1).
- vector_lwd
Numeric line width for vector glyphs.
- vals_vertices
Optional integer vector of 1-based vertex ids corresponding to
valswhenlength(vals) < n_vertices. Enables sparse data inputs.- vals_smoothing
One of
"auto"(default) or"nearest". When using sparse data,"auto"diffuses values with neighbor averaging after nearest fill;"nearest"performs nearest-neighbour fill only.- vals_smoothing_steps
Integer number of smoothing iterations applied when
vals_smoothing = "auto". Ignored otherwise.- label
Optional surface label to select when
surfgeomis aSurfaceSet. Defaults to the set'sdefault_label.- ...
Additional arguments passed directly to
rgl::shade3dfor fine-grained control over rendering (e.g.,lit,smooth).
Value
Invisibly returns the object ID(s) of the shape(s) added to the RGL scene
by rgl::shade3d. This can be useful for modifying the scene later.
Details
Coloring: Surface vertex colors are determined by the following priority:
vert_clrs: If provided, these specific hex colors are used.vals&cmap: Ifvalsis provided andvert_clrsis NULL,valsare mapped tocmapbased onirange.bgcol: If neithervert_clrsnorvalsare used for coloring,bgcolis applied uniformly. Ifbgcolis specified alongsidevert_clrsorvals, the colors are blended based on thealphaparameter.
Transparency: Overall transparency is set by alpha. Additional threshold-based
transparency can be applied using thresh when vals are provided. Vertices
with values inside the thresh band are left uncoloured.
Lighting: add_normals=TRUE is recommended for realistic lighting. The specular
parameter controls the shininess.
Viewpoint: Predefined viewpoints ("lateral", "medial", etc.) are automatically
adjusted based on the hemisphere specified in surfgeom@hemi (e.g., "lh" results
in "left_lateral"). If hemi is unknown, the current rgl view is used unless
a custom 4x4 matrix is provided.
Performance: Rendering very large surfaces or surfaces with complex coloring/transparency can be computationally intensive.
Examples
# \donttest{
surf_geom <- example_surface_geometry()
if (interactive()) {
view_surface(surf_geom, viewpoint = "lateral")
}
# }