Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is rendering so slow?[BUG] #320

Open
Bhavya031 opened this issue Oct 16, 2024 · 0 comments
Open

Why is rendering so slow?[BUG] #320

Bhavya031 opened this issue Oct 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Bhavya031
Copy link

Describe the bug
Why does rendering a scene take this long? Am I this stupid to figure it out? Because all the other code works like a charm, but when it comes to rendering, why does this take so long? Also, why doesn’t raycasting use the GPU? There’s an option for parallel processing, but it is only for the CPU because it doesn’t use the GPU. This render has been rendering for 5 hours and is still going. I switched to a VM with an A100 and 16 vCPUs, 40GB GPU RAM, and 60GB memory, but it’s the same story. How can I speed this up?

Session Info
Include the output of the sessionInfo() function here. Issues will be closed without providing this as a minimum.

R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] magick_2.8.5     rayshader_0.37.3 lubridate_1.9.3  forcats_1.0.0   
 [5] stringr_1.5.1    dplyr_1.1.4      purrr_1.0.2      readr_2.1.5     
 [9] tidyr_1.3.1      tibble_3.2.1     ggplot2_3.5.1    tidyverse_2.0.0 
[13] geodata_0.6-2    sf_1.0-18        elevatr_0.99.0   terra_1.7-83    

loaded via a namespace (and not attached):
 [1] rgl_1.3.1          Rcpp_1.0.13        lattice_0.20-45    png_0.1-8         
 [5] prettyunits_1.2.0  class_7.3-20       digest_0.6.37      foreach_1.5.2     
 [9] utf8_1.2.4         R6_2.5.1           rayvertex_0.11.4   e1071_1.7-16      
[13] httr_1.4.7         pillar_1.9.0       rlang_1.1.4        progress_1.2.3    
[17] curl_5.2.3         raster_3.6-30      htmlwidgets_1.6.4  munsell_0.5.1     
[21] proxy_0.4-27       compiler_4.1.2     xfun_0.48          pkgconfig_2.0.3   
[25] base64enc_0.1-3    htmltools_0.5.8.1  tidyselect_1.2.1   rayrender_0.34.3  
[29] progressr_0.14.0   codetools_0.2-18   fansi_1.0.6        crayon_1.5.3      
[33] tzdb_0.4.0         withr_3.0.1        wk_0.9.4           grid_4.1.2        
[37] jsonlite_1.8.9     gtable_0.3.5       lifecycle_1.0.4    DBI_1.2.3         
[41] pacman_0.5.1       magrittr_2.0.3     units_0.8-5        scales_1.3.0      
[45] KernSmooth_2.23-20 cli_3.6.3          stringi_1.8.4      sp_2.1-4          
[49] doParallel_1.0.17  generics_0.1.3     vctrs_0.6.5        s2_1.1.7          
[53] iterators_1.0.14   tools_4.1.2        rayimage_0.11.0    glue_1.8.0        
[57] hms_1.1.3          parallel_4.1.2     fastmap_1.2.0      slippymath_0.3.1  
[61] timechange_0.3.0   colorspace_2.1-1   classInt_0.4-10    knitr_1.48

Reproducible Example

# 1. LIBRARIES
#-------------

install.packages("pacman")
pacman::p_load(
    terra,
    elevatr,
    sf,
    geodata,
    tidyverse,
    rayshader
)

# 2. COUNTRY BORDERS
#-------------------

path <- getwd()

country_sf <- geodata::gadm(
    country = "CHE",
    level = 0,
    path = path
) |>
    sf::st_as_sf()

# 3. DOWNLOAD RIVERS
#-------------------

url <- "https://data.hydrosheds.org/file/HydroRIVERS/HydroRIVERS_v10_as_shp.zip"
destfile <- basename(url)

download.file(
    url = url,
    destfile = destfile,
    mode = "wb"
)

unzip(destfile)

# 4. LOAD RIVERS
#---------------

filename <- list.files(
    path = "HydroRIVERS_v10_as_shp",
    pattern = ".shp",
    full.names = TRUE
)

country_bbox <- sf::st_bbox(country_sf)

# xmin      ymin      xmax      ymax
#  5.956063 45.817059 10.495112 47.808483

bbox_wkt <- "POLYGON((
    5.956063 45.817059,
    5.956063 47.808483,
    10.495112 47.808483,
    10.495112 45.817059,
    5.956063 45.817059))"

country_rivers <- sf::st_read(
    filename,
    wkt_filter = bbox_wkt
) |>
    sf::st_intersection(
        country_sf
    )

plot(sf::st_geometry(country_rivers))

# 5. RIVER WIDTH
#---------------

sort(
    unique(
        country_rivers$ORD_FLOW
    )
)

crs_country <- "+proj=somerc +lat_0=46.9524055555556 +lon_0=7.43958333333333 +k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs +type=crs"

country_river_width <- country_rivers |>
    dplyr::mutate(
        width = as.numeric(
            ORD_FLOW
        ),
        width = dplyr::case_when(
            width == 3 ~ 16, 
            width == 4 ~ 14,
            width == 5 ~ 12,
            width == 6 ~ 10,
            width == 7 ~ 6,
            TRUE ~ 0
        )
    ) |>
    sf::st_as_sf() |>
    sf::st_transform(crs = crs_country)

# 6. DEM
#-------

dem <- elevatr::get_elev_raster(
    locations = country_sf,
    z = 9, clip = "locations"
)

dem_country <- dem |>
    terra::rast() |>
    terra::project(crs_country)

dem_matrix <- rayshader::raster_to_matrix(
    dem_country
)

# 7. RENDER SCENE
#----------------

dem_matrix |>
    rayshader::height_shade(
        texture = colorRampPalette(
            c(
                "#fcc69f",
                "#c67847"
            )
        )(128)
    ) |>
    rayshader::add_overlay(
        rayshader::generate_line_overlay(
            geometry = country_river_width,
            extent = dem_country,
            heightmap = dem_matrix,
            color = "#387B9C",
            linewidth = country_river_width$width,
            data_column_width = "width"
        ), alphalayer = 1
    ) |>
    rayshader::plot_3d(
        dem_matrix,
        zscale = 20,
        solid = FALSE,
        shadow = TRUE,
        shadow_darkness = 1,
        background = "white",
        windowsize = c(600, 600),
        zoom = .5,
        phi = 89,
        theta = 0
    )


rayshader::render_camera(
    zoom = .75
)

# 8. RENDER OBJECT
#-----------------

u <- "https://dl.polyhaven.org/file/ph-assets/HDRIs/hdr/4k/photo_studio_loft_hall_4k.hdr"
hdri_file <- basename(u)

download.file(
    url = u,
    destfile = hdri_file,
    mode = "wb"
)

file_name <- "switzerland-3d-elevation-rivers.png"

rayshader::render_highquality(
    filename = file_name,
    preview = TRUE,
    light = FALSE,
    environment_light = hdri_file,
    intensity_env = 1,
    interactive = FALSE,
    width = 3000,
    height = 3000,
    parallel=TRUE
)

@Bhavya031 Bhavya031 added the bug Something isn't working label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant