dmurdoch / rgl

rgl is a 3D visualization system based on OpenGL. It provides a medium to high level interface for use in R, currently modelled on classic R graphics, with extensions to allow for interaction.
https://dmurdoch.github.io/rgl/
GNU General Public License v2.0
85 stars 20 forks source link

Bug visualization rglwidget() #314

Closed SbastianGarzon closed 1 year ago

SbastianGarzon commented 1 year ago

I'm having problems visualizing surfaces with the rglwidget() while using Rayshader (Quarto document)

Here is a reproducible error:

library(tidyverse)
library(rayshader)
library(rgl)

set.seed(2)
elevation_matrix <- matrix(rnorm(25) , nrow = 10,ncol = 10)

elevation_matrix %>% 
  height_shade(texture = topo.colors(256)) %>%
  plot_3d(elevation_matrix,baseshape = "rectangle")

rglwidget()

rgl output:

rgl

rglwidget output (HTML):

rglwidget

Same error in another (more complex) Rayshader viz.

rgl_city

dmurdoch commented 1 year ago

I see the issue in your first example. Hopefully easy to fix.

dmurdoch commented 1 year ago

This is definitely a bug in rgl. It's triggered by the fact that the back of the surface is culled, but I don't see a way to avoid that (other than editing the source to rayshader::plot_3d). I'll track down the cause and patch it soon.

dmurdoch commented 1 year ago

I found it. The problem was that scene3d() (used by rglwidget()) removed the normals for the objects when it saw the object was unlit. That meant that they needed to be recalculated to determine front and back in order to cull the back, but the recalculation was incorrect because of the indexing used by rayshader::plot_3d().

I'll stop the removal, and not allow two-sided indexed rendering without normals. Look for rgl 1.0.10 sometime later today.