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
84 stars 20 forks source link

[enhance] Alpha-Channel for Surfaces #389

Closed discoleo closed 9 months ago

discoleo commented 9 months ago

Alpha-Channel for Surfaces

The Alpha-Channel has currently NO effect for surfaces:

It would be nice to be able to make a surface semitransparent.

dmurdoch commented 9 months ago

Could you show some code to demonstrate this? When I run

example(surface3d)
open3d()
surface3d(x, y, z, color = col, back = "lines", alpha=0.1)

I see the second surface with transparency.

discoleo commented 9 months ago

My mistake: I was trying to set it via the color "#A0A0A064":

The code is in: https://github.com/discoleo/R/blob/master/Chemistry/Chem3D.R

dmurdoch commented 9 months ago

I think that should have worked; I'll investigate further.

dmurdoch commented 9 months ago

Looking back at it, that hasn't ever been supported. It probably should be, so I'll leave this open.

dmurdoch commented 9 months ago

And now looking more closely, I still think this should be supported, but it's unlikely to happen in the near future.

The current state is this: alpha is a material property, and material properties are handled in a very convoluted way in rgl. When you call a function like triangles3d(col = "red"), the color is changed to "red" for the duration of the call, then restored to a default afterwards. If you usecol = "#A0A0A064", currently the alpha channel is completely ignored in the color, but it can be specified with the alpha argument, and that value is restored afterwards.

So the problem with using the alpha channel in a color is that it is hard for the code to know if the current default alpha value should be overridden or not. Clearly if parameter alpha is specified in the call, it should override the value in the color. But if it is not, users might have set the default alpha to be used, so col = "red" (with an implicit alpha = 1) should not override the default, which might not be 1.

So perhaps the rule of precedence should be: use alpha if specified, use alpha from the color value if it is specified there, otherwise use the default alpha. The trouble is that the convoluted code makes it hard to know if alpha was specified by the user or by looking up the default. I think fixing this would require a lot of error-prone changes.

At some point in the distant future, this should all be rationalized, but it's not going to happen soon, so I'm going to close this, even though I haven't dealt with it.