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

render_highquality will randomly produce colorless results, despite the rgl window 3d object being colored. #308

Open
TearsRfuel opened this issue Feb 24, 2024 · 14 comments
Labels
bug Something isn't working

Comments

@TearsRfuel
Copy link

Describe the bug
When rendering with render_highquality, the render result will be black and white (colorless), despite being colored in the rgl window. Very randomly and rarely it will be colored.

The RGL window looks like this:
preview

the render result looks like this:
test_plot

Session Info

> sessionInfo()
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] rayvertex_0.10.4 colorspace_2.1-0 MetBrewer_0.2.0  rayshader_0.37.3 stars_0.6-4      abind_1.4-5      lubridate_1.9.3  forcats_1.0.0    stringr_1.5.1    dplyr_1.1.4      purrr_1.0.2     
[12] readr_2.1.5      tidyr_1.3.1      tibble_3.2.1     ggplot2_3.4.4    tidyverse_2.0.0  tigris_2.1       sf_1.0-15       

loaded via a namespace (and not attached):
 [1] gtable_0.3.4       xfun_0.42          htmlwidgets_1.6.4  tzdb_0.4.0         vctrs_0.6.5        tools_4.3.2        generics_0.1.3     curl_5.2.0         parallel_4.3.2     rgl_1.2.8         
[11] proxy_0.4-27       fansi_1.0.6        pkgconfig_2.0.3    KernSmooth_2.23-22 uuid_1.2-0         lifecycle_1.0.4    farver_2.1.1       compiler_4.3.2     progress_1.2.3     munsell_0.5.0     
[21] codetools_0.2-19   htmltools_0.5.7    class_7.3-22       rayimage_0.10.0    pillar_1.9.0       crayon_1.5.2       classInt_0.4-10    iterators_1.0.14   foreach_1.5.2      tidyselect_1.2.0  
[31] digest_0.6.34      stringi_1.8.3      fastmap_1.1.1      grid_4.3.2         cli_3.6.2          magrittr_2.0.3     base64enc_0.1-3    utf8_1.2.4         e1071_1.7-14       withr_3.0.0       
[41] prettyunits_1.2.0  scales_1.3.0       rappdirs_0.3.3     timechange_0.3.0   rayrender_0.32.2   httr_1.4.7         png_0.1-8          hms_1.1.3          knitr_1.45         doParallel_1.0.17 
[51] rlang_1.1.3        Rcpp_1.0.12        glue_1.7.0         DBI_1.2.1          rstudioapi_0.15.0  jsonlite_1.8.8     R6_2.5.1           units_0.8-5       






here is what my code looks like:

# load libraries ----------------------------------------------------------
install.packages("")
library(sf)
library(tigris)
library(tidyverse)
library(stars)
library(rayshader)
library(MetBrewer)
library(colorspace)
library(rayvertex)

# read in data ------------------------------------------------------------

data <- st_read("data/kontur_population_US_20231101.gpkg")


# load states -------------------------------------------------------------

st <- states()

florida <- st |> 
  filter(NAME == "Florida") |> 
  st_transform(crs = st_crs(data))

# check with map ----------------------------------------------------------

florida |> 
  ggplot()+
  geom_sf()

# filter USA data for only Florida via INTERSECTION  -----------------------

st_florida <- st_intersection(data, florida)

# define aspect ratio of bounding box -------------------------------------

bb <- st_bbox(st_florida)

bottom_left <- st_point(c(bb[["xmin"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

bottom_right <- st_point(c(bb[["xmax"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

top_left <- st_point(c(bb[["xmin"]], bb[["ymax"]])) |> 
  st_sfc(crs = st_crs(data))

# check bounding points by plotting ---------------------------------------


florida |> 
  ggplot()+
  geom_sf()+
  geom_sf(data = bottom_left, color = "blue")+
  geom_sf(data = bottom_right, color = "red")+
  geom_sf(data = top_left, color = "green")


width <- st_distance(bottom_left, bottom_right)
height <- st_distance(bottom_left, top_left)


# handle conditions of width or height being the longer side --------------


if(width > height){
  w_ratio <- 1
  h_ratio <- height / width
} else{
  h_ratio <- 1
  w_ratio <- width / height
}

# convert to raster so we can convert to matrix ---------------------------

size <- 1000
florida_rast <- st_rasterize(st_florida, 
                             nx = floor(size * w_ratio),
                             ny = floor(size * h_ratio))

mat <- matrix(florida_rast$population,
              nrow = floor(size * w_ratio),
              ncol = floor(size * h_ratio))

# create color palette ----------------------------------------------------

c1 <- met.brewer("OKeeffe2")
swatchplot(c1)

texture <- grDevices::colorRampPalette(c1, bias = 2)(256)
swatchplot(texture)
# plot that 3D thing! -----------------------------------------------------

rgl::close3d()

mat |> 
  height_shade(texture = texture) |> 
  plot_3d(heightmap = mat,
          zscale = 100,
          solid = FALSE,
          shadowdepth = 0)

render_camera(theta = -20, phi = 45, zoom = .8)



# test render -------------------------------------------------------------

render_highquality(
  filename = "images/test_plot",
  lightaltitude = c(20, 80),
  lightcolor = c(c1[1], "white"),
  lightintensity = c(500, 50),
  lightdirection = 280,
  interactive = FALSE
)


# FINAL RENDER ------------------------------------------------------------

outfile <- "images/final_plot_3.png"


{
  start_time <- Sys.time()
  cat(crayon::cyan(start_time), "\n")
  if(!file.exists(outfile)){
    png::writePNG(matrix(1), target = outfile)
  }
  render_highquality(
    filename = outfile,
    lightaltitude = c(20, 80),
    lightcolor = c(c1[1], "white"),
    lightintensity = c(600, 100),
    lightdirection = 280,
    interactive = FALSE,
    samples = 350, 
    width = 7000,
    height = 7000
  )
  
  end_time <- Sys.time()
  diff <- end_time - start_time
  cat(crayon::cyan(diff), "\n")
}

@TearsRfuel TearsRfuel added the bug Something isn't working label Feb 24, 2024
@francisvolh
Copy link

I am experiencing the same issue. I run the sample code from this github page
https://github.com/milos-agathon/3d-forest-type-map/blob/main/R/main.r

And I got the right output and rendered image (just like the tutorial).

But when I tried my own data, I get this in the rgl device:
image

but this is my rendered image:
test-refugia-3d

I am not sure why.

I have only tweaked the code in ways that allow me to improve speed. but my final rendering code is:

rayshader::render_highquality(
samples = 128, #def 128
filename = "plots/test-refugia-3d.png",
preview = FALSE,
light = FALSE,
environment_light = "air_museum_playground_4k.hdr",
intensity_env = 2,
rotate_env = 1,
interactive = FALSE,
parallel = TRUE, #if false uses all available cores to render
width = w, height = h
)

Even when removing the environment_light and using the default, my whole map is green.

@tylermorganwall
Copy link
Owner

Do you have an example that does not require a special dataset that reproduces this issue?

@francisvolh
Copy link

francisvolh commented Mar 1, 2024

Hello Tyler,

I have just done a test to try to replicate the issue. Funnily enough, the first render came out good. But I did it with the lowest parameters. I run it s second time, and 3 more times, even with the same lowest settings, and it never came up again. Examples here. and code and files to reproduce below.

Lowest setting first render
test-git-3d

More detailed settings
test-git-3d2

Fourth run of same initial lower settings
test-git-3d4

img <- png::readPNG("test_3d_01.png")

elevation <- terra::rast("elevation_cropped.tif")

elmat <- rayshader::raster_to_matrix(
  elevation
)



h <- nrow(elevation)
w <- ncol(elevation)
{

  begin_time <- Sys.time()
  elmat |>
    rayshader::height_shade(
      texture = colorRampPalette(
        "white"
      )(512) #any number of values you actually want
    ) |>
    rayshader::add_overlay(
      img,
      alphalayer = .9, # how transparent the raster image is
      alphacolor = "white" # should correspond to height_shade above
    ) |>
    rayshader::add_shadow(
      rayshader::lamb_shade(
        elmat,
        zscale = 50,
        sunaltitude = 90,
        sunangle = 315,
      ), max_darken = .25
    ) |>
    rayshader::add_shadow(
      rayshader::texture_shade(
        elmat,
        detail = .2, ### MOVED to 0.5 , then back to 0.2 may reduce this closer to 0 and it may reduce computation time!!!!!
        brightness = 90, 
        contrast = 80,
      ), max_darken = .1
    ) |>
    rayshader::plot_3d(
      elmat,
      zscale = 40, 
      solid = FALSE,
      shadow = TRUE,
      shadow_darkness = 1,
      background = "white",
      windowsize = c(
        w / 5, h / 5
      ),
      zoom = .5,
      phi = 85,
      theta = 0 
    )
  end_time <- Sys.time()
  print(paste0("Rayshader process 1 time ", round(difftime(end_time, begin_time, units = "mins"),2), " minutes"))
}



#tweaking to try to fix
{
  begin.time <- Sys.time()
  
  rayshader::render_highquality(
    samples =32, # MOVED TO 16 to speed up
    filename = "test-git-3d4.png",
    preview = FALSE,
    lightcolor = "white",
    light = TRUE,
    #environment_light = "air_museum_playground_4k.hdr",
    intensity_env = 2,
    rotate_env = 1,
    interactive = FALSE,
    parallel = TRUE, #if false uses all available cores to render
    width = w/5, height = h/5 ### CHANGED to higher setting : dont divide to increase size of image!!!!!!!!!!
  )
  end_time <- Sys.time()
  
  print(paste0("Rendering process time ", round(difftime(end_time, begin.time, units = "mins"),2), " minutes" ))
}

#test-git-3d.png 0.2 32 w/5 h/5

#test-git-3d2.png 0.2 32 w h

#test-git-3d3.png 0.2 32 w/10 h/10

I have tried commenting what I changed or moved to change the settings.

Edit: I restarted my computer (today, next day of previous images in this comments) thinking it may be some residual memory issue, but it did not solve it. So, I tried again a 1st rendering with good quality settings, and it came out all "grayish" when the rgl window is coloured correctly (as shown initially). But either way... it does look like some sort of bug.

next day image 1st render:
test-git-3d4

I rendered a second time, without "closing" the rgl window (still showing the good colour pattern) and reduced the rendering parameters (samples to 16 , from 64, and h and w divided by 5) and still came out gray-ish with pixels.

next day image second render, lower settings:
test-git-3d4
Unlike last night that I did get at least 1 low quality render with the right colour pattenrs ("Lowest setting first render", above), today nothing comes out right. The Original issue post title says "randomly", so it seems to me to be like now. Honestly, no clue. (end of Edit)

I have linked the files here, as I didn't know how to attach them. This would be the only two you need to reproduce my output or issue.
I hope this helps to figure out what is going on. Thanks!

Elevation raster
https://drive.google.com/file/d/12yezqqNutfD19eyxJmbL9WqvwTdcjT3e/view?usp=sharing

img (png file to overlay)

https://drive.google.com/file/d/1Ivb0uyZYgZZ3Z2e162WsEutSqgYDz2kL/view?usp=drivesdk

@tylermorganwall
Copy link
Owner

@francisvolh The second file is the TIF again: can you provide the PNG overlay?

@francisvolh
Copy link

Apologies for that. I just updated the link. Please let me know if it works now! Thanks a lot again!

@TearsRfuel
Copy link
Author

most commonly the very first render (ususally low quality) will come out colored, any following attempts to render at high or low quality will be either:

  1. completely colorless (black and white)
    or 2) only lighting is rendered, and not the density spectrum

@tylermorganwall
Copy link
Owner

Can you output the result of:

str(convert_rgl_to_raymesh())

when you have a render that isn't being colored in? And can you see if the file(s) at the location specified in the diffuse_texname entries in materials exist?

@francisvolh
Copy link

francisvolh commented Mar 5, 2024

Hello @tylermorganwall

Done. The rgl file, was fine, then the rendered file was all dark grey (no colors).

I looked for the files in the folder specified by diffuse_texname and they do exist. The specific file listed, is a blank png. There is another, which looks like the rgl image, with a larger size, and its basically the image from which the render should come but with no shades or the right light (although the original dimensions of the rgl device, as my rendered image should have been a bit smaller, but high quality, 128 samples) .

I tried it in 2 different laptops, just in case it was a laptop specific issue. And same result.

Also, I updated both laptops with the latest github versions of the pacakges today.

Attached is a screenshot of the files in the folder from diffuse_texname and the str() results
image

Surely there is something going on, not sure why this is happening in my system (or the OP's system as well). Thanks

Francis

@TearsRfuel
Copy link
Author

TearsRfuel commented Mar 15, 2024

grey render;

result of the requested command:

str(convert_rgl_to_raymesh())
List of 6
 $ shapes         :List of 2
  ..$ :List of 6
  .. ..$ indices           : num [1:4318030, 1:3] 2173 2174 5242 5243 5243 ...
  .. ..$ norm_indices      : num [1:4318030, 1:3] 2173 2174 5242 5243 5243 ...
  .. ..$ tex_indices       : num [1:4318030, 1:3] 2173 2174 5242 5243 5243 ...
  .. ..$ material_ids      : num [1:4318030] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..$ has_vertex_tex    : logi [1:4318030] TRUE TRUE TRUE TRUE TRUE TRUE ...
  .. ..$ has_vertex_normals: logi [1:4318030] TRUE TRUE TRUE TRUE TRUE TRUE ...
  ..$ :List of 6
  .. ..$ indices           : num [1:2, 1:3] 0 3 1 4 2 5
  .. ..$ norm_indices      : num [1:2, 1:3] 0 3 1 4 2 5
  .. ..$ tex_indices       : num [1:2, 1:3] 0 3 1 4 2 5
  .. ..$ material_ids      : num [1:2] 0 0
  .. ..$ has_vertex_tex    : logi [1:2] TRUE TRUE
  .. ..$ has_vertex_normals: logi [1:2] TRUE TRUE
 $ vertices       :List of 2
  ..$ : num [1:21490000, 1:3] -1534 -1534 -1532 -1532 -1530 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:3] "x" "y" "z"
  ..$ : num [1:6, 1:3] 1842 1842 -1841 -1841 1842 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:3] "x" "y" "z"
 $ texcoords      :List of 2
  ..$ : num [1:21490000, 1:2] 0 0.000326 0.000652 0.000978 0.001303 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "s" "t"
  ..$ : num [1:6, 1:2] 1 1 0 0 1 0 1 0 0 1 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "s" "t"
 $ normals        :List of 2
  ..$ : num [1:21490000, 1:3] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:3] "x" "y" "z"
  ..$ : num [1:6, 1:3] 0 0 0 0 0 0 1 1 1 1 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:3] "x" "y" "z"
 $ materials      :List of 2
  ..$ :List of 1
  .. ..$ :List of 29
  .. .. ..$ diffuse             : num [1:3] 1 1 1
  .. .. ..$ ambient             : num [1:3] 0 0 0
  .. .. ..$ specular            : num [1:3] 1 1 1
  .. .. ..$ transmittance       : num [1:3] 1 1 1
  .. .. ..$ emission            : num [1:3] 0 0 0
  .. .. ..$ shininess           : num 50
  .. .. ..$ ior                 : num 1
  .. .. ..$ dissolve            : num 1
  .. .. ..$ illum               : num 1
  .. .. ..$ ambient_texname     : chr ""
  .. .. ..$ diffuse_texname     : chr "C:\\Users\\myName\\AppData\\Local\\Temp\\RtmpGin94K\\file3c5c19cf58cb.png"
  .. .. ..$ bump_texname        : chr ""
  .. .. ..$ emissive_texname    : chr ""
  .. .. ..$ specular_texname    : chr ""
  .. .. ..$ normal_texname      : chr ""
  .. .. ..$ diffuse_intensity   : num 1
  .. .. ..$ bump_intensity      : num 1
  .. .. ..$ specular_intensity  : num 1
  .. .. ..$ emission_intensity  : num 1
  .. .. ..$ ambient_intensity   : num 1
  .. .. ..$ culling             : num 1
  .. .. ..$ type                : chr "color"
  .. .. ..$ translucent         : logi TRUE
  .. .. ..$ toon_levels         : num 5
  .. .. ..$ toon_outline_width  : num 0.05
  .. .. ..$ toon_outline_color  : num [1:3] 0 0 0
  .. .. ..$ reflection_intensity: num 0
  .. .. ..$ reflection_sharpness: num 0
  .. .. ..$ two_sided           : logi FALSE
  ..$ :List of 1
  .. ..$ :List of 29
  .. .. ..$ diffuse             : num [1:3] 1 1 1
  .. .. ..$ ambient             : num [1:3] 0 0 0
  .. .. ..$ specular            : num [1:3] 1 1 1
  .. .. ..$ transmittance       : num [1:3] 1 1 1
  .. .. ..$ emission            : num [1:3] 0 0 0
  .. .. ..$ shininess           : num 50
  .. .. ..$ ior                 : num 1
  .. .. ..$ dissolve            : num 1
  .. .. ..$ illum               : num 1
  .. .. ..$ ambient_texname     : chr ""
  .. .. ..$ diffuse_texname     : chr "C:\\Users\\myName\\AppData\\Local\\Temp\\RtmpGin94K\\file3c5c787a3906.png"
  .. .. ..$ bump_texname        : chr ""
  .. .. ..$ emissive_texname    : chr ""
  .. .. ..$ specular_texname    : chr ""
  .. .. ..$ normal_texname      : chr ""
  .. .. ..$ diffuse_intensity   : num 1
  .. .. ..$ bump_intensity      : num 1
  .. .. ..$ specular_intensity  : num 1
  .. .. ..$ emission_intensity  : num 1
  .. .. ..$ ambient_intensity   : num 1
  .. .. ..$ culling             : num 1
  .. .. ..$ type                : chr "color"
  .. .. ..$ translucent         : logi TRUE
  .. .. ..$ toon_levels         : num 5
  .. .. ..$ toon_outline_width  : num 0.05
  .. .. ..$ toon_outline_color  : num [1:3] 0 0 0
  .. .. ..$ reflection_intensity: num 0
  .. .. ..$ reflection_sharpness: num 0
  .. .. ..$ two_sided           : logi FALSE
 $ material_hashes: chr [1:2] "eb865e641849bdf1e2b067b3e24f265e" "9b38a44fc264b5838dc0d15e55142503"
 - attr(*, "class")= chr [1:2] "ray_mesh" "list"

The file in the abovementioned filepath exists, yes, here it is:

file3c5c787a3906

Here is the grey render:

test_plot

@TearsRfuel
Copy link
Author

any news:(

@das-mechaniker
Copy link

das-mechaniker commented May 4, 2024

Any updates on this?

Edit: see below for possible workaround.

@das-mechaniker
Copy link

das-mechaniker commented May 5, 2024

Edit: After further investigation, this was not the only variable causing this issue, therefore the fix may not 100% work.

@TearsRfuel @francisvolh I might have found a temporary fix. I believe this issue may have to do with the sample_method. Try forcing the sample_method to sobol.

sample_method = "sobol"

@tylermorganwall I have run into this issue on Windows as well (tested on mac, no issues) but I have noticed that regardless of sample size, if you specify the sample_method to sobol, it will correctly render colors. Specifying method as sobol-blue or not specifying will cause colorless renderings.

Colors Rendered - sample_method = sobol

render_highquality(
    outfile, 
    sample_method = "sobol",
    samples = 300,
    preview = TRUE,
    parallel = TRUE,
    light = TRUE,
    width = 500, height = 500
  )

No Color Rendered - sample_method = sobol-blue

render_highquality(
    outfile, 
    sample_method = "sobol-blue",
    samples = 300,
    preview = TRUE,
    parallel = TRUE,
    light = TRUE,
    width = 500, height = 500
  )

No Color Rendered - sample_method not specified

render_highquality(
    outfile, 
    samples = 300,
    preview = TRUE,
    parallel = TRUE,
    light = TRUE,
    width = 500, height = 500
  )

sessionInfo()

R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default

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

other attached packages:
 [1] NatParksPalettes_0.2.0 stars_0.6-5            abind_1.4-5            tigris_2.1            
 [5] colorspace_2.1-0       glue_1.7.0             rayshader_0.38.0       elevatr_0.99.0        
 [9] lubridate_1.9.3        forcats_1.0.0          stringr_1.5.1          dplyr_1.1.4           
[13] purrr_1.0.2            readr_2.1.5            tidyr_1.3.1            tibble_3.2.1          
[17] ggplot2_3.5.1          tidyverse_2.0.0        sf_1.0-16             

loaded via a namespace (and not attached):
 [1] gtable_0.3.5       xfun_0.43          htmlwidgets_1.6.4  tzdb_0.4.0         vctrs_0.6.5       
 [6] tools_4.4.0        generics_0.1.3     curl_5.2.1         parallel_4.4.0     rgl_1.3.1         
[11] proxy_0.4-27       fansi_1.0.6        pkgconfig_2.0.3    KernSmooth_2.23-22 uuid_1.2-0        
[16] lifecycle_1.0.4    farver_2.1.1       compiler_4.4.0     progress_1.2.3     munsell_0.5.1     
[21] codetools_0.2-20   htmltools_0.5.8.1  class_7.3-22       rayimage_0.10.0    Rttf2pt1_1.3.12   
[26] pillar_1.9.0       crayon_1.5.2       extrafontdb_1.0    classInt_0.4-10    magick_2.8.3      
[31] iterators_1.0.14   foreach_1.5.2      tidyselect_1.2.1   digest_0.6.35      stringi_1.8.3     
[36] extrafont_0.19     fastmap_1.1.1      grid_4.4.0         cli_3.6.2          magrittr_2.0.3    
[41] base64enc_0.1-3    utf8_1.2.4         e1071_1.7-14       withr_3.0.0        rappdirs_0.3.3    
[46] prettyunits_1.2.0  scales_1.3.0       timechange_0.3.0   rayrender_0.34.0   httr_1.4.7        
[51] progressr_0.14.0   png_0.1-8          hms_1.1.3          knitr_1.46         rayvertex_0.10.6  
[56] doParallel_1.0.17  rlang_1.1.3        Rcpp_1.0.12        DBI_1.2.2          rstudioapi_0.16.0 
[61] jsonlite_1.8.8     R6_2.5.1           units_0.8-5 

@TearsRfuel
Copy link
Author

Unfortunately this didn't work, although I have tried changing the sample method and sample size before I made my original post, and it would randomly and rarely produce colored results, but nothing fixed it definitively.

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

4 participants