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

Texture doesn't map correctly using rglWidget in Shiny #360

Closed can-taslicukur closed 1 year ago

can-taslicukur commented 1 year ago

Hi, thank you for this amazing package! I am trying to visualize a sphere with a texture in a shiny app. However, I've realized that texture isn't mapped correctly in the shiny output compared to RGL Device.

Here is an example:

RGL Device

library(rgl)

spheres3d(
  x = 0,
  y = 0,
  z = 0,
  radius = 1,
  texture = system.file("textures/sunsleep.png", package = "rgl"),
  alpha = 1,
  texmode = "replace",
  textype = "rgb",
  col = "white"
)
Screenshot 2023-04-18 at 13 21 39

You can see that texture was mapped nicely, there is no overlap between the left and the right of the image.

Shiny

library(shiny)
library(rgl)

shinyApp(
  ui = bootstrapPage(
    rglwidgetOutput("widget")
  ),
  server = function(input, output, session) {
    output$widget <- renderRglwidget({
      try(close3d(), silent = TRUE)
      open3d(useNULL = TRUE)
      spheres3d(
        x = 0,
        y = 0,
        z = 0,
        radius = 1,
        texture = system.file("textures/sunsleep.png", package = "rgl"),
        alpha = 1,
        texmode = "replace",
        textype = "rgb",
        col = "white"
      )
      rglwidget()
    })
  }
)

Screenshot 2023-04-18 at 13 24 19

In this case, the left and right edges of the image overlap and create a deformed texture.

Same issue also happening using the bg3d function when the sphere = TRUE and you can see it in the example here: https://dmurdoch.github.io/rgl/reference/bg.html

dmurdoch commented 1 year ago

Thanks, I'll look into it. (I don't think Shiny is involved at all; just calling rglwidget() after your first example is sufficient to illustrate the problem.)

dmurdoch commented 1 year ago

The problem is that in the rglwidget() display, the texture coordinates don't go all the way from 0 to 1, they run over a slightly smaller range, horizontally from 0 to 0.94 and vertically from 0.06 to 0.94. What you see that looks like overlap is really the texture in reverse, going from 0.94 to 0.06. This should be fairly easy to fix, but I don't think I'll have time to get to it today.

can-taslicukur commented 1 year ago

Good to hear that it's an easy fix, I cant open a PR because I don't know how GL works :) Thank you so much!

dmurdoch commented 1 year ago

Thanks for the report, should be all fixed now (though Github isn't running R properly, so the website may take a while to be updated).