ropengl3dmeshrgl

How to get a dull and shadowed mesh with rgl?


Below are two meshes of the Barth sextic. The first one is obtained with the R package rgl. It is rather shiny and it has almost no shadow. The second one is obtained with the Haskell package OpenGL. It is dull and shadowed.

Barth sextic - rgl

Barth sextic - haskell

I'd like to know how to obtain a rendering similar to the Haskell one with rgl? As far as I know, both libraries are wrapper of openGL, so that should be possible.

# isosurface f=0
phi <- (1 + sqrt(5)) / 2
f <- function(x, y, z){
  4 * (phi^2*x^2 - y^2) * (phi^2*y^2 - z^2) * (phi^2*z^2 - x^2) - 
    (1 + 2*phi) * (x^2 + y^2 + z^2 - 1)^2
}
# make the isosurface
nx <- 220L; ny <- 220L; nz <- 220L
x <- seq(-1.8, 1.8, length.out = nx) 
y <- seq(-1.8, 1.8, length.out = ny)
z <- seq(-1.8, 1.8, length.out = nz) 
Grid <- expand.grid(X = x, Y = y, Z = z)
voxel <- array(with(Grid, f(X, Y, Z)), dim = c(nx, ny, nz))
mask  <- array(with(Grid, X^2 + Y^2 + Z^2 > 3), dim = c(nx, ny, nz))
voxel[mask] <- -1000
library(rmarchingcubes)
cont <- contour3d(voxel, level = 0, x = x, y = y, z = z)
# plot
library(rgl)
mesh <- tmesh3d(
  vertices = t(cont[["vertices"]]),
  indices  = t(cont[["triangles"]]),
  normals  = cont[["normals"]],
  homogeneous = FALSE
)
#
open3d(windowRect = c(50, 50, 562, 562), zoom = 0.65)
shade3d(mesh, color = "#ff00ff")

The (unique) light in the Haskell scene is located at (-50, 100, 100) and has colors ambient=black, diffuse=white, specular=white.


Solution

  • I think I get it. One has to clear the default lighting first, then add a light like the one in the Haskell scene. I also set the specular color of the mesh to black.

    open3d(windowRect = c(50, 50, 562, 562), zoom = 0.65)
    clear3d(type = "lights")
    light3d(x = -50, y = 100, z = 100, ambient = "black")
    shade3d(mesh, color = "#ff00ff", specular = "black")
    

    enter image description here