Makie isosurface transparency?

In Makie is there a way to make an isosurface have some partial transparency? There used to be I think, but I cant see an alpha option or similar

http://makie.juliaplots.org/dev/plotting_functions/volume.html

Over at https://lazarusa.github.io/BeautifulMakie/surfWireLines/KleinBottle/

I notice that a partially transparent colormap can be passed to mesh:

colormap = (:Spectral_11,0.95)

but if I try it with volume the transparency doesn’t work.

if I use transparency=true, i.e.

r = LinRange(-1, 1, 100)
cube = [(x.^2 + y.^2 + z.^2) for x = r, y = r, z = r]
cube_with_holes = cube .* (cube .> 1.4)
volume(cube_with_holes, algorithm = :iso, isorange = 0.05, isovalue = 1.7,transparency=true)

I get the following. But this is not really transparent in the sense that matters for me. The back surface is not visible through the front surface.

Check the absorption and contour suggestions in this amazing post.

Ok, some progress. If I do, e.g.

contour(cube_with_holes,levels=[1.65,1.7],colormap=:blues,alpha=.3,colorrange=[-8,8])

I get the result

Not quite what I want though. Quite dark, hard to get a clear surface

This would be really good to add to the docs, I never need volume plots but there is just one example and it’s not clear at all how to use the other modes.

I figured finding an example of roughly what I am trying to do would be more useful. I am aiming for something like the fourth example here:

https://plotly.com/python/3d-isosurface-plots/

I use this trick often(https://), it gives nice results.

colors = to_colormap(:balance)
n = length(colors)
alpha = [ones(n÷3);zeros(n-2*(n÷3));ones(n÷3)]
cmap_alpha = RGBAf0.(colors, alpha)

I think this shows the issue clearly.

I would expect to see two intersecting ellipsoids, with axis sitting behind (less visible due to being behind more than one layer). Here I plotted the smaller (closer) surface first. The impression is that both sit behind the axis, and the closer one appears behind the further because transparency is only with respect to axes. I have tried applying an alpha to colormap (suggested above), but I haven’t had much luck improving this

	using GLMakie
	X = -40:5:40
	ellipse1 = [X^2/2 + Y^2 + 2Z^2 for X in X, Y in X, Z in X];
	ellipse2 = [X^2/2 + (Y-20)^2 + 2Z^2 for X in X, Y in X, Z in X];
	v1=volume(ellipse2, algorithm = :iso,isovalue =350,isorange = 50,transparency=true)
	volume!(ellipse1, algorithm = :iso, isovalue =700,isorange = 50,transparency=true)
	v1

did you tried like this:

using GLMakie, ColorSchemes
cbarPal= :plasma
cmap = get(colorschemes[cbarPal], LinRange(0,1,100))
cmap2 = [(cmap[i],0.65) for i in 1:100]

X = -40:5:40
ellipse1 = [X^2/2 + Y^2 + 2Z^2 for X in X, Y in X, Z in X];
ellipse2 = [X^2/2 + (Y-20)^2 + 2Z^2 for X in X, Y in X, Z in X];
volume(ellipse2, algorithm = :iso,isovalue =350,isorange = 50, colormap=cmap2)
volume!(ellipse1, algorithm = :iso, isovalue =700,isorange = 50, colormap=cmap2)
current_figure()

2 Likes

Ok that looks great, thanks!

Ok, getting closer. Still puzzling: I make a torus geometry. Why isn’t the full torus visible even though there is an alpha? It seems that the alpha is applied to another surface (dual ellipsoid case above), but not to different parts of the same surface. We should be able to see the core of the torus, and the back surface:

using GLMakie
using ColorSchemes
cmap3 = get(colorschemes[:plasma], LinRange(0,1,100))
	cmap4 = [(cmap3[i],0.65) for i in 1:100]
	ξ = -40:1:40
	c = 20
	a = 15
	torus = [((hypot(X,Y)-c)^2+Z^2-a^2) for X in ξ, Y in ξ, Z in ξ]
	volume(torus, algorithm = :iso,isovalue =100,isorange =50, colormap=cmap4)
	current_figure()

I would expect something more like this:

Screen Shot 2021-08-20 at 2.02.55 PM

It seems like contour can sort of do this, but the visibility is not great and there is also a loss of surface information and the result is not really very clear:

(@v1.10) pkg> status GLMakie
Status `~/.julia/environments/v1.10/Project.toml`
  [e9467ef8] GLMakie v0.10.2

using GLMakie
using ColorSchemes
cmap3 = get(colorschemes[:plasma], LinRange(0,1,100))
	cmap4 = [(cmap3[i],0.65) for i in 1:100]
	ξ = -40:1:40
	c = 20
	a = 15
	torus = [((hypot(X,Y)-c)^2+Z^2-a^2) for X in ξ, Y in ξ, Z in ξ]
contour(torus,levels=[1],colormap=:greens,alpha=0.1)

Maybe I am not using the right parameters or light sources?
Is there a way to get the best of both worlds, more like the matlab figure above?

I have also tried making the colormap transparent, and that didn’t help.

After a bit of playing around with parameters, I reached this result for a transparent isosurface using a mesh:

The code looks like this

using GLMakie, Meshing, GeometryBasics

ξ = -40:.5:40
c = 20
a = 15
torus = [((hypot(X,Y)-c)^2+Z^2-a^2) for X in ξ, Y in ξ, Z in ξ]

##
with_theme(theme_dark()) do
algo = MarchingCubes(iso=100)

vts, fcs = isosurface(torus, algo)
mc =  Makie.GeometryBasics.Mesh(vts .|> Makie.Point3, fcs .|> Makie.GeometryBasics.TriangleFace)

f ,ax,pl=mesh(normal_mesh(mc), 
    color=(:dodgerblue, .7), 
    diffuse = Vec3f0(0.8),
    specular = Vec3f0(1.1),
    shininess = 30f0,
    backlight = 5f0,
    axis = (show_axis = false, ))
    f
end
3 Likes

I have updated this to work with the for recent changes to GeometryBasics and Meshing.

1 Like