Skip to content

Commit

Permalink
rayshader v0.38.3:
Browse files Browse the repository at this point in the history
-`render_highquality()` Fix water attenuation, add options to set water surface color and index of refraction
-`plot_gg()` Fix deprecation warnings with ggplot2 guides
-`render_snapshot()` adjust line size default
-Fix some tests
  • Loading branch information
tylermorganwall committed Dec 9, 2024
1 parent 3f2daf5 commit 6428f15
Show file tree
Hide file tree
Showing 58 changed files with 1,122 additions and 52 deletions.
17 changes: 8 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: rayshader
Type: Package
Title: Create Maps and Visualize Data in 2D and 3D
Version: 0.38.1
Date: 2024-05-10
Version: 0.38.3
Date: 2024-12-08
Authors@R:
person("Tyler", "Morgan-Wall", email = "[email protected]",role = c("aut", "cph", "cre"),
comment = c(ORCID = "0000-0002-3131-3814"))
Expand All @@ -26,9 +26,9 @@ Imports: doParallel,
utils,
methods,
terrainmeshr,
rayimage (>= 0.10.0),
rayvertex (>= 0.10.4),
rayrender (>= 0.32.2)
rayimage (>= 0.12.1),
rayvertex (>= 0.11.4),
rayrender (>= 0.36.5)
Suggests:
reshape2,
viridis,
Expand All @@ -47,13 +47,12 @@ Suggests:
gridExtra,
testthat (>= 3.0.0),
osmdata,
raybevel
raybevel (>= 0.1.3)
LinkingTo: Rcpp, progress, RcppArmadillo
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Encoding: UTF-8
URL: https://www.rayshader.com,
https://github.com/tylermorganwall/rayshader
BugReports: https://github.com/tylermorganwall/rayshader/issues
Config/testthat/edition: 3
Additional_repositories: https://tylermorganwall.r-universe.dev/
Remotes: tylermorganwall/raybevel
Remotes: tylermorganwall/raybevel, tylermorganwall/rayimage, tylermorganwall/rayrender
19 changes: 17 additions & 2 deletions R/convert_rgl_to_raymesh.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#'@description Converts the current RGL rayshader scene to a `ray_mesh` object (see `rayvertex` package for more information)
#'
#'@param save_shadow Default `FALSE`. If `TRUE`, this saves a plane with the shadow texture below the model.
#'@param water_attenuation Default `0`, no attenuation. Amount that light should be attenuated when traveling through water.
#'@param water_surface_color Default `TRUE`. Whether the water should have a colored surface or not. This is in contrast to
#' setting a non-zero water attenuation, where the color comes from the attenuation of light in the water itself.
#'@param water_ior Default `1`. Water index of refraction.
#'@return A `ray_mesh` object
#'@export
#'@examples
Expand All @@ -15,7 +19,8 @@
#'
#'rm_obj = convert_rgl_to_raymesh()
#'}
convert_rgl_to_raymesh = function(save_shadow = TRUE) {
convert_rgl_to_raymesh = function(save_shadow = TRUE, water_attenuation = 0,
water_surface_color = TRUE, water_ior = 1) {
if(rgl::cur3d() == 0) {
stop("No rgl window currently open.")
}
Expand All @@ -25,6 +30,7 @@ convert_rgl_to_raymesh = function(save_shadow = TRUE) {
vertex_info = get_ids_with_labels()
basic_load_mesh = function(row, texture_loc,
color = "white", alpha=1, obj = FALSE, specular = "white",shininess = 50,
attenuation = 0, water_surface = TRUE,
lit = FALSE, quads = FALSE) {
id = as.character(vertex_info$id[row])

Expand Down Expand Up @@ -82,6 +88,11 @@ convert_rgl_to_raymesh = function(save_shadow = TRUE) {
} else {
type_val = "color"
}
if(water_surface) {
specular = convert_color(specular)
} else {
specular = c(1,1,1)
}

texture_loc = ifelse(!is.na(texture_loc), texture_loc, "")
return(rayvertex::construct_mesh(indices = indices,
Expand All @@ -92,9 +103,11 @@ convert_rgl_to_raymesh = function(save_shadow = TRUE) {
norm_indices = norm_indices,
material = rayvertex::material_list(texture_location = texture_loc,
diffuse = color,
transmittance = (1-convert_color(color)) * water_attenuation,
type = type_val,
shininess = shininess,
dissolve = alpha,
ior = water_ior,
specular = specular)))
}
for(row in seq_len(nrow(vertex_info))) {
Expand Down Expand Up @@ -160,7 +173,9 @@ convert_rgl_to_raymesh = function(save_shadow = TRUE) {
texture_loc = NA,
color = vertex_info$water_color[[row]],
alpha = vertex_info$water_alpha[[row]],
specular = vertex_info$water_color[[row]])
specular = vertex_info$water_color[[row]],
water_surface = water_surface_color,
attenuation = water_attenuation)
} else if (vertex_info$tag[row] == "north_symbol") {
final_scene[[num_elems]] = basic_load_mesh(row,
lit = lit_val,
Expand Down
24 changes: 12 additions & 12 deletions R/plot_gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,15 @@ plot_gg = function(ggobj, ggobj_height = NULL, width = 3, height = 3,
if(has_guide) {
if(height_aes == "fill") {
if(is.null(ggplotobj2$guides$fill)) {
ggplotobj2 = ggplotobj2 + ggplot2::guides(fill = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000,order=i))
ggplotobj2 = ggplotobj2 + ggplot2::guides(fill = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000,order=i))
} else {
if(any(ggplotobj2$guides$fill != "none")) {
copyguide = ggplotobj2$guides$fill
copyguide$frame.linewidth = 0
copyguide$ticks = FALSE
copyguide$legend.ticks = ggplot2::element_blank()
copyguide$nbin = 1000
ggplotobj2 = ggplotobj2 +
ggplot2::guides(fill = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(fill = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
ggplotobj2$guides$fill = copyguide
}
}
Expand All @@ -438,15 +438,15 @@ plot_gg = function(ggobj, ggobj_height = NULL, width = 3, height = 3,
}
} else {
if(is.null(ggplotobj2$guides$colour)) {
ggplotobj2 = ggplotobj2 + ggplot2::guides(colour = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000,order=i))
ggplotobj2 = ggplotobj2 + ggplot2::guides(colour = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000,order=i))
} else {
if(any(ggplotobj2$guides$colour != "none")) {
copyguide = ggplotobj2$guides$colour
copyguide$frame.linewidth = 0
copyguide$ticks = FALSE
copyguide$legend.ticks = ggplot2::element_blank()
copyguide$nbin = 1000
ggplotobj2 = ggplotobj2 +
ggplot2::guides(colour = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(colour = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
ggplotobj2$guides$colour = copyguide
}
}
Expand All @@ -464,12 +464,12 @@ plot_gg = function(ggobj, ggobj_height = NULL, width = 3, height = 3,
if(height_aes == "colour") {
ggplotobj2 = ggplotobj2 +
ggplot2::scale_color_gradientn(colours = grDevices::colorRampPalette(c("white","black"))(256), na.value = "white") +
ggplot2::guides(colour = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(colour = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
}
if(height_aes == "fill") {
ggplotobj2 = ggplotobj2 +
ggplot2::scale_fill_gradientn(colours = grDevices::colorRampPalette(c("white","black"))(256), na.value = "white") +
ggplot2::guides(fill = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(fill = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
}
}
} else {
Expand All @@ -478,19 +478,19 @@ plot_gg = function(ggobj, ggobj_height = NULL, width = 3, height = 3,
if(height_aes == "fill") {
ggplotobj2 = ggplotobj2 +
ggplot2::scale_fill_gradientn(colours = grDevices::colorRampPalette(c("white","black"))(256), na.value = "white") +
ggplot2::guides(fill = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(fill = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
} else {
ggplotobj2 = ggplotobj2 +
ggplot2::scale_color_gradientn(colours = grDevices::colorRampPalette(c("white","black"))(256), na.value = "white") +
ggplot2::guides(colour = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(colour = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
}
} else {
if(height_aes == "fill") {
ggplotobj2 = ggplotobj2 + ggplot2::scale_fill_gradientn(colours = grDevices::colorRampPalette(c("white","black"))(256), na.value = "white") +
ggplot2::guides(fill = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(fill = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
} else {
ggplotobj2 = ggplotobj2 + ggplot2::scale_color_gradientn(colours = grDevices::colorRampPalette(c("white","black"))(256), na.value = "white") +
ggplot2::guides(colour = ggplot2::guide_colourbar(ticks = FALSE,nbin = 1000))
ggplot2::guides(colour = ggplot2::guide_colourbar(legend.ticks = ggplot2::element_blank(),nbin = 1000))
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions R/render_highquality.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#'@param lightsize Default `NULL`. Radius of the light(s). Automatically chosen, but can be set here by the user.
#'@param lightintensity Default `500`. Intensity of the light.
#'@param lightcolor Default `white`. The color of the light.
#'@param water_attenuation Default `0`, no attenuation. Amount that light should be attenuated when traveling through water. This
#'calculates 1-color
#'@param water_surface_color Default `TRUE`. Whether the water should have a colored surface or not. This is in contrast to
#' setting a non-zero water attenuation, where the color comes from the attenuation of light in the water itself.
#'@param water_ior Default `1`. Water index of refraction.
#'@param material Default `rayrender::diffuse()`. The material properties of the object file. Only used if `override_material = TRUE`
#'@param override_material Default `FALSE`. Whether to override the default diffuse material with that in argument `material`.
#'@param cache_scene Default `FALSE`. Whether to cache the current scene to memory so it does not have to be converted to a `raymesh` object
Expand Down Expand Up @@ -164,6 +169,7 @@ render_highquality = function(filename = NA, samples = 128,
light = TRUE,
lightdirection = 315, lightaltitude = 45, lightsize=NULL,
lightintensity = 500, lightcolor = "white", material = rayrender::diffuse(),
water_attenuation = 0, water_surface_color = TRUE, water_ior = 1,
override_material = FALSE,
cache_scene = FALSE, reset_scene_cache = FALSE,
width = NULL, height = NULL,
Expand Down Expand Up @@ -364,30 +370,39 @@ render_highquality = function(filename = NA, samples = 128,
camera_lookat = camera_interpolate[2] * camera_lookat
}
}
water_attenuation = abs(water_attenuation)
if(any(water_attenuation > 1)) {
warnings("`water_attenuation` should only be a value of 1 or less--clamping at 0-1")
water_attenuation[water_attenuation > 1] = 1
}
if(cache_scene) {
ray_scene = get("scene_cache", envir = ray_cache_scene_envir)
if(is.null(ray_scene)) {
ray_scene = convert_rgl_to_raymesh(save_shadow = FALSE)
ray_scene = convert_rgl_to_raymesh(save_shadow = FALSE,
water_attenuation = water_attenuation,
water_surface_color = water_surface_color, water_ior = water_ior)
assign("scene_cache", ray_scene, envir = ray_cache_scene_envir)
}
} else {
ray_scene = convert_rgl_to_raymesh(save_shadow = FALSE)
ray_scene = convert_rgl_to_raymesh(save_shadow = FALSE,
water_attenuation = water_attenuation,
water_surface_color = water_surface_color, water_ior = water_ior)
}

if(!override_material) {
scene = rayrender::raymesh_model(ray_scene,
x = -bbox_center[1],
y = -bbox_center[2],
z = -bbox_center[3],
override_material = FALSE,
override_material = FALSE, flip_transmittance = FALSE,
calculate_consistent_normals = calculate_consistent_normals)
} else {
scene = rayrender::raymesh_model(ray_scene,
x = -bbox_center[1],
y = -bbox_center[2],
z = -bbox_center[3],
material = material,
override_material = TRUE,
override_material = TRUE, flip_transmittance = FALSE,
calculate_consistent_normals = calculate_consistent_normals)
}
has_rayimage = TRUE
Expand Down
8 changes: 4 additions & 4 deletions R/render_snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
#'will specify the absolute angle all the labels are facing. If three angles, this will specify all three orientations
#'(relative to the x,y, and z axes) of the text labels.
#'@param text_size Default `30`. Height of the text.
#'@param point_radius Default `1`. Radius of 3D points (rendered with `render_points()`). This scales the existing
#'@param point_radius Default `0.5`. Radius of 3D points (rendered with `render_points()`). This scales the existing
#'value of size specified in `render_points()`.
#'@param line_offset Default `1e-7`. Small number indicating the offset in the scene to apply to lines if using software rendering. Increase this if your lines
#'aren't showing up, or decrease it if lines are appearing through solid objects.
#'@param thick_lines Default `TRUE`. If `software_render = TRUE`, this will render path segments as thick cylinders. Otherwise, it will
#'render the lines using a single-pixel anti-aliased line algorithm.
#'@param line_radius Default `0.5`. The radius of the thick cylinders if `thick_lines = TRUE` and `software_render = TRUE`.
#'@param line_radius Default `0.25`. The radius of the thick cylinders if `thick_lines = TRUE` and `software_render = TRUE`.
#'@param camera_location Default `NULL`. Custom position of the camera. The `FOV`, `width`, and `height` arguments will still
#'be derived from the rgl window.
#'@param camera_lookat Default `NULL`. Custom point at which the camera is directed. The `FOV`, `width`, and `height` arguments will still
Expand Down Expand Up @@ -118,8 +118,8 @@ render_snapshot = function(filename, clear=FALSE,
software_render = FALSE, camera_location = NULL, camera_lookat = c(0,0,0),
background = NULL,
text_angle = NULL, text_size = 30, text_offset = c(0,0,0),
point_radius = 1,
line_offset = 1e-7, thick_lines = TRUE, line_radius = 1,
point_radius = 0.5,
line_offset = 1e-7, thick_lines = TRUE, line_radius = 0.25,
cache_scene = FALSE, reset_scene_cache = FALSE, new_page = TRUE,
print_scene_info = FALSE, fsaa = 1,
rayvertex_lighting = FALSE, rayvertex_lights = NULL,
Expand Down
14 changes: 13 additions & 1 deletion man/convert_rgl_to_raymesh.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added man/figures/websiteorange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions man/render_highquality.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/render_snapshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6428f15

Please sign in to comment.